最近在学习Spring Cloud,下面简单的记录下过程。
本人用的开发工具是sts-3.8.2.RELEASE,可以很方便的创建Spring Cloud的项目。
创建配置中心的文件
在Github上创建一个叫springcloudconfigserver的项目,目录结构如下:
springcloudconfigserver
+config-repo
-springcloudconfigserver-dev.properties
-springcloudconfigserver-test.properties
两个配置文件的内容如下:
springcloudconfigserver-dev.properties:
config.name=dev12
springcloudconfigserver-test.properties:
config.name=test
这个文件就创建结束了。
创建配置中心项目
利用STS创建config server项目,目录结果都生成好了,很方便,我把项目命名为:springcloud-config-server,目录结构如下:
我选择的spring cloud的版本是Dalston.SR2。
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.mk.springcloud.config.server</groupId>
<artifactId>springconfigserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-config-server</name>
<description>Spring Cloud Config Server</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
<spring-cloud.version>Dalston.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
之前使用了最新的版本,发现有很多的BUG,在我本机无法运行起来,所以改成了Dalston.SR2。
application.yml配置文件
配置内容如下:
server:
port: 8070
spring:
application:
name: springconfigserver
cloud:
config:
server:
git:
uri: //git项目的地址
search-paths: //git项目的文件地址
username: //git用户名
password: //git密码
agement:
security:
enabled: false
SpringcloudConfigServerApplication
启动类的代码如下:
package com.mk.springcloud.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class SpringcloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigServerApplication.class, args);
}
}
这样一个简单的配置中心就完成了。
进入springcloudconfigserver的路径,执行mvn clean install成功后,在执行mvn spring-boot:run,这个就可以在本地访问配置中心的内容了。
在浏览器中打入:http://localhost:8070/springcloudconfigserver/dev
输出内容如下:
{"name":"springcloudconfigserver","profiles":["dev"],"label":null,"version":"5a41f10e7fb16c2449e3d6ff86dc7bae9c8bdfdf","state":null,"propertySources":[{"name":"https://github.com/chenpeihai1026/springcloudconfigserver.git/config-repo/springcloudconfigserver-dev.properties","source":{"config.name":"dev12"}}]}