本篇内容介绍了“如何用spring-cloud-alibaba-nacos注册中心实现服务”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
父工程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 服务注册与发现--> <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注册中心实现服务”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。