美文网首页
java代码实现通过consul获取微服务所在dataCente

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

作者: stepyu | 来源:发表于2016-04-27 16:09 被阅读2068次
    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.<init>(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文件中引入

      <dependency>
       <groupId>org.glassfish.jersey.core</groupId>
       <artifactId>jersey-client</artifactId>
       <version>2.17</version>
      </dependency>
    

    问题解决

    相关文章

      网友评论

          本文标题:java代码实现通过consul获取微服务所在dataCente

          本文链接:https://www.haomeiwen.com/subject/zljcrttx.html