当前项目接入了Nacos,主要目的是为了管理配置以及提供注册中心,之前用SpringCloudConfig做配置中心,Eureka做注册中心。对比起来Nacos提供了配置中心功能更强大,可以动态更新配置,而SpringCloudConfig需要动态更新配置要做的事情太多。做注册中心的功能倒是简单,增加了在页面管理注册应用和下线的功能。
使用之前制作的nacos的starter包 phenix-spring-boot-starter-nacos
com.phenix
phenix-spring-boot-starter-nacos
上面的包是我放在云效私有仓库上的。
在项目的启动模块中,resources目录下新增bootstrap.yml的配置文件,新增以下配置:
spring:
application:
name: xx
profiles:
active: dev
cloud:
nacos:
config:
enabled: true
file-extension: yaml
encode: UTF-8
ext-config[0].data-id: application.${spring.cloud.nacos.config.file-extension}
ext-config[0].refresh: true
---
spring:
profiles: dev
cloud:
nacos:
config:
server-addr: 192.168.0.200:8848
namespace: ecab5663-eae7-44a9-8e1c-bf0015bf9b04
discovery:
server-addr: 192.168.0.200:8848
namespace: ecab5663-eae7-44a9-8e1c-bf0015bf9b04
当前的配置是分了环境来配的,不同的环境nacos配置不一样。
而且为了提取配置方便,将应用的公共配置提取到了application.yaml中,这个配置文件放在nacos的配置中。
application.yaml 中配置了公共的配置,跟环境相关的配置,例如mysql、redis、mq等
server:
max-http-header-size: 10240000
tomcat:
max-http-post-size: 10240000
max-connections: 3000
max-threads: 1000
session:
timeout: 864000000
spring:
main:
allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
profiles:
active: dev
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 10MB
aop:
proxy-target-class: true
expose-proxy: true
sleuth:
web:
client:
enabled: true
sampler:
probability: 0.1
datasource:
name: hikariDataSource
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://192.168.0.200:3306/${phenix.database}?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false
username: 1a6ebfe8d107064bdd26f0b8397d1d64
password: 1a6ebfe8d107064bdd26f0b8397d1d64
# Hikari 连接池配置
# 最小空闲连接数量
hikari:
minimum-idle: 5
# 空闲连接存活最大时间,默认600000(10分钟)
idle-timeout: 180000
# 连接池最大连接数,默认是10
maximum-pool-size: 10
# 此属性控制从池返回的连接的默认自动提交行为,默认值:true
auto-commit: true
# 连接池名称
pool-name: MyHikariCP
# 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
max-lifetime: 1800000
# 数据库连接超时时间,默认30秒,即30000
connection-timeout: 30000
connection-test-query: SELECT 1
redis:
database: 0
host: 192.168.0.200
port: 6379
password: phenix
jedis:
pool:
max-active: 10000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: 1s # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 100 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
timeout: 5s # 连接超时时间(毫秒
rabbitmq:
addresses: amqp://192.168.0.200:5672
username: phenix
password: phenix
listener:
simple:
retry:
enabled: true #是否开启消费者重试
max-attempts: 5 #最大重试次数
initial-interval: 5000 #重试间隔时间(单位毫秒)
default-requeue-rejected: false #重试次数超过上面的设置之后是否丢弃(false不丢弃时需要写相应代码将该消息加入死信队列)
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
basic:
enabled: true
cache:
type: redis
jackson:
default-property-inclusion: ALWAYS
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
boot:
admin:
client:
enabled: true
instance:
metadata:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
username: ${spring.security.user.name}
password: ${spring.security.user.password}
mvc:
static-path-pattern: /**
thymeleaf:
mode: HTML
cache: false
servlet:
content-type: text/html
encoding: UTF-8
prefix: classpath:/static/pages/
klock:
address: ${spring.redis.host}:${spring.redis.port}
password: ${spring.redis.password}
mqtt:
username: admin
password: public
host-url: tcp://192.168.0.200:1883
client-id: dingjust-${random.value}
default-topic: $SYS/brokers/+/clients/#
completionTimeout: 3000
keepAlive: 60
security:
basic:
enabled: true
oauth2:
sso:
login-path: ${phenix.oauth2.common.auth-server-addr}/login
client:
client-id: ${phenix.oauth2.client.admin.client-id}
client-secret: ${phenix.oauth2.client.admin.client-secret}
user-authorization-uri: ${phenix.oauth2.common.user-authorization-uri}
access-token-uri: ${phenix.oauth2.common.access-token-uri}
resource:
user-info-uri: ${phenix.oauth2.common.user-info-uri}
prefer-token-info: false
mybatis-plus:
# xml
mapper-locations: classpath*:mapper/*.xml,classpath*:mapper/**/*.xml
global-config:
#刷新mapper 调试神器
db-config:
#主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
id-type: id_worker #id_worker
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy: not_empty
#驼峰下划线转换
#column-underline: true
#数据库大写下划线转换
#capital-mode: true
#逻辑删除配置
logic-delete-value: 1
logic-not-delete-value: 0
db-type: mysql
refresh: true
#自定义填充策略接口实现
#meta-object-handler: com.baomidou.springboot.xxx
#自定义SQL注入器
#sql-injector: com.baomidou.mybatisplus.extension.injector.LogicSqlInjector
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
endpoints:
health:
sensitive: true
cors:
allowed-methods: HEAD,GET,PUT,DELETE,POST
shutdown:
enabled: true
sensitive: false
feign:
httpclient:
enabled: true
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
swagger:
basic:
enabled: true
username: dingjust
password: dingjust2020
dingjust:
oauth2:
common:
admin-server-addr: https://www.dingjust.com/
api-server-addr: https://www.dingjust.com/
auth-server-addr: https://www.dingjust.com/
user-authorization-uri: ${dingjust.oauth2.common.auth-server-addr}/oauth/authorize
access-token-uri: ${dingjust.oauth2.common.api-server-addr}/admin/oauth/token
token-info-uri: ${dingjust.oauth2.common.api-server-addr}/admin/oauth/check_token
user-info-uri: ${dingjust.oauth2.common.api-server-addr}/admin/current/user/info
client:
admin:
access-token-uri: ${dingjust.oauth2.common.access-token-uri}
client-id: 7gBZcbsC7kLIWCdELIl8nxcs
client-secret: 0osTIhce7uPvDKHz6aa67bhCukaKoYl4
login-success-uri: ${dingjust.oauth2.common.admin-server-addr}/login/success
redirect-uri: ${dingjust.oauth2.common.api-server-addr}/auth/oauth/gitee/callback
scope: user_info
user-authorization-uri: ${dingjust.oauth2.common.access-token-uri}/oauth/authorize
user-info-uri: ${dingjust.oauth2.common.access-token-uri}/admin/current/user/info
需要用到这个公共配置,要在代码的bootstrap.yml文件中配置额外配置内容,就是ext-config[0].data-id 这里,配置了 application.${spring.cloud.nacos.config.file-extension} ,有了这个配置,程序启动的时候就能拉到nacos中的 application.yaml ,然后会默认拉取nacos中的当前服务的配置文件。
例如我有一个cloudadmin服务,则程序启动会拉取到nacos中的 application.yaml和cloudadmin.yaml的配置,2个配置中有相同的配置,cloudadmin.yaml会覆盖application.yaml中的配置。
cloudadmin.yaml配置如下:
server:
port: 8030
spring:
application:
name: cloudadmin
boot:
admin:
notify:
mail:
from:
to:
monitor:
connect-timeout: 30s
read-timeout: 30s
endpoints:
health:
sensitive: true
cors:
allowed-methods: HEAD,GET,POST
shutdown:
enabled: true
sensitive: false
至此,项目就已经集成了nacos。