nacos service registration and discovery

nacos service registration

Please refer to the previous blog for basic knowledge of nacos and download and installation: nacos installation, operation and feature understanding
The topic of this blog: both consumers and producers are registered in nacos, and consumer services can achieve load balancing and call producers. And it shows that the CAP principle of nacos conforms to that.
The basic environment required for microservices to register with nacos:

  1. jdk8+
  2. nacos is already running

Approximate code structure:

Producer code

maven
Add nacos dependency

<!--nacos-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

yml
1. Configure the service address of nacos
2. Expose service endpoints

server:
  port: 9001
spring:
  application:
    name: nacos-provider-payment
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Tell the microservice the access address of nacos

management:
  endpoints:
    web:
      exposure:
        include: '*' #Exposure endpoint

Main start
1. Add @ EnableDiscoveryClient: enable the registry to discover and scan the service

/**
 * @author Small fish
 * @version 1.0
 * @date 2021/8/4 11:20 morning
 *  @EnableDiscoveryClient: It enables the registry to discover and scan the changed services
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosPaymentMain9001 {
    public static void main(String[] args) {
        SpringApplication.run(NacosPaymentMain9001.class);
    }
}

Add test class for consumers

/**
 * @author Small fish
 * @version 1.0
 * @date 2021/7/26 8:53 afternoon
 */
@RestController
public class PaymentNacosController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/getPort")
    public String getPort(){
        return "it  is PaymentNacos port "+port;
    }
}

verification
Log in to nacos and view the service list. If the service name appears, it proves that the service is registered with nacos

Micro service cluster registration nacos

Copy a microservice like a feather and modify the yml port number

Consumer code

maven is consistent with the producer
yml

server:
  port: 83
spring:
  application:
    name: nacos-consumer-order
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #nacos address, and register the service in nacos
management:
  endpoints:
    web:
      exposure:
        include: '*'
service-url:
  nacos-user-service: http://Nacos provider payment # producer service name, used for remote call

Main startup class

/**
 * @author Small fish
 * @version 1.0
 * @date 2021/8/4 5:16 afternoon
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerMain83 {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerMain83.class);
    }
}

Remote call configuration class
nacos defaults to automatic load balancing and integrates ribbon itself.

/**
 * @author Small fish
 * @version 1.0
 * @date 2021/8/4 5:22 afternoon
 * @LoadBalanced:Enable load balancing, default polling algorithm
 */
@Configuration
public class MyRest {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

Test request class

/**
 * @author Small fish
 * @version 1.0
 * @date 2021/8/4 5:20 afternoon
 */
@RestController
@Slf4j
public class OrderController {

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

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/getPort")
    public String getPort(){
        return restTemplate.getForObject(url+"/getPort",String.class);
    }
}

verification
visit: http://localhost:83/getPort , if there are different ports in more than two accesses, it proves successful

nacos conforms to the two types of CAP principles

nacos can be switched between AP and CP modes. The default mode is AP mode.
C is all nodes, and the data seen at the same time is consistent; The definition of A is that all requests will receive A response.

How to select mode
In general,
If you do not need to store service level information, and the service instance is registered through the Nacos client and can maintain heartbeat reporting, you can select the AP mode. The current mainstream services, such as Spring cloud and Dubbo services, are applicable to the AP mode. The AP mode weakens the consistency for the possibility of services. Therefore, only temporary instances can be registered in the AP mode.

If you need to edit or store configuration information at the service level, CP is required, and K8S service and DNS service are applicable to CP mode.
In CP mode, persistent instance registration is supported. In this case, the Raft protocol is used as the cluster operation mode. In this mode, the service must be registered before registering the instance. If the service does not exist, an error will be returned.

Keywords: Distribution Spring Cloud Microservices

Added by j0hn_ on Sun, 02 Jan 2022 15:22:17 +0200