微服务的feign调用header头被丢弃两种解决方案(附源码)

微服务的feign调用header头被丢弃两种解决方案(附源码)

问题背景

在使用springcloud的微服务feign调用时,会自动把header自动扔掉,但基本上每个服务都会有一个全局globalId,能够清除调用链路,可以有两种解决方案
注意事项:

解决方案一

1 可以在每次远程调用时,使用@RequestHeader注解重新封装请求头

    @GetMapping("/mem1")
    String mem1(String res, @RequestHeader String globalId);

解决方案二

1 可以使用springcloud提供的feign拦截器RequestInterceptor,拦截请求头重新进行封装

解决方案代码示例

1 feign拦截器配置类

package com.lanran.feignheader.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: feign拦截器功能, 解决header丢失问题
 * @Created: IDEA2021
 * @author: 蓝染
 * @createTime: 2022-07-02 21:10
 **/

@Configuration
public class FeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {

        RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                //1、使用RequestContextHolder拿到刚进来的请求数据
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

                if (requestAttributes != null) {
                    //老请求
                    HttpServletRequest request = requestAttributes.getRequest();

                    if (request != null) {
                        //2、同步请求头的数据(主要是cookie)
                        //把老请求的cookie值放到新请求上来,进行一个同步
                        String cookie = request.getHeader("Cookie");
                        template.header("Cookie", cookie);
                    }
                }
            }
        };
        return requestInterceptor;
    }

}

2 请求测试接口

package com.lanran.feignheader.controller;

import com.lanran.feignheader.feign.MemFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 * @Author suolong
 * @Date 2022/7/22 10:58
 * @Version 2.0
 */
@RestController
public class FeignController {

    @Autowired
    MemFeign memFeign;


    @GetMapping("/test1")
    public String test1() {
        //获取当前线程请求头信息(解决Feign异步调用丢失请求头问题)
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //每一个线程都来共享之前的请求数据
        RequestContextHolder.setRequestAttributes(requestAttributes);
        String a = memFeign.mem1("a", "dsahjkdhsakj54646");
        System.out.println(a);
        return "success";
    }

    @GetMapping("/test2")
    public String test2() {
        //获取当前线程请求头信息(解决Feign异步调用丢失请求头问题)
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //调用微服务之前,每一个线程都来共享之前的请求数据
        RequestContextHolder.setRequestAttributes(requestAttributes);
        String a = memFeign.mem2("a");
        System.out.println(a);
        return "success";
    }


}

3 feign调用

package com.lanran.feignheader.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

/**
 * @Author suolong
 * @Date 2022/7/22 10:59
 * @Version 2.0
 */
@FeignClient("mem")
public interface MemFeign {

    @GetMapping("/mem1")
    String mem1(String res, @RequestHeader String globalId);


    @GetMapping("/mem2")
    String mem2(String res);

}

4 启动类,开启feign

package com.lanran.feignheader;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;


@EnableFeignClients
@SpringBootApplication
public class FeignHeaderApplication {

    public static void main(String[] args) {
        SpringApplication.run(FeignHeaderApplication.class, args);
    }

}

5 依赖导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lanran</groupId>
    <artifactId>feignHeader</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>feignHeader</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

6 整体目录示例

总结

之前总是获取不到头




作为程序员第 215 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 哪里都是你

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微服务之间的调用,也可以使用Feign来进行,同样需要在Feign的配置中启用HTTPS支持。具体步骤如下: 1. 引入Feign的依赖 ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 配置Feign的HTTPS支持 ``` @Configuration public class FeignConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Client feignClient() { return new Client.Default(getSSLSocketFactory(), null); } private SSLSocketFactory getSSLSocketFactory() { try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }, new SecureRandom()); return sslContext.getSocketFactory(); } catch (Exception e) { throw new RuntimeException(e); } } @Bean public Decoder feignDecoder() { return new ResponseEntityDecoder(new SpringDecoder(messageConverters)); } } ``` 其中,`getSSLSocketFactory`方法返回一个`SSLSocketFactory`对象,用于创建HTTPS连接。这里采用了忽略证书的方式,实际生产环境中应该使用可信的证书。 3. 在Feign的接口中使用`https`协议 ``` @FeignClient(name = "example", url = "https://example.com") public interface ExampleClient { @GetMapping("/api") String get(); } ``` 其中,`url`参数指定了HTTPS协议的地址。 4. 在微服务中使用Feign进行调用 ``` @RestController public class ExampleController { @Autowired private ExampleClient exampleClient; @GetMapping("/example") public String get() { return exampleClient.get(); } } ``` 这样就可以在微服务中使用Feign调用HTTPS接口了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值