nacos as a service registry

Introduction to nacos

  • Why is it called nacos
前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service。
  • What is it
一个更易于构建云原生应用的动态服务发现,配置管理和服务管理中心
Nacos:Dynamic Naming and Configuration Service
Nacos就是注册中心+配置中心的组合
Nacos = Eureka+Config+Bus
  • What can you do
 - 注册中心
 - 配置中心
  • download
https://github.com/alibaba/Nacos

# 文档
https://nacos.io/zh-cn/index.html

https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_nacos_discovery
  • Comparison of various service registration centers
Service registration and discovery framework cap model Console management Community activity
eureka ap stand by 2.x closed source
zk cp not support in
consul cp stand by high
nacos ap or cp switch stand by high

ps: ap means high availability, cp means data must be consistent.


  • Install and run nacos
1. Java + maven ok
2. https://github.com/alibaba/nacos/releases/tag/1.1.4 下载
3. 解压,startup.cmd双击
4. 访问http://localhost:8848 然后账号密码都是nacos

Nacos as a service registry demo

Nacos-based service provider

  • pom

<!--spring cloud alibaba 2.1.0.RELEASE-->

<dependencyManagement>
	<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>
</dependencyManagement>

<!-- 上面是父pom -->
 
<dependencies>
    <!-- nacos -->
    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
 </dependencies>
 

  • yaml
server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址

management:
  endpoints:
    web:
      exposure:
        include: '*'

  • As a client of nacos
@EnableDiscoveryClient
  • The rest interface is omitted, and the interface address is exposed for consumers to access
  • According to the above, you can also build a service provider 9002

Service consumers based on Nacos

  • pom ibid
  • yaml
server:
  port: 83


spring:
  application:
    name: nacos-order-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

# 这里将服务消费者的地址写到了配置文件,方便更改
service-url:
  nacos-user-service: http://nacos-payment-provider


  • As a client consumer of nacos, the master starts
@EnableDiscoveryClient
  • Configure the restTemplate class. And load balancing.
  • Consumer controller
import lombok.extern.slf4j.Slf4j;
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;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;


@RestController
@Slf4j
public class OrderNacosController
{
    
    
    @Resource
    private RestTemplate restTemplate;

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

    @GetMapping(value = "/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Long id)
    {
    
    
        // 服务提供者暴露的rest接口地址  /payment/nacos
        return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
    }

}
 
 

  • test

It was found that 83 calls 9001,9002 were successful and supported the fall.

Nacos switch between ap and cp mode

# cp
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'

# ap
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=AP'

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/111476606