寰湇鍔¤縼绉昏锛堜笁锛夛細閰嶇疆涓績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浠庨浂鍒颁竴瀛︿範璁板綍锛堜笁锛氬嚱鏁帮級