当前位置: 首页 > news >正文

Nacos的基本功能以及使用Feign进行微服务间的通信

Nacos是Dynamic Naming and Configuration Service的缩写。What’s Nacos?

下面结合SpringBoot项目,为你介绍Nacos的基本功能以及如何使用Feign进行微服务间的通信。

一、Nacos的基本功能

Nacos是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。它的主要功能包括:

  1. 服务发现与注册:微服务可以在Nacos中注册自己,并发现其他服务。
  2. 配置管理:集中管理所有微服务的配置。
  3. 服务健康监测:检查服务的健康状态。
  4. 动态DNS服务:通过域名来访问服务。

二、依赖配置

要在SpringBoot项目中使用Nacos和Feign,需要在pom.xml中添加以下依赖:

<!-- 服务注册与发现 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency><!-- 配置管理 -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency><!-- Feign客户端 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency><!-- Spring Cloud 依赖管理 -->
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.9.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

三、配置文件

application.propertiesapplication.yml中配置Nacos服务地址:

# 应用名称
spring:application:name: service-consumercloud:nacos:discovery:server-addr: 127.0.0.1:8848  # Nacos服务器地址config:server-addr: 127.0.0.1:8848  # Nacos配置中心地址file-extension: yaml  # 配置文件格式# 端口号
server:port: 8080

四、代码实现

1. 服务提供者(service-provider)

首先创建一个简单的服务提供者,提供一个API接口:

package com.example.provider.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello/{name}")public String sayHello(@PathVariable String name) {return "Hello, " + name + "! I'm from service-provider.";}
}

启动类上添加@EnableDiscoveryClient注解:

package com.example.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {public static void main(String[] args) {SpringApplication.run(ServiceProviderApplication.class, args);}
}
2. 服务消费者(service-consumer)

创建Feign客户端接口,调用服务提供者的API:

package com.example.consumer.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(name = "service-provider")  // 服务提供者的应用名称
public interface HelloFeignClient {@GetMapping("/hello/{name}")  // 服务提供者的API路径String sayHello(@PathVariable String name);
}

创建Controller,使用Feign客户端调用服务:

package com.example.consumer.controller;import com.example.consumer.feign.HelloFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConsumerController {@Autowiredprivate HelloFeignClient helloFeignClient;@GetMapping("/call/{name}")public String callProvider(@PathVariable String name) {return helloFeignClient.sayHello(name);}
}

启动类上添加@EnableFeignClients注解:

package com.example.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
public class ServiceConsumerApplication {public static void main(String[] args) {SpringApplication.run(ServiceConsumerApplication.class, args);}
}

五、Nacos配置中心使用示例

创建一个配置类,读取Nacos配置中心的配置:

package com.example.consumer.config;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;@Component
@RefreshScope  // 支持配置动态刷新
public class AppConfig {@Value("${app.name:default}")private String appName;@Value("${app.version:1.0.0}")private String appVersion;public String getAppName() {return appName;}public String getAppVersion() {return appVersion;}
}

创建Controller,获取配置信息:

package com.example.consumer.controller;import com.example.consumer.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConfigController {@Autowiredprivate AppConfig appConfig;@GetMapping("/config")public String getConfig() {return "App Name: " + appConfig.getAppName() + ", Version: " + appConfig.getAppVersion();}
}

在Nacos配置中心创建配置文件service-consumer.yaml

app:name: MyServiceConsumerversion: 1.0.1

六、测试步骤

  1. 启动Nacos服务器(下载并解压Nacos,运行startup.sh -m standalone)。
  2. 启动服务提供者(service-provider)。
  3. 启动服务消费者(service-consumer)。
  4. 访问服务消费者的API:http://localhost:8080/call/World,会返回Hello, World! I'm from service-provider.
  5. 访问配置信息:http://localhost:8080/config,会返回Nacos配置中心的配置信息。

通过上述示例,你可以看到Nacos的服务注册与发现、配置管理功能,以及Feign如何简化微服务间的通信。

http://www.dtcms.com/a/272636.html

相关文章:

  • 【网络编程】 TCP 协议栈的知识汇总
  • ZW3D 二次开发-创建圆柱体
  • Qt cannot find C:\WINDOWS\TEMP\cctVBBgu: Invalid argument
  • QT5使用cmakelists引入Qt5Xlsx库并使用
  • 达梦数据库不兼容 SQL_NO_CACHE 报错解决方案
  • C++交叉编译工具链制作以及QT交叉编译环境配置
  • 生产环境CI/CD流水线构建与优化实践指南
  • 医院多部门协同构建知识库-指南库-预测模型三维网络路径研究
  • 12大产品规划工具对比:功能、价格与适用场景
  • (LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
  • 2023 年 12 月青少年软编等考 C 语言七级真题解析
  • 2025年语言处理、大数据与人机交互国际会议(DHCI 2025)
  • QBoost 2025版:加速手机性能,提升使用体验
  • django中如何使用Django REST Framework
  • 基于SpringBoot旅游资源信息管理系统的设计与实现
  • 【Learning Notes】 Derak Callan‘s Business English P30~31
  • Redis数据库基础
  • 网安系列【15】之Docker未授权访问漏洞
  • Pytest 预期失败测试:如何标记“已知问题”用例
  • squash压缩合并
  • 【无标题】 RV1126平台(Buildroot Linux)+ SunplusIT SPCA2688 USB摄像头 RTSP推流全流程复盘与问题解决记录
  • 系统性部署系统母盘【rhel7和rhel9】
  • 7月10号总结 (1)
  • 大数据的安全挑战与应对
  • Linux:库的原理
  • swift开发,关于应用、页面、视图的生命周期
  • [C++ STL] list类的刨析及简易实现
  • 亚马逊首个“海折节”,缘何加码进口电商?
  • java多线程环境下资源隔离机制ThreadLocal详解
  • C#内插字符串:从语法糖到深度优化