feign服务之间调用问题

本文探讨了Feign在服务间调用时遇到的编码错误和Header值传递问题。针对post请求的编码错误,解决方案是确保调用方在请求时指定正确的编码。对于Header值传递,文章提到了通过编写拦截器和服务调用方的配置来解决,同时也建议开启服务调用方的传递模式。

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

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
以下是一个Spring Boot服务之间使用Feign进行调用的示例: ### 环境准备 客户端微服务服务端微服务必须注册到同一注册中心,以Eureka为例,配置文件`yaml`中注册中心地址如下: ```yaml eureka: client: serviceUrl: defaultZone: http://ip:port/eureka/ ``` 这里客户端微服务服务端微服务要注册到同一Eureka [^4]。 ### 服务端示例 #### 1. 创建Spring Boot项目 添加必要依赖,如`spring-boot-starter-web`、`spring-cloud-starter-netflix-eureka-client`。 #### 2. 启动类添加注解 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class FeignServerApplication { public static void main(String[] args) { SpringApplication.run(FeignServerApplication.class, args); } } ``` #### 3. 创建Controller ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class FeignServerController { @GetMapping("/hello") public String hello() { return "Hello from Feign Server!"; } } ``` ### 客户端示例 #### 1. 创建Spring Boot项目 添加依赖`spring-boot-starter-web`、`spring-cloud-starter-netflix-eureka-client`、`spring-cloud-starter-openfeign`。 #### 2. 启动类添加注解 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @EnableFeignClients @RestController public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } } ``` #### 3. 创建Feign客户端接口 ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient("feign-server") // 要调用服务名称 public interface FeignClientService { @GetMapping("/hello") String hello(); } ``` #### 4. 创建Controller调用Feign客户端 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class FeignConsumerController { @Autowired private FeignClientService feignClientService; @GetMapping("/call-server") public String callServer() { return feignClientService.hello(); } } ``` ### 测试 启动服务端`FeignServerApplication`和客户端`FeignConsumerApplication`,在注册中心(如Eureka)可以看到这两个服务调用客户端的`/call-server`接口,会返回服务端`/hello`接口的响应内容,即`Hello from Feign Server!` [^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值