欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud之分布式配置中心Spring Cloud Config高可用配置實例代碼

 更新時間:2018年04月08日 10:05:35   作者:smartdt  
這篇文章主要介紹了SpringCloud之分布式配置中心Spring Cloud Config高可用配置實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、簡介

當要將配置中心部署到生產環(huán)境中時,與服務注冊中心一樣,我們也希望它是一個高可用的應用。Spring Cloud Config實現(xiàn)服務端的高可用非常簡單,主要有以下兩種方式。

傳統(tǒng)模式:不需要為這些服務端做任何額外的配置,只需要遵守一個配置規(guī)則,將所有的Config Server都指向同一個Git倉庫,這樣所有的配置內容就通過統(tǒng)一的共享文件系統(tǒng)來維護。而客戶端在指定Config Server位置時,只需要配置Config Server上層的負載均衡設備地址即可, 就如下圖所示的結構。

服務模式:除了上面這種傳統(tǒng)的實現(xiàn)模式之外,我們也可以將Config Server作為一個普通的微服務應用,納入Eureka的服務治理體系中。這樣我們的微服務應用就可以通過配置中心的服務名來獲取配置信息,這種方式比起傳統(tǒng)的實現(xiàn)模式來說更加有利于維護,因為對于服務端的負載均衡配置和客戶端的配置中心指定都通過服務治理機制一并解決了,既實現(xiàn)了高可用,也實現(xiàn)了自維護。由于這部分的實現(xiàn)需要客戶端的配合,具體示例讀者可詳細閱讀 “客戶端詳解 ”一節(jié)中的 “服務化配置中心” 小節(jié)。

二、前期準備

一個服務注冊中心,EUREKASERVER,端口為5555;

三、改造Config-Server

(1)pom.xml,添加spring-cloud-starter-eureka依賴

<dependencies> 
  <dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-config-server</artifactId> 
  </dependency> 
  <dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-eureka</artifactId> 
  </dependency> 
   
  <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-test</artifactId> 
    <scope>test</scope> 
  </dependency> 
</dependencies> 

(2)application.yml,配置參數(shù)eureka.client.serviceUrl.defaultZone以指定服務注冊中心的位置

server: 
 port: 5588 
 
spring: 
 application: 
  name: config-server 
   
eureka: 
 client: 
  serviceUrl: 
   defaultZone: http://localhost:5555/eureka/ #配置服務注冊中心 
 
 cloud: 
  config: 
   server: 
    git: 
     uri: https://gitee.com/smartdt/springcloudconfig.git #配置Git倉庫位置。 
     searchPaths: config-repo #配置倉庫路徑下的相對搜索位置,可以配置多個。 
     username: username #訪問 Git 倉庫的用戶名。 
     password: password #訪問 Git 倉庫的用戶密碼。 
     label: master #配置倉庫的分支 
     ###如果Git倉庫為公開倉庫,可以不填寫用戶名和密碼,如果是私有倉庫需要填寫。 

(3)入口類,新增@EnableDiscoveryC巨ent注解,用來將config-server注冊到上面配置的服務注冊中心上去。

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

(4)啟動config-server,通過Eureka-Server查看


四、改造Config-Client

(1)pom.xml,添加spring-cloud-starter-eureka依賴

<dependencies> 
  <dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-config</artifactId> 
  </dependency> 
  <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId> 
  </dependency> 
  <dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-eureka</artifactId> 
  </dependency> 
 
  <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-test</artifactId> 
    <scope>test</scope> 
  </dependency> 
</dependencies> 

(2)bootstrap.properties,添加配置服務中心信息

spring.application.name=configspace 
spring.cloud.config.label=master 
spring.cloud.config.profile=dev 
spring.cloud.config.uri= http://localhost:5588/ 
server.port=5589 
eureka.client.serviceUrl.defaultZone=http://localhost:5555/eureka/ 

(3)入口類,添加@EnableDiscoveryClient

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

(4)測試類不變

@RefreshScope 
@RestController 
public class ConfigController { 
 
  @Value("${from}") 
  private String from; 
  @Value("${username}") 
  private String username; 
  @Value("${password}") 
  private String password; 
 
  @RequestMapping("/from") 
  public String from() { 
    return this.from + "~user:" + this.username + "~pass:" + this.password; 
  } 
} 

(5)啟動測試,通過Eureka-Server查看



(6)瀏覽器測試,訪問http://localhost:5589/from


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • @Transactional注解:多個事務嵌套時,獨立事務處理方式

    @Transactional注解:多個事務嵌套時,獨立事務處理方式

    這篇文章主要介紹了@Transactional注解:多個事務嵌套時,獨立事務處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 淺談Spring Boot中Redis緩存還能這么用

    淺談Spring Boot中Redis緩存還能這么用

    這篇文章主要介紹了淺談Spring Boot中Redis緩存還能這么用,這種方式是Spring Cache提供的統(tǒng)一接口,實現(xiàn)既可以是Redis,也可以是Ehcache或者其他支持這種規(guī)范的緩存框架,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Java集合-HashMap

    Java集合-HashMap

    這篇文章主要介紹了Java集合HashMap,也叫散列表,是一種非常重要的數(shù)據(jù)結構,應用場景及其豐富,許多緩存技術(比如memcached)的核心其實就是在內存中維護一張大的哈希表,下面來看看文章的具體內容吧,需要的小伙伴也可參考一下
    2022-01-01
  • JAVA多線程之實現(xiàn)用戶任務排隊并預估排隊時長

    JAVA多線程之實現(xiàn)用戶任務排隊并預估排隊時長

    本文主要介紹了Java多線程之實現(xiàn)用戶任務排隊并預估排隊時長的問題,文中的代碼具有一定的學習和工作價值,感興趣的小伙伴快跟隨小編一起學習一下吧
    2021-12-12
  • springboot手動動態(tài)注入controller和service方式

    springboot手動動態(tài)注入controller和service方式

    這篇文章主要介紹了springboot手動動態(tài)注入controller和service方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • springboot之Jpa通用接口及公共方法使用示例

    springboot之Jpa通用接口及公共方法使用示例

    這篇文章主要為大家介紹了springboot?之Jpa通用接口及公共方法使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Spring手動獲取bean的四種方式

    Spring手動獲取bean的四種方式

    本文主要介紹了Spring手動獲取bean的四種方式,包括BeanFactoryPostProcessor接口,ApplicationContextAware接口,注解 @PostConstruct 初始化時獲取,啟動類ApplicationContext獲取這四種方法,感興趣的可以了解一下
    2024-01-01
  • Spring為singleton?bean注入prototype?bean

    Spring為singleton?bean注入prototype?bean

    這篇文章主要介紹了Spring為singleton?bean注入prototype?bean,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • Java之Scanner.nextLine()讀取回車的問題及解決

    Java之Scanner.nextLine()讀取回車的問題及解決

    這篇文章主要介紹了Java之Scanner.nextLine()讀取回車的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 關于Java中如何實現(xiàn)文件的讀寫操作

    關于Java中如何實現(xiàn)文件的讀寫操作

    在Java中,可以使用File和FileInputStream、FileOutputStream、BufferedReader、PrintWriter等類來進行文件讀寫操作,需要的朋友可以參考下
    2023-05-05

最新評論