当前位置:网站首页>nacos服务配置和持久化配置
nacos服务配置和持久化配置
2022-07-01 09:04:00 【建桥之魂】
目录

一、Nacos服务注册中心对比提升
| 服务注册与发现框架 | CAP模型 | 控制台管理 | 社区活跃度 |
|---|---|---|---|
| Eureka | AP | 支持 | 低(2.x版本闭源) |
| Zookeeper | CP | 不支持 | 中 |
| Consul | CP | 支持 | 高 |
| Nacos | AP | 支持 | 高 |
CAP模型
- 一致性(Consistency):同一时刻的同一请求的实例返回的结果相同,所有的数据要求具有强一致性(Strong Consistency)
- 可用性(Availability):所有实例的读写请求在一定时间内可以得到正确的响应
- 分区容错性(Partition tolerance):在网络异常(光缆断裂、设备故障、宕机)的情况下,系统仍能提供正常的服务
以上三个特点就是CAP原则(又称CAP定理),但是三个特性不可能同时满足,所以分布式系统设计要考虑的是在满足P(分区容错性)的前提下选择C(一致性)还是A(可用性),即:CP或AP。
二、Nacos服务配置中心
1. 添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
2. 配置application.yaml
spring:
profiles:
active: dev #表示开发环境
3. 配置bootstrap.yaml
# nacos配置
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery: #注册
server-addr:
localhost: 8848 #Nacos服务注册中心地址
config: #开启配置中心
server-addr:
localhost: 8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式的配置
# 官方给出的格式
# ${prefix}-${spring.profiles.active}.${file-extension}
# ${spring.application.name}-${spring.profiles.active}.${file-extension}
# 实际格式
# nacos-config-client-dev.yaml
4. 平台创建配置规则


5. 业务层编写
@RestController
@RefreshScope //支持Nacos动态刷新功能
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo(){
return configInfo;
}
}
6. 访问获取配置内容
7. Nacos动态刷新(@RefreshScope注解)
业务层引入的注解@RefreshScope支持Nacos动态刷新功能
三、切换不同环境
1. DateId方案
首先按照之前的步骤,在平台创建两个配置(dev和test)
重启项目,测试是否切换到test环境的配置
2. Group方案
Group分组默认是
DEFAULT_GROUP,所以我们需要分出两组:一组是dev开发组,一组为test测试组查看配置列表,检查是否配置成功
变更application.yaml和bootstrap.yaml配置文件,测试info下的test:
重新访问,查看是否切换成功分组
3. Namespace空间方案
新建test和dev两个命名空间
修改配置文件
手动在test命名空间下新建三个分组
访问测试
四、Nacos持久化配置
1. derby数据库
Nacos中会默认自带嵌入式数据库derby,所以我们每次创建一个Nacos实例就会有一个derby,当有多个Nacos节点的时候,就会出现一致性问题,所以Nacos支持外部数据库统一管理MySql。
2. 切换MySql数据库
① 创建数据库
在数据库中创建nacos_config库CREATE DATABASE nacos_config;
② 在数据库中执行navos-mysql.sql脚本

该库中会出现如下的表:

③ 修改application.properties文件
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://11.162.196.16:3306/nacos_devtest?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=UTC
db.user=nacos_devtest
db.password=youdontknow


④ 测试
启动nacos
新建配置
查看本地数据库,数据存在,说明切换mysql数据库成功!
边栏推荐
- Jeecg restart alarm 40001
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
- 用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
- Shell script echo command escape character
- Principles of Microcomputer - Introduction
- FreeRTOS learning easy notes
- Redis source code learning (29), compressed list learning, ziplist C (II)
- 日常办公耗材管理解决方案
- Jetson Nano 安装TensorFlow GPU及问题解决
- Shell script -if else statement
猜你喜欢

Bird recognition app

Imitation of Baidu search results top navigation bar effect

Jetson Nano 安装TensorFlow GPU及问题解决

It is designed with high bandwidth, which is almost processed into an open circuit?

【pytorch】softmax函数

Why is the Ltd independent station a Web3.0 website!

Daily practice of C language - day 80: currency change

Installing Oracle EE
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list

猿人学第20题(题目会不定时更新)
随机推荐
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
如何做好固定资产管理?易点易动提供智能化方案
FreeRTOS learning easy notes
Shell script - string
Shell脚本-for循环和for int循环
[interview brush 101] linked list
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
Flink interview questions
Nacos - Configuration Management
序列化、监听、自定义注解
Football and basketball game score live broadcast platform source code /app development and construction project
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
任务、线程、进程 区别
【检测技术课案】简易数显电子秤的设计与制作
LogBack
Embedded Engineer Interview frequently asked questions
日常办公耗材管理解决方案
NiO zero copy
Nacos - 配置管理
软件工程师面试刷题网站、经验方法








查看配置列表,检查是否配置成功











