Spend small money, peripheral tour, grab immediately, please pay attention to the public number: love to order not to order

Author: Don Juan

Promotion.aliyun.com/ntms/yunpar… Ali cloud server special discount

This chapter is based on the fact that you’ve learned about microservices and Nacos, downloaded it, installed it, and started it. If you haven’t started Nacos yet, check out our previous article: Getting started with Nacos (I) — Single-machine startup. Due to the current 1.0 version is not very stable, I encountered a heartbeat detection failure problem during the test, and temporarily switched to 0.9.0 version to start the demo.

Before we look at service registration and discovery for Nacos, let’s take a look at the official architecture diagram of Nacos:

So let’s look at the official example (nacos-spring-Cloud-discovery-example) and try to register the service with NACOS.

First, we create a new project to add dependencies to pom.xml.

<dependency> <groupId>org.springframework.cloud</groupId> < artifactId > spring - the cloud - starter - alibaba - nacos - discovery < / artifactId > < version > 0.2.1. RELEASE < / version > < / dependency >Copy the code

Note: Version 0.2.x. lease corresponds to Spring Boot 2.x, and version 0.1.x. lease corresponds to Spring Boot 1.x. I’m currently using the latest version of Spring Boot 2.x tassel.

Second, we create a service provider Module under this item and add dependencies in pom.xml.

<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>
Copy the code

Properties to register the service with the Nacos service center.

server.port=18080 spring.application.name=nacos-demo-rest-provider Spring. Cloud. Nacos. Discovery. Server - addr = 127.0.0.1:8848Copy the code

The third step is to start the SpringBoot class with the @enableDiscoveryClient annotation to enable service registration discovery. Official examples:

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

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

    @RestController
    class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery "+ string; }}}Copy the code

What is provided here is a REST interface, and we’ll look at Dubbo’s service invocation later. Note: When using Ribbon load balancing, do not use underscores in the service name, otherwise the service will not be found.

We then start the service, observe the Nacos console and see that the service is registered.

The REST interface can be accessed through a browser:

After stopping the project, we looked at the Nacos console and saw the following interface:

Fourth, we create another service consumer Module under this item and add dependencies to pom.xml.

<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.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> < version > 2.1.0. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework. Cloud < / groupId > < artifactId > spring - the cloud - starter - openfeign < / artifactId > < version > 2.1.0. RELEASE < / version > < / dependency >Copy the code

Properties to register the service with the Nacos service center.

server.port=18081 spring.application.name=nacos-demo-rest-consumer Spring. Cloud. Nacos. Discovery. Server - addr = 127.0.0.1:8848Copy the code

Fifth, in the SpringBoot startup class, the @enableDiscoveryClient annotation is used to enable service registration discovery. Official examples:

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        returnnew RestTemplate(); } public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } @RestController public class TestController { private final RestTemplate restTemplate; @Autowired public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate; } @RequestMapping(value ="/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://nacos-demo-rest-provider/echo/"+ str, String.class); }}}Copy the code

Nacos-demo-rest-provider is the previous service provider name in the code.

The @loadBalanced annotation is added to the RestTemplate to support load balancing. If you don’t add the @loadBalanced annotation, Will quote java.net.UnknownHostException anomalies, fail to register at this time Nacos Server service name to invoke the service, because RestTemplate is not from a service is mapped to the IP, port, The mapping function is implemented by the LoadBalancerClient.

Ok, now we start the service, look at the Nacos console, and the new service is registered with Nacos.

It can be accessed through a browser and you can see that it has been called:

Here is another official diagram to let you have a clearer understanding of the whole service registration and discovery and interface call process.

This is the end of Spring Cloud’s nacOS-based service registration and discovery, isn’t it easy? There are probably many people using Dubbo, so in the next chapter we will start to try Dubbo service invocation.

Promotion.aliyun.com/ntms/yunpar… Ali cloud server special discount