小编典典

如何用枚举中每种环境类型的值列表表示键?

java

我有两个环境PRODSTAGING。在督促环境中,我们有三个数据中心ABCDEFPQR和分期有一个数据中心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_DCSTAGING_SERVERS_BY_DC

Map<Datacenter, ImmutableList<String>> machinesByDC = Utils.isProd() ? Utils.PROD_SERVERS_BY_DC : Utils.STAGING_SERVERS_BY_DC;

现在,我认为可以用某种Enum更好地表示它,而不必像上面定义的那样定义常量,但是我不知道该怎么办?我从此开始,但是对于如何为每个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;
  ...


}

阅读 207

收藏
2020-11-30

共1个答案

小编典典

我从此开始,但是对于如何为每个DC拥有一个键,然后为该DC的计算机列表拥有多个值感到困惑,然后我还需要按环境对它们进行分组。

您忘记了一个重要的建议:枚举可能存储特定于每个枚举值的成员(方法和字段),但static如果需要,它也可能存储成员。
您需要直接在enum类中移动按环境分组的地图:

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;

Env另一个枚举在哪里:

public static enum Env {
    PROD, STAGING
}

在我将要提供的代码中,我将其添加为封闭类型,Datacenter但是在其自己的文件中可能会更好,因为环境可能不是数据中心专用的概念。

最后,您使用的按环境检索服务器的方式将相关职责划分为两个类(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));
}

输出

产品数据中心= ABC,网址= [tcp:// machineA:8081,tcp:// machineA:8082]

产品数据中心= DEF,网址= [tcp:// machineB:8081,tcp:// machineB:8082]

产品数据中心= PQR,网址= [tcp:// machineA:8081,tcp:// machineA:8082]

登台数据中心= CORP,网址= [tcp:// machineC:8081,tcp:// machineC:8082]

2020-11-30