前言:
必需學會SpringBoot基礎知識
簡介:
spring cloud 為開發人員提供了快速構建分布式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分布式會話等等。它運行環境簡單,可以在開發人員的電腦上跑。
工具:
JDK8
apache-maven-3.5.2
IntelliJ IDEA 2018.1 x64
一、簡介
在分布式系統中,由於服務數量巨多,為了方便服務配置文件統一管理,實時更新,所以需要分布式配置中心組件。在Spring Cloud中,有分布式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。在spring cloud config 組件中,分兩個角色,一是eureka-config-server,二是eureka-config-client。
二、構建 eureka-config-server
(1) 創建springboot項目, 命名: eureka-config-server
(2) 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.lwc</groupId>
<artifactId>eureka-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-config-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</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>
(3) 啟動類: 添加注解 @EnableConfigServer
package com.lwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author Eddie
*/
@EnableConfigServer
@SpringBootApplication
public class EurekaConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConfigServerApplication.class, args);
}
}
(4) application.yml 和 說明
| application.yml | application-dev.yml |
spring: |
server: |
spring.cloud.config.server.git.uri:配置git倉庫地址
spring.cloud.config.server.git.searchPaths:配置倉庫路徑
spring.cloud.config.label:配置倉庫的分支
spring.cloud.config.server.git.username:訪問git倉庫的用戶名 (公共庫可無視)
spring.cloud.config.server.git.password:訪問git倉庫的用戶密碼 (公共庫可無視)
里面存在一個config-repo目錄, 里面幾個app*配置文件 (為方便大家,可使用本人提供的)
啟動項目, 訪問: http://localhost:8888/app/local/master
{
"name": "app",
"profiles": ["local"],
"label": "master",
"version": "e366cdb9f58c68216631b39432dbbaba7ad70809",
"state": null,
"propertySources": [{
"name": "https://github.com/eddie-code/SpringCloudDemo/config-repo/app-local.properties",
"source": {
"eddie": "local config"
}
},
{
"name": "https://github.com/eddie-code/SpringCloudDemo/config-repo/app.properties",
"source": {
"eddie": "default config"
}
}]
}
證明配置服務中心可以從遠程程序獲取配置信息。
http請求地址和資源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
三、構建 eureka-config-client
(1) 創建springboot項目, 命名: eureka-config-client
(2) 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.lwc</groupId>
<artifactId>eureka-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-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.10.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>Edgware.SR3</spring-cloud.version>
</properties>
<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>
<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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
(3) 新建配置文件命名: bootstrap-*.yml
(local=本地測試, dev=開發環境, pro=正式環境)
(spring.cloud.config.label=指明遠程倉庫的分支, spring.cloud.config.uri=配置服務中心的網址)
| bootstrap.yml | bootstrap-dev.yml (其余自己新建) |
spring: |
server: |
(4) 控制層相關代碼
package com.lwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Eddie
*/
@SpringBootApplication
public class EurekaConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConfigClientApplication.class, args);
}
}
package com.lwc.controller;
import com.lwc.dto.GitDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName AppController
* @description 獲取配置中心屬性
* @date created in 2018-04-03 14:22
* @modified by
*/
@RestController
public class AppController {
@Autowired
private GitDto gitDto;
@Value("${eddie}")
private String eddie;
@Value("${url}")
private String url;
@GetMapping("/url")
public String url() {
return this.url;
}
@GetMapping("/eddie")
public String eddie() {
return this.eddie;
}
@GetMapping("/configs")
public String configs() {
return "This is " + gitDto.getEddie() + " Blog: " + gitDto.getUrl();
}
}
(5) 測試
訪問: http://localhost:8881/configs
This is local config Blog: http://www.cnblogs.com/EddieBlog/
(6) 圖解
四、源碼下載
標簽 6-1, 6-2
https://github.com/eddie-code/SpringCloudDemo/tree/master/eureka-config-server
https://github.com/eddie-code/SpringCloudDemo/tree/master/eureka-config-client
五、踩過的坑
源碼有記錄


