当前位置 博文首页 > 蜗牛为梦想而生H:04-注册中心之Consul

    蜗牛为梦想而生H:04-注册中心之Consul

    作者:[db:作者] 时间:2021-09-07 19:16

    1. 问题描述

    eureka学习了那么多,但是eureka已经停止维护了,springCloud在Greenwich将netflix的一些组件改成了维护模式 (维护模式意味着这些组件不会有太大的更新了 只会修改大的bug以及安全问题),当然目前来看可以使用,但是从长远的角度考虑很显然不够优雅 ? 注意: 注意consoul不属于netflix家族的组件 而是另外一个对SpringCloud规范的一个实现
    

    2. Consul能做什么

    服务注册发现: Consul客户能够注册一个服务,比如api或mysql,其他客户可以在Consul上查询一个指定服务的提供者。Consul提供DNS和HTTP的服务发现接口。 ? 
    
    健康检查: Consul可以灵活的使用脚本等来检测注册在其上的服务是否可用,不健康的服务Consul也能够灵活处理,比如提供服务的主机内存使用超过90%,我们可以配置让Consul不要把这样的服务提供给服务调用者。 ? 
    
    key/value存储: 这个功能和etcd有些类似,可以通过HTTP API方便地使用。
     ? 
    多数据中心支持: Consul支持开箱即用的多数据中心支持,这意味着用户不用建立额外的抽象层让业务扩展到各个区域。
    

    3. consul的官网下载

    ?

    • 注意点

      consul和eureka都可以做注册中心,但是eureka的注册中心其实就是一个工程(我们只需要添加服务端的依赖即可),但是consul的使用必须要安装后 才可以使用
    • 下载解压

    ?

    ?

    • 启动和关闭

      ## 启动命令
      consul agent -dev
      ?
      ## 关闭命令
      consul leave
    • 启动测试

      访问地址

    ?

    ?

    4. 接入consul

    4.1. 新建consul-provider

    ?

    4.2. 添加依赖

    <dependencies>
     ? ? ? ?<!--监控可选 可以不加-->
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
     ? ? ? ? ? ?<artifactId>spring-boot-starter-actuator</artifactId>
     ? ? ? ?</dependency>
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
     ? ? ? ? ? ?<artifactId>spring-boot-starter-web</artifactId>
     ? ? ? ?</dependency>
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.cloud</groupId>
     ? ? ? ? ? ?<artifactId>spring-cloud-starter-consul-discovery</artifactId>
     ? ? ? ?</dependency>
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>com.cloud.cn</groupId>
     ? ? ? ? ? ?<artifactId>cloud-entity</artifactId>
     ? ? ? ? ? ?<version>1.0-SNAPSHOT</version>
     ? ? ? ?</dependency>
     ? ?</dependencies>

    4.3. 编写主配置类

    ?

    4.4. 编写控制器

    ?

    4.5. 编写配置文件

    ?

    4.6. 启动测试

    ?

    4.7. 新建consul-consumer

    ?

    4.8. 添加依赖

    <dependencies>
     ? ? ? ?<!--监控可选 可以不加-->
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
     ? ? ? ? ? ?<artifactId>spring-boot-starter-actuator</artifactId>
     ? ? ? ?</dependency>
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
     ? ? ? ? ? ?<artifactId>spring-boot-starter-web</artifactId>
     ? ? ? ?</dependency>
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>org.springframework.cloud</groupId>
     ? ? ? ? ? ?<artifactId>spring-cloud-starter-consul-discovery</artifactId>
     ? ? ? ?</dependency>
     ? ? ? ?<dependency>
     ? ? ? ? ? ?<groupId>com.cloud.cn</groupId>
     ? ? ? ? ? ?<artifactId>cloud-entity</artifactId>
     ? ? ? ? ? ?<version>1.0-SNAPSHOT</version>
     ? ? ? ?</dependency>
     ? ?</dependencies>

    4.9. 编写主配置类

    ?

    4.10. 编写控制器

    ?

    4.11. 编写配置文件

    ?

    4.12. 启动测试

    ?

    4.13. 远程调用测试

    ?

    4.14. 集群搭建

    集群搭建

    cs
    下一篇:没有了