springboot2 springcloud Greenwich.SR3 構建微服務--4.統一配置中心

新建完product 和 order 項目就會發現, 裏面有很多的配置是重複的了.

(order 和 product 還沒有分庫, 這個暫時沒有做, 記住就行了, 暫時不這麼做)

現在我們需要做的就是做一個服務專門提供各個服務的配置文件, 進行所有項目配置文件的統一管理, 更新.

新建一個叫做config-server 的項目,作爲配置中心的服務端 (注意先把你本地的rabbitMQ啓動了)

完整的pom文件是:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.example</groupId>
		<artifactId>micro-service</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.example</groupId>
	<artifactId>config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>config-server</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<!-- 需要配置這個才能啓動成功 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
         <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>
	</dependencies>

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

</project>

注意,我這裏的parent已經修改成了 父工程, 不再依賴一個具體的springboot版本的了, 這樣能夠統一項目中所有使用到的springboot和springcloud的版本, 而不是每個地方都重複一遍. 前面的三章是沒有改的.提醒你們注意

 

在config-server  啓動類上添加添加註解:

@EnableConfigServer          config的服務端
@EnableEurekaClient          eureka的客戶端

修改config-server 配置

spring.application.name=config-service
server.port=8083
eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka
#git的地址
spring.cloud.config.server.git.uri=https://github.com/changhe626/app1
[email protected]
spring.cloud.config.server.git.password=1234
#還可以寫成 http://localhost:8083/order-1.properties  http://localhost:8083/order-1.yml / json
#本地的git地址
spring.cloud.config.server.git.basedir=D:\\idea3\\micro-service\\config-server\\config-dir
#rabbitmq的配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

這樣就配置好了

spring.cloud.config.server.git.basedir  這個會清空此文件夾, 注意不要亂放.

上面的配置中github的地址是對的, 但是賬號密碼是錯的,不用試了.

 

上傳一些配置到git上去

本次我們使用dev 環境, 就是這個文件 order-dev.properties

#數據庫的鏈接配置
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=utf-8&serverTimezone=UTC
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#rabbitmq的配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#自定義配置
user.env=onyx60
girl.name=xiexin
girl.age=20

現在就可以啓動服務訪問了. 

 

http://localhost:8083/order-1.properties   

獲取到配置. , 後綴也可以改成.json,   或者.yml


獲取配置的配置鏈接規則是:
/{name}-{profiles}.properties
/{label}/{name}-{profiles}.properties
name 服務名
profiles 環境
label  分支branch

現在我們改造order項目, 使其從github上獲取配置, 因爲拉取配置啓動需要的時間很長了, 我就只改造了order這一個項

0.添加依賴

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

1.註釋掉application.properties 中所有的配置, 但是這個文件要保留, 否則就會出現讀取配置錯亂的問題,  我整了1個小時才解決這個問題

2.新建bootstrap.properties 文件, 在裏面寫一些必要的配置, 因爲我們的服務還是註冊到eureka上去, 在springboot 的啓動過程中, 會先去讀取bootstrap.properties 中的配置文件.再讀取application.properties 中的, 這是springboot啓動順序問題.

bootstrap.properties 內容是:

#端口號
server.port=8083
#應用名字
spring.application.name=order
#啓用config配置
spring.cloud.config.enabled=true
#config 的 id
# config的默認配置端口是8888, 所以出錯的時候會出現鏈接的url defaults to "http://localhost:8888
spring.cloud.config.uri=http://localhost:8083
# 配置獲取服務, 我電腦上這樣配置不行,可能是因爲版本問題....
# spring.cloud.config.discovery.service-id=config-service
#快速報錯
spring.cloud.config.fail-fast=true

#哪個環境
spring.cloud.config.profile=dev
#哪個分支
spring.cloud.config.label=master
#eureka的註冊地址
eureka.client.eureka-server-u-r-l-context=http://localhost:8761/eureka

 

springboot 在啓動時候. 會先去加載bootstrap.properties  中的配置, 從gtihub上加載配置下來. 再啓動

 

現在的我們的新需求是,不停機同時從git上更新配置 , 我們在配置中 , 我們添加三個自定義的配置,

user.env=onyx60
girl.name=xiexin
girl.age=20

就用這三個屬性 來測試配置的更新

 

添加girl 的配置, 獲取配置文件中的值. 注意我們在整個類上加了註解  @RefreshScope

/**
 * @author zk
 * @Description:
 * @date 2019-09-20 13:50
 */
@Component
@ConfigurationProperties("girl")
@RefreshScope
public class GirlConfig {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "GirlConfig{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
import com.alibaba.fastjson.JSON;
import com.example.order.dto.GirlConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zk
 * @Description:
 * @date 2019-09-20 13:52
 */
@RestController
public class GirlController {
    /**
     * 因爲在 GirlConfig 上已經添加了 RefreshScope 註解,
     * 所以GirlController 上就不要再加了
     */
    @Autowired
    private GirlConfig girlConfig;

    @GetMapping("girl")
    public String getGirlConfig(){
        return JSON.toJSONString(girlConfig);
    }
}

在這裏訪問獲取到的值.

 

獲取user.env 的值, 注意此controller 的類上需要註解,   @RefreshScope

//讀取配置的文件的值
    @Value("${user.env}")
    private String env;

    @GetMapping("env")
    public String getEnv(){
        return env;
    }

現在就可以啓動項目了.

 

首先訪問url獲取兩個的值, 再修改github 上文件的值, 再次獲取, 發現沒有變化, 那是因爲沒人去通知, 沒有去github上拉取最新的配置到本地.

這裏我們先手工的通知一下,  使用postman發送一個請求. 

刷新鏈接: 在請求頭添加一個Content-Type:application/json

POST 方法, 請求   http://localhost:8083/actuator/bus-refresh

再訪問url獲取值, 就可以看到新的配置了.

 

下面要說的一個工具就是

 * 推薦一個網站 https://natapp.cn   這網站很好用, 給你生成一個公網ip地址, 然後請求轉發到本地的一個端口上.

在config的webhook裏面會用到.

這樣就可以從git上面獲取配置了

在github 的項目中添加webhook, 來自動的通知項目去刷新. 就是代替了我們上面手動使用postman去刷新的過程.

這樣配置一下就可以使用了, 注意後面的/monitor  就是專門去通知的.

 

請重啓刷新, 進行測試. 

 

Java Framework,歡迎各位前來交流java相關
QQ羣:965125360

整個代碼地址是:

https://github.com/changhe626/micro-service

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章