項目父工程spring boot版本為2.2.2,spring cloud版本為Hoxton.SR1,
spring cloud alibaba版本為2.1.0。版本最佳搭配詳見官網(wǎng)。
項目大致結(jié)構(gòu)

image.png
- gateway模塊引入依賴
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<scope>provided</scope>
</dependency>
-
配置yml
image.png
spring:
profiles:
# active: dev
active: @profiles.active@
application:
name: glorious-gateway
cloud:
nacos:
discovery: #Nacos注冊中心地址
server-addr: 127.0.0.1:8848
gateway:
discovery:
locator:
enabled: true #開啟注冊中心路由功能
# lower-case-service-id: true
routes:
- id: mini-api
uri: lb://glorious-mini-api
predicates:
- Path=/mini/**
filters:
- StripPrefix=1
- id: system-api
uri: lb://glorious-system-api
predicates:
- Path=/system/**
filters:
- StripPrefix=1
啟動類加上發(fā)現(xiàn)服務(wù)注解
@EnableDiscoveryClient
-
啟動網(wǎng)關(guān)gateway以及其他服務(wù)模塊,然后登錄nacos,服務(wù)已經(jīng)發(fā)現(xiàn)
image.png
查看swagger

image.png
訪問網(wǎng)關(guān)接口數(shù)據(jù),登錄返回token

image.png
訪問微服務(wù)接口數(shù)據(jù)

image.png
關(guān)于nacos安裝我之前博客介紹過,感興趣的可以看看,詳情

