This article describes SpringBoot application discovery using the Nacos service.
The previous article introduced SpringBoot using Nacos as the configuration center, and this article introduces SpringBoot using Nacos for service discovery.
1. Eureka closed source
By now, Eureka 2.0 closed source is nothing new. In 2017-2018, there was almost a surge of SpringCloud in China. Almost a large number of people began to pay attention to SpringCloud with their attention to SpringBoot. The Eureka registry is the preferred registry for most SpringCloud users due to its easy integration and other advantages. But as announced on Eureka’s website, here’s what it says.
Eureka’s Github address is github.com/Netflix/eur… .
Nacos is also an excellent registry, and is open source by Alibaba, and has been updated to version 0.8.0, which is basically updated frequently, and is also a good choice after Eureka closed source.
2.SpringBoot uses the Nacos service to discover
First, you need to start Nacos, which I won’t cover here.
Create a project, add Nacos to the service discovery dependency nacOS-discovery-spring-boot-starter, and complete the POM as shown in the code listing.
<? 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.1. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.dalaoyang</groupId> < artifactId > springboot2_nacos_discovery < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <name>springboot2_nacos_discovery</name> <description>springboot2_nacos_discovery</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-discovery-spring-boot-starter</artifactId> <version>0.2.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
The configuration file configures the address of the Nacos service, as shown in the code listing.
Server port = 8080 spring. Application. Name = springboot2 nacos - discovery nacos. Discovery. The server - addr = 127.0.0.1:8848Copy the code
SpringBoot uses the Nacos service to discover that it needs to register with the Nacos service. You can choose to register directly using the Nacos Api, as shown in the code listing.
//curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance? ServiceName = springboot2 - nacos - discovery&ip = 127.0.0.1 & port = 8080 '
Copy the code
This article uses the @postConstruct annotation to register with the Nacos service after the service is started, and the creation method gets the instance based on the instance name. The complete startup class is shown in the code listing.
package com.dalaoyang;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.util.List;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
//curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance? ServiceName = springboot2 - nacos - discovery&ip = 127.0.0.1 & port = 8080 '
@SpringBootApplication
@RestController
public class Springboot2NacosDiscoveryApplication {
@NacosInjected
private NamingService namingService;
@Value("${server.port}")
private int serverPort;
@Value("${spring.application.name}")
private String applicationName;
@PostConstruct
public void registerInstance() throws NacosException{
namingService.registerInstance(applicationName,"127.0.0.1",serverPort);
}
@RequestMapping(value = "/getInstance". method = GET) @ResponseBody public List<Instance> getInstance(@RequestParam String serviceName) throws NacosException {returnnamingService.getAllInstances(serviceName); } public static void main(String[] args) { SpringApplication.run(Springboot2NacosDiscoveryApplication.class, args); }}Copy the code
Two Nacos methods are used in this paper, as follows:
- The method used in this article takes three parameters: the service name, IP address, and port number.
- GetAllInstances: Obtains the instance and passes in the service name.
At this point the configuration is complete, start the project, and view the Nacos service as shown in the figure.
In the browser to http://localhost:8080/get? ServiceName = SpringBoot2-nacos-discovery, as shown in the figure, you can also query the newly registered instance.
IO /zh-cn/docs/…