Exemplo n.º 1
0
  @Test
  @Ignore
  public void test2() {
    Consul consul =
        Consul.builder().withHostAndPort(HostAndPort.fromParts("192.168.22.100", 8500)).build();
    CatalogClient catalogClient = consul.catalogClient();

    ConsulResponse<Map<String, List<String>>> services = catalogClient.getServices();
    Map<String, List<String>> map = services.getResponse();
    for (String serviceName : map.keySet()) {
      System.out.println("服务:" + serviceName);

      ConsulResponse<List<CatalogService>> service = catalogClient.getService(serviceName);
      for (CatalogService s : service.getResponse()) {
        System.out.println(
            "id="
                + s.getServiceId()
                + ", name="
                + s.getServiceName()
                + ", ip="
                + s.getServiceAddress()
                + ", addr="
                + s.getAddress()
                + ", port="
                + s.getServicePort());
      }
    }
  }
Exemplo n.º 2
0
  @Test
  public void test() throws NotRegisteredException {
    Consul consul =
        Consul.builder().withHostAndPort(HostAndPort.fromParts("192.168.1.245", 8500)).build();
    AgentClient agentClient = consul.agentClient();

    ImmutableRegistration regist =
        ImmutableRegistration.builder()
            .id("1")
            .name("MyService")
            .address("192.168.1.99")
            .port(8888)
            .addTags("mok")
            .build();

    ImmutableRegistration regist2 =
        ImmutableRegistration.builder()
            .id("2")
            .name("MyService")
            .address("192.168.1.100")
            .port(8888)
            .addTags("mok")
            .build();

    consul.agentClient().register(regist);

    agentClient.register(regist2);
  }