I have learned about SpringCloud Alibaba before and started to formally learn how to use and build Spring Cloud project. Version information: Spring Boot 2.5.0 + Spring Cloud 2020.0.2 + Spring Cloud Alibaba 2021.1

Nacos as the registry

Dubbo2.7.x uses Nacos as the registry’s learning notes for Nacos from a long time ago, when dubbo was used to build microservices. You need to download and install NACOS before using it, see: NACOS Cluster and Single-machine Startup

How do you use nacos as a registry in springcloud

Create service A and register with Nacos

Create a service

To avoid trouble, click Next during the creation process without selecting any SpringBoot-related jars.

Introduce relevant jars

The complete POM file is shown below

<? The 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.5.0 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>cn.hjljy.demo</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <name> SpringCloud </name> <description> Spring Cloud nacOS Registry </description> < properties > < Java version > 1.8 < / Java version > < / properties > <! <dependencies> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.2</version> <type> POm </type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2021.1</version> <type> POM </type> <scope>import</scope> </dependency> </dependencies> </dependencies> <dependencies> <! --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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> <version>2.5.0</version> </plugin> </plugins> </build> <repository> < ID > Central </ ID > <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <layout>default</layout> <! Enable releases download --> < Releases > <enabled> True </enabled> </ Releases > <! Snapshots > <enable >false</enable > </snapshots> </repository> </project>Copy the code

New configuration in the configuration file

Spring. Cloud. Nacos. Discovery. Service = service - a spring cloud. Nacos. Server - addr = 127.0.0.1:8848 server port = 8001Copy the code

Create the test service interface

@RestController
public class TestController {

    @GetMapping(value = "/echo/{string}")
    public String echo(@PathVariable String string) {
        return "Hello Im Service A "+ string; }}Copy the code

Services are registered with NACOS

Add @enableDiscoveryClient to the startup class

/ * * *@author hjljy
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceAApplication {

    public static void main(String[] args) { SpringApplication.run(ServiceAApplication.class, args); }}Copy the code

Verify that the registration is successful

Start nacOS first, then start the project, and finally enter the NACOS management interface to see if there is a service named service-a. Existence means success.

About Service Invocation

As of May 29, 2019, Nacos, either in official documentation or in official instance code, makes service calls that disable Robbion+RestTemplate. But in the spring 2020 edition of cloud, officials removed Robbion, so according to the official sample document called complains: java.net.UnknownHostException: service – a service – a cannot be found. This is when a new load balancer is called!!

Loadbalancer Call for load balancing

Spring Cloud Loadbalancer is the new official recommended Loadbalancer.

Create service B and invoke service A

Create the service and import the JAR

The POM file is the same as the creation process of service A and needs to be added

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
Copy the code

New configuration in the configuration file

Spring. Cloud. Nacos. Discovery. Service = service - b spring. Cloud. Nacos. Server - addr = 127.0.0.1:8848 server port = 8002Copy the code

Enable service discovery and load balancing

Also add @enableDiscoveryClient to the startup class and @loadBalanced to the RestTemplate to enable load balancing so that service calls can be made by service name, without using IP+ port

/** * @author hjljy */ @SpringBootApplication @EnableDiscoveryClient public class ServiceBApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ServiceBApplication.class, args); }}Copy the code

Create the test service interface

@RestController public class TestController { private final RestTemplate restTemplate; @Autowired public TestController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @GetMapping(value = "/echo/{string}") public String echo(@PathVariable String string) { return restTemplate.getForObject("http://service-a/echo/" + string, String.class); }}Copy the code

In this example, service-a is the service a created earlier. You can use the service name without using the IP address and port number.

The test call verifies that it was successful

1 start nacos

2 Start service A

3 Start service B

4 if browser enter http://127.0.0.1:8002/echo/123456 right to return to the Hello Im Service 123456 indicates A success!!!!!!