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 software

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use nacos software". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use nacos software.

1. Download the windows version of nacos software

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

After the download is complete, extract it. According to different platforms, execute different commands to start the stand-alone Nacos service:

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

Windows:cmd startup.cmd-m standalone / Today's bin directory runs startup.cmd directly

Will report an error, and then modify the startup command, because the default startup of startup.cmd is in cluster startup mode (when modified, it is about line 27 of startup.cm

Rem set MODE= "cluster"

Set MODE= "standalone"

),

If you use Nacos version 0.8.0 or later, the login page appears. The default username password is: nacos.

Service provider

Step 1: create a Spring Boot application that can be named: alibaba-nacos-discovery-server. If you do not know or are not familiar with Spring Boot applications, it is recommended to study the "Spring Boot basic tutorial" first.

Step 2: edit the pom.xml and add the necessary dependency configuration, 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

The above contents are mainly divided into three parts:

Parent: defines the version of spring boot

The version of dependencyManagement:spring cloud and the 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: dependent content to be used by the current application. Here is a new addition to Nacos's service registration and discovery module: spring-cloud-starter-alibaba-nacos-discovery. Since a version has been introduced in dependencyManagement, there is no need to specify a specific version here.

Step 3: create the application main class and implement a 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 definition is a SpringBoot application; @ EnableDiscoveryClient enables service registration and discovery of Spring Cloud. Because the spring-cloud-starter-alibaba-nacos-discovery module is introduced here, the interfaces defined in Spring Cloud Common related to service governance will use the implementation of Nacos.

Step 4: configure the service name and Nacos address

Create a bootstrap.yml under resources

Spring: application: name: alibaba-nacos-discovery-server cloud: nacos: discovery: server-addr: 127.0.0.1:8848server: port: 8001

Step 5: start the application created above, and start the application to see the log below, indicating the registered nacos.

After launching du ok, we can visit the management page http://127.0.0.1:8848/nacos/ of Nacos to view the list of services. At this time, we can see the following:

This will show all the services currently registered, as well as the number of clusters, instances, and healthy instances for each service. Click details, and we can also see the specific instance information of each service, as shown in the following figure:

Service consumers

Next, implement an application to consume services that are already registered with Nacos.

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

Step 2: edit the dependency content in the pom.xml, just as the service provider above.

Step 3: create the application main class and implement a HTTP interface in which the 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 () {/ / through the load balancer interface in spring cloud common, select the service provider node to implement the API call 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;} "

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

Step 4: configure the service name and Nacos address so that the service consumer can discover the services that are already registered with 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$ curl localhost:9000/testInvoke: http://10.123.18.216:8002/hello?name=didi, return: hello didi

As you can see, when there are two different requests, the actual service provider instance is different, that is to say, when the service instance is obtained through the LoadBalancerClient interface, the load balance of the service provider instance has been realized. But it is obvious that this kind of implementation is still quite tedious. The next few articles are about several different postures of service consumption.

At this point, I believe you have a deeper understanding of "how to use nacos software". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report