Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use spring-cloud-alibaba-nacos registry to implement service

Shulou Source: shulou.com Published: 2022-06-03 05:41:29 11月17日 Update

This article introduces the relevant knowledge of "how to use spring-cloud-alibaba-nacos registry to implement services". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Version selection

Parent project pom file:

1.8 UTF-8 UTF-8 2.1.17.RELEASE Greenwich.RELEASE 2.1.2.RELEASE org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import Org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} Pom import

Universal dependency:

Org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.alibaba.cloud spring-cloud-starter-alibaba-nacos- Discovery org.projectlombok lombok true server side (Provider) module:

Add @ EnableDiscoveryClient annotation to the startup class

/ * * @ description: * @ Project www.fhadmin.org * @ create 2021-04 * * / @ SpringBootApplication@EnableDiscoveryClientpublic class NacosProviderApplication {public static void main (String [] args) {SpringApplication.run (NacosProviderApplication.class, args);}} / / java Project www.fhadmin.org@RestController@RequestMapping ("/ goods") public class GoodsController {@ Value ("${server.port}") private int port @ GetMapping ("/ findOne3") public Goods findGoodsById3 () {/ / the current thread sleeps for 2 seconds try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace ();} Goods goods = new Goods (). SetId (1) .setPrice (123.123) .setCount (1200). SetTitle ("title") Return goods.setTitle (goods.getTitle () + ":" + port);}}

Application.yml:

Configure the service name and nacos address and port, regardless of the bottom configuration

Server: port: 8000spring: application: name: nacos-provider # Service name cloud: nacos: discovery: server-addr: 127.0.0.1 management 8848 # configure nacos server address management: endpoints: web: exposure: include:'* 'pay side (Consumer) module

Add @ EnableDiscoveryClient annotation to the startup class

/ * * @ description: * java Project www.fhadmin.org * @ create 2021-04 * * / @ SpringBootApplication@EnableDiscoveryClientpublic class NacosConsumerApplication {public static void main (String [] args) {SpringApplication.run (NacosConsumerApplication.class, args);}}

Application.yml:

Configure the service name and nacos address and port, regardless of the bottom configuration

Server: port: 9000spring: application: name: nacos-consumer # Service name cloud: nacos: discovery: server-addr: 127.0.0.1 management 8848 # configure nacos server address management: endpoints: web: exposure: include:'*'

Start the test:

User names and passwords are all nacos

This is the end of the content of "how to use spring-cloud-alibaba-nacos registry to implement services". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Service configuration address project no content name more module annotation knowledge port faceted UTF-8 practical learned next dilemma actual password Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Docker OPPO Reno NVidia