项目父工程spring boot版本为2.2.2,spring cloud版本为Hoxton.SR1,
spring cloud alibaba版本为2.1.0。版本最佳搭配详见官网。
项目大致结构
- 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
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
启动类加上发现服务注解
@EnableDiscoveryClient
-
启动网关gateway以及其他服务模块,然后登录nacos,服务已经发现
查看swagger
访问网关接口数据,登录返回token
访问微服务接口数据
关于nacos安装我之前博客介绍过,感兴趣的可以看看,详情