#|# ZUUL 使用

ZUUL 使用 Zuul项目 在pom.xml 中添加

org.springframework.cloud spring-cloud-starter-netflix-zuul

ZuulDemoApplication.java中标记启用Zuul
/** * @author HouZm * @EnableZuulProxy简单理解为@EnableZuulServer的增强版,当Zuul与Eureka、Ribbon等组件配合使用时,我们使用@EnableZuulProxy。 * @EnableZuulProxy 启用Zuul */ @SpringBootApplication @EnableZuulProxy public class ZuulApplication {public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }

application.properties中添加配置
zuul.routes.client1.path=/client1/** zuul.routes.client1.service-id=DEV-CLIENT-1zuul.routes.client2.path=/client2/** zuul.routes.client2.service-id=dev-client-2 ## zuul.routes.client2.strip-prefix= true

其他配套代码 另外启用了 Eureka注册中心、client-1、client-2
2个客户端都向Eureka进行了注册
client-1
application.properties
#向erueka注册 eureka.instance.leaseRenewalIntervalInSeconds= 30 eureka.instance.prefer-ip-address=true eureka.client.registryFetchIntervalSeconds= 15 ## 配置的Eureka服务地址 以下地址为内网 eureka.client.serviceUrl.defaultZone=http://10.60.110.8:8199/eureka-server/eureka/,http://10.60.110\ .9:8199/eureka-server/eureka/,http://10.60.110.10:8199/eureka-server/eureka/server.servlet.context-path= # 如果没有contextPath的话这些不用配置 eureka.instance.home-page-url-path=${server.servlet.context-path} eureka.instance.health-check-url-path=${server.servlet.context-path}/actuator/health eureka.instance.status-page-url-path=${server.servlet.context-path}/actuator/info eureka.instance.metadata-map.management.context-path=${server.servlet.context-path}/actuatorserver.port=8182spring.application.name=DEV-CLIENT-1management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always

IndexController.java
/** * @author HouZm * @date 2018/11/1 10:39 */ @RestController @RequestMapping("/") public class IndexController {@RequestMapping("") public String index(){ return "我是Client1 "; } }

client-2
application.properties
#向erueka注册 eureka.instance.leaseRenewalIntervalInSeconds= 30 eureka.instance.prefer-ip-address=true eureka.client.registryFetchIntervalSeconds= 15 eureka.client.serviceUrl.defaultZone=http://10.60.110.8:8199/eureka-server/eureka/,http://10.60.110\ .9:8199/eureka-server/eureka/,http://10.60.110.10:8199/eureka-server/eureka/server.servlet.context-path= # 如果没有contextPath的话这些不用配置 eureka.instance.home-page-url-path=${server.servlet.context-path} eureka.instance.health-check-url-path=${server.servlet.context-path}/actuator/health eureka.instance.status-page-url-path=${server.servlet.context-path}/actuator/info eureka.instance.metadata-map.management.context-path=${server.servlet.context-path}/actuatorserver.port=8183spring.application.name=DEV-CLIENT-2 ## 启用actuator 端口,默认只公开了/health 和 /info 端点 ## 公开除 env 端点之外的所有(已启用的) web 端点: management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always

IndexController.java
/** * @author HouZm * @date 2018/11/1 10:39 */ @RestController @RequestMapping("/") public class IndexController {@RequestMapping("") public String index(){ return "我是Client2 "; } }

测试访问 启动3个服务
访问 http://localhost:8184/client2,出现
我是Client2

访问:http://localhost:8184/client1
我是Client1

证明Zuul路由已成功
【#|# ZUUL 使用】代码地址:代码地址

    推荐阅读