SpringCloudAlibaba-注册中心_配置中心_负载均衡_消息总线(Nacos)
Nacos
概述
为什么叫Nacos?
前4个分别为 Naming和Configuration的前2个字母,s是Service;
what
Dynamic Naming and Configuration Service
一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台;
(注册中心+配置中心)
Nacos 等价于 Eureka+Config+Bus
https://github.com/alibaba/Nacos
https://nacos.io/zh-cn/docs/what-is-nacos.html
How-注册中心
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
NacosServer安装与运行
下载
https://github.com/alibaba/nacos/tags
解压包 并 运行bin下的startup.sh
登录
http://127.0.0.1:8848/nacos/#/login
默认账号/密码 nacos/nacos
NacosClient
父pom
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
provider
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
server:
port: 9001
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: '*'
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderDemoApplication.class, args);
}
}
@RestController
public class EchoController {
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
consumer
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
server:
port: 80
spring:
application:
name: nacos-cosumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
service-url: #消费者要调用的服务名称
nacos-user-service: http://nacos-provider
@Configuration
public class Config {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConsumer80 {
public static void main(String[] args) {
SpringApplication.run(NacosConsumer80.class, args);
}
}
@RestController
public class Controller {
@Autowired
private RestTemplate restTemplate;
@Value(value = "${service-url.nacos-user-service}")
private String serviceUrl;
@GetMapping(value = "/nacos/order")
public String test(){
return restTemplate.getForObject(serviceUrl + "/echo/s", String.class);
}
}
注册中心对比


Nacos支持AP和CP的切换
前言
C:一致性
所有的节点在同一时间看到的数据都是一致的;
A:可用性
所有的请求都有响应;
何时选择何种模式?
如果不需要存储 服务级别的信息 且 服务实例通过nacos-client注册,能保持心跳,可选AP;
当前主流的Spring Cloud/Dubbo,都适用于AP;
AP模式为了服务的可用性,减弱了一致性,因此只适合注册临时实例;
如果 需要存储/编辑 服务级别信息 ,CP是必选;
CP模式支持 注册持久化实例;
以Raft协议为集群运行模式;
How
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'
Nacos-配置中心
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
ConfigServer
nacos-server下载运行
ConfigClient
基础配置
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
---bootstrap.yml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos-server服务注册中心地址
config:
server-addr: localhost:8848 #nacos-server作为配置中心
file-extension: yaml #指定yaml格式配置
---application.yml
spring:
profiles:
active: dev
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClient3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigClient3377.class, args);
}
@RefreshScope //支持Nacos动态刷新
@RestController
public class Controller {
@Value(value = "${config.info}")
private String info;
@GetMapping(value = "/getConfig")
public String getConfig(){
return info;
}
}
}
配置文件
Nacos与SpringCloudConfig一样,在项目初始化的时候,保证先从配置中心进行配置拉取,拉取成功后,才能保证项目的正常运行;
分类配置
why
问题1:实际开发中,一个项目会存在dev,test,prod等不同的环境;
问题2:每个微服务项目都有不同的环境;
what

在 Nacos Spring Cloud 中,dataId 的完整格式如下:${prefix}-${spring.profiles.active}.${file-extension}
Namespace+Group+DataId;
Namespace:区分部署环境;
Group+DataId:逻辑上区分2个目标对象;
默认情况,Namespace=public、Group=DEFAULT_GROUP、Cluster=DEFAULT
How
---bootstrap.yml
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos-server服务注册中心地址
config:
server-addr: localhost:8848 #nacos-server作为配置中心
file-extension: yaml #指定yaml格式配置
namespace: 49415b4e-f691-48cb-851e-84c994d248d5 #dev namespace ID
group: DEV_GROUP #分组
application.yml
spring:
profiles:
active: dev
Nacos-负载均衡

Nacos集群/持久化配置
默认Nacos使用 嵌入式数据库 存储数据;
如果启动多个默认配置下的Nacos节点,多个Nacos节点存在数据一致性问题;
为了解决多Nacos节点数据一致性问题,Nacos采用 集中式存储的方式(目前只支持MySQL) 支持集群部署;
Nacos持久化
Nacos默认自带数据库
嵌入式数据库derby
https://github.com/alibaba/nacos/blob/develop/config/pom.xml

derby转换到MySQL
https://nacos.io/zh-cn/docs/deployment.html
1、Nacos-Server目录 /nacos/conf 下找到 nacos-mysql.sql,在mysql执行;
2、Nacos-Server目录 /nacos/conf 下找到 application.properties,增加支持mysql数据源配置(目前只支持mysql),添加mysql数据源的url、用户名和密码;
Nacos集群
https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
1 Nginx+3 NacosServer+1 Mysql

nacosserver集群
老版本
1、MySQL脚本执行
from nacos-server/conf/ nacos-mysql.sql
2、nacos-server 切换至MySQL
from /nacos/conf 下找到 application.propertie
3、nacos-server 集群配置
from /nacos/conf 下找到 cluster.conf
查看本机ip hostname -i

4、编辑nacos启动脚本startup.sh,使其能接受不同的启动端口 (老版本)


新版本
1、将nacos解压复制3份,分别为nacos2222、nacos3333、nacos4444
2、修改nacosxxx/conf/application.properties 的端口号,切换为MySQL数据源
3、修改nacosxxx/conf/cluster.conf 内容
4、分别启动3个nacosserver
Nginx
1、修改nginx.conf的内容

2、指定nginx.conf启动nginx
验证
http://localhost:1111/nacos/#/login
nacosClient微服务访问nginx,由nginx负责路由到不同的nacosServer
浙公网安备 33010602011771号