nacos config 配置整合
1 修改 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>yyb</groupId>
<artifactId>scorder</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.8.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
<!-- https://mvnrepository.com/artifact/com.kennycason/kumo-core -->
<!--<dependency>
<groupId>com.kennycason</groupId>
<artifactId>kumo-core</artifactId>
<version>1.22</version>
</dependency>
<dependency>
<groupId>com.kennycason</groupId>
<artifactId>kumo-tokenizers</artifactId>
<version>1.12</version>
</dependency>-->
2 bootstrap.yml
server:
port: 8091
spring:
application:
# 注册到Nacos上的服务名称
name: yyborder
# profiles:
# active: dev
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
namespace: b057d2b1-f25a-4a6b-8750-ba9582784c3a
logging:
level:
root: info
# 配置日志级别,让hibernate打印出执行的SQL参数
management:
endpoints:
web:
exposure:
# 开放所有监控端点
include: '*'
endpoint:
health:
# 是否展示健康检查详情
show-details: always
3 启动nacos 配置
生产一般使用 (Namespace隔离租户 + group环境分组)
我这只是简单使用 namespace 隔离单项目配置文件
- 从一个租户(用户)的角度来看,如果有多套不同的环境,那么这个时候可以根据指定的环境来创建不同的 namespce,以此来实现多环境的隔离。例如,你可能有日常,预发和生产三个不同的环境,那么使用一套 nacos 集群可以分别建以下三个不同的 namespace。如下图所示:
在nacos 新增 namespace
在namesapce 新增配置文件
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL
mybatis:
mapper-locations: classpath:mapper/*.xml
typeAliasesPackage: com.springcloud.entity.entity #实体类对应包
yyb:
name: 1234567
4 验证 @RefreshScope 是否生效
访问 http://localhost:8091/getValue
修改 yyborder.yml 的 yyb.name 值会发现 name 的值动态刷新了
package com.springcloud.controller;
import com.alibaba.fastjson.JSON;
import com.springcloud.entity.JobInfo;
import com.springcloud.service.JobInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* (JobInfo)表控制层
*
* @author xiaolei
* @since 2020-01-27 14:33:31
*/
@RestController
@RefreshScope
public class JobInfoController {
/**
* 服务对象
*/
@Resource
private JobInfoService jobInfoService;
@Value("${yyb.name}")
private String name;
@RequestMapping("/getValue")
public String getValue() {
return name;
}
}
参考文章
链接: link.
[nacos namespace 最佳实践]: https://nacos.io/zh-cn/blog/namespace-endpoint-best-practices.html
[nacos多环境下管理及隔离配置]: https://segmentfault.com/a/1190000020186040
本项目gitee 代码