SpringCloud分布式配置中心(SpringCloud Config)

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

1:新建一个工程,命名为config-server,同意idea支持创建该类工程 
pom如下:


<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.0modelVersion>

    <groupId>com.springcloudgroupId>
    <artifactId>config-serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>config-servername>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.2.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-config-serverartifactId>
           
        dependency>
        <dependency>
            <groupId>org.eclipse.jgitgroupId>
            <artifactId>org.eclipse.jgitartifactId>
            <version>4.8.0.201706111038-rversion>
        dependency>

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Camden.SR6version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <repositories>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>

project>

注意,自动新建完的工程 中的版本可能会比较高,我们需要手动改成 1.5.2.RELEASE,这样才能保证下面的操作可用。

启动类加入注解,如下:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

修改配置文件,如下:

spring.application.name=config-server
server.port=8888


#配置文件在本地
#spring.profiles.active=native
#配置文件的目录
#spring.cloud.config.server.native.search-locations=D:/workspace/GitHub/spring-config/config-file

#spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.uri=https://github.com/iot-wangshuyu/springcloud
#配置仓库路径
spring.cloud.config.server.git.searchPaths=SpringcloudConfig/respo/
#配置仓库的分支
spring.cloud.config.label=master
#访问git仓库的用户名,仓库为public则不用填写
spring.cloud.config.server.git.username=
#访问git仓库的用户密码
spring.cloud.config.server.git.password=

如果想使用git仓库就把本地配置注释掉,如果想使用本地配置,就把git仓库注释掉,然后再本地对应目录下新建一个config-client-dev.properties文件,内容为:

foo = foo version 2

我使用的是git远程仓库。

配置完成后启动程序,访问http://localhost:8888/config-client/dev/ 
返回:

{“name”:”config-client”,”profiles”:[“dev”],”label”:”master”,”version”:”3205652bcac310656b2f00790e7130e7df19a64e”,”state”:null,”propertySources”:[{“name”:”https://github.com/iot-wangshuyu/springcloud/SpringcloudConfig/respo/config-client-dev.properties“,”source”:{“foo”:”foo version 2”}}]}

返回以上结果,说明配置成功了。

2:构建config client 
新建一个config client工程,命名为config-client,pom配置如下:


<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.0modelVersion>

    <groupId>com.springcloudgroupId>
    <artifactId>config-clientartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>config-clientname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.2.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-configartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        dependency>


    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Dalston.RC1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

    <repositories>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>

project>

配置文件如下:

#服务名称
spring.application.name=config-client
#仓库分支
spring.cloud.config.label=master
#dev开发环境配置文件
#test测试环境
#pro正式环境
spring.cloud.config.profile=dev
#使用eureka server的时候需要注释掉
spring.cloud.config.uri= http://localhost:8888/

在程序中写一个API接口,返回从配置文件中读取的foo变量的值,代码如下:

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/hi")
    public String hi(){
        return foo;
    }
}

在浏览器中访问:http://localhost:8881/hi 
返回:

foo version 2 
说明,config-client从config-server获取了foo的属性,而config-server是从git仓库读取的,如图:SpringCloud分布式配置中心(SpringCloud Config)_第1张图片

你可能感兴趣的:(SpringCloud,SpringCloud)