spring cloud 2.x版本 Zuul路由网关教程
前言
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3
本文基于前两篇文章eureka-server、eureka-client、eureka-ribbon和eureka-feign的实现。
参考
概念
Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,例如将请求/api/goods转发到商品服务上、/api/order转发到订单服务上等。
Zull默认和Ribbon结合实现了负载均衡功能。
创建Zuul工程
1.1 创建sping boot工程:eureka-zuul
1.2 添加pom.xml相关依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
1.3 application添加配置信息
spring: application: name: eureka-zuul server: port: 8400 eureka: instance: hostname: localhost lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/ zuul: routes: eureka-ribbon: #对应服务名称,可以自定义(最好保持一致) path: /ribbon/* serviceId: eureka-ribbon #对应服务名称 eureka-feign: path: /eureka-feign/* serviceId: eureka-feign #对应服务名称
1.4 启动类EurekaZuulApplication增加注解
package spring.cloud.demo.eurekazuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class EurekaZuulApplication { public static void