Spring Cloud Alibaba tutorial series – Registering and discovering Nacos services

preface

Recently I have been working with Nacos. I should have played it once half a year ago. Now I will review it and feel the latest version of Nacos.

There’s not much nonsense. Let’s practice.

Version Description:

component The version number
spring-boot-starter-parent 2.2.6. RELEASE
spring-cloud-alibaba-dependencies 2.2.0. RELEASE

The preparatory work

A quick introduction to NACOS:

Nacos is dedicated to helping you discover, configure, and manage microservices. Nacos provides an easy-to-use feature set that helps you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.

Nacos is a component that combines a service registry and a service configuration center. Equivalent to a combination of Eureka and Config in the Spring Cloud, doesn’t nacOS feel powerful?

Remark:Nacos Describes the address

Install nacos

Installation of nacOS is not explained here, you can refer to the nacOS website (nacOS website).

Simply record the startup command:

  • The Windows platform

    cmd startup.cmd

  • Linux/Unix/Mac

    Sh -m standalone mode: standalone mode

Verify nacos

After local boot nacos, enter http://localhost:8848/nacos/ to nacos landing page, then enter user name and password, the default user name password is nacos. Nacos landing page as shown in the figure below:

The page after successful login is as follows:

image-20200507092213234

One of the great things about NACOS is that it gives us an easy-to-use action page.

Next we start the project, (formula: build module, change POM, change YML, configure startup class).

The new module

Creating a Module is only introduced here. If you are not familiar with it, you can check out my previous spring Cloud tutorial.

Three new modules are created here, adopting the parent-child engineering structure, namely, NACOS-Serever, provider-Server and consumer-Servere.

Modified pom

Only the poMs used are listed here, and other introduced POMs can be viewed in the project source code

  • nacos-server pom
<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> Copy the code
  • provider-server pom
<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> Copy the code
  • consumer-server pom
<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> Copy the code

Configuration yml

  • nacos-server application.yml
server:
  port: 8001
Copy the code

Spring: Cloud: nacos: Discovery: server-addr: 127.0.0.1:8848 Application: name: nacos-server

  • provider-server application.yml
server:
  port: 9001

spring: application: name: provider-server

Copy the code

Cloud: nacos: discovery: server-addr: localhost:8848 # nacos: discovery: server-addr: localhost:8848 # nacos

  • consumer-server application.yml
server:
  port: 9002

spring: application: name: consumer-serve

Copy the code

Cloud: nacos: discovery: server-addr: localhost:8848 # nacos: discovery: server-addr: localhost:8848 # nacos

Configure the primary startup class

  • nacos-server NacosServerApplicationClass: add@EnableDiscoveryClientannotations
@SpringBootApplication
@EnableDiscoveryClient
public class NacosServerApplication {

 public static void main(String[] args) {
 SpringApplication.run(NacosServerApplication.class, args);  }  } Copy the code

Similarly the provider – server ProviderServerApplication class increases @ EnableDiscoveryClient annotations, Consumer – server ConsumerServerApplication class increases @ EnableDiscoveryClient annotation.

Then start the three projects respectively, as shown in the figure :(nacos-server, provider-server and consumer-server have been registered successfully)

image-20200508234859463

At this point, the nacOS service discovery setup is complete. It’s relatively simple. Next, let’s look at the discovery of services.

Discovery of services

Provider -server adds service classes

  • TestProviderController class
@RestController
@RequestMapping("/provider")
public class TestProviderController {

 @Value("${server.port}")
 private String port;   @RequestMapping("/test/{name}")  public String test(@PathVariable("name") String name) {  return "nacos server - provider - server : port : " + port + ">>> operator:" + name + "Charge forward";  } } Copy the code

Consumer – server application. Yml modification

Application. Yml adds the following configuration:

The name of the microservice that the consumer will access (the microservice provider that successfully registered in NACOS, corresponding to the provider's spring.application.name)
remote-service-url:
  provider-service-url: http://provider-server
Copy the code

Add configuration classes and business classes to consumer-server

  • RestTemplate configuration class
/ * ** Configure the RestTemplate Bean* /
@Configuration
public class ApplicationContextConfig {
  @Bean  @LoadBalanced  public RestTemplate getRestTemplate(a) {  return new RestTemplate();  } } Copy the code
  • TestConsumerController business class
/ * ** Consumer test classes* /
@RestController
@RequestMapping("/consumer")
public class TestConsumerController {   @Resource  private RestTemplate restTemplate;   // application.yml Specifies the remote address prefix  @Value("${remote-service-url.provider-service-url}")  private String serverUrl;   @RequestMapping("/test/{name}")  public String test(@PathVariable("name") String name) {  String url = serverUrl + "/provider/test/" + name;  return restTemplate.getForObject(url, String.class);  } } Copy the code

Start the project

Start three projects respectively, and then access the consumer class – the test server IP address: http://localhost:9002/consumer/test/maomao,

image-20200509213807465

As shown in the figure above, the service that calls provider-Server on behalf of consumer-Server has been configured successfully.

At this point, the service discovery of NACOS is completed.

conclusion

This paper briefly introduces the service registration and discovery of Spring Cloud Alibaba’s NACOS. Compared with other registry plug-ins, nacOS is relatively simple to use, and it also provides us with an operating interface, saving my secondary development.

Also, NacOS is a natural load balancer, so we can look at the jar dependencies and have introduced Cloud Ribban, which supports load balancers and RestTemplate requests.

Other instructions

Nacos supports SWITCHING between AP and CP:

  1. If you do not need to store service level information, and the service instance is registered through nacos-Client and can keep heartbeat reporting, you can choose AP mode.

    Current mainstream service registries, such as Spring Cloud and Dubbo services, are applicable to AP. Ap reduces consistency for service availability, so only temporary instances can be registered in AP mode.

  2. If configuration information needs to be edited or stored at the service level, it must be in CP mode. The K8S service and DNS service are suitable for CP mode.

    Cp mode supports registering persistent instances, in this case Raft

    In this mode, the service must be registered before the instance is registered. If the service does not exist, an error message is displayed.

Switch command: value=cp/ap

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

The source address

  • github
  • gitee