Spring Cloud远程调用之OpenFeign

Feign是一个声明式的伪RPC(Feign英文意思为“假装,伪装,变形”)的REST客户端,它用了基于接口的注解方式,可以以Java接口注解的方式调用Http请求,从而将请求模块化。Feign被广泛应用在Spring Cloud的解决方案中,是学习基于Spring Cloud微服务架构不可或缺的重要组件。

使用

1、添加依赖

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

2、启动类添加@EnableFeignClients注解

在这里插入图片描述

3、定义注解接口

@FeignClient注解里面的user-server为调用服务名

package demo.newsweb.controller;

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

@FeignClient("user-server")
public interface UserControllerFeign {

    @GetMapping("/user/login")
    String login(@RequestParam("user") String user, @RequestParam("pwd") String pwd);
}

4、使用注解接口远程调用

package demo.newsweb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserControllerFeign userControllerFeign;

    @GetMapping("/login")
    public String login(String user, String pwd) {
        return  userControllerFeign.login(user, pwd);
    }
}

特性

开启Gzip压缩

# 请求开启压缩
feign.compression.request.enabled=true
# 最小压缩大小(字符),低于这个值则不压缩
feign.compression.request.min-request-size=2048
# 响应开启压缩
feign.compression.response.enabled=true
# 压缩类型 "text/xml", "application/xml", "application/json"
feign.compression.request.mime-types=text/xml,application/xml,application/json

Feign日志配置

1、配置日志输出级别

# 日志级别
logging.level.[demo.newsweb.controller.UserControllerFeign]=debug

2、配置日志输出方式

FULL为输出全部信息。

package demo.newsweb.controller;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeginLogConfig {

    @Bean
    Logger.Level feignLogger() {
        return Logger.Level.FULL;
    }
}

3、Feign接口注解@FeignClient加入configuration = FeginLogConfig.class使用设置的日志输出方式

package demo.newsweb.controller;

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

@FeignClient(value = "user-server",configuration = FeginLogConfig.class)
public interface UserControllerFeign {

    @GetMapping("/user/login")
    String login(@RequestParam("user") String user, @RequestParam("pwd") String pwd);
}

输出信息如下

在这里插入图片描述

替换默认的底层通信

底层通信默认是URLConnection,可替换成性能更好的okHttp

添加依赖

 <dependency>
    <groupId>io.github.openfeign</groupId>
     <artifactId>feign-okhttp</artifactId>
 </dependency>

添加配置启用

feign.httpclient.enabled=false
feign.okhttp.enabled=true
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值