由于在本地调试,代码发布到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设置即可