Spring Boot 整合 Nacos
-
maven依赖
<!-- nacos 服务注册 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 服务配置 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- nacos 工具包 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
-
启动类注解
@RefreshScope // 支持动态刷新配置,一般配置在注册变量类上 @Value
@SpringBootApplication
@EnableDiscoveryClient
@ServletComponentScan // 支持扫描@WebFilter,@WebServlet
@ComponentScans(@ComponentScan("com.demo.cloud"))
public class TemplateApplication {
public static void main(String[] args) {
SpringApplication.run(TemplateApplication.class, args);
}
}
-
bootstrap.yaml 配置文件(resources目录下)
spring:
application:
## 服务名称
name: demo-com-template
cloud:
nacos:
## 服务注册配置
discovery:
## nacos 服务通讯地址 ip + 端口 (域名),注意服务地址不能以 ‘/’结尾
server-addr: https://nacos.demo-sec.net
## 命名空间
namespace: cccca938-9f18-43ed-32d2-2a44fd9ddd67
## 配置读取地址
config:
## nacos 服务通讯地址
server-addr: https://nacos.demo-sec.net
## 命名空间
namespace: cccca938-9f18-43ed-32d2-2a44fd9ddd67
## 不指定,就会引用默认分组
group: DEV_GROUP
file-extension: yaml
shared-configs:
## 配置文件名称
- dataId: template.yaml
## 刷新开关
refresh: true
-
nacos 配置(结合实际需要配置,配置文件在对应的命名空间下)
server:
port: 7777
spring:
profiles:
active: dev
cloud:
nacos:
discovery:
namespace: cccca938-9f18-43ed-32d2-2a44fd9ddd67
datasource:
druid:
url: jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
filters: stat,wall
redis:
port: 6379
database: 1
password: root
host: 127.0.0.1
lettuce:
shutdown-timeout: 100ms
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 1000ms
resilience4j.circuitbreaker:
configs:
default:
minimumNumberOfCalls: 5
permittedNumberOfCallsInHalfOpenState: 3
automaticTransitionFromOpenToHalfOpenEnabled: true
waitDurationInOpenState: 2s
failureRateThreshold: 50
slowCallRateThreshold: 80
slowCallDurationThreshold: 5s
recordExceptions:
- org.springframework.web.client.HttpServerErrorException
- java.io.IOException
instances:
normal:
baseConfig: default
longtime:
baseConfig: default
minimumNumberOfCalls: 10
slowCallDurationThreshold: 10s
-
服务调用
@Autowired
private DiscoveryClient discoveryClient;
protected String getUri() {
String[] split = applications.split(",");
/*获取provider1实例列表*/
List<ServiceInstance> provider = discoveryClient.getInstances("demo-com-test1"]);
/*生成一个列表长度内的随机数*/
Integer index = ThreadLocalRandom.current().nextInt(provider.size());
/*获取完整接口地址*/
return provider.get(index).getUri().toString();
}
-
动态刷新
-
如果我们需要不停机改变我们的生产环境的某个值(例如文件保存地址)来控制业务逻辑。我们需要在对应的Controller上添加@RefreshScope 进行动态刷新;
-
如果存在部分配置为开发环境、生产环境、测试环境等不同环境共通,我们只需在配置中心创建例如wx-pa-api.yml的配置文件。配置的优先级 精准配置 会覆盖 与通用配置 相同的配置,然后再和通用配置互补;
-
不同服务存在相同的配置:
-
a) 通过spring.cloud.nacos.config.shared-config ,配置文件中增加:#各个微服务共享的配置,注意越排到后面的公共配置yml优先级越高
-
b) 通过spring.cloud.nacos.config.extension-configs(优先级高于shared-config)
-
-
group:默认属性为DEFAULT_GROUP
-
默认的服务注册名称:spring.application.name 属性
-