亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747


Zookeeper作為配置中心使用說明

 

為了保證數(shù)據(jù)高可用,那么我們采用Zookeeper作為配置中心來保存數(shù)據(jù)。SpringCloud對(duì)Zookeeper的集成官方也有說明:https://cloud.spring.io/spring-cloud-static/Greenwich.SR3/single/spring-cloud.html#_spring_cloud_zookeeper

這里通過實(shí)踐的方式講解下使用方式。

1、添加依賴包

<!-- 運(yùn)維監(jiān)控 -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Web 應(yīng)用程序-->
<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>

配置說明:

org.springframework.cloud#spring-cloud-starter-zookeeper-config 是zookeeper作為配置中心的配置項(xiàng)目。

若是web工程需要添加 org.springframework.boot#spring-boot-starter-web。

配置項(xiàng)目 org.springframework.boot#spring-boot-starter-actuator 是用來與zookeeper通信使用的

2、添加配置文件

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

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

#ZooKeeper的連接字符串,如果是集群,逗號(hào)分隔節(jié)點(diǎn),格式: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 = :

配置說明:

請(qǐng)修改 192.168.0.1:2181 為項(xiàng)目中的Zookeeper地址,默認(rèn)是localhost:2181

根據(jù)以上配置,讀取zookeeper中的地址為: /config/config-demo:dev

登錄zookeeper需要手動(dòng)創(chuàng)建,或使用zkui來界面維護(hù)

3、在Zookeeper中手動(dòng)創(chuàng)建配置

示例:

[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

4、程序中使用

1)創(chuàng)建配置文件

示例: 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 特性,標(biāo)記類為配置文件

2)激活自動(dòng)裝配

在啟動(dòng)類激活配置文件

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)程序調(diào)用配置

示例:調(diào)用配置中心的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();
 }
}

5、界面式維護(hù)

界面采用的zkui的部署,針對(duì)zkui的說明請(qǐng)移步zkui :https://github.com/DeemOpen/zkui還有本地zk可視化界面 ZooViewer,針對(duì)ZooViewer的使用說明請(qǐng)移步: https://github.com/HelloKittyNII/ZooViewer

本文由博客一文多發(fā)平臺(tái) https://openwrite.cn?from=article_bottom 發(fā)布!

分享到:
標(biāo)簽:Zookeeper
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定