SpringCloud学习系列之六 ----- 路由网关Zuul基础使用教程

328 阅读6分钟

前言

在上篇中介绍了SpringCloud Config的完美使用版本,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的路由网关(SpringCloud Zuul)的使用教程。

SpringCloud Zuul

介绍

Spring Cloud Zuul 主要的功能是提供负载均衡、反向代理、权限认证、动态路由、监控、弹性、安全等的边缘服务。其主要作用是为微服务架构提供了前门保护的作用,同时将权限控制这些较重的非业务逻辑内容迁移到服务路由层面,使得服务集群主体能够具备更高的可复用性和可测试性。

通俗一点来说,就是对服务提供一层保护,对外界的请求进行过滤转发到后端服务中。 这里我们可以通过几张简单的示例图来进行了解.。

不使用路由网关的示例图:

在这里插入图片描述

使用路由网关的示例图:

在这里插入图片描述

使用路由网关并且加上注册中心的示例图:

在这里插入图片描述

从上述的示例图中,我们发现加了路由网关之后,实际上是将一对多的关系转变成了一对一的关系,这样的好处是我们可以在网关层进行数据合法校验、权限认证、负载均衡等统一处理,这样可以在很大的程度上节省的人力和物力,但是这种方式也有一定的弊端,就是以后新增了服务或者在服务中新增方法,就会使得网关层可能需要进行改动。幸好在Spring Cloud 有 Zuul 这样一个组件,通过eureka将网关和微服务之间相互关联起来,都会在eureka上进行注册,这样Zuul就能感知到哪些服务在线,并且可以通过配置路由规则将请求自动转发到指定的后端微服务上,这样即使后续新增了服务或者在服务中新增了某些方法,那么只需在Zuul层进行简单配置即可。

开发准备

开发环境

  • JDK:1.8
  • SpringBoot:2.0.6.RELEASE
  • SpringCloud:Finchley.SR2

注:不一定非要用上述的版本,可以根据情况进行相应的调整。需要注意的是SpringBoot2.x以后,jdk的版本必须是1.8以上!

服务端

由于我们这里是使用的第三种模式,所以需要使用到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

配置说明:

  • spring.application.name: 这个是指定服务名称。
  • server.port:服务指定的端口。
  • eureka.client.serviceUrl.defaultZone: 这个是设置与Eureka Server交互的地址,客户端的查询服务和注册服务都需要依赖这个地址。
  • zuul.routes.{route}.path:自定义路由的规则,通过path配置路径进行过滤;
  • zuul.routes.{route}.url: 自定义路由的规则,访问上述的路径会转发到该配置的地址;

注:上述的zuul.routes.{route}.pathzuul.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-server1springcloud-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-server1application.properties配置:

spring.application.name=springcloud-zuul-server1
server.port=9010
eureka.client.serviceUrl.defaultZone=http://localhost:8006/eureka/

springcloud-zuul-server2application.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-eurekaspringcloud-zuul-gatewayspringcloud-zuul-server1springcloud-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版本开发的地址:github.com/xuwujing/sp…

如果感觉项目不错,希望能给个star,谢谢!

音乐推荐

这首纯音乐听起来有种似曾相识的感觉,但仔细听下来,又并非是自己熟悉的一首。不过真是因为这样,才有感觉吧。

原创不易,如果感觉不错,希望留言推荐!您的支持是我写作的最大动力! 版权声明: 作者:虚无境 博客园出处:www.cnblogs.com/xuwujing CSDN出处:blog.csdn.net/qazwsxpcm     个人博客出处:www.panchengming.com