寰湇鍔¤縼绉昏锛堜笁锛夛細閰嶇疆涓績SpringCloud Config鎼缓

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了寰湇鍔¤縼绉昏锛堜笁锛夛細閰嶇疆涓績SpringCloud Config鎼缓相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/public' title='public'>public   not   framework   鍩烘湰   label   搴旇   鏂板缓   str   閫氳繃   

springboot鎺ㄨ崘浣跨敤娉ㄨВ鏂瑰紡锛屽噺灏戜簡澶ч噺鐨剎ml閰嶇疆銆傜郴缁熺殑鍩烘湰閰嶇疆鏂囦欢鎴戦€夋嫨鐢▂ml鏍煎紡锛岀浉瀵逛簬properties锛屼唬鐮佹洿绠€娲侊紙涓嶇敤閲嶅鍐欏睘鎬э級锛岀粨鏋勫寲鏇存竻鏅颁竴鐐癸紝璇诲彇閫熷害涔熷簲璇ヨ兘鐣ュ揩涓€鐐瑰惂銆傞厤缃枃浠跺悕bootstrap.yml浼樺厛浜巃pplication.yml銆?/p>

鍒嗗竷寮忛厤缃腑蹇冿紝涓昏鏄皢閰嶇疆淇℃伅淇濆瓨鍦ㄩ厤缃腑蹇冪殑鏈湴鏂囦欢鎴栨暟鎹簱鎴栬繙绋嬬増鏈帶鍒朵腑蹇冿紙svn銆乬it锛変腑銆傜爺绌朵簡涓€娈垫椂闂撮樋娉㈢綏锛屼笉鐭ラ亾涓哄暐铏氭嫙鏈鸿兘telnet瀹夸富mysql锛屼絾闃挎尝缃楀缁堟彁绀烘暟鎹簱杩炴帴涓嶄笂锛岄亗鏀惧純銆傝繘涓€姝ョ爺绌禼onfig锛屽苟灏嗛厤缃暟鎹垚鍔熶繚瀛樿嚦mysql鏁版嵁搴撲腑銆?/p>

 

涓€銆佹暟鎹簱鍒涘缓

寤虹珛涓€寮爐b_config_server銆備负闃叉瀛楁涓庡叧閿瓧閲嶅锛屽墠闈㈠姞浜嗗瓧姣峚銆?/p>

akey锛氶厤缃悕绉?/p>

avalue锛氶厤缃€?/p>

application锛氬搴旀敞鍐屼腑蹇冩敞鍐屽簲鐢ㄧ殑鏈嶅姟鍒悕

aprofile锛歞ev 寮€鍙戠幆澧冿紝prd锛氱敓浜х幆澧?/p>

label锛氭爣绛?/p>

DROP TABLE IF EXISTS `tb_config_server`;
CREATE TABLE `tb_config_server` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`akey` varchar(255) DEFAULT NULL,
`avalue` varchar(255) DEFAULT NULL,
`application` varchar(255) DEFAULT NULL,
`aprofile` varchar(255) DEFAULT NULL,
`label` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

 

澧炲姞涓€鏉℃祴璇曟暟鎹細

insert into tb_config_server(akey,avalue,application,aprofile,label) values(鈥榮ite_url鈥?鈥榟ttp://www.baidu.com鈥?鈥榵xproject-api-service-sys鈥?鈥榙ev鈥?;

鍏朵腑锛歺xproject-api-service-sys锛氫唬琛ㄦ槸api椤圭洰涓郴缁熺淮鎶ゆā鍧楁湇鍔★紝杩欎釜鍚嶅瓧鏄繖涓」鐩敞鍐屽埌娉ㄥ唽涓績鐨勫埆鍚嶃€?/p>

浜屻€乧onfig閰嶇疆涓績鎼缓

1. 鏂板缓椤圭洰锛歺xproject-config-server

2. 瀵煎寘锛屼富瑕佹槸config鍜屾暟鎹簱锛屽悓鏃堕渶瑕佸皢config娉ㄥ唽鍒版敞鍐屼腑蹇冿紝鎵€浠onsul鐨勫寘涔熷鍏?/p>

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version> 2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!-- 浣跨敤鏁版嵁搴撻厤缃?/span>-->
        <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.48</version>
        </dependency>
    </dependencies>

 

3. 缂栧啓閰嶇疆鏂囦欢

spring.application.name锛氭敞鍐屽埌娉ㄥ唽涓績鐨勫埆鍚嶏紝瀹㈡埛绔€氳繃杩欎釜鍒悕杩炴帴閰嶇疆涓績銆?/p>

spring.cloud.config.server.jdbc锛氫富瑕佹槸涓€鏉QL璇彞锛屾彁鍙栭厤缃垪琛ㄣ€?/p>

spring:
  application:
    name: config-server
  cloud:
    consul:
      host: 192.168.0.7
      port: 8500
      discovery:
        hostname: 192.168.0.6
    config:
      server:
        jdbc:
          sql: SELECT akey,avalue FROM tb_config_server WHERE application=? AND aprofile=? AND label=?
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/zyconfig??useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 123456
  profiles:
    active: jdbc
server:
  port: 9000

 

4. 缂栧啓鍚姩鍏ュ彛

@EnableDiscoveryClient锛氭敞鍐屼腑蹇?br />
@EnableConfigServer锛氶厤缃腑蹇?/span>
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class AppConfigServer {
    public  static  void main(String[] args){
        SpringApplication.run(AppConfigServer.class,args);
    }
}

 

鍚姩绋嬪簭鍚庯紝灏卞彲浠ュ湪娉ㄥ唽涓績鐪嬪埌閰嶇疆涓績鐨勬湇鍔★紝閰嶇疆涓績鎼缓瀹屾瘯

 

涓夈€佸鎴风璇诲彇娴嬭瘯

1. config鍖呭鍏?/p>

<!-- config client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version> 2.1.4.RELEASE</version>
        </dependency>

2. 閰嶇疆鏂囦欢锛堜娇鐢╞ootstrap.yml锛?/p>

spring:
  application:
    ## 绯荤粺绠$悊鏈嶅姟锛屾敞鍐屼腑蹇冨埆鍚?    name: xxproject-api-service-sys
  cloud:
    config:
      discovery:
        ## 閰嶇疆涓績鍦ㄦ敞鍐屼腑蹇冪殑鍒悕
        service-id: config-server
        enabled: true
      profile: dev
      label: dev
    consul:
      host: 192.168.0.7
      discovery:
        hostname: 192.168.0.6
      port: 8500
server:
  port: 9001

3. 鍚姩鍏ュ彛

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

4. 缂栧啓绠€鍗曠殑娴嬭瘯鎺у埗鍣?/p>

@RestController
public class testController{
    @Value("{site_url}")
    private String site_url;

    @RequestMapping("/index")
    public String index(){
        return site_rul;
    }
}

璁块棶锛歨ttp://localhost:9001/index

鍙互鎴愬姛鎵撳嵃鍑猴細http://www.baidu.com

以上是关于寰湇鍔¤縼绉昏锛堜笁锛夛細閰嶇疆涓績SpringCloud Config鎼缓的主要内容,如果未能解决你的问题,请参考以下文章

TCP鍗忚鎺㈢┒锛堜笁锛夛細RTT銆佹粦鍔ㄧ獥鍙e拰闃诲澶勭悊

Spring Cloud Alibaba锛堜簩锛?閰嶇疆涓績澶氶」鐩€佸閰嶇疆鏂囦欢銆佸垎鐩綍瀹炵幇

TypeScript浠庨浂鍒颁竴瀛︿範璁板綍锛堜笁锛氬嚱鏁帮級

python dlib瀛︿範锛堜簲锛夛細姣斿浜鸿劯

銆怬racle Database銆慜racle RAC锛堜節锛夛細绠$悊宸ュ叿--crsctl & srvctl

Tomcat8瀹夎锛?瀹夊叏閰嶇疆涓庢€ц兘浼樺寲