- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个环境 PROD
和 STAGING
。在 prod 环境中,我们有三个数据中心 ABC
、DEF
和 PQR
,staging 有一个数据中心 CORP
。每个数据中心都有几台机器,我为它们定义了常量,如下所示:
// NOTE: I can have more machines in each dc in future
public static final ImmutableList<String> ABC_SERVERS = ImmutableList.of("tcp://machineA:8081", "tcp://machineA:8082");
public static final ImmutableList<String> DEF_SERVERS = ImmutableList.of("tcp://machineB:8081", "tcp://machineB:8082");
public static final ImmutableList<String> PQR_SERVERS = ImmutableList.of("tcp://machineC:8081", "tcp://machineC:8082");
public static final ImmutableList<String> STAGING_SERVERS = ImmutableList.of("tcp://machineJ:8087","tcp://machineJ:8088");
现在我在同一个类中定义了另一个常量,它按 DC 分组为每种环境类型的机器列表。
public static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC =
ImmutableMap.<Datacenter, ImmutableList<String>>builder()
.put(Datacenter.ABC, ABC_SERVERS).put(Datacenter.DEF, DEF_SERVERS)
.put(Datacenter.PQR, PQR_SERVERS).build();
public static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC =
ImmutableMap.<Datacenter, ImmutableList<String>>builder()
.put(Datacenter.CORP, STAGING_SERVERS).build();
现在在其他一些类中,根据我在 (Utils.isProd())
中的环境,我得到 PROD_SERVERS_BY_DC
或 STAGING_SERVERS_BY_DC
.
Map<Datacenter, ImmutableList<String>> machinesByDC = Utils.isProd() ? Utils.PROD_SERVERS_BY_DC : Utils.STAGING_SERVERS_BY_DC;
现在我认为这可以用某种枚举以更好的方式表示,而不是像上面那样定义常量,但我无法弄清楚我该怎么做?我从这个开始,但对如何为每个 DC 使用单个键然后将多个值作为该 DC 的机器列表感到困惑,然后我还需要按环境对它们进行分组。
// got confuse on how can I make key (DC) and list of values (machines) for each environment type.
public enum DCToMachines {
abc(tcp://machineA:8081", "tcp://machineA:8082"),
private final List<String> machines;
private final Datacenter datacenter;
...
}
最佳答案
I started off with this but got confuse on how can I have single key for each DC and then multiple values as List of machines for that DC and then I need to group them by environment as well.
你忘记了一个重要的只是:枚举可以存储特定于每个枚举值的成员(方法和字段),但如果需要它也可以存储 static
成员。
您需要的是直接在枚举类中移动按环境分组的 map :
private static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC;
private static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC;
您可以使用内部 map 根据用户请求返回预期的 map :
private static final Map<Env, ImmutableMap<Datacenter, ImmutableList<String>>> SERVERS_BY_ENV;
Env
是另一个枚举:
public static enum Env {
PROD, STAGING
}
在我将要展示的代码中,我添加了 Datacenter
的封闭类型,但它在自己的文件中可能会更好,因为环境可能不是数据中心专门使用的概念。
最后,您用来按环境检索服务器的方式将相关职责分为 2 个类(Datacenter
枚举和当前类):
Map<Datacenter, ImmutableList<String>> machinesByDC = Utils.isProd() ? Utils.PROD_SERVERS_BY_DC : Utils.STAGING_SERVERS_BY_DC;
在枚举中引入一个方法来执行此操作会更好:
public static ImmutableMap<Datacenter, ImmutableList<String>> getServers(Env env){...}
这会增加枚举类的凝聚力。
这是更新的枚举:
import java.util.HashMap;
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
public enum Datacenter {
ABC(Env.PROD, "tcp://machineA:8081", "tcp://machineA:8082"),
DEF(Env.PROD, "tcp://machineB:8081", "tcp://machineB:8082"),
PQR(Env.PROD, "tcp://machineA:8081", "tcp://machineA:8082"),
CORP(Env.STAGING, "tcp://machineC:8081", "tcp://machineC:8082");
public static enum Env {
PROD, STAGING
}
private Env env;
private String[] url;
private static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC;
private static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC;
private static final Map<Env, ImmutableMap<Datacenter, ImmutableList<String>>> SERVERS_BY_ENV;
static {
Builder<Datacenter, ImmutableList<String>> prodDataCenterBuilder = ImmutableMap.<Datacenter, ImmutableList<String>>builder();
Builder<Datacenter, ImmutableList<String>> stagingDataCenterBuilder = ImmutableMap.<Datacenter, ImmutableList<String>>builder();
for (Datacenter datacenter : Datacenter.values()) {
if (datacenter.env == Env.PROD) {
prodDataCenterBuilder.put(datacenter, ImmutableList.of(datacenter.url));
} else if (datacenter.env == Env.STAGING) {
stagingDataCenterBuilder.put(datacenter, ImmutableList.of(datacenter.url));
}
else {
throw new IllegalArgumentException("not exepected env " + datacenter.env);
}
}
PROD_SERVERS_BY_DC = prodDataCenterBuilder.build();
STAGING_SERVERS_BY_DC = stagingDataCenterBuilder.build();
SERVERS_BY_ENV = new HashMap<>();
SERVERS_BY_ENV.put(Env.PROD, PROD_SERVERS_BY_DC);
SERVERS_BY_ENV.put(Env.STAGING, STAGING_SERVERS_BY_DC);
}
Datacenter(Env env, String... url) {
this.env = env;
this.url = url;
}
public static ImmutableMap<Datacenter, ImmutableList<String>> getServers(Env env) {
ImmutableMap<Datacenter, ImmutableList<String>> map = SERVERS_BY_ENV.get(env);
if (map == null) {
throw new IllegalArgumentException("not exepected env " + env);
}
return map;
}
}
这里是如何使用它:
public static void main(String[] args) {
ImmutableMap<Datacenter, ImmutableList<String>> servers = Datacenter.getServers(Env.PROD);
servers.forEach((k, v) -> System.out.println("prod datacenter=" + k + ", urls=" + v));
servers = Datacenter.getServers(Env.STAGING);
servers.forEach((k, v) -> System.out.println("staging datacenter=" + k + ", urls=" + v));
}
输出:
prod datacenter=ABC, urls=[tcp://machineA:8081, tcp://machineA:8082]
prod datacenter=DEF, urls=[tcp://machineB:8081, tcp://machineB:8082]
prod datacenter=PQR, urls=[tcp://machineA:8081, tcp://machineA:8082]
staging datacenter=CORP, urls=[tcp://machineC:8081, tcp://machineC:8082]
关于java - 我如何用枚举中每种环境类型的值列表来表示键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47956136/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!