只提供使用demo,原理请自行百度
1.引入项目依赖
2.添加bootstrap配置,因为springcloud config加载优先级需要配置在此文件中,系统级别配置
3.添加dev命名空间
4.在dev环境下配置中心添加配置信息
*******重点*******
DataId:项目启动,根据你项目中bootstrap文件中的配置信息组装成nacos上的DataId读取配置信息
DataId = ${spring.application.name}-${spring.profile.active}.${file-extension}
启动项目可以看到加载需要加载的信息:
具体代码如下:
依赖配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
Bootstrap.yml配置:
spring:
profiles:
active: dev
application:
name: ebiz-agent-center
cloud:
nacos:
config:
server-addr: 118.190.5.81:8848
file-extension: "yaml"
namespace: "05a4b23f-aff1-4a75-b1ef-e25a51b57d8a"
#server-addr:nacos地址
#profiles.active:环境地址
#namespace:命名空间id
#file-extension:文件后缀名,目前支持yaml或properties
#huaxiaozhou.add 20190819
nacos配置:
server:
port: 5555
servlet:
context-path: /agent
spring:
cloud:
nacos:
discovery:
server-addr: 118.190.1.84:8848
namespace: "05a4b23f-aff1-4a75-b1ef-e25a51b57b1a"
datasource:
driverClassName: com.mysql.jdbc.Driver
jdbc-url:
username:
password:
filters: stat
maxActive: 1000
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT COUNT(*) FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 100
redis:
host:
port: 7003
database: 0
password:
timeout: 4000
lettuce:
lettuce:
pool:
max-active: 200
max-idle: 20
min-idle: 5
max-wait: 2000
management:
endpoints:
web:
exposure:
include: "*"