Skip to content

Commit 280c7b7

Browse files
committed
feat(统一配置管理):添加spring cloud config
1 parent 570b8f3 commit 280c7b7

File tree

11 files changed

+150
-3
lines changed

11 files changed

+150
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
profile=master1
1+
profile=master

config-server/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/

config-server/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.coderqian</groupId>
7+
<artifactId>config-server</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>config-server</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.9.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-config-server</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<dependencyManagement>
42+
<dependencies>
43+
<dependency>
44+
<groupId>org.springframework.cloud</groupId>
45+
<artifactId>spring-cloud-dependencies</artifactId>
46+
<version>${spring-cloud.version}</version>
47+
<type>pom</type>
48+
<scope>import</scope>
49+
</dependency>
50+
</dependencies>
51+
</dependencyManagement>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-maven-plugin</artifactId>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
62+
63+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coderqian.configserver;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.config.server.EnableConfigServer;
6+
7+
@SpringBootApplication
8+
@EnableConfigServer
9+
public class ConfigServerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(ConfigServerApplication.class, args);
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring.application.name=config-server
2+
server.port=1201
3+
4+
spring.cloud.config.server.git.uri=https://github.com/coderqianlq/spring-cloud/
5+
spring.cloud.config.server.git.search-paths=config-coderqian
6+
spring.cloud.config.server.git.username[email protected]
7+
spring.cloud.config.server.git.password=qlq1995/
8+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.coderqian.configserver;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class ConfigServerApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

eureka-customer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
<artifactId>spring-cloud-starter-eureka</artifactId>
6464
</dependency>
6565

66+
<dependency>
67+
<groupId>org.springframework.cloud</groupId>
68+
<artifactId>spring-cloud-starter-config</artifactId>
69+
</dependency>
70+
6671
<!-- 引入MySQL连接依赖 -->
6772
<dependency>
6873
<groupId>mysql</groupId>

eureka-customer/src/main/java/com/coderqian/eurekacustomer/controller/TestController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.annotations.Api;
66
import io.swagger.annotations.ApiOperation;
77
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Value;
89
import org.springframework.web.bind.annotation.RequestMapping;
910
import org.springframework.web.bind.annotation.RequestMethod;
1011
import org.springframework.web.bind.annotation.RequestParam;
@@ -26,6 +27,9 @@ public class TestController {
2627
@Autowired
2728
private TestService testService;
2829

30+
@Value("${profile}")
31+
private String profile;
32+
2933
@ApiOperation(value = "返回用户输入的结果", notes = "返回用户输入的结果")
3034
@RequestMapping(value = "/result", method = RequestMethod.GET)
3135
public String test(@RequestParam(value = "text") String text) {
@@ -43,4 +47,10 @@ public void testException(@RequestParam(value = "text") String text) {
4347
public BaseResult testBaseResult(@RequestParam(value = "text") String text) {
4448
return testService.testBaseResult(text);
4549
}
50+
51+
@ApiOperation(value = "测试读取配置文件中的值", notes = "测试读取配置文件中的值")
52+
@RequestMapping(value = "/config", method = RequestMethod.GET)
53+
public String testConfig() {
54+
return profile;
55+
}
4656
}

eureka-customer/src/main/resources/application.properties

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
spring.application.name=eureka-customer
2-
server.port=8200
31
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka
42

53
# 配置数据源
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring.application.name=eureka-customer
2+
server.port=8200
3+
4+
spring.cloud.config.profile=master
5+
spring.cloud.config.label=
6+
spring.cloud.config.uri=http://127.0.0.1:1201/
7+

0 commit comments

Comments
 (0)