最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring cloud config集成過程詳解

 更新時間:2019年12月02日 09:42:07   作者:慕塵  
這篇文章主要介紹了spring cloud config集成過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了spring cloud config集成過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Spring Cloud Config 分為

  • Config Server:
    • 分布式配置中心,是一個獨立的微服務應用,用來連接配置服務器并為客戶端提供獲取配置信息
  • Config Client:
    • 通過指定配置中心來管理應用資源,以及與業(yè)務相關的配置內(nèi)容,并在啟動的時候從配置中心獲取和加載配置信息

Spring boot版本2.1.8.RELEASE

服務中心使用Consu,啟動Consu

1.配置中心(服務端)

easy-config

(1)添加依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--配置中心-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

(2)配置

  在resources下

  A. 添加 bootstrap.properties

  spring.profiles.active=native本地存儲配置方式

  也可以使用git方式

server.port=8091
spring.application.name=easy-config
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config/

  B. 添加config/easy-api-dev.properties

hello-string=我是來自配置中心的
(3)修改啟動類

添加注解@EnableConfigServer開啟配置服務支持

package com.tydt.easy.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class EasyConfigApplication {

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

}

啟動easy-config

瀏覽器訪問 http://localhost:8091/easy-api/dev

返回結果

{
 "name": "easy-api",
 "profiles": [
  "dev"
 ],
 "label": null,
 "version": null,
 "state": null,
 "propertySources": [
  {
   "name": "classpath:/config/easy-api-dev.properties",
   "source": {
    "hello-string": "我是來自配置中心的"
   }
  }
 ]
}

說明:

  配置中心的配置文件會被轉化成相應的web接口

  •   /{application}/{profile}[/{label}]
  •   /{application}/{profile}.yml
  •   /{label}/{application}-{profile}.yml
  •   /{application}/{profile}.properties
  •   /{label}/{application}-{profile}.properties

2.客戶端

easy-api

(1)添加依賴

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

(2)配置

  添加配置bootstrap.properties

  通過注冊中心的發(fā)現(xiàn)服務,去配置中心查找配置

server.port=8083
spring.application.name=easy-api
spring.profiles.active=dev
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.heartbeat.enabled=true

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=easy-config
#設為true,如果無法連接config server,啟動時會拋異常,并停止服務
spring.cloud.config.fail-fast=true

(3)測試方法

package com.tydt.engine.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
 @Value("${hello-string}")
 private String helloString;
 @RequestMapping("/")
 public String Hello(){
  return "hello,easy-api,"+helloString;
 }
}

3.測試

啟動easy-api

測試地址

  http://localhost:8083/

返回結果

  hello,easy-api,我是來自配置中心的

4.更新

修改了配置中心的配置后,如何讀取到新的配置呢

(1)修改測試方法

添加注解 @RefreshScope

package com.tydt.easy.api.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class HelloController {
 @Value("${hello-string}")
 private String helloString;
 @RequestMapping("/")
 public String Hello(){
  return "hello,easy-api,"+helloString;
 }
}

啟動easy-config

啟動easy-api

測試地址

  http://localhost:8083/

返回結果

  hello,easy-api,我是來自配置中心的

(2)修改配置

easy-config的resources/config/easy-api-dev.properties

hello-string=我是來自配置中心的111

重啟easy-config

執(zhí)行http://localhost:8083/actuator/refresh

輸出

[
 "hello-string"
]
http://localhost:8083/

輸出

hello,esay-api,我是來自配置中心的111

說明:

  •   如果出現(xiàn)Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
  •   無論在 Config Server 中配置什么端口,Config Client 啟動時,會去訪問都默認的 8888 端口
  •   出現(xiàn)這種情況可以刪掉以前的配置文件
  •   在resources文件夾下,新建 bootstrap.properties 文件( bootstrap.yml)

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

相關文章

  • 使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決

    使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決

    這篇文章主要介紹了使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Spring?component-scan?XML配置與@ComponentScan注解配置

    Spring?component-scan?XML配置與@ComponentScan注解配置

    這篇文章主要介紹了Spring?component-scan?XML配置與@ComponentScan注解配置,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • 詳解Java的日期時間新特性

    詳解Java的日期時間新特性

    隨著時間的不斷推移,現(xiàn)實的需求也在不斷更新,原先的一些API已經(jīng)難以滿足開發(fā)需求了,從JDK?8之后,為了滿足更多的開發(fā)需求,Java給我們增加了不少關于日期時間的新特性,接下來就帶各位來看看這些新特性有哪些,需要的朋友可以參考下
    2023-06-06
  • Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析

    Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析

    這篇文章主要介紹了Java實現(xiàn)簡易生產(chǎn)者消費者模型過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟

    IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟

    這篇文章主要介紹了IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟,首先安裝GenerateSerialVersionUID插件,當出現(xiàn)添加serialVersionUID選項,選中則會自動生成serialVersionUID,感興趣的朋友一起學習下吧
    2024-02-02
  • IDEA修改SVN地址的實現(xiàn)

    IDEA修改SVN地址的實現(xiàn)

    如果你正在使用SVN進行版本控制,并且需要更改你的SVN地址,那么這篇文章將為你提供必要的步驟和指導,感興趣的可以了解一下
    2023-12-12
  • Maven的幾個常用plugin

    Maven的幾個常用plugin

    本文主要介紹了Maven的幾個常用plugin。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • 使用spring?security?BCryptPasswordEncoder接入系統(tǒng)

    使用spring?security?BCryptPasswordEncoder接入系統(tǒng)

    這篇文章主要介紹了使用spring?security?BCryptPasswordEncoder接入系統(tǒng)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 在IntelliJ IDEA中為自己設計的類庫生成JavaDoc的方法示例

    在IntelliJ IDEA中為自己設計的類庫生成JavaDoc的方法示例

    這篇文章主要介紹了在IntelliJ IDEA中為自己設計的類庫生成JavaDoc的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • SwiftUI中級List如何添加新內(nèi)容(2020年教程)

    SwiftUI中級List如何添加新內(nèi)容(2020年教程)

    這篇文章主要介紹了SwiftUI中級List如何添加新內(nèi)容,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01

最新評論

澄迈县| 鄱阳县| 曲阜市| 汉川市| 都安| 石林| 泰和县| 济宁市| 双峰县| 大埔区| 桓台县| 南宁市| 永丰县| 静宁县| 赤城县| 濉溪县| 宜川县| 无棣县| 福清市| 乌鲁木齐市| 巢湖市| 平江县| 和平县| 黄石市| 郸城县| 扎囊县| 大方县| 楚雄市| 南江县| 清涧县| 广水市| 林口县| 宁津县| 和平县| 屏南县| 隆昌县| 当雄县| 全南县| 乌审旗| 卓资县| 交城县|