nacos - do you understand?

preface:

The previously mentioned zookeeper of the registration center designated by dubbo's official website, now let's talk about the more commonly used nacos

nacos: it is an Alibaba project focusing on service discovery and configuration management. nacos can be integrated with many technologies. Let me talk about the commonly used integration.

Preliminary preparation:

Download the nacos service and decompress it directly.
nacos Download
Note:
If you use the latest version, you may report an error, such as 2.0.3

Recommended command: startup cmd -m standalone
If you want to start directly by double clicking, the recommended version is nacos-server-1.1.3

Integrated use

**
Integration of spring cloud and nacos

  • Add dependency
<!--nacos Start configuration management-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>${latest.version}</version>
</dependency>
<!--start-up nacos Service discovery function-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>0.2.2.RELEASE</version>
</dependency>
<!--nacos client-->
<dependency>
	<groupId>com.alibaba.nacos</groupId>
	<artifactId>nacos-client</artifactId>
	<version>1.1.0</version>
</dependency>
  • Add configuration
# Configuration of service provider
server:
  port: 8000
spring:
  cloud:
    nacos:
      discovery:
        server-addr:  127.0.0.1:8848 # Configure nacos server address
  application:
    name: nacos-provider # Service name
# Consumer configuration
server:
  port: 9000
spring:
  cloud:
    nacos:
      discovery:
        server-addr:  127.0.0.1:8848 # Configure nacos server address
  application:
    name: nacos-consumer # Service name
  • adding annotations
Annotate the position of the startup class:@EnableDiscoveryClient

Core code

dubbo and nacos integration

  • Add dependency
 <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.3.1</version>
        </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>
  • Configuring nacos in dubbo
## Other properties remain unchanged

server:
  port: 8082
dubbo:
  registry:
    address: nacos://127.0.0.1:8848
  application:
    name: nacos-provider-application


Annotate the startup class @ EnableDubbo
Just restart your dubbo.

  • Visit your nacos
  • Address: localhost:8848/nacos account password: nacos by default

    In fact, the method of integrating BBO is the same as that of the registration center.

    nacos as the source code of the registry

Other concepts of nacos

  • Namespace
    You may see a namespace attribute in the configuration file. This is the namespace. You can understand it as a different hotel. Different hotels can have the same house number (the same Group or Data ID). Often when developing, we will have different environments, such as development environment and production environment, We can distinguish through this namespace
  • service name
    The service provision flag, in other words, is equivalent to the unique identification of the service, just like the primary key in the database.
  • Registration Center
    A database that stores service instances and service load balancing policies. To put it bluntly, the service you write will generate an instance, and the location of this instance is our registry.
  • Service provider and service consumer
    It is the application party that we provide reusable and callable services, and the consumer is the application party that we initiate calls to our registered service party. Our use is based on these two aspects.

Keywords: Java Spring Cloud Microservices

Added by ohdang888 on Mon, 21 Feb 2022 13:42:17 +0200