Spring cloud Alibaba - integrating Nacos to realize service registration and discovery

Keywords: Spring Cloud Nacos

preface

Record the integration process of Nacos

environment

Win 10 + Spring Cloud 2020.0.1 + Spring Cloud Alibaba 2021.1 + Nacos 2.0.0


What is Nacos?

  • Nacos is a service discovery component and a configuration server. Nacos solves two problems:
1,service A How to find service B 
2,Manage the configuration of microservices


Principle of service discovery

  • Service discovery mechanism
    Service discovery mechanism is the mechanism that service consumers can always find service providers. For example, service discovery is like a business card in a mobile phone. It records all contact information of a person. A person can be contacted through the business card, and when a contact information does not exist (service DOWN), it will be deleted from the business card.

  • Service discovery mechanism for production:
    1. Cache the micro service, send the current address to the scheduled task, and take the address of the micro service in the cache when calling. The first advantage of this method is that the pressure is small, so you don't have to take it in real time every time. Secondly, even if the service finds that the component crashes, you can get the value in the cache, but you can't update the value in the cache.
    2. Each microservice instance sends a heartbeat (i.e. a request) to the service discovery component to tell it that it is alive. If the service discovery component finds that an instance has not sent a heartbeat to itself for a long time, it is considered that the instance is hung, and its health status is marked as DOWN, and the service consumer will no longer call the instance.


Build Nacos Server

Nacos Server download address

https://github.com/alibaba/nacos/releases


Version selection of Nacos Server

The version selection reference standard of Nacos Server is recorded in spring-cloud-alibaba-dependencies-xxx.pom. For example, the version of my Spring Cloud Alibaba is 2021.1, and the recommended version is 1.4.1 (production environment is preferred)

Because I am a development environment here, the version I choose is 2.0.0


Running Nacos Server

startup.cmd -m standalone

  • visit http://localhost:8848/nacos/#/login (the default port is 8848)

  • Default login account / password: nacos/nacos

... Nacos Server setup completed

Nacos Client registration

User center microservice registration

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.coisini</groupId>
    <artifactId>user-center</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>user-center</name>
    <description>user-center project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.1</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--spring-cloud-starter-{spring cloud Name of subproject}-[{Module name}]-->
        <!-- nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!-- integration spring cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- integration spring cloud alibaba -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • application.yml
spring:
  application:
    # Service name
    name: user-center
  cloud:
    nacos:
      discovery:
        # Specify the address of the nacos server
        server-addr: localhost:8848
  • Start project
  • Visit Nacos

Content center microservice registration

  • pom.xml dependency is the same as above
  • application.yml
spring:
  application:
    # Service name
    name: content-center
  cloud:
    nacos:
      discovery:
        # Specify the address of the nacos server
        server-addr: localhost:8848
  • Visit Nacos


Service discovery test

  • Write test code in content center
@RestController
public class TestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * Testing: service discovery, proving that the content center can always find the user center
     * @return Address information of all instances of user center
     */
    @GetMapping("test")
    public List<ServiceInstance> getInstances() {
        // Query the information of all instances of the specified service
        return discoveryClient.getInstances("user-center");
    }
}
  • As shown below, the content center can obtain the address information of all instances of the user center


- End - ﹀ ﹀ ﹀ White whoring is risky Point zanga collection

Posted by lprocks on Tue, 21 Sep 2021 15:14:53 -0700