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>服务网关服务应用
GatewayServerApplication1
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.yml1
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>启动类添加
@EnableZuulProxy1
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.ZuulControllerZuul网关的控制器,内部调用是ZuulServletcom.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>服务应用启动类
GatewayApplication1
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路由信息的仓储