• 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
  • 相关阅读:
    P1486 [NOI2004]郁闷的出纳员
    P1966 火柴排队
    P2627 修剪草坪
    P1621 集合
    P1025 数的划分
    中国剩余定理
    P2043 质因子分解
    P1075 质因数分解
    C#之引用类型参数
    C#之方法的定义及调用学习案例
  • 原文地址:https://www.cnblogs.com/sunnyguo/p/11913819.html
Copyright © 2020-2023  润新知