Springcloudconfig集成的示例分析

這篇文章主要介紹了Spring cloud config集成的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

在相城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都營銷網(wǎng)站建設(shè),外貿(mào)網(wǎng)站制作,相城網(wǎng)站建設(shè)費(fèi)用合理。

Spring Cloud Config 分為

  • Config Server:

    • 分布式配置中心,是一個(gè)獨(dú)立的微服務(wù)應(yīng)用,用來連接配置服務(wù)器并為客戶端提供獲取配置信息

  • Config Client:

    • 通過指定配置中心來管理應(yīng)用資源,以及與業(yè)務(wù)相關(guān)的配置內(nèi)容,并在啟動(dòng)的時(shí)候從配置中心獲取和加載配置信息

Spring boot版本2.1.8.RELEASE

服務(wù)中心使用Consu,啟動(dòng)Consu

1.配置中心(服務(wù)端)

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本地存儲(chǔ)配置方式

也可以使用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)修改啟動(dòng)類

添加注解@EnableConfigServer開啟配置服務(wù)支持

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);
 }

}

啟動(dòng)easy-config

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

返回結(jié)果

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

說明:

配置中心的配置文件會(huì)被轉(zhuǎn)化成相應(yīng)的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)服務(wù),去配置中心查找配置

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
#設(shè)為true,如果無法連接config server,啟動(dòng)時(shí)會(huì)拋異常,并停止服務(wù)
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.測試

啟動(dòng)easy-api

測試地址

http://localhost:8083/

返回結(jié)果

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;
 }
}

啟動(dòng)easy-config

啟動(dòng)easy-api

測試地址

http://localhost:8083/

返回結(jié)果

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 啟動(dòng)時(shí),會(huì)去訪問都默認(rèn)的 8888 端口

  • 出現(xiàn)這種情況可以刪掉以前的配置文件

  • 在resources文件夾下,新建 bootstrap.properties 文件( bootstrap.yml)

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Spring cloud config集成的示例分析”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

網(wǎng)站標(biāo)題:Springcloudconfig集成的示例分析
路徑分享:http://muchs.cn/article30/ghijso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站內(nèi)鏈、電子商務(wù)品牌網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)