Nacos

Nacos

1.简介

Nacos 是一个开源的动态服务发现、配置管理和服务管理平台,最初由阿里巴巴公司开发。

它提供了一种简单和强大的方式来实现微服务架构中的服务注册、发现和配置管理。

Nacos 的核心功能包括服务注册发现、动态配置管理服务健康监测以及动态 DNS 服务等。通过 Nacos,开发人员可以轻松地管理和发现各种微服务,实现了微服务架构中的解耦和弹性扩展。

官方文档入口:什么是 Nacos | Nacos

2.为什么需要Nacos?

  1. 微服务中有很多相同的配置,如何实现共享一份?

  2. 服务中业务数据可能会发生变化,每次修改都要重启服务.

3.注册服务发现

3.1 导入Nacos依赖

注意自己引入版本号

1
2
3
4
5
<!--nacos 服务注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

3.2 配置application.yaml文件

1
2
3
4
5
6
spring:
application:
name: item-service # 服务名称
cloud:
nacos:
server-addr: 192.168.150.101:8848 # 你的nacos地址

4. 配置中心

4.1 导入Nacos配置中心的依赖

每个需要Nacos配置的Module都需导入这些依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  <!--nacos配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--读取bootstrap文件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--nacos 服务注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

4.2 配置Nacos中的配置中心

4.2.1 数据库和Mybatis-Plus配置

注意这里的jdbc的相关参数并没有写死,例如:

  • 数据库ip:通过${hm.db.host:101.35.55.85}配置了默认值为101.35.55.85,同时允许通过${hm.db.host}来覆盖默认值
  • 数据库端口:通过${hm.db.port:3306}配置了默认值为3306,同时允许通过${hm.db.port}来覆盖默认值
  • 数据库database:可以通过${hm.db.database}来设定,无默认值
  • 后端本地配置大于Nacos配置中心的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
datasource:
url: jdbc:mysql://${hm.db.host:101.35.55.85}:3306/${hm.db.database}?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: ${hm.db.pw:123}
mybatis-plus:
configuration:
default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
global-config:
db-config:
update-strategy: not_null
id-type: auto

4.2.2 Swagger 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
knife4j:
enable: true
openapi:
title: ${hm.swagger.title:黑马商城接口文档}
description: ${hm.swagger.description:黑马商城接口文档}
email: ${hm.swagger.email:zhanghuyi@itcast.cn}
concat: ${hm.swagger.concat:虎哥}
url: https://www.itcast.cn
version: v1.0.0
group:
default:
group-name: default
api-rule: package
api-rule-resources:
- ${hm.swagger.package}

4.2.3 日志配置

1
2
3
4
5
6
7
logging:
level:
com.hmall: debug
pattern:
dateformat: HH:mm:ss:SSS
file:
path: "logs/${spring.application.name}"

4.3 后端配置

4.3.1 配置bootstrap.yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
application:
name: cart-service
cloud:
nacos:
config:
server-addr: 101.35.55.85 # nacos地址
file-extension: yaml # 文件后缀名
shared-configs: # 共享配置
- dataId: shared-jdbc.yaml # 共享mybatis配置
- dataId: shared-log.yaml # 共享日志配置
- dataId: shared-swagger.yaml # 共享日志配置
group: DEFAULT_GROUP
refresh: true # 配置是否动态刷新

hm:
swagger:
title: 购物车服务接口文档
controller: com.hmall.cart.controller

4.3.2 完善数据库连接配置

1
2
3
4
5
hm:
db:
host: 101.35.55.85 # 修改为你自己的虚拟机IP地址
pw: 123 # 修改为docker中的MySQL密码
database: hm-cart #增加这句来指定相应的数据库

4.3.3 减去相应的配置

1
2
3
4
5
6
7
8
9
server:
port: 8083
spring:
cloud:
nacos:
discovery:
server-addr: 101.35.55.85:8848
profiles:
active: dev

4.4 实现热更新配置

4.4.1 Nacos新增配置

1
2
3
hm:
cart:
maxNum: 1 # 购物车商品数量上限

4.4.2 修改后端代码

方式一 适合单个属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Service
@RequiredArgsConstructor
@RefreshScope # 在类名上添加注解 开启热更新
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {


#注入属性
@Value("${hm.cart.maxNum}")
private Integer maxNum;


#添加判断
//方法二
if (count>= maxNum){
throw new BizIllegalException(StrUtil.format("用户购物车课程不能超过{}", maxNum));
}
方式二 适合多个属性

创建实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.hmall.cart.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* 通过配置文件自动绑定属性值到该类的字段上。
* 使用lombok简化了getter和setter方法的编写。
*/
@Data // Lombok注解,自动生成getter和setter方法,以及toString、equals、hashCode方法
@Component // 表明该类是一个组件,Spring会自动检测并注册到ApplicationContext中
@ConfigurationProperties(prefix = "hm.cart") // 指定配置属性的前缀,用于绑定nacos配置文件中的属性
public class CartProperties {

/**
* 购物车最大商品数量。
* 配置属性示例:hm.cart.maxAmount=100
*/
private Integer maxNum;
}

直接使用类属性

1
2
3
4
//配置热部署 编写逻辑
if (count>= cartProperties.getMaxAmount()){
StrUtil.format("用户购物车课程不能超过{}", cartProperties.getMaxAmount());
}

Nacos
http://example.com/2024/03/21/Nacos/
作者
kangkang
发布于
2024年3月21日
许可协议