springCloud入门(一)分布式配置管理

来源:互联网 发布:软件测试大赛 编辑:程序博客网 时间:2024/05/18 03:04

使用maven构建项目,先贴代码再说:

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/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>springCloud</groupId>  <artifactId>configServer</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>jar</packaging>  <name>configServer</name>  <url>http://maven.apache.org</url>  <parent>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-parent</artifactId>    <version>Brixton.BUILD-SNAPSHOT</version>    <relativePath /> <!-- lookup parent from repository -->  </parent>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <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-server</artifactId>    </dependency>  </dependencies>  <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>  <build>    <plugins>      <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>      </plugin>    </plugins>    <defaultGoal>compile</defaultGoal>  </build>  <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>

配置文件:application.properties

server.port=8888spring.cloud.config.server.git.uri=https://gitee.com/ren365880/spring-cloud-demo-config.gitspring.cloud.config.server.git.searchPaths=cloud-config-repospring.cloud.config.label=masterspring.application.name=cloud-config-server


启动类:ConfigServerApplication

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;
@EnableDiscoveryClient@SpringBootApplication@EnableConfigServer@EnableAutoConfigurationpublic class ConfigServerApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigServerApplication.class, args);    }}
说明:maven的配置就不用说了,都是依赖的包

配置文件:springcloud配置管理可读取git文件,所以需要在git上新建项目,url出填写git地址,searchPaths填写文件所在文件夹,label是分支

启动main后访问127.0.0.1:端口/文件前缀/不同标识/即可获得配置文件的内容。

访问关系:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties对应的配置文件,{label}对应git上不同的分支

返回内容:


项目和git目录


原创粉丝点击