Record the operation process of configuring the nacos registry with spring cloud 2.2.7

Record the operation process of configuring the nacos registry with spring cloud 2.2.7

1. Import pom dependency

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.2.7.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

2. Create a registry

Registry: records the mapping relationship between the service and the service address. After the service is registered in the registry, when the service needs to call other services, it will find the service address in the registry to call.

  1. Service discovery:
    Service registration / de registration: saves the information of service providers and service callers
    Service subscription / unsubscribe: the service caller subscribes to the information of the service provider, preferably with the function of real-time push
    Service routing (optional): it has the ability to filter and integrate service providers.

  2. Service configuration:
    Configure subscription: service providers and service callers subscribe to micro service related configurations
    Configuration distribution: actively push the configuration to service providers and service callers

  3. Service health test:
    Check the health of the service provider

    Create registry nacos

    1. Import dependency

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.1.4.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
org.springframework.cloud spring-cloud-starter-netflix-ribbon

This dependency is used to clear the package of spring cloud starter Netflix ribbon contained in spring cloud starter Alibaba Nacos discovery. The reason is that the latest version of spring cloud does not support ribbon load balancing, but uses spring cloud starter loadbalancer to enable load balancing

Import spring cloud starter loadbalancer

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    <version>3.1.0</version>
</dependency>

Spring cloud starter loadbalancer is used in conjunction with feign. This does not affect the use of nacos

4. Download the nacos service from the official website and start the nacos service

On the left is the style after the successful start of the nacos service, and on the right is the nacos service downloaded from the official website. Click start under the bin folder CMD start

5. In application YML configuring the nacos address

cloud:
  nacos:
    discovery:
      server-addr: 127.0.0.1:8848
application:
  name: The service name of the service is named by itself

6. Configure annotations on the service startup class

@EnableDiscoveryClient

7. Start the service and enter 127.0.0.1:8848/nacos on the web page to join the login interface of nacos service management system

Both account and password are nacos

After successful login, you can see the service registered with nacos
Well, after this, it proves that our nacos registry has been successful!
Here I would also like to add that when importing nacos dependencies, do not import them first

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>${latest.version}</version>
</dependency>

This dependency is the configuration center dependency of nacos. After importing this dependency, it needs to be in bootstrap YML configuration. If it is not configured, an error will be reported after starting the service. In addition, an error will be recorded when some small partners import the dependency of the nacos registry from the official website

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

Most of the reasons for error reporting are that there is no guidance in Maven. After research, this reason is that we can't find nacos in Maven warehouse. At this time, we need to add < Version > version number to indicate the dependency of which version to download. Another solution is to modify the alibaba image address in the setting file in Maven

Keywords: Java Spring Cloud Microservices

Added by aaadispatch on Mon, 17 Jan 2022 17:46:45 +0200