Spring Cloud 学习笔记08----服务消费者(Feign)(Finchley版本)

简介: 接上一篇《Spring Cloud 学习笔记06----断路器(Hystrix)(Finchley版本)》,今天我们来学习另外一种服务调用方式(Feign),之前我们介绍了 RestTemplate+Ribbon 消费服务的方式。

概要

接上一篇《Spring Cloud 学习笔记06----断路器(Hystrix)(Finchley版本)》,今天我们来学习另外一种服务调用方式(Feign),之前我们介绍了 RestTemplate+Ribbon 消费服务的方式。


Feign简介

Feign 是一个声明式的伪Http客户端,它使得写Http 客户端变得更简单,使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign注解和JAX-RS注解,Feign 也支持可插拔的编码器和解码器,Spring Cloud 扩展了对Spring MVC的注解支持,在Spring Web 中同样使用HttpMessageConverters 。Feign 默认集成了Ribbon, 并和Eureka 结合,默认实现了负载均衡的效果。

简单来说:


Feign 采用的是基于接口的注解

Feign 整合了Ribbon, 具有负载均衡的能力

整合了Hystrix,具有熔断能力。

快速入门

首先,沿用前面的服务注册中心(eureka-server)以及服务提供者(order-provider)。然后新建一个SpringBoot 项目,命名为service-feign。添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <!--必须添加spring-boot-starter-web,否则注册失败-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

然后,在application.yml 加上如下配置,指定端口为8765,服务名为service-feign:

spring:
  application:
    name: service-feign
eureka:
  client:
    service-url:
#    用于指定注册中心的地址
      defaultZone: http://localhost:1111/eureka/
server:
  port: 8765

接着,我们在ServiceFeignApplication 中添加@EnableFeignClients以激活Feign。添加@EnableEurekaClient 以开启负载均衡,使其可以注册到Eureka 中。

@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class ServiceFeignApplication {
  public static void main(String[] args) {
  SpringApplication.run(ServiceFeignApplication.class, args);
  }
}

接着,我们新建一个接口,用于调用服务提供者提供的服务,使用

@FeignClient(serviceId = "order-service") 指定调用的服务名为order-service。
@FeignClient(serviceId = "order-service")
public interface HelloService {
    @GetMapping(value = "/dc")
    String getOrderService(@RequestParam("name") String name);
}

最后,我们新建一个controller 调用刚刚新建接口HelloService中的方法,测试下是否可以调用成功

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/dc")
    public String getOrderService(String name) {
        return helloService.getOrderService(name);
    }
}

测试结果,我们启动两个order-provider 服务,端口号分别为 8082,8083,

然后,启动 service-feign, 端口号为:8765,

在Eureka面板中查看

bfd00078d06634b99838ecb8b4a71e63_20190214223211549.png

访问http://localhost:8765/dc?name=jay会交替出现如下结果:

hi: jay;port=8083
hi: jay;port=8082

参考代码

https://github.com/XWxiaowei/SpringCloud-Learning/tree/master/2-Finchley版教程示例/Chapter6-1


参考文献

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html#netflix-feign-starter

https://blog.csdn.net/forezp/article/details/81040965


相关文章
|
3月前
|
负载均衡 算法 Java
【SpringCloud(4)】OpenFeign客户端:OpenFeign服务绑定;调用服务接口;Feign和OpenFeign
Feign是一个WebService客户端。使用Feign能让编写WebService客户端更加简单。 它的使用方法是定义一个服务接口然后再上面添加注解。Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行了封装,十七支持了SpringMVC标准注解和HttpMessageConverters。 Feign可用于Eureka和Ribbon组合使用以支持负载均衡
743 138
|
人工智能 Java Serverless
【MCP教程系列】搭建基于 Spring AI 的 SSE 模式 MCP 服务并自定义部署至阿里云百炼
本文详细介绍了如何基于Spring AI搭建支持SSE模式的MCP服务,并成功集成至阿里云百炼大模型平台。通过四个步骤实现从零到Agent的构建,包括项目创建、工具开发、服务测试与部署。文章还提供了具体代码示例和操作截图,帮助读者快速上手。最终,将自定义SSE MCP服务集成到百炼平台,完成智能体应用的创建与测试。适合希望了解SSE实时交互及大模型集成的开发者参考。
13457 61
|
6月前
|
Prometheus 监控 Cloud Native
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务实现步骤
664 0
|
10月前
|
人工智能 自然语言处理 Java
对话即服务:Spring Boot整合MCP让你的CRUD系统秒变AI助手
本文介绍了如何通过Model Context Protocol (MCP) 协议将传统Spring Boot服务改造为支持AI交互的智能系统。MCP作为“万能适配器”,让AI以统一方式与多种服务和数据源交互,降低开发复杂度。文章以图书管理服务为例,详细说明了引入依赖、配置MCP服务器、改造服务方法(注解方式或函数Bean方式)及接口测试的全流程。最终实现用户通过自然语言查询数据库的功能,展示了MCP在简化AI集成、提升系统易用性方面的价值。未来,“对话即服务”有望成为主流开发范式。
7572 7
|
SpringCloudAlibaba API 开发者
新版-SpringCloud+SpringCloud Alibaba
新版-SpringCloud+SpringCloud Alibaba
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
424 1
|
10月前
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
11月前
|
人工智能 SpringCloudAlibaba 自然语言处理
SpringCloud Alibaba AI整合DeepSeek落地AI项目实战
在现代软件开发领域,微服务架构因其灵活性、可扩展性和模块化特性而受到广泛欢迎。微服务架构通过将大型应用程序拆分为多个小型、独立的服务,每个服务运行在其独立的进程中,服务与服务间通过轻量级通信机制(通常是HTTP API)进行通信。这种架构模式有助于提升系统的可维护性、可扩展性和开发效率。
4107 2
|
SpringCloudAlibaba 负载均衡 Dubbo
【SpringCloud Alibaba系列】Dubbo高级特性篇
本章我们介绍Dubbo的常用高级特性,包括序列化、地址缓存、超时与重试机制、多版本、负载均衡。集群容错、服务降级等。
1874 6
【SpringCloud Alibaba系列】Dubbo高级特性篇

热门文章

最新文章