首先请看下图
这个方案在之前的文章有介绍过【利用灰度方式解决微服务测试环境共用问题】,这里就不做太多的文字介绍了,图中内部请求部分省略掉了,和网关那一层的负载策略一致,获取头部version
信息,然后负载到相应的服务去。网关层比内部服务调用多做了一件事情就是ip
绑定操作。
整个k8s
集群维护一套跟生产一样的稳定版本,当没有进行ip绑定操作时,请求不会调用到开发机器去,就不会出现一些乱七八糟的问题:比如谁打了个断点,然后请求一直pending
到超时之类的。
我们此处采用git
分支号作为负载的版本号,关于git
信息的获取可以使用maven
的git
插件打包后会把分支相关信息给你写到classpath
下git.version
中,springboot
默认提供的属性实例直接就能获取到了。这样能有效的测试的时候能快速有效的定位到具体错误的分支和服务。
废话不多说,上代码:
不管是在服务还是在网关层中我们都是去重写负载均衡规则即可
继承LoadBalancerClientFilter重写choose方法返回实例,这里的目的主要是为了将版本信息写到请求头去
package com.omsa.api.gateway.filter;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
import com.omsa.api.gateway.cache.VersionCacheUtil;
import com.omsa.api.gateway.utils.IPUtil;
import java.net.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/12/22 17:32
*/
public class VersionLoadBalancerClientFilter extends LoadBalancerClientFilter {
private static final Logger logger= LoggerFactory.getLogger(VersionLoadBalancerClientFilter.class);
private final static String version_key="branch";
public VersionLoadBalancerClientFilter(LoadBalancerClient loadBalancer, LoadBalancerProperties properties) {
super(loadBalancer, properties);
}
@Override
protected ServiceInstance choose(ServerWebExchange exchange) {
String ipAddress = IPUtil.getIpAddress(exchange.getRequest());
logger.info("转发{}请求",ipAddress);
if (ipAddress == null) {
return super.choose(exchange);
}
if (this.loadBalancer instanceof RibbonLoadBalancerClient) {
RibbonLoadBalancerClient client = (RibbonLoadBalancerClient) this.loadBalancer;
String serviceId = ((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).getHost();
ServerHttpRequest request = exchange.getRequest().mutate().header(version_key, VersionCacheUtil
.get(ipAddress)).build();
exchange.mutate().request(request).build();
return client.choose(serviceId, ipAddress);
}
return super.choose(exchange);
}
}
重写负载规则,继承AbstractLoadBalancerRule
,直接重写choose
方法即可,此处主要作用是从可用服务中获取出跟版本信息匹配的节点。
package com.omsa.api.gateway.filter;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.Server;
import com.omsa.api.gateway.cache.VersionCacheUtil;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.zookeeper.discovery.ZookeeperServer;
import org.springframework.util.StringUtils;
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/12/23 9:49
*/
public class VersionRule extends AbstractLoadBalancerRule {
private static Logger logger = LoggerFactory.getLogger(VersionRule.class);
private final static String version_key = "branch";
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
@Override
public Server choose(Object key) {
List<Server> servers = getLoadBalancer().getReachableServers();
for (Server server : servers) {
ZookeeperServer zkServer = (ZookeeperServer) server;
Map<String, String> metadata = zkServer.getInstance().getPayload().getMetadata();
String version = metadata.get(version_key);
if (!StringUtils.isEmpty(version)) {
if (VersionCacheUtil.get(key).equals(version)) {
logger.info("调用实例{},{},{}", key, version, zkServer.getHost());
return server;
}
}
}
return null;
}
}
提供个缓存工具类用户存储用户绑定信息,key
为ip
信息,value
为版本信息,这样所有该ip
的请求遇到有该版本信息时就会优先负载到对应节点,稍后要提供两个接口用于绑定和解绑
package com.omsa.api.gateway.cache;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/12/22 17:35
*/
public class VersionCacheUtil {
private final static Logger logger= LoggerFactory.getLogger(VersionCacheUtil.class);
private static String DEFAULT="master";
private static Map<Object, String> bindCache=new ConcurrentHashMap<>();
public static String get(Object key){
String value = bindCache.get(key);
if (StringUtils.isEmpty(value)) {
return DEFAULT;
}
return value;
}
public static void bind(String key,String value){
if (StringUtils.isEmpty(value)) {
logger.error("绑定分支号不能为空");
return;
}
bindCache.put(key,value);
}
public static void unbind(String key){
bindCache.remove(key);
}
}
当然还要提供个工具获取请求用户的真实ip
package com.omsa.api.gateway.utils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/12/23 16:29
*/
public class IPUtil {
public static String getIpAddress(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
String ip = headers.getFirst("x-forwarded-for");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
if (ip.indexOf(",") != -1) {
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = headers.getFirst("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddress().getAddress().getHostAddress();
}
return ip;
}
}
提供两个接口用于绑定和解绑
package com.omsa.api.gateway.endpoint;
import com.omsa.api.gateway.cache.VersionCacheUtil;
import com.omsa.api.gateway.utils.IPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/12/23 14:06
*/
@RestController
public class RoutesEndPoint {
private static final Logger logger= LoggerFactory.getLogger(RoutesEndPoint.class);
@GetMapping("/bind/{branch}")
public Result<Object> bind(ServerWebExchange exchange,@PathVariable("branch") String branch) {
if (StringUtils.isEmpty(branch)) {
return Result.fail("绑定的分支号不能为空");
}
String ipAddress = IPUtil.getIpAddress(exchange.getRequest());
logger.info("客户端绑定请求{}:{}",ipAddress,branch);
VersionCacheUtil.bind(ipAddress,branch);
return Result.success();
}
@GetMapping("/unbind")
public Result<Object> ubind(ServerWebExchange exchange) {
String ipAddress = IPUtil.getIpAddress(exchange.getRequest());
logger.info("客户端解绑请求{}",ipAddress);
VersionCacheUtil.unbind(ipAddress);
return Result.success();
}
}
返回结果
package com.omsa.api.gateway.endpoint;
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/12/23 14:11
*/
public class Result<T> {
private int code=0;
private String message;
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> Result success(){
Result res = new Result<>();
res.setCode(0);
res.setMessage("success");
return res;
}
public static <T> Result fail(String message){
Result res = new Result<>();
res.setCode(-1);
res.setMessage(message);
return res;
}
}
配置类把刚刚的filter
和rule
配置成bean
。此时默认配置就会被覆盖,这套体系不需要放到生产使用。生产根据需要走不同的负载策略。
package com.omsa.api.gateway.config;
import com.netflix.loadbalancer.IRule;
import com.omsa.api.gateway.filter.VersionLoadBalancerClientFilter;
import com.omsa.api.gateway.filter.VersionRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.gateway.config.LoadBalancerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/12/22 17:46
*/
@Configuration
public class LoadBalancedConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public VersionLoadBalancerClientFilter versionLoadBalancerClientFilter(LoadBalancerClient client, LoadBalancerProperties properties) {
return new VersionLoadBalancerClientFilter(client, properties);
}
@Bean
public IRule rule(){
return new VersionRule();
}
}
这样网关的负载就做好了,服务层的负载,也很简单,同样去重写负载规则,并且把版本号写到注册中心即可
这样只要一套环境解决所有开发测试问题,并且还能维护一套与生产代码一致的测试环境,有效节省了大量资源,并且代码的管理也可以变得很简单,直接master-------->feature
即可,需要上线时把feature
合并至master
未上线前不需要做代码合并等动作,提高开发效率。但是这样有一点点瑕渍就是不同需求如果产生代码冲突测试可能无法覆盖测试到,一般上线前还需要提供一个验收环境,进行功能把关,所以此处也不会出现问题。