基于文件系统的Spring Cloud 配置服务器

如果想根据不同环境,使用不同的启动命令,读取不同的配置去启动项目,可以将不同环境信息写入文件中,在启动项目的时候,指定环境就可以了.

创建一个SpringCloud工程.

pom文件:


       
4.0.0

com.thoughtmechanix
configurationserver
0.0.1-SNAPSHOT
jar

Config Server
Config Server demo project


org.springframework.boot
spring-boot-starter-parent
1.4.4.RELEASE




org.springframework.cloud
spring-cloud-dependencies
Camden.SR5
pom
import




org.springframework.cloud
spring-cloud-config-server


org.springframework.cloud
spring-cloud-starter-config



javax.xml.bind
jaxb-api
2.3.0


com.sun.xml.bind
jaxb-impl
2.3.0


com.sun.xml.bind
jaxb-core
2.3.0


javax.activation
activation
1.1.1




<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
com.thoughtmechanix.confsvr.ConfigServerApplication
<java.version>1.8</java.version>
<docker.image.name>johncarnell/tmx-confsvr</docker.image.name>
<docker.image.tag>chapter3</docker.image.tag>






maven-resources-plugin


copy-resources

validate

copy-resources


${basedir}/target/dockerfile


src/main/docker
true







org.springframework.boot
spring-boot-maven-plugin


org.apache.maven.plugins
maven-jar-plugin
2.4


org.apache.maven.plugins
maven-resources-plugin
2.4


maven-compiler-plugin
2.3.2

1.8
1.8



com.spotify
docker-maven-plugin
0.4.10

\({docker.image.name}:\){docker.image.tag}
\({basedir}/target/dockerfile</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>\){project.build.directory}
${project.build.finalName}.jar





      

项目路径:

config下的licensingservice是不同环境的配置文件.

在application.yml中配置项目扫描的配置文件路径:

server:
           port: 8888
        spring:
          profiles:
            active: native
          cloud:
             config:
               server:
                   native:
                       searchLocations: classpath:config/,classpath:config/licensingservice

在licensingservice中的配置文件命名规则是:应用程序名称-环境名称.yml

配置好后,启动程序,就能够得到相应环境的配置信息.

http://localhost:8888/licensingservice/dev2

是json字符串.

posted @ 2021-04-30 15:26  Monstro  阅读(145)  评论(0)    收藏  举报