SpringCloud之分布式配置Config(一)
标签:
Spring Cloud
统一的配置管理中心
所需资源
一个服务端
一个客户端
一个Git配置中心仓库
服务端使用端口:8888
客户端使用端口:9000
先来开发服务端
三个步骤
添加依赖
server的配置文件:application.properties
启动类添加注解
看一下具体的操作步骤
添加依赖,完整的pom.xml文件
<?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>com.example</groupId> <artifactId>learn03-config</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>learn03-config</name> <description>Demo project for Spring Boot</description> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
完整的application.properties文件
server.port=8888spring.cloud.config.server.git.uri=https://git.oschina.net/sjk707069921/config-file.git//配置文件所在git仓库地址
启动类添加注解@EnableConfigServer
@SpringBootApplication@EnableConfigServerpublic class Learn03ConfigApplication { public static void main(String[] args) { SpringApplication.run(Learn03ConfigApplication.class, args); } }
服务端到此完成,但是别着急
git仓库还没创建
创建git仓库,提交两个文件
database-dev.properties
database.properties
内容也很简单
database-dev.properties文件内容
hello=dev file
database.properties文件内容
hello=this is product file
OK,到现在可以启动服务端,打开浏览器看一下 http://localhost:8888/database/dev
看到以下json表示服务端是OK的
444950aa-246f-49e4-be50-d9fc99cf9244.png
接下来 开发客户端
也是三步
添加依赖
修改配置文件
测试
新建一个项目,添加依赖
<?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>com.example</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <repositories> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.3.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
然后配置文件,这里注意,不能使用默认的application,propertis,而应将其重命名为bootstrap.properties
spring.application.name=database #这里要对应配置文件的名字spring.cloud.config.profile=prod #使用哪个profilespring.cloud.config.label=master #git仓库的分支spring.cloud.config.uri=http://localhost:8888/ #服务器的地址server.port=9000
OK,然后在启动类写一个测试,获取,并打印配置文件中的值
@SpringBootApplication@EnableAutoConfiguration@RestControllerpublic class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${hello}") private String foo; @GetMapping("/hi") public String hi(){ System.err.println(foo); return foo; } }
打开浏览器 ,访问 http://localhost:9000/hi
看到如下输出
c54cbb6b-61fe-4e0a-8053-4ee98285d8b7.png
至此,简单的配置中心开发完成.
作者:罗曼蒂克
链接:https://www.jianshu.com/p/0cb5c94ce485
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦