亚洲AVI,黑人巨茎大战欧美白妇,初高中生洗澡自慰高清网站,欧美日韩无砖专区一中文字

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)

Springcloudconfig集成的示例分析-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司主營(yíng)拜城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件開(kāi)發(fā)公司,拜城h5成都微信小程序搭建,拜城網(wǎng)站營(yíng)銷推廣歡迎拜城等地區(qū)企業(yè)咨詢

Spring Cloud Config 分為

  • Config Server:

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

  • Config Client:

    • 通過(guò)指定配置中心來(lái)管理應(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)添加依賴


  org.springframework.boot
  spring-boot-starter-web



  org.springframework.cloud
  spring-cloud-config-server


  org.springframework.cloud
  spring-cloud-starter-consul-discovery

(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=我是來(lái)自配置中心的
(3)修改啟動(dòng)類

添加注解@EnableConfigServer開(kāi)啟配置服務(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

瀏覽器訪問(wèn) 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": "我是來(lái)自配置中心的"
   }
  }
 ]
}

說(shuō)明:

配置中心的配置文件會(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)添加依賴


 org.springframework.boot
 spring-boot-starter-web


 org.springframework.cloud
 spring-cloud-starter-consul-discovery


 org.springframework.cloud
 spring-cloud-starter-config

(2)配置

添加配置bootstrap.properties

通過(guò)注冊(cè)中心的發(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,如果無(wú)法連接config server,啟動(dòng)時(shí)會(huì)拋異常,并停止服務(wù)
spring.cloud.config.fail-fast=true

(3)測(cè)試方法

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.測(cè)試

啟動(dòng)easy-api

測(cè)試地址

http://localhost:8083/

返回結(jié)果

hello,easy-api,我是來(lái)自配置中心的

4.更新

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

(1)修改測(cè)試方法

添加注解 @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

測(cè)試地址

http://localhost:8083/

返回結(jié)果

hello,easy-api,我是來(lái)自配置中心的

(2)修改配置

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

hello-string=我是來(lái)自配置中心的111

重啟easy-config

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

輸出

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

輸出

hello,esay-api,我是來(lái)自配置中心的111

說(shuō)明:

  • 如果出現(xiàn)Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available

  • 無(wú)論在 Config Server 中配置什么端口,Config Client 啟動(dòng)時(shí),會(huì)去訪問(wèn)都默認(rèn)的 8888 端口

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

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

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。


網(wǎng)頁(yè)名稱:Springcloudconfig集成的示例分析-創(chuàng)新互聯(lián)
鏈接分享:http://news.spvevtbd.cn/article/ppjcs.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP