SpringBoot应用使用application.properties或application.yml作为配置文件,
SpringCloud微服务框架管理诸多服务,如果每个服务都配置自己配置文件,配置工作
非常繁琐,SpringCloud提供了Config子项目,此项目核心就是配置中心,通过一个服务器和多个客户端实现配置服务。
例程:
一、在Git创建仓库
二、定义Config-Server
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gf</groupId>
<artifactId>eureka-order</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-order Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties配置
server.port=8150
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://github.com/qixiangchen/springcloudconfig
spring.cloud.config.server.git.username=xxx
spring.cloud.config.server.git.password=xxx
启动类
package com.gf.eureka_order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServer {
public static void main( String[] args )
{
SpringApplication.run(ConfigServer.class, args);
}
}
通过浏览器访问配置
http://localhost:8150/order/prod
http://localhost:8150/order/test
三、定义Config客户端
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gf</groupId>
<artifactId>eureka-order</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-order Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
扫描二维码关注公众号,回复:
5726128 查看本文章
配置文件bootstrap.properties
server.port=8250
spring.application.name=order
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8150/
启动类
package com.gf.eureka_order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClient {
public static void main( String[] args )
{
SpringApplication.run(ConfigClient.class, args);
}
}
Controller
package com.gf.eureka_order;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/configclient")
public class ConfigCtrl {
@Value("${name}")
String name = null;
@RequestMapping("/showname")
public String showname()
{
return name;
}
}
测试效果
Config-Client项目通过Config-Server使用Git仓库中的配置