服务之间出现的调用问题——下一篇文章会写服务调用方法
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
本文探讨了Feign在服务间调用时遇到的编码错误和Header值传递问题。针对post请求的编码错误,解决方案是确保调用方在请求时指定正确的编码。对于Header值传递,文章提到了通过编写拦截器和服务调用方的配置来解决,同时也建议开启服务调用方的传递模式。
2万+

被折叠的 条评论
为什么被折叠?



