为什么要使用微服务网关
一个系统拆分成若干个微服务后,如果不使用网关,那么客户端会请求很多个不同的微服务,增加了客户端的复杂性。另外可能还会存在一些跨域的请求、微服务访问的问题、重构困难等问题。
整合zuul网关
首先创建项目micro-service-zuul,需要引入spring-cloud-starter-zuul的依赖,pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<artifactId>micro-service-zuul</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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>
<!-- 重试机制 ,必须配,否则重试不生效 -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>micro-service-zuul</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml的配置文件为:
spring:
application:
name: zuul
cloud:
#启动负载均衡的重试机制,默认false
loadbalancer:
retry:
enabled: true
zuul:
ribbon:
eager-load: #ribbon饿加载
enabled: true
add-host-header: true
servlet-path: /
# host:
# socket-timeout-millis: 10000
# connect-timeout-millis: 10000
retryable: true
hystrix:
command:
default:
execution:
timeout:
enabled: false #关闭Hystrix的超时
isolation:
thread:
timeoutInMilliseconds: 84000 #需要大于ribbon的超时时间,不然不会触发重试
ribbon:
ConnectTimeout: 2000
ReadTimeout: 40000 #真正起作用的超时时间
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 0
OkToRetryOnAllOperations: true #默认为false,则只允许GET请求被重试
eureka:
enabled: true
# 使用okhttp3.OKHttpClient
okhttp:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
management:
security:
enabled: false
logging:
level: debug
server:
port: 8040
接下来是启动类,我们需要在启动类上添加@EnableZuulProxy注解,声明这是一个Zuul代理。该注解使用Ribbon来定位注册在Eureka Server中的微服务;同时该代理还整合了Hystrix,从而实现了容错,所有经过Zuul的请求都会在Hystrix命令中执行。
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
@Bean
public PreRequestLogFilter preRequestLogFilter() {
return new PreRequestLogFilter();
}
@Bean
public AfterRequestLogFilter afterRequestLogFilter() {
return new AfterRequestLogFilter();
}
@Bean
public RoutingRequestLogFilter routingRequestLogFilter() {
return new RoutingRequestLogFilter();
}
}
完成以上的步骤,我们就将Zuul注册到了Eureka上了,默认情况下Zuul会代理所有注册到Eureka Server的微服务,并且Zuul的路由规则如下:
http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的ServiceId/**,会被转发到serviceId对应的微服务。
分别启动micro-service-discovery-eureka、micro-service-provider-user、micro-service-zuul、micro-service-hystrix-dashboard、micro-service-consumer-ribbon-hystrix。
浏览器请求:http://localhost:8040/cunsumer-ribbon-custom-hystrix/movie/ribbon/1会被路由到:http://localhost:7908/movie/ribbon/1
但奇怪的是请求:http://localhost:8040/micro-service-provider/user/simple/1会报错No route found for uri: /micro-service-provider/user/simple/1。
此时将micro-service-provider-user的spring.application.name值改为micro-service-provider,将虚拟主机名virtual-host-name和appname的配置注释掉,然后重启执行就可以了。
在HystrixDashboard中输入http://localhost:8040/hystrix.stream,提示:Unable to connect to Command Metric Stream。
输入http://localhost:8040/cunsumer-ribbon-custom-hystrix/hystrix.stream可以看到监控信息。
出现上述问题的原因是我在apllication.yml文件中配置了:
# servlet-path: /
把上面这个注释掉即可。
zuul的路由端点
由于spring-cloud-starter-zuul已经包含了spring-boot-starter-actuator,当@EnableZuulProxy注解和Spring Boot Actuator结合时,Zuul会暴露一个路由管理断点/routes,使用GET方法访问该端点,即可返回Zuul当前映射的路由列表,如果使用POST方法访问该断点,会强制刷新zuul的路由列表。
使用GET方法访问:http://localhost:8040/routes
返回的结果为:
{"/user-haha/**":"user-haha","/cunsumer-ribbon-custom-hystrix/**":"cunsumer-ribbon-custom-hystrix"}
可以看到zuul映射micro-service-provider-user的路径是/user-haha,这正是我配置的appname,可惜请求http://localhost:8040/user-haha/user/simple/1后,
依旧报Caused by: com.netflix.client.ClientException: Load balancer does not have available server for client: user-haha这个错误。还是需要将虚拟主机名virtual-host-name和appname的配置注释掉。