
# 接着上一次的nacos初步讲解和安装
# 任意门:https://www.cnblogs.com/Yangbuyi/p/13479767.html
# 如果启动失败的话 上一篇也是讲解过的.
接着上一次的nacos初步讲解和安装
如果启动失败的话 上一篇也是讲解过的.

本文章开始服务注册和发现
工程准备
创建一个父模块将里面的src删除即可留下.idea和pox.xml文件

| <parent> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-parent</artifactId> |
| <version>2.2.3.RELEASE</version> |
| </parent> |
| |
| |
| <properties> |
| <springCloud.version>Hoxton.SR7</springCloud.version> |
| <springCloud-alibaba.version>2.1.1.RELEASE</springCloud-alibaba.version> |
| </properties> |
| |
| <dependencyManagement> |
| <dependencies> |
| |
| <dependency> |
| <groupId>org.springframework.cloud</groupId> |
| <artifactId>spring-cloud-dependencies</artifactId> |
| <version>${springCloud.version}</version> |
| <type>pom</type> |
| <scope>import</scope> |
| </dependency> |
| |
| |
| <dependency> |
| <groupId>com.alibaba.cloud</groupId> |
| <artifactId>spring-cloud-alibaba-dependencies</artifactId> |
| <version>${springCloud-alibaba.version}</version> |
| <type>pom</type> |
| <scope>import</scope> |
| </dependency> |
| </dependencies> |
| |
| </dependencyManagement> |
创建consumer工程模块和provide工程模块(调用者、服务者)

1.在consumer模块当中添加依赖
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| </dependency> |
| <!-- 服务发现 nacos --> |
| <dependency> |
| <groupId>com.alibaba.cloud</groupId> |
| <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> |
| </dependency> |
2. 手写启动类

| |
| |
| |
| |
| |
| |
| |
| |
| |
| @SpringBootApplication |
| @Slf4j |
| |
| @EnableDiscoveryClient |
| |
| public class AppConsumer { |
| |
| public static void main(String[] args) throws UnknownHostException { |
| ConfigurableApplicationContext application = SpringApplication.run(AppConsumer.class, args); |
| Environment env = application.getEnvironment(); |
| String ip = InetAddress.getLocalHost().getHostAddress(); |
| String port = env.getProperty("server.port"); |
| String path = env.getProperty("server.servlet.context-path"); |
| String hj = env.getProperty("spring.profiles.active"); |
| log.info("\n----------------------------------------------------------\n\t" + |
| "Application yangbuyiCloud is running! Access URLs:\n\t" + |
| "Local: \t\thttp://localhost:" + port + path + "/\n\t" + |
| "当前配置环境: \t\t当前环境为:" + hj + "/\n\t" + |
| "----------------------------------------------------------"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Bean |
| @LoadBalanced |
| public RestTemplate restTemplate() { |
| return new RestTemplate(); |
| } |
| |
| |
| } |
| |
3.在启动类上添加注解
nacos 服务发现 客户端 @EnableDiscoveryClient
4.在配置文件添加添加配置
| server: |
| port: 5000 |
| servlet: |
| context-path: /cloud-user |
| spring: |
| application: |
| name: consumer-user |
| cloud: |
| nacos: |
| discovery: |
| |
| |
| |
| |
| server-addr: 192.168.43.204:80 |
| register-enabled: true |
| |
| |
| |
| name: 默认环境 |
| |
上面看不懂的话也可直接写这样子的
| server: |
| port: 5000 |
| servlet: |
| context-path: /cloud-user |
| spring: |
| application: |
| name: user-client |
| |
| cloud: |
| nacos: |
| discovery: |
| server-addr: localhost:8848 |
5.启动工程后, 在nacos当中查询服务列表

nacos服务没有下载的去观看上一章节的nacos服务安装和错误解决
6.使用相同方式 把provide服务注册到nacos上
创建启动类
| package top.yangbuyi; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @SpringBootApplication |
| @Slf4j |
| |
| @EnableDiscoveryClient |
| |
| public class AppProvide { |
| |
| public static void main(String[] args) throws UnknownHostException { |
| ConfigurableApplicationContext application = SpringApplication.run(AppProvide.class, args); |
| Environment env = application.getEnvironment(); |
| String ip = InetAddress.getLocalHost().getHostAddress(); |
| String port = env.getProperty("server.port"); |
| String path = env.getProperty("server.servlet.context-path"); |
| String hj = env.getProperty("spring.profiles.active"); |
| log.info("\n----------------------------------------------------------\n\t" + |
| "Application yangbuyiCloud is running! Access URLs:\n\t" + |
| "Local: \t\thttp://localhost:" + port + path + "/\n\t" + |
| "当前配置环境: \t\t当前环境为:" + hj + "/\n\t" + |
| "----------------------------------------------------------"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @Bean |
| @LoadBalanced |
| public RestTemplate restTemplate() { |
| return new RestTemplate(); |
| } |
| |
| |
| } |
| |
| |
| |
创建yml配置
| server: |
| port: 7000 |
| servlet: |
| context-path: /cloud-yby |
| spring: |
| application: |
| name: provide-goods |
| cloud: |
| nacos: |
| discovery: |
| server-addr: localhost:8085 |
| |
| name: 默认环境 |
| |
7. 启动两个服务 consumer和 provide 去nacos查看是否有

8. 在consumer工程中通过服务发现调用provide 工程
自己对着图片的打一遍 别偷懒


到此即可通过 consumer访问provide工程当中的方法啦
9.测试

nacos有问题请评论区联系我
完结 下一章节 nacos集群

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南