赞
踩
我们的前篇示例,是单独使用Gateway,我们只导入了Gateway的依赖,路由到uri配置的是实际的服务提供方地址,在Spring Cloud环境下,服务的提供方可能是一个集群,这时我们就需要将我们前面提到的Nacos相关的配置、Spring Cloud LoadBalancer依赖等加入,所以我们完整的示例如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>myGateway</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<build.name>myGateway</build.name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.cloud.version>2021.0.4</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 使用Nacos服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 使用Nacos配置管理 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 支持读取bootstrap配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- 使用spring-cloud-starter-loadbalancer做负载均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- 添加网关支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<profileActive>pre</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>${build.name}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>mapper/**/*.xml</include>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>application.properties</include>
<include>application-${profileActive}.properties</include>
<include>bootstrap.properties</include>
<include>logback-spring.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
service: myGateway
gateway:
routes:
- id: myCloud_route
# uri: http://127.0.0.1:8701/myCloud
uri: lb://myCloud # lb开头来表示使用服务发现负载均衡来路由,后面就是我们注册的服务名称
predicates:
- Path=/myCloud/**
我们配置了服务发现的Nacos地址,uri的配置使用 lb 开头,这样网关就会根据服务名通过负载均衡策略查找对应的服务来进行调用了。
server:
ssl:
enabled: true
key-alias: scg
key-store-password: scg1234
key-store: classpath:scg-keystore.p12
key-store-type: PKCS12
spring:
cloud:
gateway:
httpclient:
ssl:
useInsecureTrustManager: true
注:此设置不推荐在生产环境使用
spring:
cloud:
gateway:
httpclient:
ssl:
trustedX509Certificates:
- cert1.pem
- cert2.pem
spring:
cloud:
gateway:
httpclient:
ssl:
handshake-timeout-millis: 10000 # 此值即为默认值
close-notify-flush-timeout-millis: 3000 # 此值即为默认值
close-notify-read-timeout-millis: 0 # 此值即为默认值
我们前文提到:路由是网关的基础构件,其内部包括id(路由的名称)、uri(目标url)、predicates(断言集合)、filters(过滤器集合),其实路由还包括一个字段(即路由元数据 metadata,其类型为 Map<String, Object> )
spring:
cloud:
gateway:
routes:
- id: route_with_metadata
uri: https://example.org
metadata:
optionName: "OptionValue"
compositeObject:
name: "value"
iAmNumber: 1
设置好的元数据,我们可以在Filter中使用ServerWebExchange exchange对象获取
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
// get all metadata properties
route.getMetadata();
// get a single metadata property
route.getMetadata(someKey);
spring:
cloud:
gateway:
httpclient:
connect-timeout: 1000 # 连接超时,必须为毫秒格式
response-timeout: 5s # 响应超时,必须为 java.time.Duration 格式
为负值则不超时,建议不要设置为负值
- id: per_route_timeouts
uri: https://example.org
predicates:
- name: Path
args:
pattern: /delay/{timeout}
metadata:
response-timeout: 200 # 必须是毫秒格式
connect-timeout: 200 # 必须是毫秒格式
注意:这里的response-timeout 和全局配置的格式不一样
以下以logback配置为例
<!-- 网关的日志支持配置 -->
<logger name="org.springframework.cloud.gateway" level="DEBUG"/>
<logger name="org.springframework.http.server.reactive" level="DEBUG"/>
<logger name="org.springframework.web.reactive" level="DEBUG"/>
<logger name="org.springframework.boot.autoconfigure.web" level="DEBUG"/>
<logger name="reactor.netty" level="DEBUG"/>
<logger name="redisratelimiter" level="DEBUG"/>
DEBUG的信息就比较详细了,如果还想更详细,日志级别可以配置为TRACE,以便可以排查。可根据自身的实际要求来配置。
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "https://docs.spring.io"
allowedMethods:
- GET
cors-configurations是一个Map<String, CorsConfiguration>,CorsConfiguration包含了对cors的参数定义。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。