springcloudAlibaba_nacos 服务注册中心
概述
为什么叫Nacos
前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service,也就是命名配置服务
Nacos是什么
一个更易于构建云原生应用的动态服务发现,配置管理和服务管理中心
Nacos:Dynamic Naming and Configuration Service
Nacos就是注册中心+配置中心的组合,可以类比为 Eureka + Config + Bus
Nacos与其他注册中心对比
安装和运行
1.预备环境准备
Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用:
- 64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。
- 64 bit JDK 1.8+;下载 & 配置。
- Maven 3.2.x+;下载 & 配置。
2.下载编译后压缩包方式
您可以从 最新稳定版本 下载 nacos-server-$version.zip 包。
|
|
3.启动服务器
|
|
4.进入管理后台:http://localhost:8848/nacos
默认账号密码nacos/nacos
4.服务注册&发现和配置管理
服务注册
|
|
服务发现
|
|
发布配置
|
|
获取配置
|
|
服务注册中心演示
nacos和eureka在代码上集成ribbon,feion是一样的,没有任何区别,只是配置中心从eureka换成了nacos
基于Nacos的服务提供者
-
新建moudle cloudalibaba-provider-payment9001
-
父pom
1 2 3 4 5 6 7 8
<!--spring cloud alibaba 2.2.0.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>
-
本模块pom
1 2 3 4
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
完整配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <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> </dependencies>
-
yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
server: port: 9001 spring: application: name: cloudalibaba-provider-payment9001 cloud: nacos: discovery: #配置nacos地址 server-addr: localhost:8848 management: endpoints: web: exposure: include: '*'
-
主启动
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.eh.cloud.alibaba.nacos.payment; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class NacosPaymentMain9001 { public static void main(String[] args) { SpringApplication.run(NacosPaymentMain9001.class, args); } }
-
业务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package com.eh.cloud.alibaba.nacos.payment.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id) { return "nacos registry, serverPort: " + serverPort + "\t id" + id; } }
-
验证
运行main程序,访问 http://localhost:9001/payment/nacos/1, 成功响应
查看nacos管理后台,可以看到服务已经注册进来了
nacos同样集成了ribbon,天然支持负载均衡,再启动一个端口为9002的服务提供者实例,供下一节演示使用
设置一个新的端口号9002
基于Nacos的服务消费者
-
新建moudle,cloudalibaba-consumer-nacos-order83
-
pom
同服务提供者
-
yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
server: port: 83 spring: application: name: cloudalibaba-consumer-nacos-order83 cloud: nacos: discovery: #配置nacos地址 server-addr: localhost:8848 # 消费者将要去访问的微服务名称(成功注册进nacos的微服务提供者) # 待会在controller里注入供restTemplate使用 service-url: nacos-user-service: http://cloudalibaba-provider-payment9001
-
主启动类
同服务提供者
-
业务类
BeanConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package com.eh.cloud.alibaba.nacos.consumer.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class BeanConfig { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
OrderNacosController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package com.eh.cloud.alibaba.nacos.consumer.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.client.RestTemplate; public class NacosOrderController { @Autowired private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}") private String serverURL; @GetMapping(value = "/order/payment/nacos/{id}") public String paymentInfo(@PathVariable("id") Long id) { return restTemplate.getForObject(serverURL + "/payment/nacos/" + id, String.class); } }
-
验证
访问http://localhost:83/order/payment/nacos/1,刷新可以看到俩端口号交替出现
查看nacos后台管理界面,可以看到服务消费者注册进来了
服务注册中心对比
Nacos生态图
Nacos和CAP
Nacos支持AP和CP模式的切换
C
一致性 A
高可用 P
容错性,主流选用的都是 AP 模式,保证系统的高可用。
何时选择使用何种模式
一般来说,如果不需要存储服务级别的信息,且服务实例是通过 Nacos-client 注册,并能够保证心跳上报,那么就可以选择 AP 模式。当前主流的服务如 Spring Cloud 和 Dubbo 服务,都适用于 AP 模式,AP模式为了服务的可用行而减弱了一致性,因此 AP 模式下只支持注册临时实例。
如果需要在服务级别编辑或者存储配置信息,那么 CP 是必须的,K8S服务和 DNS服务则适用于 CP 模式。CP模式下则支持注册持久化实例,此时则是以 Raft 协议为集群运行模式,该模式下注册实例之前必须先注册服务,如果服务不存在,则会返回错误。
Nacos AP/CP模式切换命令
Nacos 集群默认支持的是CAP原则中的 AP原则
,但是 也可切换为CP原则
,切换命令如下:
|
|
同时微服务的 bootstrap.properties 需配置如下选项指明注册为临时/永久实例,AP模式不支持数据一致性,所以只支持服务注册的临时实例
,CP模式支持服务注册的永久实例,满足配置文件的一致性。
|
|