Spring Cloud Feign使用

Feign是一款声明式的WebService客户端,简化了服务间的调用。整合Ribbon和Hystrix,实现负载均衡和熔断功能。通过@FeignClient注解定义HTTP请求,轻松实现服务调用。

背景

Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单。Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解,就可以定义好HTTP请求的参数、格式、地址等信息。

而Feign则会完全代理HTTP请求,我们只需要像调用方法一样调用它就可以完成服务请求及相关处理。Feign整合了Ribbon和Hystrix(关于Hystrix我们后面再讲),可以让我们不再需要显式地使用这两个组件。

特点

总起来说,Feign具有如下特性:

  • 可插拔的注解支持,包括Feign注解和JAX-RS注解;

  • 支持可插拔的HTTP编码器和解码器;

  • 支持Hystrix和它的Fallback;

  • 支持Ribbon的负载均衡;

  • 支持HTTP请求和响应的压缩。

FeignClient注解的一些属性

feign.png

快速开始

1.引入Feign

     <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
     </dependency>

2.@FeignClient注解

SpringbootApplication启动类加上@FeignClient注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServiceFeignApplication {

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

}
//@EnableDiscoveryClient 和@EnableEurekaClient是等效的

3.yaml配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

4.Feign的使用

feign接口

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam("name") String name);
}

访问接口

@RestController
public class HiController {

    @Autowired
    SchedualServiceHi schedualServiceHi;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);

    }
}

实战

1.创建项目

创建一个Maven项目feign,新建2个module service-hiservice-feign

2.service-hi

图片.png

pom

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

yml

server:
  port: 8763

spring:
  application:
    name: service-hi

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

ServiceHiApplication

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
public class ServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceHiApplication.class, args );
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

}

3.service-feign

图片.png
pom

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

ServiceFeignApplication

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

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

}

SchedualServiceHi

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam("name") String name);
}

HiController

@RestController
public class HiController {

    @Autowired
    SchedualServiceHi schedualServiceHi;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);

    }
}

测试

启动eureka
访问http://localhost:8761/
图片.png
启动service-hi
刷新http://localhost:8761/
图片.png
访问service-hihttp://localhost:8763/hi?name=kobe
图片.png
启动service-feign
刷新http://localhost:8761/
图片.png
同过feign访问接口http://localhost:8765/hi?name=KG
图片.png
可以看到去到了service-hi服务,端口8763

个人博客
腾讯云社区
掘金
优快云
简书
GitHub
码云
OSCHINA
Segmentfault
公众号:wx.jpg

Spring Cloud Feign 是一个声明式的 HTTP 客户端,它使得编写 Web 服务客户端变得更加简单。以下是 Spring Cloud Feign使用指南: ### 1. 添加依赖 在 `pom.xml` 中添加 Spring Cloud Feign 的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` ### 2. 启用 Feign 客户端 在 Spring Boot 应用的主类上添加 `@EnableFeignClients` 注解来启用 Feign 客户端: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` ### 3. 创建 Feign 客户端接口 创建一个接口,并使用 `@FeignClient` 注解来指定要调用的服务名: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "your-service-name") public interface YourFeignClient { @GetMapping("/your-api-path") String yourApiMethod(); } ``` 在上述代码中,`name` 属性指定了要调用的服务名,接口中的方法使用 Spring MVC 的注解来定义要调用的 API。 ### 4. 使用 Feign 客户端 在需要调用远程服务的地方注入 Feign 客户端接口,并调用其中的方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class YourController { @Autowired private YourFeignClient yourFeignClient; @GetMapping("/call-remote-service") public String callRemoteService() { return yourFeignClient.yourApiMethod(); } } ``` ### 5. 与 Eureka 集成 如果使用 Eureka 进行服务发现,需要在 `pom.xml` 中添加 Eureka 客户端依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ``` 并在 `application.properties` 或 `application.yml` 中配置 Eureka 服务器地址: ```properties eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ ``` 通过上述配置和步骤,Spring Cloud 应用可以通过 Feign 客户端与 Eureka 集成,利用 Eureka 进行服务发现,同时通过 Feign 进行简化的 HTTP 远程调用,当需要调用远程服务时,不需要关心服务的具体地址和负载均衡,Spring Cloud 负责这一切的处理 [^1]。 ### 6. 与 Hystrix 结合使用 Feign 默认集成了 Hystrix,可以通过配置来启用 Hystrix 熔断机制。在 `application.properties` 或 `application.yml` 中添加以下配置: ```properties feign.hystrix.enabled=true ``` 创建一个实现了 Feign 客户端接口的 fallback 类: ```java import org.springframework.stereotype.Component; @Component public class YourFeignClientFallback implements YourFeignClient { @Override public String yourApiMethod() { return "Fallback response"; } } ``` 在 `@FeignClient` 注解中指定 fallback 类: ```java @FeignClient(name = "your-service-name", fallback = YourFeignClientFallback.class) public interface YourFeignClient { @GetMapping("/your-api-path") String yourApiMethod(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值