zoukankan      html  css  js  c++  java
  • SpringCloud zookeeper和consul 的介绍和基本搭建

    1. Spring Cloud 集成 zookeeper

    Spring Cloud 集成了 zookeeper ,通过一些简单的注释,就可以快速注册到zookeeper 服务中
    并且支持Spring Cloud LoadBalancer ,Spring Cloud OpenFeign 等 服务调用 负载均衡方案

    zookeeper官网: https://zookeeper.apache.org/

    springcloud-zookeeper文档:https://spring.io/projects/spring-cloud-zookeeper

    pom依赖: 需要将默认集成的 zookeeper 客户端排除 并导入你的zookeeper服务的版本对应的客户端

     <dependencies>
           
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
           
            
            <!--   引入zookeeper客户端     -->
            <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.9</version>
            </dependency>
        </dependencies>
    

    yml配置

    server:
      port: 8004
    
    # 服务别名---zookeeper注册中心名称
    spring:
      application:
        name: cloud-provider-payment
      cloud:
        zookeeper:
          connect-string: 192.168.113.6:2181 #指定zookeeper服务的地址
    

    提供服务controller

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Value("${server.port}")
        private String serverPort;
    
        @RequestMapping(value = "/payment/zk")
        public String paymentzk(){
            return "springcloud with zookeeper: " + serverPort;
        }
    }
    

    主启动类:

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

    服务提供方已经注册进zookeeper

    消费方:

    pom依赖和yaml文件,主启动类 参考 提供方 ,都是为一个客户端注册进zookeeper

    使用RestTemplate 调用服务

    @Configuration
    public class RestTemplateConfig {
    
        @Bean
        @LoadBalanced
        public RestTemplate initRestTemplate(){
            return new RestTemplate();
        }
    }
    

    消费方消费接口

    @RestController
    @Slf4j
    public class OrderController {
    
        private static final String INVOKE_URL = "http://cloud-provider-payment";
    
        @Resource
        private RestTemplate restTemplate;
    
        @GetMapping(value = "/consumer/payment/zk")
        public String paymentInfo(){
            String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
            return result;
        }
    }
    

    调用消费接口http://127.0.0.1/consumer/payment/zk,成功调用服务,打印springcloud with zookeeper: 8004

    2. Spring Cloud 集成 consul

    consul也是一个注册中心, 并且也被spring-cloud集成, consul的注册中心和zookeeper一样 无须自己搭建,从网上下载即可, 并且有可视化的web界面

    consul官网: https://www.consul.io/intro/index.html

    springcloud-consul文档: https://www.springcloud.cc/spring-cloud-consul.html

    服务提供方:

    pom:

      <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>
           
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    

    yaml:

    server:
      port: 8006
    
    
    spring:
      application:
        name: consul-provider-payment
      cloud:
        consul:
          host: localhost
          port: 8500
          discovery:
            service-name: ${spring.application.name}
    

    主启动:

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

    3. eureka, zookeeper,consul 对cap 的支持

    • C:Consistency(强一致性)

    • A:Availability(可用性)

    • P:Partition tolerance(分区容错)

    Eureka因为默认的自我保护机制,可以保证服务节点不会被立即剔除,保证高并发情况下的可用性 所以为AP

    consul 和 zookeeper 都是检测到不健康的服务节点立即剔除 保证了一致性 所以为 CP

  • 相关阅读:
    luogu3242 接水果 (整体二分+树状数组)
    [BZOJ3449] [Usaco2014 Feb]Secret Code
    [BZOJ2821] 作诗(Poetize)
    [BZOJ2434] [Noi2011]阿狸的打字机
    [BZOJ1212] [HNOI2004]L语言
    [JZOJ100026]【NOIP2017提高A组模拟7.7】图
    [BZOJ2467] [中山市选2010]生成树
    [Luogu3868] [TJOI2009]猜数字
    [POJ1006] Biorhythms
    [BZOJ2733] [HNOI2012]永无乡
  • 原文地址:https://www.cnblogs.com/xjwhaha/p/14000408.html
Copyright © 2011-2022 走看看