1.概述
平台采用Nacos配置与服务中心,致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施,关键特性包括:
- 服务发现和服务健康监测
- 动态配置服务
- 动态 DNS 服务
- 服务及其元数据管理
2. 服务注册
平台使用NACOS作为服务注册中心,同一个微服务应用,可以注册到NACOS上面,那么一个微服务的应用可以有多个实例,那么我们在访问微服务的时候,可以通过这种方式实现负载均衡。
2.1 在项目中实现注册步骤
2.1.1 在项目的pom.xml 中增加
<dependency>
<groupId>com.redxun</groupId>
<artifactId>jpaas-config</artifactId>
</dependency>
这个jpaas_config 主要是完成配置和注册的任务,另外我们需要指定nacos.address的环境变量。
2.1.2 在项目的bootstrap.yml增加配置
spring:
application:
name: 微服务实例名称
这里我们需要指定微服务实例的名称。
2.1.3 启用服务发现
@EnableDiscoveryClient
这样一个服务自动发现到nacos,并连接到nacos服务器中。
3.服务配置
在平台中,比如数据库的配置,队列的配置,系统参数的配置,我们都存放于nacos 中,当用户需要修改数据库的配置的时候,这个时候用户不需要对程序进行再次打包,只需要修改nacos的配置,程序通过 nacos 获取这些配置就能够实现相关配置的更改,而不需要修改程序,对于微服务应用来讲这个很方便。
3.1 jpaas-config 的bootstrap.properties 文件
这个文件指定了微服务应用从这里读取配置的方式,这里是每个微服务应用都可以读取到的配置。
########################## bootstrap级别通用配置 ##########################
# 默认开发环境
spring.profiles.active=dev
##### nacos(注册中心)地址
spring.cloud.nacos.discovery.server-addr=${nacos.address}
##### nacos(配置中心)地址
spring.cloud.nacos.config.server-addr=${nacos.address}
spring.cloud.nacos.config.max-retry=20
nacos.server-addr=${nacos.address}
spring.cloud.nacos.config.file-extension=properties
spring.cloud.nacos.config.shared-dataids=nacos-config-${spring.profiles.active}.properties
spring.cloud.nacos.config.refreshable-dataids=nacos-config-${spring.profiles.active}.properties
##### spring-boot-actuator配置
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
##### 允许bean覆盖
spring.main.allow-bean-definition-overriding=true
这个指定了服务注册和配置的地址,项目中我们使用一个nacos作为配置中心和注册中心。
3.1.1 系统配置
在nacos中我们可以看到:
nacos-config-dev.properties
nacos-config-prod.properties
这两个文件是存放 数据库配置,redis,mq 等一些系统的配置。
dev 和 prod 是我们在发布产品时指定生产配置和 开发配置。
这个配置如下:
# bpm
bpm.datasource.url=jdbc:mysql://localhost:3306/ztj_bpm?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
bpm.datasource.username=root
bpm.datasource.password=root
bpm.datasource.driver=com.mysql.cj.jdbc.Driver
bpm.datasource.connection=
# form
form.datasource.url=jdbc:mysql://localhost:3306/ztj_form?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
form.datasource.username=root
form.datasource.password=root
form.datasource.driver=com.mysql.cj.jdbc.Driver
form.datasource.connection=
# portal
portal.datasource.url=jdbc:mysql://localhost:3306/paas_portal?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
portal.datasource.username=root
portal.datasource.password=root
portal.datasource.driver=com.mysql.cj.jdbc.Driver
portal.datasource.connection=
# system
system.datasource.url=jdbc:mysql://localhost:3306/ztj_system?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
system.datasource.username=root
system.datasource.password=root
system.datasource.driver=com.mysql.cj.jdbc.Driver
system.datasource.connection=
# user
user.datasource.url=jdbc:mysql://localhost:3306/ztj_user?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
user.datasource.username=root
user.datasource.password=root
user.datasource.driver=com.mysql.cj.jdbc.Driver
user.datasource.connection=
# ureport
ureport.datasource.url=jdbc:mysql://localhost:3306/paas_ureport?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
ureport.datasource.username=root
ureport.datasource.password=root
ureport.datasource.driver=com.mysql.cj.jdbc.Driver
ureport.datasource.connection=
job.datasource.url=jdbc:mysql://localhost:3306/jpaas_job?Unicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=UTC
job.datasource.username=root
job.datasource.password=root
# redis 配置
spring.redis.host=192.168.1.10
spring.redis.port=6379
spring.redis.timeout=5000
spring.redis.password=redxun
spring.redis.database=10
# 数据源监控用户名密码配置
druid.account=admin
druid.password=admin
# 平台发送邮件配置
spring.mail.port=25
spring.mail.username=58133370@qq.com
spring.mail.password=hpfktfkykxedbjge
spring.mail.protocol=smtp
spring.mail.default-encoding=utf-8
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 消息队列配置(rocketmq)
spring.cloud.stream.rocketmq.binder.name-server=127.0.0.1:9876
# rabbit 消息队列配置
#spring.cloud.stream.binders.local_rabbit.type= rabbit
#spring.cloud.stream.binders.local_rabbit.environment.spring.rabbitmq.host=192.168.1.10
#spring.cloud.stream.binders.local_rabbit.environment.spring.rabbitmq.port=5672
#spring.cloud.stream.binders.local_rabbit.environment.spring.rabbitmq.username=admin
#spring.cloud.stream.binders.local_rabbit.environment.spring.rabbitmq.password=admin
#spring.cloud.stream.binders.local_rabbit.environment.spring.rabbitmq.virtual-host= /
这个配置需要和应用程序一起进行使用。
比如在 jpaas-bpm 项目中。
用户可以在这里定义参数,并相应的在 nacos-config-dev.properties 配置中进行配置。
3.1.2 应用配置参数
应用配置指得是,在应用中的一些配置,比如上传配置,是否启用验证等。
这个也是配置在nacos中:
配置的 dataId为 nacos-config.properties
可以参考