美文网首页
Consul HTTP API

Consul HTTP API

作者: torres9gogogo | 来源:发表于2016-07-20 21:19 被阅读754次
  1. 服务注册

(1)maven依赖:

    <dependency>
        <groupId>com.orbitz.consul</groupId>
        <artifactId>consul-client</artifactId>
        <version>0.12.4</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>bintray</name>
        <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>```
(2)使用SpringBoot架构
consul的一些配置写在一个配置文件(服务名字、端口):
 public class ConsulConfigProperties {
  /** *服务名字*/
  @Value("${service.name}")
  @NotNull
  private String name;
  /** * 服务对外端口 */
  @Value("${server.port:8080}")
  private int servicePort;
      ...

}```
实现applicationlistener的一个服务注册类:ServiceRegister

public class ServiceRegister implements ApplicationListener<ContextRefreshedEvent> 
{
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event)
        {   
         //获取上面的consul 配置文件 
          ConsulConfigProperties configuration = event.getApplicationContext().getBean(ConsulConfigProperties.class);   
         // 与consul连接 == Consul consul = Consul.builder().build();
          Consul  consul = event.getApplicationContext().getBean(Consul.class); 
        //获取 agent
          AgentClient agent = consul.agentClient();  
        try { 
           // 获取配置中的健康检查URL
           //Consul agent 会来ping这个URL以确定service是否健康     
            URL healthURL  = URI.create(configuration.getHttpUrl()).toURL();     
           //  服务注册 
          //public void register(int port, URL http, long interval, String name, String id, String... tags)  
          //port  注册服务的端口 http 健康检查 interval 健康检查时间间隔  name  服务名字 id 注册ID  tags   注册的tag 比如 Dev  用于consul中取不同的配置。
           agent.register(configuration.getExternalPort(), healthURL, configuration.getHttpInterval(),configuration.getName(), ""+configuration.getExternalPort(), severTag);  
            } 
           catch (MalformedURLException e)
           {      
               throw new RuntimeException(e);   
           }
     }

相关文章

  • Consul HTTP API

    服务注册 (1)maven依赖: }```实现applicationlistener的一个服务注册类:Servic...

  • consul API

    配置文件 Consul API

  • 第1章 consul简介

    1、consul的作用 服务发现Consul clients提供服务(例如API)其他的client发现服务的提供...

  • 删除consul的无效/失效服务

    consul的常用api可以参考: https://my.oschina.net/guol/blog/353394...

  • consul 的api接口

    1、添加服务https://www.consul.io/api-docs/agent/service#regist...

  • consul

    Consul是一个分布式高可用的系统,它有以下特点: 服务发现:Consul客户能够注册一个服务,比如api或my...

  • .net开发常用组件

    consul go语言开发的,服务发现、服务注册 ocelot .net api网关超时、熔断避免某个服务挂断导致...

  • 【consul】consul deregister servic

    通过postman 执行put http://consul01.dmz.cn-north-1a.prod.xx.c...

  • go-micro本地环境部署及开发步骤

    关于启动本地服务一、启动consul 二、启动micro api网关 三、启动配置中心box-config 四、启...

  • consul api读取服务列表

    一、背景 需要使用acl_token读取consul上的服务列表,主要使用的api为/v1/catalog/ser...

网友评论

      本文标题:Consul HTTP API

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