Spring Cloud Alibaba官方:https://sca.aliyun.com/zh-cn/
Spring Cloud官網(wǎng):https://spring.io/projects/spring-cloud
Spring Cloud與Spring Cloud Alibaba版本對應(yīng)說明:https://sca.aliyun.com/zh-cn/docs/2022.0.0.0/overview/version-explain
1、nacos下載安裝
下載地址:https://github.com/alibaba/nacos/releases
下載編譯壓縮并解壓:nacos-server-2.2.3.zip。
1.1、啟動服務(wù)器
注:Nacos的運行需要以至少2C4g60g*3的機器配置下運行。
#啟動命令(standalone代表著單機模式運行,非集群模式):
#Linux/Unix/Mac
sh startup.sh -m standalone
#如果您使用的是ubuntu系統(tǒng),或者運行腳本報錯提示[[符號找不到,可嘗試如下運行:
bash startup.sh -m standalone
#Windows
startup.cmd -m standalone
1.2、關(guān)閉服務(wù)器
#Linux/Unix/Mac
sh shutdown.sh
#Windows
shutdown.cmd
#或者雙擊shutdown.cmd運行文件。
1.3、服務(wù)注冊&發(fā)現(xiàn)和配置管理接口
#服務(wù)注冊
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
#服務(wù)發(fā)現(xiàn)
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'
#發(fā)布配置
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
#獲取配置
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
參考自官方安裝說明:https://nacos.io/zh-cn/docs/quick-start.html
2、代碼示例
示例代碼分為3個工程:app1(服務(wù)1工程),app2(服務(wù)2工程),gateway(網(wǎng)關(guān)工程),使用的依賴包版本:
com.alibaba.cloud:2022.0.0.0
org.springframework.cloud:2022.0.4
org.springframework.boot:3.0.9
app1,app2都提供個接口:goods(商品信息接口),user(用戶信息接口)
goods接口通過網(wǎng)關(guān),app1和app2提供負載模式訪問
user接口通過網(wǎng)關(guān),代理方式訪問
2.1、app1工程代碼
pom.xml
<?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>com.penngo.app1</groupId>
<artifactId>gateway-app1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2022.0.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2022.0.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
配置文件:application.yml
spring:
application:
name: app-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
locator:
lower-case-service-id: true
server:
port: 9091
servlet:
encoding:
force: true
charset: UTF-8
enabled: true
業(yè)務(wù)代碼:AppMain.java
package com.penngo.app1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@EnableDiscoveryClient
public class AppMain {
public static void main(String[] args) {
SpringApplication.run(AppMain.class, args);
}
@RestController
public class HelloController {
@GetMapping("/goods")
public Map goods(){
Map<String, String> data = new HashMap<>();
data.put("name", "手機");
data.put("service", "app1");
return data;
}
@GetMapping("/user")
public Map<String, String> user(){
Map<String, String> data = new HashMap<>();
data.put("user", "test");
data.put("service", "app1");
return data;
}
}
}
2.2、app2工程代碼
pom.xml與app1工程一樣。
配置文件:application.yml,與app2區(qū)分不同的端口
spring:
application:
name: app-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
locator:
lower-case-service-id: true
server:
port: 9091
servlet:
encoding:
force: true
charset: UTF-8
enabled: true
2.3、gateway網(wǎng)關(guān)工程代碼
pom.xml
<?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>com.penngo.gateway</groupId>
<artifactId>gateway-nacos</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2022.0.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2022.0.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
配置application.yml
server:
port: 9090
spring:
application:
name: gatewayapp
cloud:
nacos:
discovery:
server-addr: localhost:8848
locator:
lower-case-service-id: true
config:
server-addr: localhost:8848
# 加載 dataid 配置文件的后綴,默認是 properties
file-extension: yml
# 配置組,默認就是 DEFAULT_GROUP
group: DEFAULT_GROUP
# 配置命名空間,此處寫的是 命名空間的id 的值,默認是 public 命名空間
# namespace:
# data-id 的前綴,默認就是 spring.application.name 的值
prefix: ${spring.application.name}
GatewayMain.java
package com.penngo.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayMain {
public static void main(String[] args) {
SpringApplication.run(GatewayMain.class, args);
}
}
3、動態(tài)配置網(wǎng)關(guān)路由
三個工程啟動后,nacos的服務(wù)列表
3.1、配置動態(tài)路由
spring:
cloud:
gateway:
routes:
- id: app1
uri: http://localhost:9091/
predicates:
- Path=/app1/**
filters:
- StripPrefix=1
- id: app2
uri: http://localhost:9092/
predicates:
- Path=/app2/**
filters:
- StripPrefix=1
配置后,可以通過網(wǎng)關(guān)的端口9090和地址訪問
http://localhost:9090/app1/user
http://localhost:9090/app2/user
3.2、配置為負載模式
spring:
cloud:
gateway:
routes:
- id: app1
uri: http://localhost:9091/
predicates:
- Path=/app1/**
filters:
- StripPrefix=1
- id: app2
uri: http://localhost:9092/
predicates:
- Path=/app2/**
filters:
- StripPrefix=1
- id: app
uri: lb://app-service
predicates:
- Path=/app/**
filters:
- StripPrefix=1
配置后,可以通過同一個地址訪問到兩個服務(wù)返回的數(shù)據(jù)
http://localhost:9090/app/goods
4、gateway配置規(guī)則
參數(shù)說明
id: 路由ID
uri: 目標(biāo)地址,可以是服務(wù),如果服務(wù)Spring推薦用全大寫,實際調(diào)用大小寫不敏感,都可以調(diào)通。
predicates: 匹配路徑,以瀏覽器請求的端口號后面的第一級路徑為起始。
filters: 過濾器,包含Spring Gateway 內(nèi)置過濾器,可以自定義過濾器。
4.1、請求轉(zhuǎn)發(fā),轉(zhuǎn)發(fā)指定地址
routes:
# 跳轉(zhuǎn)URL
- id: 163_route
uri: http://localhost:9091/
predicates:
- Path=/app
- 訪問地址:http://localhost:9090/app/index
- 真實地址:http://localhost:9091/app/index
4.2、去掉指定的前綴路徑
- id: app1
uri: http://localhost:9091/
predicates:
- Path=/app1/**
filters:
- StripPrefix=1
去掉第1層的路徑前綴app1
- 訪問地址:http://localhost:9090/app1/user
- 真實地址:http://localhost:9091/user
4.3、正則匹配重寫路徑
- id: test
uri: lb://app-service
predicates:
- Path=/test/**
filters:
- RewritePath=/test/(?<path>.*), /$\{path}
去掉第1層的路徑前綴app1文章來源:http://www.zghlxwxcb.cn/news/detail-672855.html
- 訪問地址:http://localhost:9090/app/goods
- 真實地址:http://localhost:9091/goods 或 http://localhost:9091/goods
源碼下載文章來源地址http://www.zghlxwxcb.cn/news/detail-672855.html
到了這里,關(guān)于Spring Cloud 2022.x版本使用gateway和nacos實現(xiàn)動態(tài)路由和負載均衡的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!