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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM