一 consul安装和启动
1首先去官网下载consul

https://www.consul.io/downloads.html
选择linux版本即可 然后上传到linux服务器
在这里插入图片描述

2 解压

我上传到/opt 目录,当前使用1.8.4版本
解压:unzip consul_1.8.4_linux_amd64.zip

在解压目录使用指令,也可以像配置JDK一样配置环境变量,在任意位置启动

./consul   获取指令帮助

./consul --version  查看版本

./consul agent -dev -ui  -client=192.168.0.104   虚拟机单机启动,访问ui界面,相当于nohup
# 192.168.0.104 是我的ip 自行更换为自己实际的

效果如下:
在这里插入图片描述

3阿里云服务器启动dev模式:

./consul agent -dev -ui -client=阿里云私网ip
生产者的服务注册中心依然是阿里云公网ip

4 虚拟机集群启动

1首先准备3台虚拟机,分别安装consul
2 分别启动3台consul
启动第一台时候:
第一台作为leader

./consul agent -server -bind=192.168.0.185 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/local/consul/data/ -node=server1

启动第二台时候:

./consul agent -server -bind=192.168.0.104 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/local/consul/data/ -node=server2

启动第三台时候,把他作为server,并且使用这台的ui界面进行可视化界面查看

./consul agent -server -bind=192.168.0.184 -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/usr/local/consul/data/ -node=server3 -ui

3 二三台的加入到第一台中
在二三台都使用如下命令

./consul join 192.168.0.185

4 查看成员

./consul members  #集群成员查看

在这里插入图片描述

5查看集群中leader和follower的关系

./consul operator raft list-peers 

在这里插入图片描述
6以下以3台为例,分别为ip1、ip2、ip3:
./consul agent -server -bootstrap-expect=3 -data-dir /tmp/consul -node=consul1 -bind=ip1 -ui -client=0.0.0.0 &

./consul agent -server -bootstrap-expect=3 -data-dir /tmp/consul -node=consul2 -bind=ip2 -join=ip1 -ui -client=0.0.0.0 &

./consul agent -server -bootstrap-expect=3 -data-dir /tmp/consul -node=consul3 -bind=ip3 -join=ip1 -ui -client=0.0.0.0 &

5 访问ip:8500 即可出现consul界面

浏览第三台consul的ui界面
在这里插入图片描述

二 consul的生产者消费者案例

先创建maven的父工程,在分别创建子模块

1 生产者
1.1添加consul依赖
<!--consul  begin-->
        <dependency><!--        健康检查依赖于此包-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!--consul  end-->
1.2 application.yml配置文件
server:
  port: 8502

# 应用名称
spring:
  application:
    name: consul-provider
  #consul配置
  cloud:
    consul:
      host: 192.168.0.104
      port: 8500

      # 服务发现配置
      discovery:
        #spring.cloud.consul.discovery.serviceName 是指注册到 Consul 的服务名称,后期客户端会根据这个名称来进行服务调用
        service-name: consul-service-producer
        # 启用服务发现
        enabled: true
        # 启用服务注册
        register: true
        # 服务停止时取消注册
        deregister: true
        # 表示注册时使用IP而不是hostname
        prefer-ip-address: true
        #  ip-address: 如果是云服务器,如阿里云。可以通过此属性来指定外网调用地址
		ip-address: 192.168.0.117 #指定程序运行ip
        # 执行监控检查的频率
        health-check-interval: 10s
        # 设置健康检查失败多长时间后,取消注册
        health-check-critical-timeout: 10s
        # 健康检查的路径
        health-check-path: /actuator/info
        # 服务注册标识,格式为:应用名称+服务器IP+端口+随机数
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}:${random.value}

prefer-ip-address: true
# ip-address: 如果是云服务器,如阿里云。可以通过此属性来指定外网调用地址
ip-address: 192.168.0.117 #指定程序运行ip

这两行之前没有加正常.后来就注册不到consul中了
报错分析如下:

很多朋友在开发的时候,生产环境里总会有个虚拟机,那虚拟网卡的存在是必然的。 本文遇到的这个问题,理论上适用于大部分注册中心。
最近项目在consul注册时,发现一个问题:注册的IP地址不是 192.168.1.XXX 的网络IP,而是另外一个网段的地址, 直观的可以从控制ui看到,注册中心获取到的ip完全是不对的。
加上就正常

参考:https://blog.csdn.net/a007007007csy/article/details/105360760

参考:https://blog.csdn.net/wangchaox123/article/details/103005628

1.3添加注解

启动类添加注解:
@EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient // 添加了 @EnableDiscoveryClient 注解表示支持服务发现
public class ComsulProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ComsulProviderApplication.class, args);
    }

}

1.4业务逻辑
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "helle consul-8502";
    }
}
2消费者

消费者调用生产者这里采用feign调用

2.1添加consul依赖
<!--consul  begin-->
        <dependency><!--        健康检查依赖于此包-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!--consul  end-->
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
2.2 application.yml配置文件
server:
  port: 8503

spring:
  application:
    name: consul-consumer
  cloud:
    consul:
      host: 192.168.0.128
      port: 8500
      discovery:
        register: false # 消费者,不需要注册到consul中
2.3添加注解

启动类添加feign注解:
@EnableFeignClients

@SpringBootApplication
@EnableFeignClients //开启Feign服务调用
public class ConsulConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsulConsumerApplication.class, args);
    }
}
2.4定义feign服务调用接口
//定义接口,其中 value 是 provide 注册在 consul 中的服务名(spring.cloud.consul.discovery.service-name)。
// RequestMapping 对应 provider 的具体映射
@FeignClient(value = "consul-service-producer")
public interface IUserService {
    @RequestMapping("/hello")// 这里地址就是provider里面controller中方法的地址
    public String hello();
}
2.5在消费者controller使用IUserService接口进行服务调用
@RestController
public class TestController {

    @Resource
    private IUserService iUserService;

    @RequestMapping("/aa")
    public  Object aa(){
        String hello = iUserService.hello();
        return hello;
    }
}
2.6访问

http://localhost:8503/aa

在这里插入图片描述

Logo

更多推荐