Zookeeper做爲配置中心使用說明

爲了保證數據高可用,那麼咱們採用Zookeeper做爲配置中心來保存數據。SpringCloud對Zookeeper的集成官方也有說明:spring_cloud_zookeeperhtml

這裏經過實踐的方式講解下使用方式。java

一、添加依賴包

<!-- 運維監控 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Web 應用程序-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 提供zookeeper - config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
</dependency>

配置說明:git

org.springframework.cloud#spring-cloud-starter-zookeeper-config 是zookeeper做爲配置中心的配置項目。github

如果web工程須要添加 org.springframework.boot#spring-boot-starter-webweb

配置項目 org.springframework.boot#spring-boot-starter-actuator 是用來與zookeeper通訊使用的spring

二、添加配置文件

在bootstrap.properties文件下添加 如下配置bootstrap

spring.application.name=config-demo
spring.profiles.active=dev

#ZooKeeper的鏈接字符串,若是是集羣,逗號分隔節點,格式:ip:port[,ip2:port2,.....]
spring.cloud.zookeeper.connect-string = 192.168.0.1:2181
#指定zookeeper目錄的根目錄
spring.cloud.zookeeper.config.root = config
#啓用zk的配置
spring.cloud.zookeeper.config.enabled = true
spring.cloud.zookeeper.config.profileSeparator = :

配置說明:微信

請修改 192.168.0.1:2181 爲項目中的Zookeeper地址,默認是localhost:2181app

根據以上配置,讀取zookeeper中的地址爲: /config/config-demo:dev運維

登陸zookeeper須要手動建立,或使用zkui來界面維護

三、在Zookeeper中手動建立配置

示例:

[zk: localhost:2181(CONNECTED) 3] create /config/config-demo:dev
Created /config/config-demo:dev
[zk: localhost:2181(CONNECTED) 4] create /config/config-demo:dev/user.name yuesf
Created /config/config-demo:dev/user.name

四、程序中使用

1)建立配置文件

示例: UserProperties.java

package com.example.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

/*
 * @auth yuesf
 * @data 2019/10/29
 */
@ConfigurationProperties(prefix = "user")
public class UserProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

配置說明:

使用@ConfigurationProperties 特性,標記類爲配置文件

2)激活自動裝配

在啓動類激活配置文件

package com.example.config;

import com.example.config.demo.UserProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

/*
 * @auth yuesf
 * @data 2019/11/12
 */
@EnableConfigurationProperties(value = {UserProperties.class })
@SpringBootApplication
public class ConfigApplication {

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

配置說明:

上例代碼中 @EnableConfigurationProperties(UserProperties.class) 是把UserProperties.class 激活配置

3)程序調用配置

示例:調用配置中心的user.name 變量

package com.example.config.controller;

import com.example.config.demo.UserProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * @auth yuesf
 * @data 2019/11/12
 */
@RestController
public class UserController {

    @Autowired
    private UserProperties userProperties;

    @GetMapping("/user")
    public String getUser(){
        return userProperties.getName();
    }
}

五、界面式維護

界面採用的zkui的部署,針對zkui的說明請移步zkui :https://github.com/DeemOpen/zkui
還有本地zk可視化界面 ZooViewer,針對ZooViewer的使用說明請移步: https://github.com/HelloKittyNII/ZooViewer

本文由博客一文多發平臺 OpenWrite 發佈!

再次感謝!!! 您已看徹底文,歡迎關注微信公衆號猿碼 ,你的支持是我持續更新文章的動力!

相關文章
相關標籤/搜索