Spring Cloud构建微服务架构(二):路由网关(Zuul)

版权声明:转载请标明出处 https://blog.csdn.net/weixin_36759405/article/details/82919686

上一篇博客简单介绍了Spring Cloud,微服务的架构,如何解决服务治理中服务统一管理的问题,以及搭建基础的Spring Cloud的教程 。今天则主要介绍Spring Cloud的另一个服务治理组件——服务网关,或者说路由网关。

1. Zuul简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到user服务,/api/shop转发到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。我们可以简单地将它想象为一个类似于Servlet中过滤器(Filter)的概念。

2. Zuul工作原理

就像上图中所描述的一样,Zuul提供了四种过滤器的API,分别为前置(Pre)、后置(Post)、路由(Route)和错误(Error)四种处理方式。

一个请求会先按顺序通过所有的前置过滤器,之后在路由过滤器中转发给后端应用,得到响应后又会通过所有的后置过滤器,最后响应给客户端。在整个流程中如果发生了异常则会跳转到错误过滤器中。

一般来说,如果需要在请求到达后端应用前就进行处理的话,会选择前置过滤器,例如鉴权、请求转发、增加请求参数等行为。在请求完成后需要处理的操作放在后置过滤器中完成,例如统计返回值和调用时间、记录日志、增加跨域头等行为。路由过滤器一般只需要选择Zuul中内置的即可,错误过滤器一般只需要一个,这样可以在Gateway遇到错误逻辑时直接抛出异常中断流程,并直接统一处理返回结果。

四种过滤器的实际应用场景可参考 聊聊 API Gateway 和 Netflix Zuul

3. 动手配置路由网关

在上一个项目的基础上继续我们的路由网关配置。首先新建一个Spring boot项目,命名为api-geteway,并在pom.xml中引入需要的依赖内容:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.9.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-zuul</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Dalston.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

在应用启动类中通过加上@EnableZuulProxy开启网关代理功能,如下:

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ApiGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }
}

在配置文件中作如下配置:

spring.application.name=api-gateway
server.port=9090
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka

# 禁止服务自动添加
zuul.ignoredServices='*'

zuul.routes.api-a.path=/customer/**
zuul.routes.api-a.serviceId=eureka-customer

zuul.routes.api-b.path=/order/**
zuul.routes.api-b.serviceId=eureka-order

完成以上的配置后,我们在请求/api/customer/时会由Zuul转发到对应的eureka-customer服务,其中eureka-customer对应我们第一篇博客中在搭建eureka时注册的服务名。同样我们在请求/api/order/时会由Zuul转发到eureka-order服务。

我们启动api-gateway服务,将其注册到服务注册中心上,出现下图内容表示网关服务注册成功:

我们在eureka-customer中写一个服务进行测试,代码如下:

@RestController
@RequestMapping(value = "/test")
@Api(value = "测试", description = "测试模块", position = 1)
public class TestController {

    @ApiOperation(value = "返回用户输入的结果", notes = "返回用户输入的结果")
    @RequestMapping(value = "/result", method = RequestMethod.GET)
    public String test(@RequestParam(value = "text") String text) {
        return text;
    }
}

然后我们在游览器中输入 http://127.0.0.1:9090/customer/test/result?text=Coderqian,请注意,我们这里访问的是9090端口,而不是8200端口,实际执行结果:

到这里我们就已经发现Zuul的作用了,它可以将我们的请求分发到具体的服务上,而不是需要我们指定每一个服务的访问路径,当然这只是Zuul最基本的功能,但也是因为Zuul具备服务分发的功能,我们可以在服务分发之前进行权限校验,负载分配,可以对请求的结果进行洞察监测等等其他功能。

后面会陆续为大家介绍Zuul以及Zuul结合其他服务组件实现更多更强大的功能。项目地址:https://github.com/coderqianlq/spring-cloud

猜你喜欢

转载自blog.csdn.net/weixin_36759405/article/details/82919686