前言
本篇文章將介紹Spring Cloud Alibaba體系下Spring Cloud Gateway的搭建,服務(wù)注冊(cè)中心和分布式配置中心使用Nacos,后續(xù)將會(huì)持續(xù)更新,介紹集成Sentinel,如何做日志鏈路追蹤,如何做全鏈路灰度發(fā)布設(shè)計(jì),以及Spring Cloud Gateway的擴(kuò)展等。
POM依賴
? Spring Boot,Spring Cloud,Discovery,Config等基礎(chǔ)依賴在父pom中已經(jīng)配置如下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- bootstrap 啟動(dòng)器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- spring cloud alibaba nacos服務(wù)注冊(cè)發(fā)現(xiàn) -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- spring cloud alibaba nacos配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lomnok.version}</version>
</dependency>
<!-- SpringCloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- SpringCloud Loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
配置文件
- 本地bootstrap.yml配置文件
? 本地的配置文件指定網(wǎng)關(guān)服務(wù)的配置中心為nacos,指定共享配置文件${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
,其規(guī)則是服務(wù)名-active profile-spring.cloud.nacos.config.file-extension,所以此服務(wù)配置中心生效的配置文件為ruuby-gateway-dev.yml
并且允許配置熱更新refresh-enabled: true
。
spring:
application:
name: ruuby-gateway
profiles:
active: dev
cloud:
nacos:
username: "nacos"
password: "nacos"
config:
server-addr: 127.0.0.1:8848
# namespace id
namespace: 3ef5e608-6ee8-4881-8e50-ed47a5a04af2
# 阿里云平臺(tái)ak,sk
# access-key:
# secret-key:
# 配置文件格式
file-extension: yml
shared-configs:
- ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
refresh-enabled: true
- Nacos配置中心配置文件ruuby-gateway-dev.yml
? spring.cloud.nacos.dicovery
配置服務(wù)發(fā)現(xiàn)注冊(cè)中心;
? spring.cloud.gateway
配置網(wǎng)關(guān)路由信息,predicates
配置根據(jù)Path
進(jìn)行路由,id
進(jìn)行服務(wù)發(fā)現(xiàn),uri: lb://account-svc
通過(guò)負(fù)載均衡請(qǐng)求后端。
server:
port: 8081
spring:
application:
name: ruuby-gateway
profiles:
active: dev
cloud:
nacos:
username: "nacos"
password: "nacos"
discovery:
# 服務(wù)注冊(cè)中心地址
server-addr: 127.0.0.1:8848
# 阿里云平臺(tái)ak,sk
# access-key:
# secret-key:
namespace: 3ef5e608-6ee8-4881-8e50-ed47a5a04af2
config:
server-addr: 127.0.0.1:8848
# 阿里云平臺(tái)ak,sk
# access-key:
# secret-key:
# 配置文件格式
file-extension: yml
shared-configs:
- ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
namespace: 3ef5e608-6ee8-4881-8e50-ed47a5a04af2
group: DEFAULT-GROUP
gateway:
routes:
- id: account-svc
uri: lb://account-svc
predicates:
- Path=/gateway/account/**
filters:
- StripPrefix=1
# url黑名單 這是一個(gè)局部過(guò)濾器
# - name: BlackListUrlFilter
# args:
# blackListUrl:
# - /account/list
# 鑒權(quán)過(guò)濾器
# - name: AuthFilter
# args:
# type: jwt
# publicKey: 11111
- id: order-svc
uri: lb://order-svc
predicates:
- Path=/gateway/order/**
filters:
- StripPrefix=1
# 監(jiān)控指標(biāo)收集endpoint
management:
endpoints:
web:
exposure:
include: "*"
網(wǎng)關(guān)啟動(dòng)
@SpringBootApplication
@EnableDiscoveryClient
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class, args);
}
}
? 出現(xiàn)下面的日志,啟動(dòng)成功,這里日志的格式是經(jīng)過(guò)日志插件格式化的,后面會(huì)單獨(dú)寫一篇日志插件的文檔。
{"@timestamp":"2023-05-22T17:21:27.100+08:00","@version":"0.0.1","message":"Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.","logger_name":"org.springframework.cloud.loadbalancer.config.LoadBalancerCacheAutoConfiguration$LoadBalancerCaffeineWarnLogger","thread_name":"main","level":"WARN","level_value":30000}
{"@timestamp":"2023-05-22T17:21:27.767+08:00","@version":"0.0.1","message":"Started GateWayApplication in 7.757 seconds (JVM running for 8.699)","logger_name":"io.redick.cloud.account.GateWayApplication","thread_name":"main","level":"INFO","level_value":20000}
網(wǎng)關(guān)轉(zhuǎn)發(fā)請(qǐng)求測(cè)試
? 接口請(qǐng)求地址為http://127.0.0.1:8081/gateway/order/accountLis
,是通過(guò)網(wǎng)關(guān)轉(zhuǎn)發(fā)的,匹配了/gateway/order/*
規(guī)則,將請(qǐng)求轉(zhuǎn)發(fā)到order-svc
服務(wù)。
? ~ curl -X GET http://127.0.0.1:8081/gateway/order/accountList
{"code":200,"msg":null,"data":[{"pageIndex":0,"pageSize":10,"orderByColumn":"","asc":"asc","productId":"123","totalCount":200,"productName":"華為P100","beginTime":null,"endTime":null,"productDesc":"華為手機(jī)牛逼"},{"pageIndex":0,"pageSize":10,"orderByColumn":"","asc":"asc","productId":"200","totalCount":100,"productName":"華為Mate100","beginTime":null,"endTime":null,"productDesc":"華為手機(jī)真牛逼"}]}
? 下面是不通過(guò)網(wǎng)關(guān)轉(zhuǎn)發(fā)訪問(wèn)該接口,通過(guò)對(duì)比可以看到網(wǎng)關(guān)實(shí)現(xiàn)了請(qǐng)求轉(zhuǎn)發(fā)和負(fù)載均衡,并且網(wǎng)關(guān)通過(guò)應(yīng)用名進(jìn)行服務(wù)發(fā)現(xiàn)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-507070.html
? ~ curl -X GET http://127.0.0.1:8089/order/accountList
{"code":200,"msg":null,"data":[{"pageIndex":0,"pageSize":10,"orderByColumn":"","asc":"asc","productId":"123","totalCount":200,"productName":"華為P100","beginTime":null,"endTime":null,"productDesc":"華為手機(jī)牛逼"},{"pageIndex":0,"pageSize":10,"orderByColumn":"","asc":"asc","productId":"200","totalCount":100,"productName":"華為Mate100","beginTime":null,"endTime":null,"productDesc":"華為手機(jī)真牛逼"}]}
源碼
源碼參考GitHub,如果對(duì)您有幫助麻煩點(diǎn)個(gè)贊支持下,謝謝!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-507070.html
到了這里,關(guān)于Spring Cloud Gateway集成Nacos作為注冊(cè)中心和配置中心的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!