您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何用spring-cloud-alibaba-nacos注冊中心實現(xiàn)服務”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
父工程pom文件:
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.1.17.RELEASE</spring-boot.version> <!--spring cloud 版本--> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> <spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
通用依賴:
<dependencies> <!--springboot最基本的依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--nacos 服務注冊與發(fā)現(xiàn)--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
添加@EnableDiscoveryClient注解在啟動類上
/** * @description: * @項目 www.fhadmin.org * @create 2021-04 **/ @SpringBootApplication @EnableDiscoveryClient public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } }
//java項目www.fhadmin.org @RestController @RequestMapping("/goods") public class GoodsController { @Value("${server.port}") private int port; @GetMapping("/findOne3") public Goods findGoodsById3() { //當前線程睡2秒 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Goods goods = new Goods().setId(1).setPrice(123.123).setCount(1200).setTitle("標題"); return goods.setTitle(goods.getTitle() + ":" + port); } }
application.yml:
配置服務名和nacos地址和端口,最下面的配置不用管
server: port: 8000 spring: application: name: nacos-provider # 服務名稱 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # 配置nacos 服務端地址 management: endpoints: web: exposure: include: '*'
添加@EnableDiscoveryClient注解在啟動類上
/** * @description: * java項目www.fhadmin.org * @create 2021-04 **/ @SpringBootApplication @EnableDiscoveryClient public class NacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } }
application.yml:
配置服務名和nacos地址和端口,最下面的配置不用管
server: port: 9000 spring: application: name: nacos-consumer # 服務名稱 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # 配置nacos 服務端地址 management: endpoints: web: exposure: include: '*'
啟動測試:
用戶名密碼都是 nacos
“如何用spring-cloud-alibaba-nacos注冊中心實現(xiàn)服務”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。