解决本地环境需要灰度发布方式才能进行联调灰度策略:提高开发便捷性
1.解决方案0:架构方面 自定义负载均衡拦截器没有灰度发布时使用 ip偏向
com.ymt.crm.common.loadbalancer.GrayServiceInstanceLoadBalancer:
// 第一种 情况 garymark有值,且服务有对应的;
// 第二种情况 garymark为空,且有默认的服务(没有设置过garymark的服务)
// 当garymark为空时 设置 使用ip偏向
if(StringUtils.isBlank(garymark)){
// 使用hutool 工具获取本机IP地址
LinkedHashSet<String> ipSet = NetUtil.localIpv4s();
for (ServiceInstance serviceInstance : instances) {
if(ipSet.contains(serviceInstance.getHost())){
result.add(serviceInstance);
}
}
}
if(CollectionUtils.isEmpty(result)){
result = map.get(garymark);
}
2.解决方式1:创建 src/main/resources/application.yml 必须是 本地最大优先配置文件
## 官方文档 https://docs.spring.io/spring-cloud-commons/docs/current/reference/html/#simplediscoveryclient
eureka:
client:
#false表示不向注册中心注册自己。
register-with-eureka: false
#检索服务
fetch-registry: false
spring:
cloud:
discovery:
client:
# SimpleDiscoveryClient与SimpleReactiveDiscoveryClient的配置
simple:
instances:
#微服务名
CRM-SPAM-ENGINE:
- uri: http://localhost:8907
3.解决方案2:创建 src/main/java/com/ymt/FeignInterceptor.java
#原理 需要看 feign 源码知道url 的填充规则
@Configuration
public class FeignInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
Target<?> target = template.feignTarget();
Map<String, String> urlMap = Maps.newHashMap();
urlMap.put("CRM-SPAM-ENGINE","http://localhost:8907");
if(MapUtils.isNotEmpty(urlMap) && urlMap.containsKey(target.name())){
template.target(urlMap.get(target.name()));
}
}
}
失败方案1:
#ribbon:
# eureka:
# enabled: false
#CRM-SPAM-ENGINE:
# ribbon:
# listOfServers: http://localhost:8907
失败方案2
#eureka:
# client:
# register-with-eureka: false
# fetch-registry: false
#ribbon:
# eureka:
# enabled: false
#CRM-SPAM-ENGINE:
# ribbon:
# NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
# listOfServers: http://localhost:8907
失败方案3#
#spring:
# cloud:
# openfeign:
# client:
# config:
# CRM-SPAM-ENGINE:
# url: http://localhost:8907
# loggerLevel: full