zoukankan      html  css  js  c++  java
  • SpringCloud服务发现Eureka、zookeeper、consul

    1、版本选择

    https://start.spring.io/actuator/info

     {
     "git": {
     "branch": "72506972926758dc1808eaa450281274ee85459a",
     "commit": {
     "id": "7250697",
     "time": "2020-08-17T15:37:11Z"
     }
     },
     "build": {
     "version": "0.0.1-SNAPSHOT",
     "artifact": "start-site",
     "versions": {
     "spring-boot": "2.3.3.RELEASE",
     "initializr": "0.9.2-SNAPSHOT"
     },
     "name": "start.spring.io website",
     "time": "2020-08-17T15:38:19.523Z",
     "group": "io.spring.start"
     },
     "bom-ranges": {
     "azure": {
     "2.0.10": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
     "2.1.10": "Spring Boot >=2.1.0.RELEASE and <2.2.0.M1",
     "2.2.4": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
     "2.3.1": "Spring Boot >=2.3.0.M1"
     },
     "codecentric-spring-boot-admin": {
     "2.0.6": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
     "2.1.6": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
     "2.2.4": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
     "2.3.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1"
     },
     "solace-spring-boot": {
     "1.0.0": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1",
     "1.1.0": "Spring Boot >=2.3.0.M1"
     },
     "solace-spring-cloud": {
     "1.0.0": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1",
     "1.1.1": "Spring Boot >=2.3.0.M1"
     },
     "spring-cloud": {
     "Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5",
     "Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5",
     "Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6",
     "Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7",
     "Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
     "Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
     "Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE",
     "Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE",
     "Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE",
     "Finchley.SR4": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT",
     "Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3",
     "Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
     "Greenwich.SR6": "Spring Boot >=2.1.0.RELEASE and <2.1.17.BUILD-SNAPSHOT",
     "Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.17.BUILD-SNAPSHOT and <2.2.0.M4",
     "Hoxton.SR7": "Spring Boot >=2.2.0.M4 and <2.3.4.BUILD-SNAPSHOT",
     "Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.3.4.BUILD-SNAPSHOT and <2.4.0.M1",
     "2020.0.0-SNAPSHOT": "Spring Boot >=2.4.0.M1"
     },
     "spring-cloud-alibaba": {
     "2.2.1.RELEASE": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1"
     },
     "spring-cloud-services": {
     "2.0.3.RELEASE": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
     "2.1.7.RELEASE": "Spring Boot >=2.1.0.RELEASE and <2.2.0.RELEASE",
     "2.2.3.RELEASE": "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1"
     },
     "spring-statemachine": {
     "2.0.0.M4": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
     "2.0.0.M5": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
     "2.0.1.RELEASE": "Spring Boot >=2.0.0.RELEASE"
     },
     "vaadin": {
     "10.0.17": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
     "14.3.3": "Spring Boot >=2.1.0.M1 and <2.4.0-M1"
     },
     "wavefront": {
     "2.0.0": "Spring Boot >=2.1.0.RELEASE"
     }
     },
     "dependency-ranges": {
     "okta": {
     "1.2.1": "Spring Boot >=2.1.2.RELEASE and <2.2.0.M1",
     "1.4.0": "Spring Boot >=2.2.0.M1 and <2.4.0-M1"
     },
     "mybatis": {
     "2.0.1": "Spring Boot >=2.0.0.RELEASE and <2.1.0.RELEASE",
     "2.1.3": "Spring Boot >=2.1.0.RELEASE and <2.4.0-M1"
     },
     "geode": {
     "1.2.9.RELEASE": "Spring Boot >=2.2.0.M5 and <2.3.0.M1",
     "1.3.2.RELEASE": "Spring Boot >=2.3.0.M1 and <2.4.0-M1",
     "1.4.0-M1": "Spring Boot >=2.4.0-M1"
     },
     "camel": {
     "2.22.4": "Spring Boot >=2.0.0.M1 and <2.1.0.M1",
     "2.25.2": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
     "3.3.0": "Spring Boot >=2.2.0.M1 and <2.3.0.M1",
     "3.4.3": "Spring Boot >=2.3.0.M1 and <2.4.0-M1"
     },
     "open-service-broker": {
     "2.1.3.RELEASE": "Spring Boot >=2.0.0.RELEASE and <2.1.0.M1",
     "3.0.4.RELEASE": "Spring Boot >=2.1.0.M1 and <2.2.0.M1",
     "3.1.1.RELEASE": "Spring Boot >=2.2.0.M1 and <2.4.0-M1"
     }
     }
     }
     

    image-20200820145837517

    2月份最近,版本选择依据官网!

    2、停更组件

    停更不停用:被动修复bugs,不再接受合并请求,不再发布新版本。停课不停学,666

    以前:

    image-20200820150209100

    目前:cloud升级

    image-20200820151257339

    3、父工厂Project空间

    步骤:

    image-20200820164525170

    走起

    dependencyManagement和dependency的区别

    dependencyManagement是父pom。

    能让所有子项目中引用一个依赖而不是显示的列出版本号,Maven为沿着父子层次向上走,知道找到一个拥有dependencyManagement元素的项目,然后它会使用这个dependencyManagement元素中指定的版本号。

    好处:

    如果多个项目子项目都引用同一个依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当你想升级或者切换到另外一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另外一个版本,只需要声明version即可。

    dependencyManagement 只是声明依赖,而不实现引入,因此子项目需要显示的声明需要的依赖,也就是dependency。

    如果不在子项目声明依赖,是不会从父项目中继承下来的。只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom。

    如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

    如何跳过test单元测试

    image-20200820174529419

     

    点击这个按钮即可。

     
     <?xml version="1.0" encoding="UTF-8"?>
     
     <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>com.fage</groupId>
        <artifactId>springclouddemo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>pom</packaging>
     
     
        <name>发哥</name>
     
        <!-- 同意管理jar包版本 -->
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <junit.version>4.12</junit.version>
            <log4j.version>1.2.17</log4j.version>
            <lombok.version>1.16.18</lombok.version>
            <mysql.version>5.1.47</mysql.version>
            <druid.version>1.1.16</druid.version>
            <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
        </properties>
     
        <!-- 子模块继承之后,提供作用:锁定版本+子module不用谢groupId和version -->
        <dependencyManagement>
            <dependencies>
                <!-- spring-boot 2.2.2 -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.2.2.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!-- spring-cloud Hoxton.SR7 spring cloud 是伦敦 地铁站 从 a到z -->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR7</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!-- spring-cloud-alibaba 2.1.0-->
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.1.0.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>${druid.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>${mybatis.spring.boot.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
     
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
     
     </project>
     

    4、构建支付模块

    image-20200820180328484

    1、cloud-provider-payment8001

    微服务提供支付的module模块

    image-20200820205722751

     

    微服务模块:

    1、建module

    2、改pom

    3、写yml

    4、主启动

    5、业务类

    编写业务:

    image-20200820212524435

    一步一步走

    idea运行:

    image-20200820223630764

    设置为run dashBoard 模式

    在 idea目录下 workspace.xml末尾增加:

         <component name="RunDashboard">
             <option name="ruleStates">
                 <list>
                     <RuleState>
                         <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
                     </RuleState>
                     <RuleState>
                         <option name="name" value="StatusDashboardGroupingRule" />
                     </RuleState>
                 </list>
             </option>
             <option name="contentProportion" value="0.22874807" />
             <option name="configurationTypes">
                 <set>
                     <option value="SpringBootApplicationConfigurationType" />
                 </set>
             </option>
         </component>
     

    测试:

    image-20200820224230975

    开启热部署:

    image-20200820224427996

    1、pom依赖

             <!-- 热部署 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>

    2、pom插件

         <build>
            <plugins>
                <!-- 配置热部署 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    3、设置idea编译检测功能

    image-20200820224645851

    3、设置idea功能

    ctrl+shift+alt+/

    image-20200820224902837

    设置image-20200820225114242

    image-20200820225153596

    5、重启即可

    5、构建消费者订单模块

    cloud-consumer-order80

    RestTemplate 提供了多种便捷访问远程http服务的方法。

    和httpClient和webservice同样的操作

    使用:

    image-20200820231140915

    config配置类:

         @Bean
         public RestTemplate restTemplate() {
             return new RestTemplate();
        }

    代码重构

    将冗余的代码重组,通过依赖使用。

    新建 cloud-api-commons模块

    6、服务发现

    服务注册中心:

    CAP理论:

    为甚需要注册中心:

    比如:服务生产者多个服务,ip和端口

    在服务调用者调用生产者接口不方便

    1、Eureka

    image-20200821100246476

    避免单点故障,使用集群方式。

    image-20200821101027409

    实践

    单机Eureka服务端

    a:新建模块 cloud-eureka-server7001

    b:改pom

             <!--eureka-server-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
     <!--       <dependency>-->
     <!--           <groupId>org.springframework.cloud</groupId>-->
     <!--           <artifactId>spring-cloud-netflix-eureka-server</artifactId>-->
     <!--           <version>${spring.boot.version}</version>-->
     <!--       </dependency>-->

     

    image-20200821101424914

    c: 配置文件

     server:
      port: 7001
     
     eureka:
      instance:
        hostname: localhost # # eureka服务端的实例名称
      client:
         # false 表示 不会向注册中心 注册自己
        register-with-eureka: false
         # false 表示自己端 就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
        fetch-registry: false
        service-url:
           # 设置与eureka server 交互的地址查询服务 和 注册服务都需要依赖这个地址
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
     

    d:启动类

    e:测试

    http://localhost:7001/

    调整cloud-provider-payment8001的服务,修改为eureka的client

    pom修改

    image-20200821111747460

    启动类增加

    @EnableEurekaClient

    刷新 7001服务端口页面

    红色EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.是Eureka 的自我保护机制。

    出错:

    image-20200821113050279

    image-20200821113043326

    yml对文件缩进要求严格。

    集群改进

    实现高可用(避免单点故障)

    image-20200821113239233

    步骤如上

    工作流程:

    image-20200821113316759

    搭建 Eureka注册中心集群,实现负载均衡+故障容错。

    互相注册,互相守望

    image-20200821122130126

    客户端访问集群

    service-url: defaultZone: 使用逗号隔开

    hosts文件修改

     C:WindowsSystem32driversetc
     127.0.0.1 eureka7001.com
     127.0.0.1 eureka7002.com
     127.0.0.1 eureka7003.com

    结果

    image-20200821125107988

    调用接口:http://localhost/consumer/payment/get/6

    image-20200821125127076

    完美

    给服务提供者做集群。

    新建cloud-provider-payment8002模块

    使用负载均衡:

    ribbon 和 eureka 整合使用

    1、RestTemplate配置增加

    轮询机制:

    @LoadBalanced

    2、路径使用应用名称(spring.application.name)

    cloud-payment-service

    actuator(图形化显示)

    image-20200821132745594

    去掉 主机名称,和ip显示问题

    yml增加:

    eureka:
      instance:
        ## 主机名称 
        instance-id: payment8001
        ## 浏览器上 浮动名称 左下角显示 ip
        prefer-ip-address: true

    查看健康状态:

    http://localhost:8001/actuator/health

    服务发现Discovery

    image-20200821133810043

    白话就是将注册到eureka的客户端的服务显示一下。

    启动类增加

    @EnableDiscoveryClient

    增加controller方法;

    @Resource
    private DiscoveryClient discoveryClient;
    
        @GetMapping(value = "/consumer/discovery")
        public CommonResult<Object> getDiscoveryClient() {
            List<String> services = discoveryClient.getServices();
            services.forEach(s -> {
                log.info("
     service = " + s);
                List<ServiceInstance> instances = discoveryClient.getInstances(s);
                instances.forEach(serviceInstance -> {
                    log.info(serviceInstance.getInstanceId() + "	" + serviceInstance.getUri());
                });
            });
            return new CommonResult<>(200, "success", discoveryClient);
        }

    Eureka 自我保护

    故障现象:

    http://eureka7001.com:7001/ 红字信息

    原因:

    某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存,属于CAP原则里面的AP分支

    高可用的一种机制

    image-20200821140552245

    image-20200821140611739

    在自我保护模式中,Eureka Server 会保护服务注册表中的信息,不再注销任何服务实例。不盲目删除,尽可能的保留实例。

    怎么禁止自我保护?

    eureka服务端

     eureka:
      server:
         # 关闭自我保护机制,保证不可用服务及时剔除
        enable-self-preservation: false
        eviction-interval-timer-in-ms: 2000
     

    eureka客户端

     eureka:
      instance:
        instance-id: order80
         ##访问路径显示ip地址
        prefer-ip-address: true
         ## Eureka 客户端向服务器发送心跳的时间间隔 ( 默认 为 30秒)
        lease-renewal-interval-in-seconds: 1
         ## Eureka 服务端在收到最后一次心跳后等待时间上线,单位为秒 (默认为90秒) ,超时将剔除该服务
        lease-expiration-duration-in-seconds: 2

    访问eureka服务页面

    显示THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

    测试: 将启动后的服务关闭,看服务页面中的实例是否消失,即可测试完成。

    Eureka停更不停用。。。就别用了

    Zookeper替换Eureka

    Zookeper是一个分布式的协调工具,可以实现注册中心,关闭Linux服务器防火墙后启动Zookeper服务器

    安装Zookeper

    docker pull zookeeper
    
    启动容器 
    docker run --privileged=true -d --name zookeeper --publish 2181:2181  -d zookeeper:latest
    
    docker ps 
    
    2020年8月21日15:26:26
    看启动日志 version  3.6.1
    
    查看容器是否启动
    docker ps
    
    进入容器
    docker exec -it f76b7e25baa9 /bin/bash
    
    进入bin目录
    cd bin
    
    登录server
    zkCli.sh -server 127.0.0.1:2181
    
    查看目录
    ls /

    搭建Zookeper服务提供者

    zookeeper版本如果高,pom中可以使用低一点的。

    如果zookeeper版本低,pom中不可以使用高版本

    1、pom

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.8</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    2、yml

    server:
      port: 8004
    
    spring:
      application:
        name: cloud-payment-service
      cloud:
        zookeeper:
          connect-string: 192.168.71.134:2181

    zookeeper服务:

    image-20200821145824365

    接口层:

        @Autowired
        private DiscoveryClient discoveryClient;
    
        @GetMapping("/payment/zk")
        public CommonResult<Object> getPaymentById() {
            List<String> services = discoveryClient.getServices();
            services.forEach(s -> {
                log.info(" service = " + s);
                List<ServiceInstance> instances = discoveryClient.getInstances(s);
                instances.forEach(serviceInstance -> {
                    log.info(serviceInstance.getInstanceId() + "	" + serviceInstance.getUri());
                });
            });
            return new CommonResult<>(200, "Spring cloud  with zookeeper ,提供者port:" + port, UUID.randomUUID().toString());
        }

    调用结果:

    2020-08-21 15:27:40.190 INFO 40864 --- [nio-8004-exec-1] c.f.s.controller.PaymentController : service = cloud-payment-service 2020-08-21 15:27:40.195 INFO 40864 --- [nio-8004-exec-1] c.f.s.controller.PaymentController : 85b4c21c-cf51-47bd-bce5-89480035ec76 http://localhost:8004

    [zk: 127.0.0.1:2181(CONNECTED) 3] ls /
    [services, zookeeper]
    [zk: 127.0.0.1:2181(CONNECTED) 4] ls /services
    [cloud-payment-service]
    [zk: 127.0.0.1:2181(CONNECTED) 5] ls /zookeeper
    [config, quota]
    [zk: 127.0.0.1:2181(CONNECTED) 6] ls /services/cloud-payment-service
    [85b4c21c-cf51-47bd-bce5-89480035ec76]
    [zk: 127.0.0.1:2181(CONNECTED) 7] ls /services/cloud-payment-service/85b4c21c-cf51-47bd-bce5-89480035ec76
    []
    [zk: 127.0.0.1:2181(CONNECTED) 8] get  /services/cloud-payment-service/85b4c21c-cf51-47bd-bce5-89480035ec76
    {"name":"cloud-payment-service","id":"85b4c21c-cf51-47bd-bce5-89480035ec76","address":"localhost","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-payment-service","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1597994727162,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
    [zk: 127.0.0.1:2181(CONNECTED) 9] 

    见到有 json字符串 即成功

    Zookeeper : 临时还是持久节点

    测试:将服务提供者关闭, 在zookeeper的zk工具查看services。

    停了之后不会直接删除。过了一段时间将其删除了(40000ms)。

    删除的序列号

    [85b4c21c-cf51-47bd-bce5-89480035ec76]

    zookeeper 里面 以序列号作为节点。~!

    消费zookeeper服务提供者模块

    cloud-consumer-zk-order80

    spring:
      application:
        name: cloud-payment-zk-service
      cloud:
        zookeeper:
          connect-string: 192.168.71.134:2181,192.168.170.131:2181
          ## 集群 使用逗号隔开就行
    @LoadBalanced
    轮训机制

    Consul

    学习 方法:是什么,能干嘛,去那下,怎么做

    理论、实操、小笔记

    是一套开源的分布式服务发现和配置管理系统。

    提供了服务治理(注册于发现)、健康检测、配置中心、KV存储、控制总线、多数据中心,可视化Web界面。

    中文文档:

    http://springcloud.cc/spring-cloud-consul.html

    1、安装consul

    # 设置yum源

    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum makecache fast

    # 安装Docker-CE

    yum -y install docker-ce

    #设置开机自启动

    systemctl enable docker 
    systemctl restart docker 

    #验证

    docker run hello-world

    #指定consul版本、这边与开发确认后获取最新版本以及单节点即可

    docker pull consul

    #期望容器中的配置文件、数据、日志再容器异常关闭或主动删除后保留,则需要配置挂载

    #容器对应配置文件路径都是从https://hub.docker.com/_/consul上对应的官网操作中确认过的

    mkdir -p /data/consul/{conf,data}

    #运行容器、映射到宿主机,挂载Volume

    docker run 
    --name consul 
    -p 8599:8500 
    -v /data/consul/conf/:/consul/conf/ 
    -v /data/consul/data/:/consul/data/ 
    -d consul

    # 不使用挂载目录

     --restart=no / always 不自动/自动开启docker容器
    
    docker run --restart=no --name consul -p 8500:8500 -d consul:latest 

    创建consul服务提供者模块

    cloud-provider-consul-payment8006

    pom依赖:

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>

    yml配置

     server:
      port: 8006
     spring:
      application:
        name: cloud-payment-consul-service
      cloud:
        consul:
          host: 192.168.170.131
          port: 8500
          discovery:
             ## 服务发现 注册
            service-name: ${spring.application.name}

    main方法

     @SpringBootApplication
     @EnableDiscoveryClient
     public class PaymentConsulApplication {
     
         public static void main(String[] args) {
             SpringApplication.run(PaymentConsulApplication.class, args);
        }
     }

    启动发现:

    docker 运行日志

     

    web页面显示

    consul启动到管理页面时,出现一个Service Checks = false

    这个是由于actuator/health无法检测,因为项目中的consul需要检测服务的健康状态。

    image-20200822122956911

    访问consul的健康检测

    image-20200822123101960

     

     

    解决

    server:
      port: 8006
    spring:
      application:
        name: cloud-payment-consul-service
      cloud:
        consul:
          host: 192.168.170.131
          port: 8500
          discovery:
            ## 服务发现 注册
            service-name: ${spring.application.name}
            enabled: true
            # 使用 consul 服务器 IP, 而不是 hostname, 需要搭配 prefer-ip-address 属性
            ip-address: 127.0.0.1
            # 在注册时使用 consul IP, 而不是 hostname
            prefer-ip-address: true
            # 制定检查的ip
            health-check-url: http://${localhost}:${server.port}/actuator/health
    ## 本地ip
    localhost: 192.168.0.4

    结果:

    image-20200822123600729

    image-20200822123628842image-20200822123629237

    没有再爆红,调用提供者服务也正常

    调用consul消费者模块

    cloud-consumer-consul-order80

    类似提供者和其他消费者。

    集群: 修改提供者端口即可。消费者使用@LoadBalanced做到轮询

    三者对比

    image-20200822125658955

    c:Consistency (强一致性)数据一致

    a:Availability (可用性)高可用

    p:Partution tolerance(分区容错性)集群

    CAP理论关注粒度是数据,而不是整体系统设计的策略。

    最多只能同时较好的满足俩个。

    image-20200822130046453

    AP Eureka :

    当网络分区出现后,为了保证可用性,系统B可以返回值,保证系统的可用性,结论:违背了一致性C的要求,只满足可用性和分区容错,即AP。

    image-20200822130245170

    CP Zookeeper/Consul :

    当网络分区出现后,为了保证一致性,就必须拒接请求,否则无法保证一致性。结论:违背了可用性A的要求,只满足一致性和分区容错,即CP。

    image-20200822130526966

     

    公众号发哥讲

    这是一个稍偏基础和偏技术的公众号,甚至其中包括一些可能阅读量很低的包含代码的技术文,不知道你是不是喜欢,期待你的关注。

    代码分享

    https://gitee.com/naimaohome/springclouddemo.git

    微信公众号 点击关于我,加入QQ群,即可获取到代码以及高级进阶视频和电子书!!

    img

    如果你觉得文章还不错,就请点击右上角选择发送给朋友或者转发到朋友圈~

    ● 扫码关注我们

    据说看到好文章不推荐的人,服务器容易宕机!

    本文版权归 发哥讲博客园 共有,原创文章,未经允许不得转载,否则保留追究法律责任的权利。

     

  • 相关阅读:
    抽屉视图第三方控件
    数组NSArry注意事项
    NSInteger 和 NSUInteger 比较大小的注意事项
    IOS应用中的二维码生成和二维码扫描解析
    FMDatabase 的使用(转)
    ASIHTTPRequest的简单使用
    关于MBProgressHUD的使用
    获取文件夹大小
    31. Next Permutation(中等,搞清楚啥是 next permutation)
    Deap Learning (吴恩达) 第一章深度学习概论 学习笔记
  • 原文地址:https://www.cnblogs.com/naimao/p/13589144.html
Copyright © 2011-2022 走看看