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

猜你喜欢

转载自www.cnblogs.com/sunnyguo/p/11913819.html