本文主要介绍如何使用Spring Gateway整合Nacos配置中心与Nacos注册中心实现一个可动态配置的网关
一、部署并启动Nacos
该部分非本文的核心内容,故将直接使用docker单机部署的方式快速启动。
# 拉取镜像
docker pull nacos/nacos-server
# 启动容器,nacos的默认端口号为8848
docker run -itd \
--name nacos \
-p 8848:8848 \
-e MODE=standalone \
nacos/nacos-server
从浏览器打开http://10.144.0.8:8848/nacos/
,可以看到已经启动成功了。
二、Spring Gateway
(一)作为独立模块,不接入Nacos
1. pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.12</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
2. application.yml
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: nacos
order: 0
filters:
- StripPrefix=0
predicates:
- Path=/nacos/**
uri: http://10.144.0.8:8848
3. GatewayApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
4. 启动Gateway
运行Spring Boot项目,打开http://10.144.0.1:8080/nacos/
可以看到,我们也能打开如(一)
中的nacos页面,说明已经启动并配置成功。
(二)使用Nacos作为配置中心
1. pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
2. bootstrap.yml
此处需注意,采用nacos配置中心,需要从nacos读取配置信息,因此必须将配置中心的信息写在
bootstrap.yml
中,而非application.yml
中,否则将无法从配置中心加载。
同时,在3.0以上版本的Spring Cloud中,需要手动导入bootstrap
依赖。
spring:
application:
name: gateway
cloud:
nacos:
config:
server-addr: http://10.144.0.8:8848
username: nacos
password: nacos
file-extension: yaml
3. GatewayApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
4. Nacos配置管理
打开nacos配置列表,创建一个新的配置。要求满足:
Data ID
与bootstrap.yml
中的spring.application.name
相同;Group
与bootstrap.yml
中的spring.cloud.nacos.config.group
相同;配置格式
与bootstrap.yml
中的spring.cloud.nacos.config.file-extension
相同;- 配置内容与
(一)
中的application.yml
格式相同,例如:
spring:
cloud:
gateway:
routes:
- id: nacos
order: 0
filters:
- StripPrefix=0
predicates:
- Path=/nacos/**
uri: http://10.144.0.8:8848
再次打开http://10.144.0.1:8080/nacos/
,可以发现与上述(一)
中的效果完全一致
(三)接入Nacos注册中心并自动转发路由
1. pom.xml
因为nacos已经不再支持ribbon组件,因此此处需要手动导入
spring-cloud-loadbalancer
。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
2. bootstrap.yml
spring:
application:
name: gateway
cloud:
nacos:
config:
server-addr: http://10.144.0.8:8848
username: nacos
password: nacos
file-extension: yaml
discovery:
server-addr: http://10.144.0.8:8848
username: nacos
password: nacos
3. GatewayApplication.java
需要在
GatewayApplication
上添加EnableDiscoveryClient
注解以开启服务发现。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
启动后,可以看到Gateway服务已经成功注册到nacos了。
4. 自动转发已注册的服务
在application.yml
或bootstrap.yml
中添加以下内容启用网关发现功能:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
filters:
- StripPrefix=1
5. 测试过程
(1)创建python-flask服务并注册到nacos。
#/usr/bin/python3
# 创建flask服务
from flask import Flask
app = Flask(__name__)
@app.get("/hello")
def hello():
return "Hello, world."
# 将flask服务注册到nacos
from nacos import NacosClient
client = NacosClient(
"10.144.0.8:8848", # nacos地址
username="nacos",
password="nacos",
)
client.add_naming_instance(
service_name="flask",
ip="10.144.0.1", # flask服务地址
port=5000, # flask服务端口
ephemeral=False,
)
# 启动flask
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=5000
)
(2)查看nacos服务列表
可以看到已经注册成功。
(3)请求服务
# 直接请求:
curl http://10.144.0.1:5000/hello
# Hello, world.
# 使用网关:
curl http://10.144.0.1:8080/flask/hello # 其中 /flask/ 为服务在nacos注册的服务名
# Hello, world.
可以看到两种方法都可以成功。
(四)补充:lb的使用
在nacos配置中心中添加以下内容:
spring:
cloud:
gateway:
routes:
- id: flask
order: 0
filters:
- StripPrefix=0
predicates:
- Path=/hello
uri: lb://flask/hello
即将/hello
的请求转发到flask
服务下的/hello
路由,同样可以成功。