在微服务的架构中,服务网关就是一个介于客户端与服务端之间的中间层。在这种情况下,客户端只需要跟服务网关交互,无需调用具体的微服务接口。这样的好处在于,客户端可以降低复杂性,无需关注具体是哪个微服务在提供服务。这一节我们将使用Spring Cloud Zuul搭建微服务网关elsa-gateway。
网关服务器搭建
创建认证服务器子项目
File==>新建==>Other==>搜索Maven,选择Maven Module,然后Next
填写Module Name:elsa-gateway,点击Next
一直Next至FInish为止,创建完成,项目结构如下
网关服务器项目已经创建完成,下面我们做相关依赖和配置。
网关服务器引入依赖
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.elsa</groupId>
<artifactId>elsa-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>elsa-gateway</artifactId>
<name>Elsa-Gateway</name>
<description>Elsa-Gateway微服务网关</description>
<dependencies>
<dependency>
<groupId>com.elsa</groupId>
<artifactId>elsa-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
网关服务器人口类
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ElsaGatewayApp {
public static void main(String[] args) {
SpringApplication.run(ElsaGatewayApp.class, args);
}
}
- @EnableDiscoveryClient注解,开启服务注册与发现
- @EnableZuulProxy注解,开启Zuul服务网关功能
基本配置文件配置
在resources目下下创建application.xml文件,配置如下
server:
port: 8301
spring:
application:
name: Elsa-Gateway
zuul:
routes:
auth:
# 以/auth开头的请求都会被转发到名称为Elsa-Auth的服务上
path: /auth/**
# 服务名
serviceId: Elsa-Auth
# 由于我们需要在请求头中携带令牌,所以sensitiveHeaders设置为*,表示不过滤请求头信息,即请求的请求头信息将原封不动的转发出去
sensitiveHeaders: "*"
# 设置为true时,表示开启重试机制;
retryable: true
# Zuul配合Eureka后会有一套默认的配置规则,这里我们只想请求根据我们显示配置的路由规则走,所以设置为*,表示关闭所有默认路由配置规则;
ignored-services: "*"
ribbon:
eager-load:
# Zuul内部通过Ribbon按照一定的负载均衡算法来获取服务,Ribbon进行客户端负载均衡的Client并不是在服务启动的时候就初始化好的,而是在调用的时候才会去创建相应的Client,所以第一次调用的耗时不仅仅包含发送HTTP请求的时间,还包含了创建RibbonClient的时间,这样一来如果创建时间速度较慢,同时设置的超时时间又比较短的话,第一次请求很容易出现超时的情况。设置为true的时候表示开启Ribbon的饥饿加载模式,即在应用启动的时候就去获取相应的Client备用。
enabled: true
ribbon:
# 设置请求超时时间,单位为毫秒;
ReadTimeout: 3000
eureka:
instance:
# 向Eureka 服务端发送心跳的间隔时间,单位为秒,用于服务续约。这里配置为20秒,即每隔20秒向febs-register发送心跳,表明当前服务没有宕机
lease-renewal-interval-in-seconds: 20
client:
# 为true时表示将当前服务注册到Eureak服务端
register-with-eureka: true
# 为true时表示从Eureka 服务端获取注册的服务信息
fetch-registry: true
# 新实例信息的变化到Eureka服务端的间隔时间,单位为秒
instance-info-replication-interval-seconds: 30
# 默认值为30秒,即每30秒去Eureka服务端上获取服务并缓存,这里指定为3秒的原因是方便开发时测试,实际可以指定为默认值即可;
registry-fetch-interval-seconds: 3
serviceUrl:
# 指定Eureka服务端地址
defaultZone: http://elsa:123456@localhost:8001/register/eureka/
此文件主要配置eureka与zuul相关配置,主要说明请看注释。
web安全配置类
在elsa-gateway模块下的com.elsa.gateway路径下新增configure包,然后在该包下新增ElsaGatewaySecurityConfigure配置类:
@EnableWebSecurity
public class ElsaGatewaySecurityConfigure extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
因为elsa-gateway引入了elsa-common模块,elsa-common模块包含了Spring Cloud Security依赖,所以我们需要定义一个自己的WebSecurity配置类,来覆盖默认的。这里主要是关闭了csrf功能,否则会报csrf相关异常。
banner
resources目录下新增banner.txt,具体参照从零开始搭建微服务:微服务注册中心
PostMan测试
分别启动应用
1.redis
2.ElsaRegesterApp
3.ElsaAuthApp
4.ElsaGateway
PostMan测试内容与从零开始搭建微服务:认证服务器内容相似,主要是把端口由认证服务器的8101改为8301即可,就不再一一说明。
测试令牌获取
获取受保护资源
/oauth/test测试
注销测试
刷新令牌
可以看到,认证服务器的几个测试都可以成功。
源码下载
源码地址:网关服务器