Nacos作为注册中心

8/24/2021 微服务

# Nacos作为注册中心💥

前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service.

一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
Nacos: Dynamic Naming and Configuration Service

Nacos就是注册中心+配置中心的组合 --> 等价于 Nacos = Eureka+Config +Bus
1
2
3
4
5
6

# 基于Nacos服务提供者💫

# 1、引入依赖💨

<dependencies>
    <!--SpringCloud ailibaba nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- SpringBoot整合Web组件 -->
    <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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 2、YML配置💨

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址
        # 换成nginx的1111端口,做集群
        #server-addr: 192.168.111.144:1111

management:
  endpoints:
    web:
      exposure:
        include: '*'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 3、主启动类开启注解💨

/**
 * @ClassName: PaymentMain9001
 * @Description: 主启动
 * @author: LiJunYi
 */
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain9001
{
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain9001.class, args);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 4、Controller测试接口💨

/**
 * @ClassName: PaymentController
 * @Description:
 * @author: LiJunYi
 */
@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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Nacos管理界面🎱

nacos_2

# 基于Nacos服务消费者💫

# 1、引入依赖💨

<dependencies>
    <!--SpringCloud ailibaba nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- SpringBoot整合Web组件 -->
    <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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Nacos自带负载均衡:集成了Ribbon💨

nacos_3

# 2、YML配置💨

server:
  port: 83
spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
  nacos-user-service: http://nacos-payment-provider
1
2
3
4
5
6
7
8
9
10
11
12

# 3、主启动类开启注解💨

/**
 * @ClassName: OrderNacosMain83
 * @Description:
 * @author: LiJunYi
 */
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain83
{
    public static void main(String[] args)
    {
        SpringApplication.run(OrderNacosMain83.class,args);
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 4、Ribbon的有关配置💨

/**
 * @author: LiJunYi
 */
@Configuration
public class ApplicationContextConfig
{
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 5、Controller测试接口💨

/**
 * @author: LiJunYi
 */
@RestController
@Slf4j
public class OrderNacosController
{
    @Resource
    private RestTemplate restTemplate;

    @Value("${service-url.nacos-user-service}")
    private String serverURL;

    @GetMapping(value = "/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Long id)
    {
        return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 服务注册中心对比💢

# Nacos支持AP和CP💨

nacos_5

# Nacos支持AP和CP切换💢

C是所有节点在同一时间看到的数据是一致的
而A的定义是所有的请求都会收到响应。

何时选择使用何种模式?

一般来说

一、 如果不需要存储服务级别的信息且服务实例是通过nacos-chient主册,并能够保持心跳上报,那么就可以选择AP模式。 当前主流的服务如Spring cloud 和 Dubbo 服务,都适用于AP模式。 AP模式为了服务的可能性而减弱了一致性,因此AP模式下只支持注册临时实例。

二、 如果需要在服务级别偏辑或者存储配置信息,那么CP是必须。K8S服务和DNS服务则适用于CP模式。 CP模式下则支持注册持久化实例,此时则是以 Raft协议为集群运行模式,该模式下注册实例之前必须先注册服务,如果服务不存在,则会返回错识.

curl -X PUT "$NACOs_SERVERA8848/nacos/1/ns/operator/switches?entrymserverMode&calue=CP
1