1. 程式人生 > >Spring Cloud入門教程-配置中心 Config

Spring Cloud入門教程-配置中心 Config

注意:這裡用到的專案都是在之前幾篇文章講解用到的專案工程基礎上進行的,在這一系列部落格寫完後會提供原始碼地址。

這裡講解一下Spring Cloud Config 的內容。

Config Server 從本地讀取配置檔案

        Config Server可以從本地倉庫讀取配置檔案,也可以從遠處Git倉庫讀取。本地倉庫是指將所有的配置檔案統一寫在 Config Server工程目錄下 Config Sever暴露 Http apI介面, Config Client通過呼叫 Config Sever的 Http Api介面來讀取配置檔案。

建立新Module config-server ,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>
    <parent>
        <groupId>com.springcloud.demo</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>Demo project for Spring Boot</description>


    <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>
        <!--構建高可用的Config Server 將config server 作為eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

在ConfigServerApplication 上添加註解 @EnableConfigServer 。

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}

新增配置

server.port=8799


#從本地讀取配置檔案####################################################
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/shared
#####################################################################

在resource 下建立資料夾shared,建立檔案 config-client-dev.properties,並新增如下配置:

server.port=8700
application.name=config-server-v1

建立新Module config-client ,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>
	<parent>
		<groupId>com.springcloud.demo</groupId>
		<artifactId>springcloud-demo</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<artifactId>config-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config-client</name>
	<description>Demo project for Spring Boot</description>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>


		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

建立bootstrap.porperties新增配置:

server.port=8798
spring.application.name=config-client


spring.profiles.active=dev
spring.cloud.config.uri=http://localhost:8797
spring.cloud.config.fail-fast=true

注意這裡用的是bootstrap.porperties,不是application.perproties,bootstrap 具有優先執行順序。

{spring.application.name} 和{spring.profiles.active} 與“-”相連構成了向 Config Server 讀取的檔名。這裡也就是config-server shared資料夾中的config-client-dev.properties 檔案。

為了進一步驗證,編寫一個controller 獲取{application.name}。

@RefreshScope
@RestController
public class MainController {
    @Value("${application.name}")
    private String name;

    @GetMapping("main")
    public String main() {
        return name;
    }
}

啟動 config-server 和config-client.

檢視列印日誌,config-client埠變為8700,