maven父项目配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
特别注意版本对应的问题
Nacos 注册中心
<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>
openFeign 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
开启openFeign
@EnableFeignClients
服务通信
@FeignClient("orders")//服务名称
public interface OrderClient {//接口名称,用于注入,建议分类
//要求请求地址,形参列表(必须加注释),返回值一样
@GetMapping("/get")
Order getOrder(@RequestParam Integer id);
}
gateway 网关
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
纯网关服务,不能导web起步依赖
server:
port: 80
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: user
uri: lb://users
predicates:
- Path=/user/**
nacos 配置管理
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
spring:
application:
name: users #服务名称
cloud:
nacos:
config:
file-extension: yaml
namespace: c25eb97b-8c4b-4ac4-8fd7-274ca9eb1d63 #命名空间的id,一个项目一个命名空间
profiles:
active: dev #开发环境
特别注意: 必须写在 bootstrap.yaml 中,代表从别处获取配置
数据实时刷新
@RefreshScope
Sentinel
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
spring:
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:8083