• SpringCloud微服务(三)——Zookeeper服务注册中心


    Zookeeper服务注册中心

    SpringCloud

    zookeeper中的节点是持久还是临时的?

    :临时的。

    服务挂了,服务节点就没了。

    安装Zookeeper

    有win版

    docker pull zookeeper3.5.2
    
    docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:latest
    
    # 进入容器
    docker exec -it id /bin/bash
    
    # 进到services
    cd services
    ls
    #可查看服务的名字
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    不需要server端:

    服务提供者

    依赖:zookeeper-discovery是基于SpringCloud依赖的

    
    <zookeeper.version>3.4.14zookeeper.version>
    
    <dependency>
        <groupId>org.apache.zookeepergroupId>
        <artifactId>zookeeperartifactId>
        <version>${zookeeper.version}version>
    dependency>
    
    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
        <exclusions>
            
            <exclusion>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
            exclusion>
        exclusions>
    dependency>
    
    
    <dependency>
        <groupId>org.apache.zookeepergroupId>
        <artifactId>zookeeperartifactId>
        <exclusions>
            
            <exclusion>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
            exclusion>
        exclusions>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    配置yml文件

    server:
      # 8004表示注册到zookeeper服务器的支付服务提供者端口号
      port: 8004
    
    spring:
      application:
        # 服务别名---注册zookeeper到注册中心的名称
        name: cloud-provider-payment
      cloud:
        zookeeper:
          # 默认localhost:2181
          connect-string: localhost:2181
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    启动类添加**@EnableDiscoveryClient**,该注解用于向使用consul或者zookeeper作为注册中心时注册服务

    服务消费者

    依赖配置一样,消费者使用RestTemplate调用服务提供者接口,@LoadBalanced实现集群负载均衡,轮询。

    /**
     * @author zzyy
     * @date 2020-02-18 17:27
     **/
    @Configuration
    public class ApplicationContextConfig {
        @Bean
        @LoadBalanced
        public RestTemplate getRestTemplate() {
            return new RestTemplate();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    /**
     * @author zzyy
     * @create 2020-02-18 17:23
     **/
    @RestController
    @Slf4j
    public class OrderZkController {
    
        public static final String INVOKE_URL = "http://cloud-provider-payment"; //提供者服务名字,前提是消费者和提供者都要注册到注册中心
    
        @Resource
        private RestTemplate restTemplate;
    
    
        /**
         * http://localhost/consumer/payment/zk
         *
         * @return
         */
        @GetMapping("/consumer/payment/zk")
        public String paymentInfo() {
            return restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    集群搭建

    注册中心集群:connect-string: localhost:2181,ip1:2181,ip2:2181

    服务集群保证服务名一致即可。

    server:
      # 8004表示注册到zookeeper服务器的支付服务提供者端口号
      port: 8004
    
    spring:
      application:
        # 服务别名---注册zookeeper到注册中心的名称
        name: cloud-provider-payment
      cloud:
        zookeeper:
          # 默认localhost:2181
          connect-string: localhost:2181,ip1:2181,ip2:2181
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    已经安装tensorflow,仍报错No module named ‘tensorflow‘
    经典卷积神经网络 - AlexNet
    C#里的var和dynamic区别到底是什么,你真的搞懂了嘛
    zookeeper核心源码分析
    腾讯云CVM标准型S5性能如何?CPU采用什么型号?
    Vite创建Vue2项目中,封装svg-icon组件并使用——插件之vite-plugin-svg-icons和fast-glob
    css:过渡transition 、转换transform、动画animation
    湾区新势力 智创大未来,数说故事大湾区总部一周年暨琴澳战略发布会成功举办
    PROFINET非周期读写分析笔记
    肠道微生物在天然产物生物转化中的潜在作用
  • 原文地址:https://blog.csdn.net/qq_43409401/article/details/127951099