在上篇中介绍了SpringCloud Config的完美使用版本,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的路由网关(SpringCloud Zuul)的使用教程。git
Spring Cloud Zuul 主要的功能是提供负载均衡、反向代理、权限认证、动态路由、监控、弹性、安全等的边缘服务。其主要做用是为微服务架构提供了前门保护的做用,同时将权限控制这些较重的非业务逻辑内容迁移到服务路由层面,使得服务集群主体可以具有更高的可复用性和可测试性。github
通俗一点来讲,就是对服务提供一层保护,对外界的请求进行过滤转发到后端服务中。
这里咱们能够经过几张简单的示例图来进行了解.。web
不使用路由网关的示例图:spring
使用路由网关的示例图:后端
使用路由网关而且加上注册中心的示例图:浏览器
从上述的示例图中,咱们发现加了路由网关以后,其实是将一对多的关系转变成了一对一的关系,这样的好处是咱们能够在网关层进行数据合法校验、权限认证、负载均衡等统一处理,这样能够在很大的程度上节省的人力和物力,可是这种方式也有必定的弊端,就是之后新增了服务或者在服务中新增方法,就会使得网关层可能须要进行改动。幸亏在Spring Cloud 有 Zuul 这样一个组件,经过eureka将网关和微服务之间相互关联起来,都会在eureka上进行注册,这样Zuul就能感知到哪些服务在线,而且能够经过配置路由规则将请求自动转发到指定的后端微服务上,这样即便后续新增了服务或者在服务中新增了某些方法,那么只需在Zuul层进行简单配置便可。安全
开发环境架构
注:不必定非要用上述的版本,能够根据状况进行相应的调整。须要注意的是SpringBoot2.x之后,jdk的版本必须是1.8以上!app
因为咱们这里是使用的第三种模式,因此须要使用到Eureka注册中心,所以这里咱们也顺便弄一个注册中心服务。
注册中心这块配置和代码和以前springcloud-config配置基本同样便可。注册中心新项目的的名称为springcloud-zuul-eureka
。负载均衡
注册中心pom
配置、application.properties
配置和代码以下:
pom:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
application.properties:
spring.application.name=springcloud-zuul-eureka server.port=8006 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
代码:
@EnableEurekaServer @SpringBootApplication public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); System.out.println("zuul注册中心服务启动..."); } }
注册中心服务配置完成以后,咱们在新增一个Zuul服务,该服务的名称为springcloud-zuul-gateway
,该服务的 pom
配置、application.properties
配置和代码以下:
pom:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <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> </dependencies>
application.properties:
spring.application.name=springcloud-zuul-gateway server.port = 9009 eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/ zuul.routes.hello.path = /hello/** zuul.routes.hello.url = http://localhost:9010/hello zuul.routes.hi.path = /hi/** zuul.routes.hi.url = http://localhost:9011/hi
配置说明:
注:上述的zuul.routes.{route}.path
和zuul.routes.{route}.url
通常来讲是做为传统的方式进行配置,是不依赖于Eureka,是属于一对一的配置。例如,访问:http://localhost:9009/hello/pancm
的话就会跳转到http://localhost:9010/hello/pancm
地址上。
代码:
@SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); System.out.println("zuul 服务启动..."); } }
这里咱们也须要建立两个客户端服务,来进行验证Zuul路由网关是否生效。
建立两个客户端服务,名称分别为springcloud-zuul-server1
和springcloud-zuul-server2
,两个pom文件的配置以下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
两个application.properties
配置文件也基本相同,除了名称和端口不一致。
springcloud-zuul-server1
的application.properties配置:
spring.application.name=springcloud-zuul-server1 server.port=9010 eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/
springcloud-zuul-server2
的application.properties配置:
spring.application.name=springcloud-zuul-server2 server.port=9011 eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/
springcloud-zuul-server1
服务的代码:
主类
@SpringBootApplication @EnableDiscoveryClient public class ZuulServerApplication1 { public static void main(String[] args) { SpringApplication.run(ZuulServerApplication1.class, args); System.out.println("zuul 第一个服务启动..."); } }
控制层:
@RestController
public class ConsumerController {
@RequestMapping("/hello/{name}") public String index(@PathVariable String name) { return name+",Hello World!"; }
}
springcloud-zuul-server2
服务的代码:
主类
@SpringBootApplication @EnableDiscoveryClient public class ZuulServerApplication2 { public static void main(String[] args) { SpringApplication.run(ZuulServerApplication2.class, args); System.out.println("zuul 第二个服务启动..."); } }
控制层:
@RestController public class ConsumerController { @RequestMapping("/hi") public String index(@RequestParam String name) { return name+",hi!"; } }
注:这里故意将两个服务的接口参数请求和返回值弄成不同,以便对Zull的功能进行多方面测试。
完成上述的代码开发后,咱们来进行测试springcloud-zuul
是否能够地址过滤转发功能。
首先依次启动springcloud-zuul-eureka
、springcloud-zuul-gateway
、springcloud-zuul-server1
和springcloud-zuul-server2
这四个项目。其中9009是服务springcloud-zuul-gateway
的端口,9010是第一个客户端springcloud-zuul-server1
的端口,9011是第二个客户端springcloud-zuul-server2
的端口。
启动成功以后,在浏览器输入:
http://localhost:9010/hello/pancm
界面返回:
pancm,hello world!!
在浏览器输入:
http://localhost:9011/hi?name=pancm
界面返回:
pancm,hi!
能够看出程序正常启动,而且客户端的接口返回正确!
那么咱们再来使用一样路径来访问zuul,由于是在本地进行测试,所以只须要更改下端口就能够了,将上述在浏览器访问的地址的端口自都改为9009。
在浏览器输入:
http://localhost:9009/hello/pancm
界面返回:
pancm,Hello World!
在浏览器输入:
http://localhost:9009/hi?name=pancm
界面返回:
pancm,hi!
示例图:
从上述示例中,咱们能够得出zuul路由网关已经生效了,成功的帮咱们的请求进行了转发!
本篇文章主要介绍了关于zuul的基本使用,使用的方式也是单例的,一个路由规则对应一个地址,按照上述的三幅示例图来讲,主要是介绍了第二种。介于篇幅问题,经过Eureka注册中心方式实现、zuul的详细配置、 以及zuul的核心模块过滤器还未讲解,这些留在下一篇再来说解。
基于SpringBoot2.x、SpringCloud的Finchley版本开发的地址:https://github.com/xuwujing/springcloud-study
若是感受项目不错,但愿能给个star,谢谢!
这首纯音乐听起来有种似曾相识的感受,但仔细听下来,又并不是是本身熟悉的一首。不过真是由于这样,才有感受吧。
原创不易,若是感受不错,但愿留言推荐!您的支持是我写做的最大动力! 版权声明: 做者:虚无境 博客园出处:http://www.cnblogs.com/xuwujing CSDN出处:http://blog.csdn.net/qazwsxpcm 我的博客出处:http://www.panchengming.com