zoukankan      html  css  js  c++  java
  • feign服务之间调用问题

    服务之间出现的调用问题——下一篇文章会写服务调用方法

    1.post请求报编码错误:

      原因:可能是用@requestBody接收,需要在调用方调用的时候,加上编码

    @RequestMapping(value = "/api/1/user/userNotice",method= RequestMethod.POST, headers = {"content-type=application/json"})
    public Map<String,Object> sendNotice(@RequestBody String str);

    2.服务直接的header值传递问题:

     a.写拦截器

    @Configuration
    public class FeignConfiguration implements RequestInterceptor {
        private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory
                .getLogger(ApiInterceptor.class);
    
        @Override
        public void apply(RequestTemplate template) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                while (headerNames.hasMoreElements()) {
                    String name = headerNames.nextElement();
                    String values = request.getHeader(name);
                    template.header(name, values);
    
                }
                logger.info("feign interceptor header:{}",template);
            }
        }
    }

    b.服务调用方,加上配置

    @ApiVersion(1)
    @FeignClient(value = "user-service",configuration = FeignConfiguration.class)
    public interface UserNoticeService {
    
       
      @RequestMapping(value = "/api/1/user/userNotice",method= RequestMethod.POST, headers = {"content-type=application/json"})
        public Map<String,Object> sendNotice(@RequestBody String str);
    }
    

    c.服务调用方开启传递模式:

    hystrix:
      command:
        default:
          execution:
            timeout:
              enabled: false
            isolation:
              strategy: SEMAPHORE
  • 相关阅读:
    Symmetric Order
    Red and Black
    Sticks(递归经典)
    Pascal Library
    cantor的数表
    OJ 调试技巧:VS2010 中 通过设置编译参数定义宏,无需修改源文件重定向标准输入输出
    strcmp
    最短周期串
    字母重排
    codeblocks 单步调试
  • 原文地址:https://www.cnblogs.com/sunnyguo/p/11913819.html
Copyright © 2011-2022 走看看