SpringCloud 之Feign服务消费者

Feign简介

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

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力

编写Feign服务

创建一个model工程作为服务消费者,即eureka-feign-client。
导入依赖

<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>

在程序的启动类上加上@EnableFeignClients注解开启Feign的功能:

 1 import org.springframework.boot.SpringApplication;
 2 import org.springframework.boot.autoconfigure.SpringBootApplication;
 3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 4 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 5 import org.springframework.cloud.openfeign.EnableFeignClients;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.web.client.RestTemplate;
 8
 9 @SpringBootApplication
10 @EnableEurekaClient
11 // 开启Feign的功能
12 @EnableFeignClients
13 public class EurekaFeignClientApplication {
14
15     public static void main(String[] args) {
16         SpringApplication.run(EurekaFeignClientApplication.class, args);
17     }
18 }

配置文件application.yml如下:

server:
  port: 8083

spring:
  application:
    name: eureka-feign-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9100/eureka/

定义一个feign接口,通过@FeignClient(“服务名”),来指定调用哪个服务。

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

// 申明这是一个Feign客户端,并且指明服务id
@FeignClient(value = "eureka-client")
public interface FeignClientInter {

    // 这里定义了类似于SpringMVC用法的方法,就可以进行RESTful方式的调用了
    @GetMapping(value = "/hello")
    String sayHelloFromClient();
}

通过上面定义的Feign客户端来消费服务。

 1 import com.yq.feign.service.FeignClientInter;
 2 import org.springframework.beans.factory.annotation.Autowired;
 3 import org.springframework.web.bind.annotation.GetMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5
 6 @RestController
 7 public class HelloController {
 8
 9     @Autowired(required = false)
10     FeignClientInter feignClientInter;
11
12     @GetMapping("/feign")
13     public String demo() {
14         return feignClientInter.sayHelloFromClient();
15     }
16 }

测试:

原文地址:https://www.cnblogs.com/const-/p/11454565.html

时间: 2024-08-01 00:19:27

SpringCloud 之Feign服务消费者的相关文章

SpringCloud(5)---Feign服务调用

SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具体参考:SpringCloud(4)---Ribbon服务调用,源码分析 这边只重写订单微服务. 一.OrderService 订单微服务 1.pom.xml 这里相对于上一篇的订单微服务只要新添加一个jar包 <!--feign依赖--> <dependency> <group

SpringCloud系列——Feign 服务调用

前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调用. Feign是一个声明性web服务客户端.它使编写web服务客户机变得更容易,本质上就是一个http,内部进行了封装而已. GitHub地址:https://github.com/OpenFeign/feign 官方文档:https://cloud.spring.io/spring-cloud-

Feign服务消费者

Feign的优点:面向接口,完全不用管实现,传入规定格式的数据就可以了 搭建消费者项目(FeignDemo) 1.创建pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 h

SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)

在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign.在这一篇文章首先讲解下基于ribbon+rest. 一.ribbon简介 Ribbon is a client side load balancer which gives you a lot of control over the behaviour o

史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)

最新Finchley版本:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/或者http://blog.csdn.net/forezp/article/details/81040946 在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是fe

《springcloud 一》搭建注册中心,服务提供者,服务消费者

注册中心环境搭建 Maven依赖信息 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManage

springcloud(七,多个服务消费者配置,以及zuul网关案例)

spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provider) spring cloud (四.服务消费者demo_consumer) springcloud(五,多个服务注册中心eureka) springcloud(六,多个服务提供者) 多个服务消费者同上面的提供者和服务注册一样的哈,就是复制出来两个,把端口改一下即可 这里就不做截图了 直接开始zu

SpringCloud中服务消费者接受前台传参问题(报错415)

服务消费者clientFeign,接受前台form表单post提交的数据,虽然传递参数是对象类型,但是后台不能添加@RequestBody注解!!!,form表单提交的数据不是json格式!!! 1. 前端 <form class="layui-form" action="/menu/save" method="post"> <div class="layui-form-item"> <label

SpringCloud 服务注册中心 服务提供者 服务消费者

1. 建立"服务注册中心" 创建一个基础的Spring Boot工程,并在pom.xml中引入需要的依赖内容: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> 在主类中通过@EnableEurekaSe