consul 安装配置 集成springcloud 详解

通过自己调研实际应用consul,这里总结分享一下consul的 安装使用。

一.安装consul

以linux为例:

官方下载https://www.consul.io/downloads.html 地址下载linux版, 解压unzip consul_1.1.0_linux_amd64.zip,得到一个文件consul。 
复制consul:cp consul /usr/local/bin/,可以直接使用访问consul命令。 

二.运行consul

每个节点都需要运行agent,他有两种运行模式server和client。每个数据中心官方建议需要3或5个server节点以保证数据安全,同时保证server-leader的选举能够正确的进行。
下面的前3条命令构建了 consul集群,第四条命令表示向consul集群中注册一个client节点
consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -node=s1 -bind=172.20.3.91 -ui -client 0.0.0.0  
consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -node=s2 -bind=172.20.3.92 -ui -client 0.0.0.0 -join 172.20.3.91
consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -node=s3 -bind=172.20.3.93 -ui -client 0.0.0.0 -join 172.20.4.91
consul agent -data-dir /tmp/consul -node=c1 -bind=192.168.87.208 -client=172.20.3.97 -join 172.20.12.2
consul agent -data-dir /tmp/consul -node=c1 -bind=192.168.87.208 -client=172.20.3.98 -join 172.20.12.3
-node:节点的名称
-bind:绑定的一个地址,用于节点之间通信的地址,可以是内外网,必须是可以访问到的地址  
-server:这个就是表示这个节点是个SERVER  如果没有表示是client模式
-bootstrap-expect:这个就是表示期望提供的SERVER节点数目,数目一达到,它就会被激活,然后就是内部选举LEADER了
-data-dir:表示持久化服务注册信息,集群信息的目录
-join:这个表示启动的时候,要加入到哪个集群内,这里就是说要加入到节点1的集群  
-client:客户端的ip地址,如果是server节点可以是0.0.0.0  
查看集群节点命令 consul members  ;查看集群节点状态 consul operator raft list-peers

三.通过springcloud集成consul

完整代码 springcloud-consul demo代码 git地址:https://github.com/475cheng/consul-demo-cheng

以springboot为1.5.19为例
1. maven配置
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.19.RELEASE</version>
        <relativePath/>
</parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


2. application.yml配置
spring:
  application:
    name: consul-producer
  cloud:
    consul:
      host: 172.20.4.98                 //默认值是localhost 
      port: 8500
      discovery:
        instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
        health-check-path: /health          //注意consul需要健康检查的接口  这里引用actuator自带的/health 接口 如果是2.0的springboot 应该是actuator/health
        health-check-interval: 10s

server:
  port: 8502

management:
  security:
    enabled: false


3. springboot的启动类要加入注解 @EnableDiscoveryClient
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值