Nacos registry of spring cloud

Nacos registry of spring cloud

In the previous section, we have built the nacos environment, and then we will reference it in the code.

Service provider

Now let's create a spring boot project that serves as the service provider. This project provides a service interface to the registry for clients to call.

First, add dependency in pom.xml

<dependencies>
    <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.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.2</version>
        <optional>true</optional>
    </dependency>
</dependencies>

For convenience, we write an interface in the main function class

@EnableDiscoveryClient
@SpringBootApplication
public class TestApplication {

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

    @Slf4j
    @RestController
    static class TestController {
        @GetMapping("/hello")
        public String hello(@RequestParam String name) {
            log.info("invoked name = " + name);
            return "hello " + name;
        }
    }
}

Note the annotation @ EnableDiscoveryClient. The service registry is opened here. After the project is started, the project will be automatically registered in the registry.

To configure

The key is configuration information

spring.application.name=alibaba-nacos-discovery-server
server.port=8001

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

spring.cloud.nacos.discovery.server-addr is the address of the Nacos environment we just set up. Now we can try to start the project and see if there are any new services registered in the Nacos interface.

The startup is successful, and there are new services in the interface. This service is the service provider we just created.

Service consumers

To consume this service, we need to create a service consumption project and use the registration center to consume the service interface.

It's the same way to create an empty springboot project. We invoke the interface of the service provider in the main class.

@EnableDiscoveryClient
@SpringBootApplication
public class TestApplication {

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

    @Slf4j
    @RestController
    static class TestController {

        @Autowired
        LoadBalancerClient loadBalancerClient;

        @GetMapping("/test")
        public String test() {
            // Select the service provider node through the load balancing interface in spring cloud common to implement the interface call
            ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
            String url = serviceInstance.getUri() + "/hello?name=" + "nn";
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForObject(url, String.class);
            return "Invoke : " + url + ", return : " + result;
        }
    }
}

configuration information

spring.application.name=alibaba-nacos-discovery-client-common
server.port=9000

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

The thing to watch out for is

 ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");

We call the interface through the service name. This reflects the role of the registry. The service consumer obtains the service list from the registration center and caches it locally. The service consumer calls the service interface through the local service list. In this way, the whole interface calling direction of the simple microservice is realized.

All of the above are my opinions. Welcome to put forward criticism and guidance. Let's discuss together!

Tags: Programming Spring xml Lombok SpringBoot

Posted on Wed, 08 Jan 2020 07:54:04 -0800 by frostyhorse