spring cloud系列一 搭建配置伺服器(分散式配置管理)configserver
分散式配置管理應該是分散式系統和微服務應用的第一步。想象一下如果你有幾十個服務或應用需要配置,而且每個服務還分為開發、測試、生產等不同維度的配置,那工作量是相當大的,而且還容易出錯。如果能把各個應用的配置資訊集中管理起來,使用一套機制或系統來管理,那麼將極大的提高系統開發的生產效率,同時也會提高系統開發環境和生產環境執行的一致性。
1、pom.xml
<parent>
<groupId>org.springframework.boot</groupId> 引入boot
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>引入spring cloud
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>引入config-server
jar
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2、啟動類
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3、配置檔案
=====application.properties
server.port=8888
#spring.cloud.config.server.git.uri=http://git.oschina.net/zhou666/spring-cloud-7simple/tree/master/cloud-config-repo
#spring.cloud.config.server.git.searchPaths=cloud-config-repo
#eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/,http\://zlhost\:8762/eureka/
spring.application.name=config-server
#使用本地屬性檔案
spring.profiles.active = native
#屬性檔案地址,只要指定資料夾的路徑
spring.cloud.config.server.native.searchLocations=classpath:/properties/
====configServer-dev.properties
spring.datasource.url: jdbc:mysql://localhost/test
#spring.datasource.url = jdbc:h2:file:~/.h2/testdb
spring.datasource.username: root
spring.datasource.password: root
driver-class-name: com.mysql.jdbc.Driver
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
4、瀏覽器訪問
http://127.0.0.1:8888/configServer/dev
5、客戶端使用configserver
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
配置bootstrap.properties
#指定配置中心
spring.cloud.config.uri:http://127.0.0.1:8888
#指定屬性檔名 configServer-dev.properties,是有命名規則的
spring.cloud.config.name=configServer
spring.cloud.config.profile=${profile:dev}