美文网首页
在SpringCloud中使用Zookeeper做注册中心和配置

在SpringCloud中使用Zookeeper做注册中心和配置

作者: 杰仕人生11 | 来源:发表于2018-12-14 13:10 被阅读0次

            使用zookeeper做注册中心和配置中心,是因为zookeeper的数据模型很简单,有一系列被称为znode的数据节点组成,与传统的磁盘文件系统不同的是,zk将全量数据存储在内存中,可谓是高性能、高可用,而且支持集群,另外支持事件监听。这些特点决定了zk特别适合作为注册中心(数据发布/订阅)。再加上eureka开源项目停止更新,同时在项目部署可以利用现有zookeeper集群资源,减少项目节点的部署。


    maven依赖:

    maven依赖:

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-zookeeper-config</artifactId>

    </dependency>

    <!-- zookeeper 依赖的健康检查的jar,所以需要引入actuator这个依赖 -->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

    在resources目录下添加bootstrap.properties,内容如下:

    ##启用zookeeper作为配置中心

    spring.cloud.zookeeper.config.enabled=true

    ##配置根路径

    spring.cloud.zookeeper.config.root=atomic-config

    ##配置默认上下文

    spring.cloud.zookeeper.config.defaultContext=atomic

    ##配置profile分隔符

    spring.cloud.zookeeper.config.profileSeparator=-

    同时在resources目录下添加application.yml配置内容:

    server:

      port: 4001

    spring:

      application:

        ## 配置应用名称

        name: atomic-config

      cloud:

        zookeeper:

          connect-string: localhost:2181

          ## zookeeper的services根目录

          discovery:

            root: atomic

      profiles:

        active: dev

    ## 关闭安全管理

    management:

      security:

        enabled: false

    endpoints:

      health:

        sensitive: false

    编写启动类:

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

    @EnableDiscoveryClient

    @SpringBootApplication

    public class ConfigApplication {

        public static void main(String[] args) {

            SpringApplication.run(ConfigApplication.class,args);

        }

    }

    在zookeeper中增加项目配置中心:

    create /atomic-config/atomic-dev/name "an"

    实际变量使用如下:

    @Value("${name}")

    privateStringname;

    zookeeper目录情况如下:

    相关文章

      网友评论

          本文标题:在SpringCloud中使用Zookeeper做注册中心和配置

          本文链接:https://www.haomeiwen.com/subject/kaowhqtx.html