SpringCloudGateway 简介及使⽤
Gateway 是什么
Spring Cloud Gateway是Spring Cloud官⽅推出的第⼆代⽹关框架,取代Zuul⽹关。⽹关作为流量的,在微服务系统中有着⾮常作⽤,⽹关常见的功能有路由转发、权限校验、限流控制等作⽤。特性基于Spring 5,Reactor(模式) 和 SpringBoot 2.0能够在任何请求属性上匹配路由断⾔和过滤器是特定于路由的Hystrix断路器集成SpringCloud DiscoveryClient集成易于编写断⾔和过滤器请求速率限制
路径重写
使⽤
⾸先搭建⼀个服务的注册中⼼,⽤来发现服务的
eureka-server 项⽬
启动类如下:
1
2
3
4@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}
1
2
3
4
5
6
7
8
9
10
然后搭建⼀个服务项⽬,注册到注册中⼼
service-one 项⽬
启动类如下:
1
2
3
4
5
67
8
9
10
11
12
13
14 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>1
2
3
4
5
6
7
8
9@SpringBootApplication @EnableEurekaClient public class ServiceOneApplication { public static v
oid main(String[] args) { SpringApplication.run(ServiceOneApplication.class, args); }}
1
2
34
5
6
7
8
9spring: application: name: service-one server: port: 8081eureka: client: service-url: defaultZone: localhost:8761/eureka/
1
2
3
4
5
6
7
8
9
10
springboot框架的作用最后创建⼀个⽹关项⽬,也注册到注册中⼼
gateway-client 项⽬
使⽤RouteLocator的Bean进⾏路由转发,将请求进⾏处理,最后转发到⽬标的下游服务。
启动类内容如下:@RequestMapping("/order")@RestController public class OrderController { @RequestMapping("/info") public String orderInfo() { return "order info date : " + new Date().toString(); }}
1
2
3
4
5
6
7
8
9
10
11@RequestMapping("/user")@RestController public class UserController { @RequestMapping("user") public String getUser() { return "I am the most handsome"; }}
1
2
3
4
5
6
7
8
9
1011 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
1
2
3
4
5
6
7
8
StripPrefix 属性的使⽤@SpringBootApplication public class GatewayClientApplication { @Value("${test.uri}") private String uri; @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { utes() //basic proxy .route(r -> r.path("/order/**") .uri(uri) ).build(); } public static void main(String[] args) { SpringApplication.run(GatewayClientApplication.class, args); }}这段代码是在访问 localhost:8080/order/ 的时候,经过⽹关将请求转发到 service-one:8081/order/ ,
service-one 服务在eureka 中有注册,最终会对应服务的ip:port
1
2
3
456
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23test: uri: lb://service-one spring: application: name: gateway-client cloud: gateway: routes: - id: route_service_one uri: ${test.uri} # uri 以lb://开头(lb 代表从注册中⼼获取服务),后⾯接的就是你需要转发到的服务名称 predicates: - Path=/user/**server: port: 8080logging: level: org.springframework.cloud.gateway: TRACE org.springframework.active: DEBUG org.active: DEBUG reactor.ipcty: DEBUG eureka: client: service-url: defaultZone: localhost:8761/eureka/ instance: prefer-ip-address: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
按照上⾯的配置,每⼀个路由只能对应⼀个控制器的转发,不够灵活,假如我想让userapi的请求都转到service-one服务。⽐如:>>
>>
修改⼀下gateway-client项⽬的配置⽂件如下:
集成Hystrix
在gateway-client项⽬中引⼊依赖:
在spring cloud gateway中可以使⽤Hystrix。Hystrix是 spring cloud中⼀个服务熔断降级的组件,在微服务系统有着⼗分重要的作⽤。Hystrix是 spring cloud gateway中是以filter的形式使⽤的。
启动类代码改成如下:test: uri: lb://service-one spring: application: name: gateway-client cloud: gateway: routes: - id: route_service_one uri: ${test.uri} # uri 以lb://开头(lb 代表从注册中⼼获取服务),后⾯接的就是你需要转发到的服务名称 predicates: - Path=/userapi/** filters: - StripPrefix=1 # 表⽰在转发时去掉userapi server: port: 8080logging: level: org.springframework.cloud.gateway: TRACE org.springframework.active: DEBUG org.active: DEBUG reactor.ipcty: DEBUG eureka: client: service-url: defaultZone: localhost:8761/eureka/ instance: prefer-ip-address: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
1
2
3
4
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论