使用sping cloud實現配置中心化

咱們使用了spring boot簡化了一系列配置,可是每一個應用都有一個application.propertities。這樣在分佈式環境或是應用包比較多時就變得很難管理,因此這裏的解決方案是使用spring cloud實現配置中心化。git

首先在這裏有一個client和server的概念。client是上面咱們說的一個個應用包,server實際上也是一個spring boot項目,用來管理咱們的配置。github

首先,我搭建一個server工程,名叫propertityweb

pom.xmlspring

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>propertity</groupId>
    <artifactId>propertity</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Brixton.BUILD-SNAPSHOT</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.3.3.RELEASE</version>
            </plugin>
        </plugins>
    </build>

    <!-- repositories also needed for snapshots and milestones -->

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>http://repo.spring.io/libs-release-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>http://repo.spring.io/libs-snapshot-local</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/libs-milestone-local</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

 

啓動器apache

package com.sunsharing.idream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class PropertityApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(PropertityApplication.class).web(true).run(args);
    }
}

配置文件bootstrap

server.port=8888
spring.cloud.config.uri=http://localhost:8888
#應用名稱
spring.cloud.config.name=cloud-config
#從本地獲取配置文件
spring.profiles.active=native
#文件路徑
spring.cloud.config.server.native.searchLocations=file:D:/config/

默認是從github上獲取配置文件,也能夠從svn獲取配置文件,我這裏讀的是本地。瀏覽器

可打開瀏覽器驗證一下是否成功app

這裏有一點須要注意:maven

以cloud-config-a.properties爲例子,它的application是cloud-config,profile是a,client會根據填寫的參數來選擇讀取對應的配置分佈式

在client端的pom中加入

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>

bootstrap.properties 的加載也是先於 application.properties 。在resource下新建一個bootstrap.properties,配置以下

# 配置中心服務的地址
spring.cloud.config.uri=http://192.168.2.112:8888
# 要讀取的配置文件application屬性
spring.cloud.config.name=cloud-config
# 要讀取的配置文件profile屬性
spring.cloud.config.profile=a

這裏有一個坑,spring.cloud.config.uri=http://192.168.2.112:8888這裏的uri配置必須帶上http://,不然會報錯,報錯的內容是

超級坑,竟然是beanUtil拷貝password屬性出錯,搞得我一臉懵b。

配置完成後,就能夠從遠程server中獲取配置文件,和在本地配置application.properties並沒有差異。

若是須要在更改配置後刷新client端的配置須要在client端加上

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

往client發送一個請求便可

相關文章
相關標籤/搜索