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

问题描述 投票:1回答:2

我有两个环境PRODSTAGING。在prod环境中,我们有三个数据中心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;
  ...


}
java oop design-patterns data-structures enums
2个回答
2
投票

我开始讨论这个但是混淆了如何为每个DC提供单个密钥,然后将多个值作为该DC的机器列表,然后我还需要按环境对它们进行分组。

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

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,但它在自己的文件中可能更好,因为环境可能不是数据中心专用的概念。

最后,您通过环境检索服务器的方式将相关职责分为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 data center = CORP,urls = [tcp:// machine:8081,tcp:// machine:8082]


1
投票

虽然在不知道完全实现的情况下这有点棘手,但我可以帮助您入门。

枚举很有意思,因为它们是不可变的,它解决了声明常量字段和变量的问题。这是我尝试解决您正在寻找的问题。

public enum Server {
    // Constant style naming because Enums are Immutable objects
    ABC_SERVERS("tcp://machineA:8081", "tcp://machineA:8082"),
    DEF_SERVERS("tcp://machineB:8081", "tcp://machineB:8082"),
    PQR_SERVERS("tcp://machineC:8081", "tcp://machineC:8082"),
    CORP("tcp://machineZ:8081");

    /* After the name declaration you can essentially do normal class 
       development
    */ Fields
    private List<String> servers = new ArrayList<>();
    public boolean isProd; // you could use something as simple as a 
    //boolean to determine the environment

    // Enums use a private constructor because they are never 
    //instantiated outside of creation of this Enum class
    private Server(String... dataCenter){

    // because of the varargs parameter there is a potential for 
    //multiple Strings to be passed
        for (String tcp :
                dataCenter) {
            this.servers.add(tcp);
        }

        // You can access the name property of the Enum that is being created
        if (this.name() == "CORP")
            this.isProd = false;
        else
            this.isProd = true;

    }

    // You could make the List public but this demonstrates some encapsulation
    public List<String> getServers() {
        return servers;
    }
}

然后我检查了每个“机器”是否正在另一个类中添加

    // these are what I used to check to make sure that each string is being "mapped" correctly
    List<String> prodServer = new ArrayList<>();
    List<String> stagingServer = new ArrayList<>();

    for (Server server :
            Server.values()) {
        if (server.isProd) {
            for (String machine :
                    server.getServers()) {
                prodServer.add(machine);
            }
        }
        else {
            for (String machine:
                    server.getServers()) {
                stagingServer.add(machine);
            }
        }
    }

    System.out.println(prodServer);

    System.out.println(stagingServer);

另请注意,要获取实际的枚举名称,请使用Enum类的.values()函数。

© www.soinside.com 2019 - 2024. All rights reserved.