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

Example Analysis of the Registry of nacos

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you an example analysis of the nacos registry, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

First, create a new project.

Service provider

Create a new springboot project

I use maven to build the project, relying on the following

Org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery

The required jar package has been downloaded

The previous article on changing configuration introduced the download and installation of nacos. Now let's take a look at the nacos registry.

First, create a new project.

Service provider creates a new springboot project

I use maven to build the project, relying on the following

The required jar package has been downloaded

Paste the configuration. I use the configuration in yaml format. It looks really clear.

Server: port: 8881spring: application: name: nacos-provider cloud: nacos: discovery: server-addr: 127.0.0.1:8848

Finally, let's do a little test class.

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

Come down and let's start the service.

See the log. Our service has been registered with nacos. Open nacos to have a look.

Haha, our service has successfully registered with nacos.

Service consumers

Let's create a consumer.

Maven dependencies are all the same, so change the configuration slightly.

Server: port: 8882spring: application: name: nacos-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848

Startup class

@ SpringBootApplication@EnableDiscoveryClient@Slf4j@RestControllerpublic class NacosConsumerApplication {@ Autowired private RestTemplate restTemplate; @ GetMapping ("/ callNacos") public String testRest (@ RequestParam ("name") String name) {String result = restTemplate.getForEntity ("http://nacos-provider/hello?name="+name,String.class).getBody(); log.info (" calling provider returns: {} ", result); return result } public static void main (String [] args) {SpringApplication.run (NacosConsumerApplication.class, args);}}

Found an error report after startup

It turns out that RestTemplate is registered with the spring container.

@ Configurationpublic class RestTemplateConfig {@ LoadBalanced @ Bean public RestTemplate resultTemplate () {RestTemplate template = new RestTemplate (); return template;}}

After registration, we start to have a look, this time there is no exception.

Open naocos to view the list of services

The above is a sample analysis of the nacos registry. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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