SpringCloud全家桶---OpenFeign

1、SpringCloud全家桶—注册中心Eureka搭建
2、SpringCloud全家桶—注册中心Eureka高可用环境搭建
3、SpringCloud全家桶—客户端负载均衡Ribbon
4、SpringCloud全家桶—OpenFeign
5、SpringCloud全家桶—断路器Hystrix
6、SpringCloud全家桶—HystrixDashboard配置与基础功能演示
7、SpringCloud全家桶—Zuul网关

OpenFeign

OpenFeign是一种声明式、模板化的HTTP请求,相比前面我们使用RestTemplate的方式,OpenFeign能够更加的便捷、优雅的完成微服务之间的HTTP调用。

OpenFeign的使用也非常简单,通过注解的方式即可完成,使调用者感觉就像是在调用服务内部的接口一样,同时Openfeign还集成了Hystrix和Ribbon,使得调用可以自然的实现负载均衡和Hystrix相关的功能。

基本使用

接着上一遍Ribbon环境,搭建OpenFeign也非常简单。

1、引入jar包

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

2、启动类添加@EnableFeignClients注解

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class MembershipApplication {

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

3、定义FeignClient接口,name是要调用的服务名。

@FeignClient(name = "order")
public interface OrderFeign {

    @RequestMapping(value = "/userOrder/getOrderInfo", method = RequestMethod.GET)
    String getOrderInfo();

}

4、调用

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

    @Autowired
    private OrderFeign orderFeign;

    @PostMapping("/queryOrder")
    public String queryOrder() {
        String str = orderFeign.getOrderInfo();
        return str;
    }
}

4步即完成了一个服务之间的请求,服务提供方无需任何修改。

Feign 日志

为每个Feign客户端创建一个logger。默认情况下,logger的名称是用于创建Feign客户端的接口的全类名称。Feign日志记录仅响应DEBUG级别。

可以通过修改Logger.Level来控制日志记录的内容:

  /**
   * Controls the level of logging.
   */
  public enum Level {
    /**
     * No logging.
     */
    NONE,
    /**
     * Log only the request method and URL and the response status code and execution time.
     */
    BASIC,
    /**
     * Log the basic information along with request and response headers.
     */
    HEADERS,
    /**
     * Log the headers, body, and metadata for both requests and responses.
     */
    FULL
  }

NONE:无日志记录(DEFAULT)。
BASIC:仅记录请求方法和URL以及响应状态代码和执行时间。
HEADERS:记录基本信息以及请求和响应头。
FULL:记录请求和响应的标题,正文和元数据。

1、配置文件的方式

feign.client.config.order.logger-level=full
logging.level.com.wyl.springcloud.feign.OrderFeign=debug

在这里插入图片描述

2、编码的方式

新增:configuration = FeignConfig.class

@FeignClient(name = "order",configuration = FeignConfig.class)
public interface OrderFeign {

    @RequestMapping(value = "/userOrder/getOrderInfo", method = RequestMethod.GET)
    String getOrderInfo();

}

FeignConfig

import feign.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignLoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    FeignLoggerFactory orderFeignLoggerFactory() {
        return new OrderFeignLoggerFactory();
    }

    public static class OrderFeignLoggerFactory implements FeignLoggerFactory {
        @Override
        public Logger create(Class<?> type) {
            return new OrderFeignLogger(LoggerFactory.getLogger(type));
        }
    }
}
import org.slf4j.Logger;

public class OrderFeignLogger extends feign.Logger {
    private final Logger logger;

    public OrderFeignLogger(Logger logger) {
        this.logger = logger;
    }

    @Override
    protected void log(String configKey, String format, Object... args) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format(methodTag(configKey) + format, args));
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值