Architectural patterns

SpringCloudAlibaba+Nacos

Briefly say the reason for selection

We know that there is a micro service called Dubbo+Zookeeper, Dubbo is owned by Ali, but later Ali stopped maintaining Dubbo and shared Dubbo with Apache. At this juncture, SpringCloud took advantage of the situation and became popular in the field of micro services. One of his architectures was supported by Netflix, including Eureka registry, etc. But Eureka stopped updating later, and at this juncture, SpringCloudAlibaba started to use its own Nacos as the registry, and it became popular. Of course, the name can be seen from it. Now SpringCloudAlibaba+Nacos is quite good. In this context, I decided to build a simple micro service by myself. I found some on the Internet, but I was not satisfied, so I stuck to the official website. Get this article!

Install Nacos

  • Installing Nacos is as simple as going to GitHub and downloading its compressed packagehttps://github.com/alibaba/nacos/releasesHere’s the link to the Releases version, and when you’re done downloading it, unpack it and run it in the bin directorystartup.cmd -m standaloneStart standalone mode (without -m standalone starts the cluster)

    When you run it, you will see the following screen



    Then you can see the visual interface through the URL above, and the account and password are nacOS

    After login, the following screen is displayed



    Now that Nacos is installed, it is time to set up microservices and register each service with the registry (Nacos is not only a registry, but also a configuration center, as you can see in the figure above).

Here, two services are set up, one is nacos-Provider and the other is nacos-Consumer

First create a SpringBoot project and then introduce the Spring-cloud-starter-Alibaba-nacos-discovery service to discover dependencies

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

And then you annotate @enableDiscoveryClient on the main startup class and you need a configuration (yamL configuration here)

server:
  port: 8080
spring:
  application:
    name: nacos-provider # service name
  cloud:
    nacos:
      discovery:
        server-addr: 127.0. 01.: 8848 # NACOS Registry address
Copy the code

Then you can write a business interface, a Controller

@RestController public class ProviderController { @GetMapping(value = "/info") public String getInfo() { return "hello world"; }}Copy the code

Start the service to register with Nacos



Next, register a service called NACos-Consumer

Again, create a SpringBoot project to import dependencies

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

The @enableDiscoveryClient annotation on the main startup class writes the configuration

server:
  port: 8081
spring:
  application:
    name: nacos-provider # service name
  cloud:
    nacos:
      discovery:
        server-addr: 127.0. 01.: 8848 # NACOS Registry address
Copy the code

Write an interface to invoke the Provider service

@RestController
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

    /** * Invoke the /config/info interface * of SpringCloud-provide@return* /
    @GetMapping("/echo/info")
    public String echoConfigInfo(a){
        //Access through the combination of LoadBalanceClient and RestTemplate
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
        String path = String.format("http://%s:%s/info",serviceInstance.getHost(),serviceInstance.getPort());
        System.out.println("request path:" +path);
        return restTemplate.getForObject(path,String.class);
    }


    //Instantiate RestTemplate Instance
    @Bean
    public RestTemplate restTemplate(a){

        return newRestTemplate(); }}Copy the code

Then start the service and register



Okay, so both services are registered, and then you can call them through the controller above, and just to clarify, it uses two apis,LoadBalancerClientandRestTemplateIs the core of microservice invocation



The /echo/info interface of the Consumer service can be accessed by calling the /echo/info interface of the Provider service.