SpringBoot Integration Nacos Registry

Keywords: Java Spring Maven Apache xml

What is Nacos

Nacos is Alibaba's open source project, and Nacos is dedicated to helping you discover, configure, and manage microservices.Nacos provides an easy-to-use set of features to help you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management.Nacos helps you build, deliver, and manage microservice platforms more quickly and easily.Nacos is a service infrastructure for building modern "service-centric" application architectures such as the micro-service paradigm and the cloud native paradigm.
Nacos will be used as a registry (instead of traditional schemes such as eurekba, consul) and a configuration center (spring cloud config) in the microservice architecture.

Nacos Ecomap

Set up Nacos environment

Download the latest address: https://github.com/alibaba/na...
Version used in this chapter: nacos-server-1.1.3
After the download is complete, unzip.Start the single-machine Nacos service by executing different commands on different platforms:

    + Linux/Unix/Mac: sh startup.sh -m standalone
    + Windows: cmd startup.cmd -m standalone

After booting up, access: http://127.0.0.1 8848/nacos/, logon account password default: Nacos
nacos wants to be a registry with the previous eureka registry. With a registry, we can now quickly implement a service registration
Create alibaba-nacos project
Introducing a pom.xml dependency

<?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.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xiaobin</groupId>
    <artifactId>alibaba-nacos</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>alibaba-nacos</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application configuration file

spring.application.name=nacos-test
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
server.port=10010

Create application main class for startup

@SpringBootApplication
@EnableDiscoveryClient
public class AlibabaNacosApplication {

    public static void main(String[] args) {
        SpringApplication.run(AlibabaNacosApplication.class, args);
    }
    
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
    

}

After successful startup, you can see the information in the list of console services in nacos, the effect diagram

Consumption operation on registering a service
Creating alibaba_nacos_provider project dependencies is the same as above
Configure application

spring.application.name=nacos-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Add Http interface for testing

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ProviderApp {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class);
    }


    @GetMapping("/get")
    public String get(){
        return "SpringCloud alibaba I am coming...";
    }

}

Start the project and you will find nacos-provider in the list of services

Back to the alibaba-nacos project, add TestController to invoke consumption

package com.xiaobin.alibabanacos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Creation time: 2019/9/25 23:02
 * Remarks:
 * Codon self-study communication group: 260532022, welcome to join, sharing learning is a pleasure
 **/

@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    public String test(){
        return  restTemplate.getForObject("http://nacos-provider/get", String.class);
    }
}

Visit http://localhost : 10010/test interface request

Summary

A Nacos is easy to do. It also brings with it the Configuration Management Center, which kills two birds with one stone.

Codon self-study communication group: 260532022, welcome to join, sharing learning is a pleasure

Posted by Daijoubu on Wed, 25 Sep 2019 19:25:11 -0700