SpringCloud-Zuul
Zuul是什么?
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得.
注意:Zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
•创建项目,添加依赖
<dependencies> <!--zuul--> <!--erueka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> <version>1.4.6.RELEASE</version> </dependency> //Zuul依赖 <!--Hystrix依赖~--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--Ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--erueka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>com.kuang</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
•编写application.yml配置
server: port: 9527 spring: application: name: springcloud-zuul #微服务注册的名字 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/ #去三个集群中发现其它服务 instance: instance-id: zuul9527.com prefer-ip-address: true #显示真实ip info: app.name: kuang-springcloud company.name: blog.kuangstudy.com
zuul:
routes:
mydept.serviceId: springcloud-provider-dept #原来需要这个访问
mydept.path: /mydept/** #替代上面访问
ignored-services: springcloud-provider-dept #忽略,不能再使用这个路径访问。
#ignored-services: "*" 隐藏全部服务
prefix: /kuang #设置公共访问前缀
•去hosts里面添加一个本地ip //看自己选择
•开启功能
@EnableZuulProxy
•测试