亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解SpringCloud mysql實(shí)現(xiàn)配置中心

 更新時間:2018年09月11日 14:49:09   作者:雨點(diǎn)的名字  
這篇文章主要介紹了詳解SpringCloud mysql實(shí)現(xiàn)配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

mysql實(shí)現(xiàn)配置中心

本公司配置數(shù)據(jù)的管理是通過mysql進(jìn)行配置管理,因?yàn)橐呀?jīng)搭建好了,所以自己動手重新搭建一遍,熟悉整個流程。有關(guān)項目源碼后期會補(bǔ)上github地址

微服務(wù)要實(shí)現(xiàn)集中管理微服務(wù)配置、 不同環(huán)境不同配置 、 運(yùn)行期間也可動態(tài)調(diào)整 、 配置修改后可以自動更新的需求 ,Spring Cloud Config同時滿足了以上要求。

一、項目搭建

本次主要用三個微服務(wù)

(1)Eureka-server: 7001 注冊中心

(2)config-server : 5001 配置中心

(3)product-server : 8001 商品微服務(wù)

1、Eureka-server注冊中心

注冊中心很簡單,這里不在重復(fù)些,注冊中心沒有變化??梢钥粗皩懙牟┛?: SpringCloud(3)---Eureka服務(wù)注冊與發(fā)現(xiàn)

2、配置中心微服務(wù)

 1、pom.xml

<!--服務(wù)中心jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!--配置中心jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

<!--連接msql數(shù)據(jù)庫相關(guān)jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>

2、application.yml

#服務(wù)名稱
 server:
  port: 5001

#連接配置信息
 spring:
  application:
   name: config-server-jdbc
  profiles:
   active: jdbc
  cloud:
   config:
    server:
     default-label: dev
     jdbc:
      sql: SELECT akey , avalue FROM config_server where APPLICATION=? and APROFILE=? and LABEL=?
 #####################################################################################################
 # mysql 屬性配置
  datasource:
   driver-class-name: com.mysql.jdbc.Driver
   url: jdbc:mysql://127.0.0.1:3306/test
   username: root
   password: root
 #####################################################################################################

#指定注冊中心地址
 eureka:
  client:
   serviceUrl:
    defaultZone: http://localhost:7001/eureka/

這里主要講下連接配置信息

(1) spring.profiles.active=jdbc ,自動實(shí)現(xiàn)JdbcEnvironmentRepository。

(2)sql語句自定義,否則會默認(rèn)為“SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?”,具體可以參考 JdbcEnvironmentRepository 實(shí)現(xiàn)。

(3)本人數(shù)據(jù)庫建表為config_server,由于key,value和profile是mysql關(guān)鍵字,所以我都在最前面加了a。當(dāng)然表名字段名都可以自定義。

(4) {application} 對應(yīng)客戶端的"spring.application.name"屬性;

{aprofile} 對應(yīng)客戶端的 "spring.profiles.active"屬性(逗號分隔的列表); 和

{label} 對應(yīng)服務(wù)端屬性,這個屬性能標(biāo)示一組配置文件的版本.

(5)只要 select出來是兩個字段 ,框架會 自動包裝到environment的map<key,value> 。

 3、mysql數(shù)據(jù)

4、springboot啟動類

添加 @EnableConfigServer 注解

@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {

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

3、product-service微服務(wù)

1、pom.xml

<!--服務(wù)中心jar-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
    <!--配置中心客戶端jar-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-client</artifactId>

2、bootstrap.yml

#指定注冊中心地址
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:7001/eureka/

#服務(wù)的名稱
spring:
 application:
  name: product-service
 #指定從哪個配置中心讀取
 cloud:
  config:
   discovery:
    service-id: config-server-jdbc
    enabled: true
   profile: dev
   label: dev

server:
 port: 8001

這里為什么用bootstrap.yml而不用application.yml,是因?yàn)槿鬭pplication.yml 和bootStrap.yml 在同一目錄下,

則 bootStrap.yml 的加載順序要高于application.yml ,即bootStrap.yml 會優(yōu)先被加載。

為何需要把 config server 的信息放在 bootstrap.yml 里?

當(dāng)使用 Spring Cloud 的時候,配置信息一般是從 config server 加載的,為了取得配置信息(比如密碼等),你需要一些提早的或引導(dǎo)配置。

因此,把 config server 信息放在 bootstrap.yml,用來加載真正需要的配置信息。

3、ConfigController類(測試用)

@RestController
@RequestMapping("/api/v1/product")
public class ConfigController {

  @Value("${item_url}")
  private String url;

  /**
   * 輸出url
   */
  @RequestMapping("url")
  public void list(){

    System.out.println(url);
  }

4、測試

通過訪問:http://localhost:8001/api/v1/product/url 進(jìn)入斷點(diǎn)。

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

相關(guān)文章

最新評論