当前位置:网站首页>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数据库成功!
边栏推荐
- It is designed with high bandwidth, which is almost processed into an open circuit?
- Redis源码学习(29),压缩列表学习,ziplist.c(二)
- Shell脚本-数组定义以及获取数组元素
- 如何解决固定资产管理和盘点的难题?
- 3. Detailed explanation of Modbus communication protocol
- Daily practice of C language - day 80: currency change
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
- 【pytorch】2.4 卷积函数 nn.conv2d
- Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
- How to manage fixed assets well? Easy to point and move to provide intelligent solutions
猜你喜欢

Imitation of Baidu search results top navigation bar effect

I use flask to write the website "one"

【pytorch】2.4 卷积函数 nn.conv2d

Ape anthropology topic 20 (the topic will be updated from time to time)

Nacos - Configuration Management

Jetson nano installs tensorflow GPU and problem solving

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

Vsync+ triple cache mechanism +choreographer

TV size and viewing distance

FAQ | FAQ for building applications for large screen devices
随机推荐
中小企业固定资产管理办法哪种好?
Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
Redis——Lettuce连接redis集群
动态代理
Common interview questions for embedded engineers 2-mcu_ STM32
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
Shell script - positional parameters (command line parameters)
Shell script -while loop explanation
2.4 激活函数
Why is the Ltd independent station a Web3.0 website!
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
Can diffusion models be regarded as an autoencoder?
Which method is good for the management of fixed assets of small and medium-sized enterprises?
如何解决固定资产管理和盘点的难题?
Key points of NFT supervision and overseas policies
C language student information management system
[interview brush 101] linked list
固定资产管理系统让企业动态掌握资产情况
集团公司固定资产管理的痛点和解决方案
LogBack








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











