1.1 Nacos 介绍
Nacos (Dynamic Naming and Confifiguration Service)是阿⾥巴巴开源的⼀个针对微服务架构中服务发现、配置管理和服务管理平台。
Nacos就是注册中⼼+配置中⼼的组合(Nacos=Eureka+Confifig+Bus)
官⽹:https://nacos.io 下载地址:https://github.com/alibaba/Nacos
Nacos功能特性
- 服务发现与健康检查
- 动态配置管理
- 动态DNS服务
服务和元数据管理(管理平台的⻆度,nacos也有⼀个ui⻚⾯,可以看到注册的服务及其实例信息(元数据信息)等),动态的服务权重调整、动态服务优雅下线,都可以去做
1.2 Nacos 单例服务部署
下载解压安装包,执⾏命令启动(我们使⽤最近⽐较稳定的版本 nacos-server-1.2.0.tar.gz)访问nacos管理界⾯:http://127.0.0.1:8848/nacos/#/login(默认端⼝8848,账号和密码nacos/nacos)
下载解压安装包,执⾏命令启动(我们使⽤最近⽐较稳定的版本 nacos-server-1.2.0.tar.gz)linux/mac:sh startup.sh -m standalone
windows:cmd startup.cmd
访问nacos管理界⾯:http://127.0.0.1:8848/nacos/#/login(默认端⼝8848,账号和密码 nacos/nacos)
1.3 Nacos 服务注册中⼼
1.3.1 服务提供者注册到Nacos(改造简历微服务)
在⽗pom中引⼊SCA依赖
<dependencyManagement> <dependencies> <!--SCA --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> <!--SCA --> </dependencyManagement>
在服务提供者⼯程中引⼊nacos客户端依赖(注释eureka客户端)
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
application.yml修改,添加nacos配置信息 ```yaml server: port: 8082 spring: application: name: lagou-service-resume datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/lagou?useUnicode=true&characterEncoding=utf8 username: root password: 123456 jpa: database: MySQL show-sql: true hibernate:
naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
nacos配置
cloud: nacos:
discovery: # 集群中各节点信息都配置在这里(域名-VIP-绑定映射到各个实例的地址信息) server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850 # nacos config 配置 config: server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850 # 锁定server端的配置文件(读取它的配置项) namespace: 07137f0a-bf66-424b-b910-20ece612395a # 命名空间id group: DEFAULT_GROUP # 默认分组就是DEFAULT_GROUP,如果使用默认分组可以不配置 file-extension: yaml #默认properties # 根据规则拼接出来的dataId效果:lagou-service-resume.yaml ext-config[0]: data-id: abc.yaml group: DEFAULT_GROUP refresh: true #开启扩展dataId的动态刷新 ext-config[1]: data-id: def.yaml group: DEFAULT_GROUP refresh: true #开启扩展dataId的动态刷新
management: endpoints: web:
exposure: include: "*"
启动简历微服务,观察nacos控制台<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1699668/1599471958317-3851899f-06e9-4739-90c8-ebd4931d4d14.png#align=left&display=inline&height=519&margin=%5Bobject%20Object%5D&name=image.png&originHeight=519&originWidth=1912&size=95971&status=done&style=none&width=1912)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1699668/1599471963740-2a3a091b-476b-46ce-b7b0-c29c54326e44.png#align=left&display=inline&height=790&margin=%5Bobject%20Object%5D&name=image.png&originHeight=790&originWidth=1728&size=165970&status=done&style=none&width=1728)<br />保护阈值:可以设置为0-1之间的浮点数,它其实是⼀个⽐例值(当前服务健康实例数/当前服务总实例数)<br />场景:<br />⼀般流程下,nacos是服务注册中⼼,服务消费者要从nacos获取某⼀个服务的可⽤实例信息,对于服务实例有健康/不健康状态之分,nacos在返回给消费者实例信息的时候,会返回健康实例。这个时候在⼀些⾼并发、⼤流量场景下会存在⼀定的问题<br />如果服务A有100个实例,98个实例都不健康了,只有2个实例是健康的,如果nacos只返回这两个健康实例的信息的话,那么后续消费者的请求将全部被分配到这两个实例,流量洪峰到来,2个健康的实例也扛不住了,整个服务A 就扛不住,上游的微服务也会导致崩溃,,,产⽣雪崩效应。<br />保护阈值的意义在于<br />当服务A健康实例数/总实例数 < 保护阈值 的时候,说明健康实例真的不多了,这个时候保护阈值会被触发(状态true)<br />nacos将会把该服务所有的实例信息(健康的+不健康的)全部提供给消费者,消费者可能访问到不健康的实例,请求失败,但这样也⽐造成雪崩要好,牺牲了⼀些请求,保证了整个系统的⼀个可⽤。<br />注意:阿⾥内部在使⽤nacos的时候,也经常调整这个保护阈值参数。
<a name="5ZUTI"></a>
### 1.3.2 服务消费者从Nacos获取服务提供者(改造⾃动投递微服务)
同服务提供者<br />测试<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1699668/1599472023308-9e99424c-350e-428e-9351-e8f0215e343a.png#align=left&display=inline&height=270&margin=%5Bobject%20Object%5D&name=image.png&originHeight=270&originWidth=1519&size=63225&status=done&style=none&width=1519)<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1699668/1599472027109-397a25c1-a8fb-41df-a6d6-f918ef733f23.png#align=left&display=inline&height=550&margin=%5Bobject%20Object%5D&name=image.png&originHeight=550&originWidth=1696&size=76199&status=done&style=none&width=1696)
<a name="up1Eh"></a>
### 1.3.3 负载均衡
Nacos客户端引⼊的时候,会关联引⼊Ribbon的依赖包,我们使⽤OpenFiegn的时候也会引⼊Ribbon的依赖,Ribbon包括Hystrix都按原来⽅式进⾏配置即可<br />此处,我们将简历微服务,⼜启动了⼀个8083端⼝,注册到Nacos上,便于测试负载均衡,我们通过后台也可以看出。
<a name="Db2Qb"></a>
### 1.3.4 Nacos 数据模型(领域模型)
Namespace命名空间、Group分组、集群这些都是为了进⾏归类管理,把服务和配置⽂件进⾏归类,归类之后就可以实现⼀定的效果,⽐如隔离<br />⽐如,对于服务来说,不同命名空间中的服务不能够互相访问调⽤<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1699668/1599472058561-f5ba5e76-e2db-4a69-9849-616bd5cab93b.png#align=left&display=inline&height=501&margin=%5Bobject%20Object%5D&name=image.png&originHeight=501&originWidth=878&size=92923&status=done&style=none&width=878)
- Namespace:命名空间,对不同的环境进⾏隔离,⽐如隔离开发环境、测试环境和⽣产环境
- Group:分组,将若⼲个服务或者若⼲个配置集归为⼀组,通常习惯⼀个系统归为⼀个组
- Service:某⼀个服务,⽐如简历微服务
- DataId:配置集或者可以认为是⼀个配置⽂件
**Namespace + Group + Service 如同 Maven 中的GAV坐标,GAV坐标是为了锁定Jar,⼆这⾥是为了锁定服务**<br />**Namespace + Group + DataId 如同 Maven 中的GAV坐标,GAV坐标是为了锁定Jar,⼆这⾥是为了锁定配置⽂件**<br />**最佳实践**<br />**Nacos抽象出了Namespace、Group、Service、DataId等概念,具体代表什么取决于怎么⽤(⾮常灵活),推荐⽤法如下**<br />**![image.png](https://cdn.nlark.com/yuque/0/2020/png/1699668/1599472097701-0c7041eb-6381-4514-9afe-cf3db333ec01.png#align=left&display=inline&height=292&margin=%5Bobject%20Object%5D&name=image.png&originHeight=292&originWidth=1032&size=51623&status=done&style=none&width=1032)**<br />Nacos服务的分级模型<br />![image.png](https://cdn.nlark.com/yuque/0/2020/png/1699668/1599472108406-6e5ff0a2-8c6b-4713-8fbe-7802973c310a.png#align=left&display=inline&height=451&margin=%5Bobject%20Object%5D&name=image.png&originHeight=451&originWidth=1001&size=94409&status=done&style=none&width=1001)
<a name="q4kRp"></a>
### 1.3.5 Nacos Server 数据持久化
Nacos 默认使⽤嵌⼊式数据库进⾏数据存储,它⽀持改为外部Mysql存储
- 新建数据库 nacos_confifig,数据库初始化脚本⽂件 ${nacoshome}/conf/nacos-mysql.sql
- 修改${nacoshome}/conf/application.properties,增加Mysql数据源配置
```java
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?
characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnec
t=true
db.user=root
db.password=123456
1.3.6 Nacos Server 集群
- 安装3个或3个以上的Nacos
复制解压后的nacos⽂件夹,分别命名为nacos-01、nacos-02、nacos-03
修改配置⽂件
- 同⼀台机器模拟,将上述三个⽂件夹中application.properties中的server.port分别改为8848、8849、8850
同时给当前实例节点绑定ip,因为服务器可能绑定多个ip
nacos.inetutils.ip-address=127.0.0.1
复制⼀份conf/cluster.conf.example⽂件,命名为cluster.conf
- 在配置⽂件中设置集群中每⼀个节点的信息
分别启动每⼀个实例(可以批处理脚本完成)# 集群节点配置 127.0.0.1:8848 127.0.0.1:8849 127.0.0.1:8850
sh startup.sh -m cluster
1.4 Nacos 配置中⼼
之前:Spring Cloud Confifig + Bus
1) Github 上添加配置⽂件
2)创建Confifig Server 配置中⼼—>从Github上去下载配置信息
3)具体的微服务(最终使⽤配置信息的)中配置Confifig Client—> ConfifigServer获取配置信息有Nacos之后,分布式配置就简单很多
Github不需要了(配置信息直接配置在Nacos server中),Bus也不需要了(依然可以完成动态刷新)
接下来
1、去Nacos server中添加配置信息
2、改造具体的微服务,使其成为Nacos Confifig Client,能够从Nacos Server中获取到配置信息Nacos server 添加配置集
Nacos 服务端已经搭建完毕,那么我们可以在我们的微服务中开启 Nacos 配置管理
1)添加依赖 ```xml
2)微服务中如何锁定 Nacos Server 中的配置⽂件(dataId)<br />通过 Namespace + Group + dataId 来锁定配置⽂件,Namespace不指定就默认public,Group不指定就默认 DEFAULT_GROUP<br />**dataId 的完整格式如下**
```xml
${prefix}-${spring.profile.active}.${file-extension}
- prefix 默认为 spring.application.name 的值,也可以通过配置项
spring.cloud.nacos.config.prefix 来配置。
- spring.profile.active 即为当前环境对应的 profifile。 注意:当 spring.profile.active为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${fileextension}
- file-exetension 为配置内容的数据格式,可以通过配置项
spring.cloud.nacos.config.file-extension 来配置。⽬前只⽀持 properties 和 yaml类型。
cloud:
nacos:
discovery:
# 集群中各节点信息都配置在这⾥(域名-VIP-绑定映射到各个实例的地址信息)
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
namespace: f965f7e4-7294-40cf-825c-ef363c269d37
group: DEFAULT_GROUP
file-extension: yaml
3)通过 Spring Cloud 原⽣注解 @RefreshScope 实现配置⾃动更新
package com.lagou.edu.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 该类用于模拟,我们要使用共享的那些配置信息做一些事情
*/
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
// 和取本地配置信息一样
@Value("${lagou.message}")
private String lagouMessage;
@Value("${mysql.url}")
private String mysqlUrl;
@Value("${abc.test}")
private String abctest;
@Value("${def.test}")
private String deftest;
@Value("${java.first}")
private String javafirst;
// 内存级别的配置信息
// 数据库,redis配置信息
@GetMapping("/viewconfig")
public String viewconfig() {
return "lagouMessage==>" + lagouMessage + " mysqlUrl=>" + mysqlUrl
+ " abctest=>" + abctest + " deftest=>" + deftest + " javafirst=>" + javafirst;
}
}
思考:⼀个微服务希望从配置中⼼Nacos server中获取多个dataId的配置信息,可以的,扩展多个dataId
server:
port: 8082
spring:
application:
name: lagou-service-resume
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/lagou?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
# nacos配置
cloud:
nacos:
discovery:
# 集群中各节点信息都配置在这里(域名-VIP-绑定映射到各个实例的地址信息)
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
# nacos config 配置
config:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
# 锁定server端的配置文件(读取它的配置项)
namespace: 07137f0a-bf66-424b-b910-20ece612395a # 命名空间id
group: DEFAULT_GROUP # 默认分组就是DEFAULT_GROUP,如果使用默认分组可以不配置
file-extension: yaml #默认properties
# 根据规则拼接出来的dataId效果:lagou-service-resume.yaml
ext-config[0]:
data-id: abc.yaml
group: DEFAULT_GROUP
refresh: true #开启扩展dataId的动态刷新
ext-config[1]:
data-id: def.yaml
group: DEFAULT_GROUP
refresh: true #开启扩展dataId的动态刷新
management:
endpoints:
web:
exposure:
include: "*"
优先级:根据规则⽣成的dataId > 扩展的dataId(对于扩展的dataId,[n] n越⼤优先级越⾼)