Skip to content

Commit

Permalink
增加环境隔离和路由功能
Browse files Browse the repository at this point in the history
  • Loading branch information
HaojunRen committed Nov 21, 2019
1 parent 82401a1 commit f92795c
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ spring.application.strategy.service.sentinel.request.origin.key=n-d-service-id
# 环境路由环境隔离下调用端实例找不到符合条件的提供端实例把流量路由到一个通用或者备份环境例如元数据Metadata环境配置值为common该值可配置但不允许为保留值default
spring.application.environment.isolation.enabled=true
# 流量路由到指定的环境下不允许为保留值default缺失则默认为common
spring.application.environment.transfer=common
spring.application.environment.route=common

# 开启和关闭使用服务名前缀来作为服务组名缺失则默认为false
spring.application.group.generator.enabled=true
Expand Down Expand Up @@ -1293,7 +1293,7 @@ spring.application.strategy.hystrix.threadlocal.supported=true
# 环境路由环境隔离下调用端实例找不到符合条件的提供端实例把流量路由到一个通用或者备份环境例如元数据Metadata环境配置值为common该值可配置但不允许为保留值default
spring.application.environment.isolation.enabled=true
# 流量路由到指定的环境下不允许为保留值default缺失则默认为common
spring.application.environment.transfer=common
spring.application.environment.route=common

# 开启和关闭使用服务名前缀来作为服务组名缺失则默认为false
spring.application.group.generator.enabled=true
Expand Down Expand Up @@ -1375,7 +1375,7 @@ spring.application.strategy.hystrix.threadlocal.supported=true
# 环境路由环境隔离下调用端实例找不到符合条件的提供端实例把流量路由到一个通用或者备份环境例如元数据Metadata环境配置值为common该值可配置但不允许为保留值default
spring.application.environment.isolation.enabled=true
# 流量路由到指定的环境下不允许为保留值default缺失则默认为common
spring.application.environment.transfer=common
spring.application.environment.route=common

# 开启和关闭使用服务名前缀来作为服务组名缺失则默认为false
spring.application.group.generator.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class DiscoveryConstant {
public static final String SPRING_APPLICATION_NO_SERVERS_RETRY_AWAIT_TIME = "spring.application.no.servers.retry.await.time";
public static final String SPRING_APPLICATION_NO_SERVERS_NOTIFY_ENABLED = "spring.application.no.servers.notify.enabled";
public static final String SPRING_APPLICATION_ENVIRONMENT_ISOLATION_ENABLED = "spring.application.environment.isolation.enabled";
public static final String SPRING_APPLICATION_ENVIRONMENT_TRANSFER = "spring.application.environment.transfer";
public static final String SPRING_APPLICATION_ENVIRONMENT_TRANSFER_VALUE = "common";
public static final String SPRING_APPLICATION_ENVIRONMENT_ROUTE = "spring.application.environment.route";
public static final String SPRING_APPLICATION_ENVIRONMENT_ROUTE_VALUE = "common";

public static final String CONTEXT_PATH = "server.servlet.context-path";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@
"description": "Whether environment isolation is enabled."
},
{
"name": "spring.application.environment.transfer",
"name": "spring.application.environment.route",
"type": "java.lang.String",
"defaultValue": "common",
"description": "If environment isolation failed, an environment will transfer."
"description": "An environment will be routed to."
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

import com.nepxion.discovery.common.constant.DiscoveryConstant;

public abstract class DefaultEnvironmentTransferAdapter implements EnvironmentTransferAdapter {
public abstract class DefaultEnvironmentRouteAdapter implements EnvironmentRouteAdapter {
@Autowired
protected PluginAdapter pluginAdapter;

@Value("${" + DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_TRANSFER + ":" + DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_TRANSFER_VALUE + "}")
protected String environmentTransfer;
@Value("${" + DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_ROUTE + ":" + DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_ROUTE_VALUE + "}")
protected String environmentRoute;

@Override
public String getTransferredEnvironment() {
return environmentTransfer;
public String getEnvironmentRoute() {
return environmentRoute;
}

public PluginAdapter getPluginAdapter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* @version 1.0
*/

public interface EnvironmentTransferAdapter {
public interface EnvironmentRouteAdapter {
// 是否要环境路由
boolean isTransferred();
boolean isRoutable();

// 路由到哪个环境中
String getTransferredEnvironment();
String getEnvironmentRoute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;

import com.nepxion.discovery.plugin.framework.adapter.EnvironmentTransferAdapter;
import com.nepxion.discovery.plugin.framework.adapter.EnvironmentRouteAdapter;

public class EnvironmentFilterDiscoveryListener extends AbstractDiscoveryListener {
@Autowired(required = false)
private EnvironmentTransferAdapter environmentTransferAdapter;
private EnvironmentRouteAdapter environmentRouteAdapter;

@Override
public void onGetInstances(String serviceId, List<ServiceInstance> instances) {
Expand All @@ -41,8 +41,8 @@ private void applyEnvironmentFilter(String providerServiceId, List<ServiceInstan
}
} else {
// 环境路由:环境隔离下,调用端实例找不到符合条件的提供端实例,把流量路由到一个通用或者备份环境,例如:元数据Metadata环境配置值为common(该值可配置,但不允许为保留值default)
if (environmentTransferAdapter != null && environmentTransferAdapter.isTransferred()) {
if (!StringUtils.equals(instanceEnvironment, environmentTransferAdapter.getTransferredEnvironment())) {
if (environmentRouteAdapter != null && environmentRouteAdapter.isRoutable()) {
if (!StringUtils.equals(instanceEnvironment, environmentRouteAdapter.getEnvironmentRoute())) {
iterator.remove();
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.nepxion.discovery.plugin.framework.adapter.EnvironmentTransferAdapter;
import com.nepxion.discovery.plugin.framework.adapter.EnvironmentRouteAdapter;
import com.netflix.loadbalancer.Server;

public class EnvironmentFilterLoadBalanceListener extends AbstractLoadBalanceListener {
@Autowired(required = false)
private EnvironmentTransferAdapter environmentTransferAdapter;
private EnvironmentRouteAdapter environmentRouteAdapter;

@Override
public void onGetServers(String serviceId, List<? extends Server> servers) {
Expand All @@ -41,8 +41,8 @@ private void applyEnvironmentFilter(String providerServiceId, List<? extends Ser
}
} else {
// 环境路由:环境隔离下,调用端实例找不到符合条件的提供端实例,把流量路由到一个通用或者备份环境,例如:元数据Metadata环境配置值为common(该值可配置,但不允许为保留值default)
if (environmentTransferAdapter != null && environmentTransferAdapter.isTransferred()) {
if (!StringUtils.equals(serverEnvironment, environmentTransferAdapter.getTransferredEnvironment())) {
if (environmentRouteAdapter != null && environmentRouteAdapter.isRoutable()) {
if (!StringUtils.equals(serverEnvironment, environmentRouteAdapter.getEnvironmentRoute())) {
iterator.remove();
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ spring.application.strategy.trace.debug.enabled=true
# 环境路由:环境隔离下,调用端实例找不到符合条件的提供端实例,把流量路由到一个通用或者备份环境,例如:元数据Metadata环境配置值为common(该值可配置,但不允许为保留值default)
# spring.application.environment.isolation.enabled=false
# 流量路由到指定的环境下。不允许为保留值default,缺失则默认为common
# spring.application.environment.transfer=common
# spring.application.environment.route=common

# 开启和关闭使用服务名前缀来作为服务组名。缺失则默认为false
# spring.application.group.generator.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ spring.application.strategy.trace.debug.enabled=true
# 环境路由:环境隔离下,调用端实例找不到符合条件的提供端实例,把流量路由到一个通用或者备份环境,例如:元数据Metadata环境配置值为common(该值可配置,但不允许为保留值default)
# spring.application.environment.isolation.enabled=false
# 流量路由到指定的环境下。不允许为保留值default,缺失则默认为common
# spring.application.environment.transfer=common
# spring.application.environment.route=common

# 开启和关闭使用服务名前缀来作为服务组名。缺失则默认为false
# spring.application.group.generator.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ spring.application.strategy.trace.debug.enabled=true
# 环境路由:环境隔离下,调用端实例找不到符合条件的提供端实例,把流量路由到一个通用或者备份环境,例如:元数据Metadata环境配置值为common(该值可配置,但不允许为保留值default)
# spring.application.environment.isolation.enabled=false
# 流量路由到指定的环境下。不允许为保留值default,缺失则默认为common
# spring.application.environment.transfer=common
# spring.application.environment.route=common

# 开启和关闭使用服务名前缀来作为服务组名。缺失则默认为false
# spring.application.group.generator.enabled=true
Expand Down

0 comments on commit f92795c

Please sign in to comment.