java代码实现通过consul获取微服务所在dataCenter

  1. 用orbitz.consul client 端来获取
    代码如下:
package com.yj.consul.client.consulClient;

import com.orbitz.consul.AgentClient;
import com.orbitz.consul.Consul;

public class App 
{
    public static void main( String[] args )
    {
        Consul consul = Consul.builder().build();
        AgentClient agentClient = consul.agentClient();
        System.out.println(agentClient.getAgent().getConfig().getDatacenter());
    }
}

问题:

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder
 at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:103)
 at com.orbitz.consul.Consul$Builder.(Consul.java:228)
 at com.orbitz.consul.Consul.builder(Consul.java:218)
 at com.yj.consul.client.consulClient.App.main(App.java:19)
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:264)
 at javax.ws.rs.client.FactoryFinder.newInstance(FactoryFinder.java:115)
 at javax.ws.rs.client.FactoryFinder.find(FactoryFinder.java:225)
 at javax.ws.rs.client.ClientBuilder.newBuilder(ClientBuilder.java:86)
 ... 3 more

问题是,缺少org.glassfish.jersey.client.JerseyClientBuilder的包
pom文件中引入

  
   org.glassfish.jersey.core
   jersey-client
   2.17
  

问题解决

你可能感兴趣的:(java代码实现通过consul获取微服务所在dataCenter)