feign拦截器实现更改请求服务名

由于在本地调试,代码发布到dev环境时,要做如下更改,更改feign的请求地址

package com.pcis.system.api;

import com.pcis.common.core.constant.ServiceNameConstants;
import com.pcis.system.api.domain.SysDictData;
import com.pcis.system.api.factory.RemoteLogFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 日志服务
 * 
 * @author ruoyi
 */
@FeignClient(contextId = "remoteDictService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteLogFallbackFactory.class)
//@FeignClient(contextId = "remoteDictService", value = "本地服务名", fallbackFactory = RemoteLogFallbackFactory.class)
public interface RemoteDictService {

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "dict/data/list/{dictType}")
    List<SysDictData> selectByDictType(@PathVariable("dictType") String dictType);
}

内部请求不需要更改路径,更改请求的服务名,我本地起的服务名带前缀crl- 

 

 

 

package com.pcis.system.api.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.Target;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;

/**
 * Feign拦截器
 *
 * @Author crl
 * @Date 2023/1/11 9:37
 * @Description:
 */
@Configuration
public class FeignConfig {
    private String servicePrefix = "crl-";

    @AllArgsConstructor
    @NoArgsConstructor
    @Getter
    public enum RemoteConfig {
        // 远程服务
        REMOTE_DICT_SERVICE("RemoteDictService", false),
        REMOTE_FILE_SERVICE("RemoteFileService", false),
        REMOTE_LOG_SERVICE("RemoteLogService", false),
        REMOTE_TOKEN_SERVICE("RemoteTokenService", false),
        REMOTE_USER_SERVICE("RemoteUserService", false),
        ;
        private String name;
        private boolean open;
    }

    @Bean
    public RequestInterceptor cloudContextInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                String[] clz = template.methodMetadata().targetType().getName().split("\\.");
                String clzName = clz[clz.length - 1];
                RemoteConfig[] remoteConfigs = RemoteConfig.values();
                for (RemoteConfig remoteConfig : remoteConfigs) {
                    // 验证是否开启
                    if (ObjectUtils.nullSafeEquals(clzName,remoteConfig.getName()) && remoteConfig.isOpen()){
                        // 拦截改变请求的服务
                        Target<?> target = template.feignTarget();
                        Class<?> type = target.type();
                        String url = target.url();

                        String finalUrl = url.replace("//", "//" + servicePrefix);
                        Target.HardCodedTarget hardCodedTarget = new Target.HardCodedTarget(type, servicePrefix + target.name(), finalUrl);
                        template.feignTarget(hardCodedTarget);
                        template.target(finalUrl);
                        System.out.println("====更改请求信息===>" + template.toString());
                    }
                }
            }
        };
    }

    /**
     * feign 日志记录级别
     * NONE:无日志记录(默认)
     * BASIC:只记录请求方法和 url 以及响应状态代码和执行时间。
     * HEADERS:记录请求和响应头的基本信息。
     * FULL:记录请求和响应的头、正文和元数据。
     *
     * @return Logger.Level
     */
//    @Bean
//    public Logger.Level feignLoggerLevel() {
//        return Logger.Level.FULL;
//    }
}

由于我用的是dev环境的配置,所以没有根据环境来动态选择。而是根据自己写的枚举来控制是否开启更换服务名。当然,请求的路径也可以改,自己根据RequestTemplate设置即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

提桶跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值