目录
1.登录Nacos分布式配置中心平台
http://192.168.66.1:8848/nacos/index.html#/configurationManagement?dataId=&group=&appName=&namespace=&pageSize=&pageNo=
demo-member-dev.yml 开发环境
demo-member-prd.yml 生产环境
2.上传配置文件到Nacos分布式配置中心平台
Data ID === 服务名称
dev:demo-member-dev.yml
spring:
application:
###服务的名称
name: demo-member
cloud:
nacos:
discovery:
###nacos注册地址
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo-member?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
logging:
level:
###打印mybatis日志
com.demo.api.impl.mapper : debug
demo:
userName: dev-demo
prd:demo-member-prd.yml
spring:
application:
###服务的名称
name: demo-member
cloud:
nacos:
discovery:
###nacos注册地址
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo-member?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
logging:
level:
###打印mybatis日志
com.demo.api.impl.mapper : debug
demo:
userName: prd-demo
3.新增Maven依赖 nacos-config
<!-- springboot nacos-config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
4.编写一个测试配置文件接口
@RequestMapping("/getTestConfig")
String getTestConfig();
Impl
@Value("${mayikt.userName}")
private String userName;
@Override
public String getTestConfig() {
return userName;
}
5.创建一个bootstrap.yml 配置nacos config相关配置
spring:
application:
###服务的名称
name: demo-member
cloud:
nacos:
discovery:
###nacos注册地址
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yml
profiles:
active: dev
# datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/demo-member?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
# username: root
# password: root
#logging:
# level:
# ###打印mybatis日志
# com.demo.api.impl.mapper : debug
server:
port: 7000
#demo:
# userName: demo
5.http://127.0.0.1:7000/getTestConfig
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/131255.html