[Microservice] Nacos realizes the only way for back-end programmers to call between services

[Nacos] implements calls between services

foreword

The previous article talked about the specific functions of Nacos, so the theory is definitely not enough. This time we will use Nacos to implement simple calls between simple services. If you don’t know much about Nacos, you can go to [[Microservices] Nacos is the only way for entry-level back-end programmers] .
Don’t talk much, open up

tool

IDEA, Nacos client (both linux and windows are available, here is windows as the explanation)

Development Process

Launch of Nacos client

Go to the decompression directory of Nacos, go to the bin directory, and double-click startup.cmd
as shown below, the startup is successful

code writing

Build a [maven] project

create project

You can directly use Idea to create a maven project. Folders such as src do not need to be left, only a pom.xml [can] be left, as shown in the figure below, only the content pointed by the red arrow can be left, and all others are deleted. The maven project is used as the main project to facilitate the unification of dependency versions between submodules.

pom.xml content

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xiaow</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dependcies</module>
        <module>provider</module>
        <module>consumer</module>
    </modules>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.2.5.RELEASE</spring-boot.version>
    </properties>
    <packaging>pom</packaging>


    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/dev</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>qa</id>
            <properties>
                <env>qa</env>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/qa</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>online</id>
            <properties>
                <env>online</env>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources_env/online</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.xiaow</groupId>
                <artifactId>dependcies</artifactId>
                <version>${project.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <name>Aliyun Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>sonatype-repos</id>
            <name>Sonatype Repository</name>
            <url>https://oss.sonatype.org/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>sonatype-repos-s</id>
            <name>Sonatype Repository</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Build a unified dependency management module

The function of this module is to make the pom.xml of the total project look neater, and to implement the dependency management
of the entire project. The same is true. To build a maven module, only one pom.xml can be left . The
content is as follows

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <groupId>com.xiaow</groupId>
    <version>1.0-SNAPSHOT</version>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>dependcies</artifactId>
    <packaging>pom</packaging>

    <properties>
        <spring-cloud-version>Hoxton.SR4</spring-cloud-version>
        <spring-cloud-alibaba-version>2.2.2.RELEASE</spring-cloud-alibaba-version>
    </properties>
    <developers>
        <developer>
            <id>1</id>
            <name>xiaow</name>
        </developer>
    </developers>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>0.9.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
                <version>0.9.0.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
                <version>0.9.0.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

In the pom.xml of the main project, it needs to be introduced in dependencyManagement, as shown in the following figure
So far, we have completed the construction of the entire environment.

producer service

The producer service, as the name suggests, is used by the consumer service for consumption, so the relationship between the analogy and the service should be called a call.

Build a maven module

Create a maven module, this time leaving the src folder.

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>test</artifactId>
        <groupId>com.xiaow</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>

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

    <!-- <dependencies>--> 
    <!-- configure dependencies--> 
    < dependencies > 
        <!-- web starter--> 
        < dependency > 
            < groupId > org.springframework.boot </ groupId > 
            < artifactId > spring-boot-starter-web </ artifactId > 
        </ dependency >

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--        </dependencies>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.xiaow.test.Provider8001</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Create an interface for consumer service calls

Here is just a simple interface to return a string

package com.xiaow.test.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping("/test")
    public String test() {
        return "Call succeeded";
    }
}

Startup class configuration

Add the @EnableDiscoveryClient annotation to the startup class. As shown in the figure below, it should be noted here that the established maven project does not have a startup class, which needs to be handwritten or copied by ourselves.

application.yml

Here we need to configure the address of the nacos we want to register

spring:
  application:
    name: provider
  cloud:
    nacos:
      discovery:
        server-addr: http://192.168.30.1:8848/
server:
  port: 8001

management:
  endpoints:
    web:
      exposure:
        include: "*"

Final project structure

Whether the test was successful

Start to see if the producer is successfully registered to Nacos, and come to the Nacos workbench, as shown in the following figure
Observe that the provider has been registered, indicating that the producer service has been successfully written.

Consumer Services

Just like the producer, a maven module needs to be established, so I won’t go into details here. The way we call the producer service this time is the openfegin method.

pom.xml

There are some small differences between the pom files of consumers and producers, mainly in openfegin

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>test</artifactId>
        <groupId>com.xiaow</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</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-openfeign</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.xiaow.consumer.Consumer9001</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

application.yml

spring:
  application:
    name: consumer
  cloud:
    nacos:
      discovery:
        server-addr: http://192.168.30.1:8848/
server:
  port: 9001

management:
  endpoints:
    web:
      exposure:
        include: "*"

Startup class configuration

Need to add @EnableDiscoveryClient, @EnableFeignClients these two annotations

package com.xiaow.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Consumer9001 {
    public static void main(String[] args) {
        SpringApplication.run(Consumer9001.class,args);
    }
}

The service that calls the producer service interface

The service here is the interface that can directly call the producer, where / test in @GetMapping(“/test”) should correspond to the mapping of the producer interface.

package com.xiaow.consumer.service;

import com.xiaow.consumer.service.impl.TestServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "provider",fallback = TestServiceImpl.class)
public interface TestSerice {
    @GetMapping("/test")
    public String test();

}

TestServiceImpl will only be called when there is an error in calling the producer interface. The specific use will be discussed in detail when the sentinel is integrated in the future.

package com.xiaow.consumer.service.impl;

import com.xiaow.consumer.service.TestSerice;

public class TestServiceImpl implements TestSerice {
    @Override
    public String test() {
        return null;
    }
}

consumer interface

Use the controller to call TestService to implement the call to the producer service

package com.xiaow.consumer.controller;

import com.xiaow.consumer.service.TestSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    TestSerice testSerice;

    @GetMapping("/consumertest")
    public String consumertest() {
        return testSerice.test();
    }
}

test

Successfully called, at this point, a basic nacos use is completed

Summarize

The directory is a little messy. If you have any questions, you can communicate at any time. You are welcome to give pointers.
The entire complete project has been uploaded to the code cloud. If you are interested, you can take a look at the
code cloud address.

Leave a Comment

Your email address will not be published. Required fields are marked *