Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of the Registry of nacos

Shulou Source: shulou.com Published: 2022-06-02 04:30:30 11月01日 Update

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.

Tags: Services engineering configuration providers examples analysis content skills consumers knowledge consumption concise successful concise containers that is articles logs more formats Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux OPPO Reno Redmi Shulou Tech Info