springboot-cloud-5-zuul

zuul

application.properties

server.port= 8004
spring.application.name=zuul-master
###配置zuul路由
zuul.routes.service-a.path= /service-a/**
zuul.routes.service-a.serviceId = client
zuul.routes..service-b.path= /service-b/**
zuul.routes.service-b.serviceId  = hystrix-master

eureka.client.serviceUrl.defaultZone=http://pear1:9998/eureka/

application

@SpringCloudApplication   //開啓消費 與 熔斷 
@EnableZuulProxy    //zuul路由
public class ZuulMasterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulMasterApplication.class, args);
    }
}

路由過濾器

/**
 * Created by guoyao on 2017/8/17.
 */
@Component
public class ZuulRequestFilter extends ZuulFilter {

    private static final Logger log=LoggerFactory.getLogger(ZuulRequestFilter.class);

    //"pre" for pre-routing filtering,
    //  "route" for routing to an origin,
    // "post" for post-routing filters
    // "error" for error handling.
    //過濾器類型,決定過濾器運行時機
    @Override
    public String filterType() {
        return "pre";
    }

    //過濾器執行順序
    @Override
    public int filterOrder() {
        return 0;
    }

    // a "true" return from this method means that the run() method should be invoked
    @Override
    public boolean shouldFilter() {
        return true;
    }


    //過濾器邏輯
    @Override
    public Object run() {
        log.info(" filter is running ");

        //  模仿token實現權限控制,實際可由會話獲取
        RequestContext requestContext =  RequestContext.getCurrentContext();
        HttpServletRequest httpRequest=requestContext.getRequest();

        String token  = httpRequest.getParameter("accessToken");
        if (!"token".equals(token)) {
            log.warn(" you do not have the token ");
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
            return null;
        }
        log.info(" access successful");
        return null;
    }
}

網關配置

server.port= 8004

spring.application.name=zuul-master

###配置zuul路由
zuul.routes.service-a.path= /service-a/**
zuul.routes.service-a.serviceId = client
#zuul.routes..service-b.path= /service-b/**
#zuul.routes.service-b.serviceId  = hystrix-master

###配置本地跳轉
##zuul.routes.hystrix-master.url= forward:/local

###關閉指定的過濾器
zuul.ZuulRequestFilter.pre.disable = true

#####指定url訪問
####zuul.routes.service-a.url= http://localhost:8080/service-a

####簡化配置爲zuul.routes.<serviceId> = <path>
####默認自動整合eureka的所有服務
###zuul.routes.hystrix-master = /hystrix-master/**

####忽略指定  service
#####zuul.ignored-services= *
####忽略指定pattern
####zuul.ignored-patterns = /**/getClientDelay/**

####路由前綴
#zuul.prefix = /hello    ###有bug 能access successful  not route found
####移除所有服務代理前綴
#zuul.strip-prefix=false

eureka.client.serviceUrl.defaultZone=http://pear1:9998/eureka/
發佈了72 篇原創文章 · 獲贊 9 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章