feign的基础配置及修改日志输出类型

来源:互联网 发布:java me基础教程 编辑:程序博客网 时间:2024/06/05 17:12

Feign配置

#Hystrix支持,如果为true,hystrix库必须在classpath中feign.hystrix.enabled=false#请求和响应GZIP压缩支持feign.compression.request.enabled=truefeign.compression.response.enabled=true#支持压缩的mime typesfeign.compression.request.enabled=truefeign.compression.request.mime-types=text/xml,application/xml,application/jsonfeign.compression.request.min-request-size=2048# 日志支持logging.level.project.user.UserClient: DEBUG

自定义配置

自定义配置类不能定义在启动类和Composcan扫描到的类中,要不会被重复初始化,引起冲突

    1. 定义自定义配置类
package com.wy2.cloud.microservice.user.configuration;import feign.Contract;import feign.Logger;import org.springframework.context.annotation.Bean;/** * 系统名称 xx平台 * 工程名称 wy2-cloud * 创建时间 2017/9/13 11:46 * * @author admin * @since 1.8 */public class FeignClientConfiguration {/*    @Bean    public Contract feignContract(){        Contract.Default contract = new Contract.Default();        return contract;    }*/    @Bean    Logger.Level feignLoggerLevel(){        return Logger.Level.FULL;    }}
  • 2.配置FeignClient日志级别(消费服务的接口所在包,必须是debug)
logging:  level:    com:      wy2:        cloud:          microservice:            user:              service: debug
  • 3.消费接口注解中增加配置
    @FeignClient(name=”${self.userServiceId}”,fallback = UserServiceHystrix.class,configuration ={FeignClientConfiguration.class})
package com.wy2.cloud.microservice.user.service;import com.wy2.cloud.microservice.pojo.user.UserLoginInput;import com.wy2.cloud.microservice.pojo.user.UserLoginResult;import com.wy2.cloud.microservice.user.configuration.FeignClientConfiguration;import com.wy2.cloud.microservice.user.hystrix.UserServiceHystrix;import com.wy2.cloud.pojo.security.User;import com.wy2.cloud.pojo.security.UserPermission;import org.springframework.cloud.netflix.feign.FeignClient;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import java.util.List;/** * 系统名称 xx平台 * 工程名称 wy2-cloud * 创建时间 2017/6/2 15:34 * ,configuration = FeignClientConfiguration.class * @author admin * @since 1.8 */@FeignClient(name="${self.userServiceId}",fallback = UserServiceHystrix.class,configuration ={FeignClientConfiguration.class})public interface UserService {    @RequestMapping(method = RequestMethod.POST,value="/login",            produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)    public UserLoginResult login(UserLoginInput userLoginInput);    @RequestMapping(method = RequestMethod.POST,value="/loadUserByUsername")    public User loadUserByUsername(@RequestParam("userName") String userName);    @RequestMapping(method = RequestMethod.POST,value="/getPermissions")    public List<UserPermission> getPermissions();}
  • 4.成功后出现如下日志:
2017-09-13 14:43:41.491 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] ---> POST http://service-user/login HTTP/1.12017-09-13 14:43:41.492 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] Accept: application/json2017-09-13 14:43:41.492 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] Content-Type: application/json2017-09-13 14:43:41.492 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] Content-Length: 382017-09-13 14:43:41.492 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] 2017-09-13 14:43:41.492 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] {"account":"dddd","password":"111111"}2017-09-13 14:43:41.492 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] ---> END HTTP (38-byte body)2017-09-13 14:43:41.502 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] <--- HTTP/1.1 200 (10ms)2017-09-13 14:43:41.503 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] content-type: application/json;charset=UTF-82017-09-13 14:43:41.503 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] date: Wed, 13 Sep 2017 06:43:41 GMT2017-09-13 14:43:41.503 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] transfer-encoding: chunked2017-09-13 14:43:41.503 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] x-application-context: service-user:dev:70102017-09-13 14:43:41.503 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] 2017-09-13 14:43:41.503 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] {"id":"81157080e6da4427a69a43b6fcb8a7fa","name":null,"email":"mengwzy@qq.com","createTime":1496374560000,"status":1,"account":"dddd","phone":null,"sex":null,"birthday":null}2017-09-13 14:43:41.503 [hystrix-service-user-3] DEBUG c.w.c.m.user.service.UserService - [UserService#login] <--- END HTTP (173-byte body)

Logger.Level支持

必须为每一个Feign Client配置来告诉Feign如何输出日志,可选:

NONE, No logging (DEFAULT).BASIC, Log only the request method and URL and the response status code and execution time.HEADERS, Log the basic information along with request and response headers.FULL, Log the headers, body, and metadata for both requests and responses.

FeignClient第一次请求报TimeOut问题解决方案

  • 延长hystix的连接超时时间,默认时间是1秒
//在application配置文件中添加如下配置:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:5000
  • 禁用hystix的超时时间
//在application配置文件中添加如下配置:hystrix.command.default.execution.timeout.enabled: false
  • 禁用hystix
//在application配置文件添加如下配置信息:feign.hystrix.enabled: false
原创粉丝点击