Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Nacos for Service Registration and Discovery

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Nacos to achieve service registration and discovery, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

What is Nacos?

Nacos is dedicated to helping you discover, configure, and manage microservices. Nacos provides an easy-to-use feature set to help you quickly implement dynamic service discovery, service provisioning, service metadata, and traffic management. Nacos helps you build, deliver, and manage microservices platforms more quickly and easily. Nacos is a service infrastructure that builds modern application architectures (e.g. microservices paradigm, cloud native paradigm) centered on "services."

In the following tutorials, you will use Nacos as a registry (instead of eurekba, consumer, etc.) and configuration center (spring cloud config) in microservices architecture.

Install Nacos

Download address: github.com/alibaba/nacos/releases

Version: 1.1.3

After downloading, extract. According to different platforms, execute different commands to start the stand-alone version of Nacos service:

Linux/Unix/Mac:sh startup.sh -m standalone

Windows:cmd startup.cmd -m standalone

After startup, visit http://127.0.0.1:8848/nacos/to enter the Nacos service management page. The default account password is nacos. The details are as follows:

Build apps Access the Nacos registry

After installing and starting the Nacos service, we can write two applications (service provider and service consumer) to verify the registration and discovery of the service.

service provider

Step 1: Create a Spring Boot application, which can be named alibaba-nacos-discovery-server.

Step 2: Edit pom.xml and add the necessary dependencies, such as:

org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.SR1 pom import org.springframework.cloud spring-cloud-alibaba-dependencies 0.2.2.RELEASE pom import org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery org.projectlombok lombok 1.18.2 true

There are three main parts to the above:

parent: Defines the version of the spring boot

dependencyManagement: version of spring cloud and version of spring cloud alibaba, since spring cloud alibaba has not been included in the main version management of spring cloud, you need to join it yourself.

dependencies: dependencies to be used by the current app. Nacos 'service registration and discovery module is newly added here: spring-cloud-starter-alibaba-nacos-discovery. Since versions are already introduced in dependency Management, there is no need to specify specific versions here.

Step 3: Create the main application class and implement an HTTP interface:

@EnableDiscoveryClient@SpringBootApplicationpublic class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @GetMapping("/hello") public String hello(@RequestParam String name) { log.info("invoked name = " + name); return "hello " + name; } }}

The content is very simple,@SpringBootApplication is defined as a Spring Boot application;@EnableDiscoveryClient enables service registration and discovery of Spring Cloud. Since the spring-cloud-starter-alibaba-nacos-discovery module is introduced here, those interfaces related to service governance defined in Spring Cloud Common will use Nacos implementation. This is true whether we use Eureka, Consul, or any other Spring Cloud integrated registry, and this is one of the benefits of Spring Cloud encapsulation.

Step 4: Configure the service name and Nacos address

spring.application.name=alibaba-nacos-discovery-serverserver.port=8001spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Step 5: Launch the app created above. You can add-Dserver.port=8001 at startup time to start multiple instances on different ports of the machine.

After the application is launched, we can see the following contents in the console or log, which means that the registration has been successful:

INFO 2998 --- [ main] o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, alibaba-nacos-discovery-server 192.168.1.139:8001 register finished

After starting Duok, we can visit the Nacos administration page http://127.0.0.1:8848/nacos/to view the list of services. At this time, we can see the following:

All currently registered services are displayed, along with the number of clusters, instances, and healthy instances of each service. Click Details, we can also see the specific instance information of each service, as shown in the following figure:

service consumer

Next, implement an application that consumes the services registered above with Nacos.

Step 1: Create a Spring Boot application named alibaba-nacos-discovery-client-common.

Step 2: Edit the dependencies in pom.xml as above for the service provider.

Step 3: Create the application main class and implement an HTTP interface where the service provider's interface is invoked.

@EnableDiscoveryClient@SpringBootApplicationpublic class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired LoadBalancerClient loadBalancerClient; @GetMapping("/test") public String test() { //Use the Load Balancer interface in spring cloud common to select a service provider node to invoke the interface ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server"); String url = serviceInstance.getUri() + "/hello? name=" + "didi"; RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.getForObject(url, String.class); return "Invoke : " + url + ", return : " + result; } }}

The LoadBalancerClient interface in Spring Cloud Common is used here to pick service instance information. Then the accessible URI is obtained from the selected instance information, and the interface rules of the service provider are spliced to initiate the call.

Step 4: Configure the service name and Nacos address so that service consumers can discover services registered to Nacos.

spring.application.name=alibaba-nacos-discovery-client-commonserver.port=9000spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Step 5: Start the service consumer, and then initiate access through tools such as curl or postman. Take curl as an example:

$ curl localhost:9000/testInvoke : http://10.123.18.216:8001/hello? name=didi, return : hello didi

After reading the above, do you know how to use Nacos to achieve service registration and discovery? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report