Preface
Through the previous article: Introduction to Nacos After a brief understanding of the development process and current situation of Nacos, we begin the first step of Nacos water test: using Nacos as registry
Last weekend (7.6) Nacos released version V1.1.0, which supports grayscale configuration, address server mode, configuration file import and export, and other functions. It feels like the older brothers in the community are very productive.
This article mainly through two projects to complete the demonstration:
- nacos-provide: a service provider
- nacos-consumer: service consumers
Register nacos-provide r and nacos-consumer to Nacos-server. Service consumer nacos-consumer obtains a list of service information he subscribes to through active polling nacos-consumer makes service invocation based on the list of service information obtained.
Students familiar with Spring Cloud + Eureka can seamlessly switch to Nacos as registry after reading this article.
My environment
- Windows10
- JDK8
- SpringCloud: Finchley.RELEASE
- SpringBoot: 2.0.4.RELEASE
- spring-cloud-alibaba-dependencies: 0.2.2.RELEASE
- Nacos-server: 1.0.1
Note: Nacos provides different dependencies for different versions of Spring Cloud. Refer to the description given in the official document for the relationship between the versions: Version description
Start Nacos-server
Nacos-server can download installation packages directly from github, but you can also pull the replacement code and pack it yourself.
In this article, I downloaded Nacos-server: V1.0.1 directly from the official website (to avoid other problems with the new version of V1.1.0, I still use V1.0.1 here)
Download address: https://github.com/alibaba/nacos/releases
After downloading and decompressing, enter the bin folder (directory: nacos-server-1.0.1\nacos\bin), double-click the startup.cmd file directly, and start successfully as follows:
After successful startup, the Nacos console can be accessed, browser access: http://127.0.0.1:8848/nacos/index.html, default account password is nacos/nacos The console page is as follows:
Create a service provider
Create the aggregation project Nacos as the parent project in IDEA, and its pom.xml is as follows (focusing on dependency management configuration):
<?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>larscheng-learning-demo</artifactId> <groupId>com.study.www</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <version>0.0.1-SNAPSHOT</version> <modelVersion>4.0.0</modelVersion> <modules> <module>nacos-provide</module> </modules> <artifactId>Nacos</artifactId> <properties> <java.version>1.8</java.version> <spring-boot.version>2.0.4.RELEASE</spring-boot.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> <nacos.version>0.2.2.RELEASE</nacos.version> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </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> <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>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${nacos.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
Create springboot subproject nacos-provide under the parent project Nacos, whose pom.xml file is:
<?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> <parent> <artifactId>Nacos</artifactId> <groupId>com.study.www</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.larscheng.www</groupId> <artifactId>nacos-provide</artifactId> <version>0.0.1-SNAPSHOT</version> <name>nacos-provide</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Provide an external interface in NacosProvideApplication.java and add the annotation @Enable Discovery Client to open the service registration discovery function:
@RestController @EnableDiscoveryClient @SpringBootApplication public class NacosProvideApplication { public static void main(String[] args) { SpringApplication.run(NacosProvideApplication.class, args); } @GetMapping("/helloNacos") public String helloNacos(){ return "Hello, nacos!"; } }
The configuration file application.yml is configured as follows
server: port: 9527 spring: application: name: nacos-provide cloud: nacos: discovery: server-addr: 127.0.0.1:8848
ok, the creation and configuration of the service provider has been completed
Creating Service Consumers
Still under the Nacos project, create a SpringBook project subproject named nacos-consumer whose pom file is the same as nacos-provide r.
Also add configuration files for nacos-consumer, as follows
server: port: 9528 spring: application: name: nacos-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848
Service consumers make service calls here through RestTemplate+Ribbon in the way that is in the official document.
NacosConsumerApplication.java code is as follows:
@SpringBootApplication @EnableDiscoveryClient @RestController public class NacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } @Autowired private RestTemplate restTemplate; @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } @GetMapping("/consumer") public String test1() { String result = restTemplate.getForObject("http://nacos-provide/helloNacos",String.class); return "Return : " + result; } }
ok, the creation of service consumers has also been completed. Here are two projects to start testing.
Call the test
When the startup is complete, you should see the following two messages in the log
o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, nacos-provide 192.168.200.1:9527 register finished o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, nacos-consumer 192.168.200.1:9528 register finished
Now login to the Nacos console, and you will find that the list of services shows the two projects we just created, which can be easily monitored and managed.
The interface http://127.0.0.1:9528/consumer accessing service consumers in browser can see the successful return result.
Return: Hello, nacos!
summary
After completing the above test of service discovery and registration, my first feeling was that Eureka could be replaced by configuration modification alone, as if Spring Cloud was seamlessly supported.
With this inner shock, I went to the company's project to simply try launching, actually directly registered successfully, and the normal use of various services, although only a separate service registration discovery function. But this shows that Nacos is born to seamlessly connect Spring Cloud ecosystems (and of course he has many pits).
Looking at the major categories in the Nacos console, it's clear that Nacos's capabilities are far more than just registries. There are more Nacos postures and pits, which we're not going to continue.~
Source code for this article: https://github.com/larscheng/larscheng-learning-demo/tree/master/Nacos