學(xué)習(xí)完整課程請移步 互聯(lián)網(wǎng) Java 全棧工程師
本節(jié)視頻
概述
創(chuàng)建一個工程名為 hello-spring-cloud-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.funtl</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-config-client</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-config-client</name>
<url>http://www.funtl.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.funtl.hello.spring.cloud.config.client.ConfigClientApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
主要增加了 spring-cloud-starter-config
依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Application
入口類沒有需要特殊處理的地方俊犯,代碼如下:
package com.funtl.hello.spring.cloud.config.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
application.yml
增加 Config Client 相關(guān)配置,并設(shè)置端口號為:8889
spring:
application:
name: hello-spring-cloud-config-client
cloud:
config:
uri: http://localhost:8888
name: config-client
label: master
profile: dev
server:
port: 8889
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
相關(guān)配置說明老速,如下:
-
spring.cloud.config.uri
:配置服務(wù)中心的網(wǎng)址 -
spring.cloud.config.name
:配置文件名稱的前綴 -
spring.cloud.config.label
:配置倉庫的分支 -
spring.cloud.config.profile
:配置文件的環(huán)境標(biāo)識- dev:表示開發(fā)環(huán)境
- test:表示測試環(huán)境
- prod:表示生產(chǎn)環(huán)境
注意事項:
- 配置服務(wù)器的默認(rèn)端口為
8888
协饲,如果修改了默認(rèn)端口畏腕,則客戶端項目就不能在application.yml
或application.properties
中配置spring.cloud.config.uri
,必須在bootstrap.yml
或是bootstrap.properties
中配置茉稠,原因是bootstrap
開頭的配置文件會被優(yōu)先加載和配置描馅,切記。
創(chuàng)建測試用 Controller
我們創(chuàng)建一個 Controller 來測試一下通過遠程倉庫的配置文件注入 foo
屬性
package com.funtl.hello.spring.cloud.config.client.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestConfigController {
@Value("${foo}")
private String foo;
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String hi() {
return foo;
}
}
一般情況下而线,能夠正常啟動服務(wù)就說明注入是成功的铭污。
測試訪問
瀏覽器端訪問:http://localhost:8889/hi 顯示如下:
foo version 1
附:開啟 Spring Boot Profile
我們在做項目開發(fā)的時候,生產(chǎn)環(huán)境和測試環(huán)境的一些配置可能會不一樣吞获,有時候一些功能也可能會不一樣况凉,所以我們可能會在上線的時候手工修改這些配置信息。但是 Spring 中為我們提供了 Profile 這個功能各拷。我們只需要在啟動的時候添加一個虛擬機參數(shù)刁绒,激活自己環(huán)境所要用的 Profile 就可以了。
操作起來很簡單烤黍,只需要為不同的環(huán)境編寫專門的配置文件知市,如:application-dev.yml
傻盟、application-prod.yml
,
啟動項目時只需要增加一個命令參數(shù) --spring.profiles.active=環(huán)境配置
即可嫂丙,啟動命令如下:
java -jar hello-spring-cloud-web-admin-feign-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod