0%

springboot+eureka+gateway实现网关模型

springboot+eureka+gateway实现网关模型

网关可提供请求路由与组合、协议转换、安全认证、服务鉴权、流量控制与日志监控等服务。可选的网关有不少,比如 Nginx、、Linkerd 、eureka、 Spring Cloud Gateway、consul等。

Spring Cloud Gateway 针对进来的请求做各种判断和处理,比如说判断请求的合法性、权限验证,请求地址改写,请求参数、头信息、cookie 信息的分析和改写,请求速率控制,日志留存等。而这些都可以方便的通过 Predicate 和 GatewayFilter 来组合实现。

创建gateway-service服务

引入依赖

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
33
34
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.4</version>
</dependency>
<!--服务注册/发现中心依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!--服务的配置中心依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!--fegin组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Feign Client for loadBalancing -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.0.2</version>
</dependency>
<!--客户端负载均衡loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

yml配置

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
server:
port: 8081
servlet:
context-path: /
#spring 相关配置
spring:
main:
allow-bean-definition-overriding:
true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
application:
name: springboot-eureka-client
cloud:
gateway:
routes:
# 透传服务
- id: gateway-client #设置路由id(理论上是可以随便写的)
uri: lb://gateway-client #设置路由的url lb:/服务注册名称
predicates:
- Path=/client/** #路径匹配规则*/
filters:
- StripPrefix=1
- id: gateway-consumer
uri: lb://gateway-consumer
predicates:
- Path=/consumer/** #*/
filters:
- StripPrefix=1
- id: test_route
uri: https://www.baidu.com
predicates:
#访问路径:localhost:88/hello?url=baidu 代表跳转到www.baidu.com/hello
#url带有baidu的自动跳转到https://www.baidu.com
- Query=url,baidu
eureka:
instance:
appname: springboot-eureka-client
prefer-ip-address: true
client:
register-with-eureka: true
enabled: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8083/eureka/



#模块配置
gisquest-client:

logging:
level:
com.gisquest: info
com.springboot.test.mapper: debug
file: log/MineEurekaClient.log

gateway启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.example.gatewaystudy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.result.view.Rendering;

@EnableEurekaClient
@SpringBootApplication
public class GatewayStudyApplication {

public static void main(String[] args) {
SpringApplication.run(GatewayStudyApplication.class, args);
}

}

跨域配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);

return new CorsWebFilter(source);
}
}
创建gateway-client服务

引入依赖

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
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!--服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<!--服务调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

yml配置

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
33
34
35
36
server:
port: 8084
servlet:
context-path: /
#spring 相关配置
spring:
main:
allow-bean-definition-overriding:
true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
application:
name: gateway-client
eureka:
instance:
appname: springboot-eureka-client
prefer-ip-address: true
client:
register-with-eureka: true
enabled: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8083/eureka/

#模块配置
gisquest-client:

logging:
level:
com.gisquest: info
com.springboot.test.mapper: debug
file: log/MineEurekaClient.log


控制层请求

1
2
3
4
5
6
7
8
9
@RestController
public class TestController {

@RequestMapping("/index")
public String index(){
return "gateway-client";
}

}

启动类

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayClientApplication {

public static void main(String[] args) {
SpringApplication.run(GatewayClientApplication.class, args);
}

}
效果

访问 http://localhost:8001/client/index 实际上网关把请求发送到gateway-client服务,返回结果gateway-client