Spring Cloud服务网关
- 服务网关特性
- 基于
RestTemplate
自定义服务网关 Spring Cloud Netflix Zuul
网关Spring Cloud Gateway
网关
Spring Cloud服务网关
版本信息
Spring Cloud : Hoxton.SR1
Spring Boot : 2.2.2.RELEASE
Zookeeper : 3.5.6 (注册中心使用)
服务网关特性
服务网关是干什么用的?
- 认证
- 安全(授权)
- 动态路由
基于RestTemplate
自定义服务网关
服务端演示提供服务
添加
pom
依赖1
2
3
4
5
6
7
8
9
10
11
12
13
14<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--zookeeper 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>服务网关服务应用
GatewayServerApplication
1
2
3
4
5
6
7
8
9
10
11
12
13/**
* 服务网关服务应用
*
* @author FelixFly <chenglinxu@yeah.net>
* @date 2020/2/8
*/
@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}演示服务端点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/**
* 演示服务端点
*
* @author FelixFly <chenglinxu@yeah.net>
* @date 2020/2/8
*/
@RestController
public class EchoController {
@Autowired
private Environment environment;
@GetMapping("/echo")
public String echo(String message) {
// 由于采用的是随机端口,这地方必须采用这个方式获取端口
String port = environment.getProperty("local.server.port");
return "ECHO(" + port + "):" + message;
}
}服务配置
application.yml
1
2
3
4
5
6
7
8spring:
application:
name: gateway-server
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
server:
port: 0
启动服务,根据启动日志查看本地的随机端口,此次端口是
54692
http://127.0.0.1:54692/echo?message=Hello
返回信息ECHO(54692):Hello
自定义服务网关
基于
Servlet
配置文件application.yml
1 |
|
基于DiscoveryClient
GatewayCustomServlet
网关Servlet
包名:
top.felixfly.cloud.gateway.custom
1 |
|
服务启动类GatewayZuulApplication
1 |
|
启动服务,访问地址
http://127.0.0.1:7070/gateway/gateway-server/echo?message=hello
返回信息ECHO(54692):Hello
基于@LoadBlanced
GatewayLoadBalancedServlet
负载均衡Servlet
包名:
top.felixfly.cloud.gateway.custom
1 |
|
服务启动类GatewayZuulApplication
1 |
|
启动服务,访问地址
http://127.0.0.1:7070/gatewaylb/gateway-server/echo?message=hello
返回信息ECHO(54692):Hello
配置文件application.yml
调整
默认是启用
ribbon
进行负载均衡,可以关闭ribbon
使用Spring Cloud LoadBalanced
1 |
|
Spring Cloud Netflix Zuul
网关
基本使用
添加
pom
依赖1
2
3
4
5<!--zuul 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>启动类添加
@EnableZuulProxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22/**
* 服务网关 Zuul 服务
*
* @author FelixFly <chenglinxu@yeah.net>
* @date 2020/2/8
*/
@SpringBootApplication
@EnableZuulProxy
@ServletComponentScan(basePackages = "top.felixfly.cloud.gateway.custom")
public class GatewayZuulApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayZuulApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}配置文件
application.yml
添加路由配置1
2
3
4
5
6
7
8
9
10
11
12
13
14spring:
application:
name: gateway-zuul
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
loadbalancer:
ribbon:
enabled: false # 关闭Ribbon
server:
port: 7070
zuul:
routes:
gateway-server: /gateway-server/**默认不配置
uri
,自动启用负载均衡,直连配置1
2
3
4
5zuul:
routes:
gateway-server:
path: /gateway-server/**
url: http://127.0.0.1:54692
启动服务,访问地址
http://127.0.0.1:7070/gateway-server/echo?message=hello
返回信息ECHO(54692):Hello
原理分析
@EnableZuulProxy
注解
1 |
|
启用了服务熔断以及导入了ZuulProxyMarkerConfiguration
ZuulProxyMarkerConfiguration
源码
1 |
|
注册了一个Marker
的Bean
,这个Bean
会作为装配条件
ZuulProxyAutoConfiguration
自动配置类
1 |
|
父类ZuulServerAutoConfiguration
自动配置类
1 |
|
从上可知:注册了
ZuulController
和ZuulServlet
ZuulController
访问地址针对的是根路径”/“,也就是基本使用的地址
ZuulServlet
访问地址针对的是Servlet
配置地址,默认是“/zuul”,如是访问地址更改为:http://127.0.0.1:7070/zuul/gateway-server/echo?message=hello
也是可以返回正常结果返回信息ECHO(54692):Hello
核心api
org.springframework.cloud.netflix.zuul.web.ZuulController
Zuul
网关的控制器,内部调用是ZuulServlet
com.netflix.zuul.http.ZuulServlet
网关的核心处理类com.netflix.zuul.ZuulFilter
网关的过滤器,有如下类型pre
前置过滤器route
路由过滤器post
后置过滤器error
错误过滤器
org.springframework.cloud.netflix.zuul.filters.RouteLocator
路由列表加载器org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator
可刷新的路由列表加载器
有两种实现:
org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator
服务发现路由列表加载器org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator
本地配置的路由列表加载器
默认实现:
org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator
Spring Cloud Gateway
网关
基本使用
添加
pom
依赖1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--zookeeper 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!--gateway 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>服务应用启动类
GatewayApplication
1
2
3
4
5
6
7
8
9
10
11
12
13/**
* 服务网关应用服务
*
* @author FelixFly <chenglinxu@yeah.net>
* @date 2020/2/9
*/
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}配置文件
application.yml
(直连方式)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20spring:
application:
name: gateway-gateway
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
loadbalancer:
ribbon:
enabled: false # 关闭Ribbon
gateway:
routes:
- id: gateway-server
uri: http://127.0.0.1:54692
predicates:
- Path=/gateway-server/**
filters:
- StripPrefix=1 #去掉Path一个/路径分割,http://127.0.0.1:54692/**
server:
port: 6060通过
uri
直连方式,访问地址:http://127.0.0.1:6060/gateway-server/echo?message=hello
访问结果:
ECHO(54692):hello
调整配置文件
application.yml
(服务发现方式)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21spring:
application:
name: gateway-gateway
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
loadbalancer:
ribbon:
enabled: false # 关闭Ribbon
gateway:
routes:
- id: gateway-server
#uri: http://127.0.0.1:54692
uri: lb://gateway-server
predicates:
- Path=/gateway-server/**
filters:
- StripPrefix=1 #去掉Path一个/路径分割,http://127.0.0.1:54692/**
server:
port: 6060服务发现方式(负载均衡),访问地址:
http://127.0.0.1:6060/gateway-server/echo?message=hello
访问结果:
ECHO(54692):hello
核心api
org.springframework.cloud.gateway.handler.predicate.RoutePredicateFactory
路由匹配工厂PathRoutePredicateFactory
路径路由匹配- …
org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory
网关过滤器工厂org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory
去掉前缀过滤器工厂- …
org.springframework.cloud.gateway.filter.GatewayFilter
网关过滤器org.springframework.cloud.gateway.filter.GatewayFilterChain
网关过滤器的链org.springframework.cloud.gateway.filter.GlobalFilter
全局的过滤器org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter
负载均衡的过滤器- …
org.springframework.cloud.gateway.route.RouteDefinition
路由信息的定义org.springframework.cloud.gateway.route.RouteDefinitionLocator
路由信息加载器org.springframework.cloud.gateway.route.RouteDefinitionRepository
路由信息的仓储