上一篇博客介紹了SpringCloud Config的簡介、Config Server及Client的使用,在微服務開發中,是需要將config整合進其他模塊裏的,本篇博客就來介紹下eureka整合config以及服務器提供者整合config,需要用到git上的兩個配置文件:
Config整合註冊中心
先搞個配置文件(eureka_config.yml)上傳到git倉庫去,文件內容如下:
spring:
profiles:
active:
- dev
---
server:
port: 2004
context-path: /
spring:
profiles: dev
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
server:
port: 2005
context-path: /
spring:
profiles: test
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
新建microservice-eureka-server-config-2004項目用來測試,在pom.xml里加上相關依賴:
<!--config配置中心相關依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
然後在項目的resources目錄下新建bootstrap.yml文件,文件內容如下:
spring:
application:
name: microservice-eureka-server-config
cloud:
config:
name: eureka_config
#配置config server地址
uri: http://configserver.test.com:4001
#級別
profile: test
#git中的分支(默認master)
label: master
項目的application.yml配置如下:
spring:
application:
name: microservice-eureka-server-config
在項目的啓動類加上@EnableEurekaServer註解:
然後先啓動microservice-config-server-4001項目,再啓動microservice-eureka-server-config-2004項目,根據具體配置情況在瀏覽器的地址欄輸入http://localhost:2004或http://localhost:2005進行測試,當配置爲“dev”時,測試結果如下:
當配置爲“test”時,測試結果如下:
測試結果如上說明成功讀取遠程git配置,eureka啓動也OK
Config整合生產者
然後把服務提供者和config進行整合,並把服務提供者註冊到eureka去;先搞個配置文件(provider_config.yml)上傳到git倉庫去,文件內容如下:
spring:
profiles:
active: dev
---
server:
port: 1007
context-path: /
#數據源配置
spring:
profiles: dev
application:
name: microservice-student
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demosite1?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1007
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka
info:
groupId: com.ue.microservice
artifactId: microservice-student-provider-config-1007
version: 1.0-SNAPSHOT
contacts: Tom
phone: 123456
---
server:
port: 1008
context-path: /
#數據源配置
spring:
profiles: test
application:
name: microservice-student
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/demosite1?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1008
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka
info:
groupId: com.ue.microservice
artifactId: microservice-student-provider-config-1008
version: 1.0-SNAPSHOT
contacts: Tom
phone: 123456
新建microservice-student-provider-config項目,其完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ue</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-provider-config</artifactId>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--修改後立即生效,熱部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--添加註冊中心Eureka相關配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--actuator監控引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com.ue</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
然後在項目的resources目錄下新建bootstrap.yml文件,文件內容如下:
spring:
application:
name: microservice-student-provider-config
cloud:
config:
name: provider_config
#配置config server地址
uri: http://configserver.test.com:4001
#級別
profile: test
#git中的分支(默認master)
label: master
項目的application.yml配置如下:
spring:
application:
name: microservice-student-provider-config
項目的啓動類代碼如下:
package com.ue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan("com.ue.entity")
@EnableJpaRepositories("com.ue.repository")
@EnableEurekaClient
public class MicroserviceStudentProviderConfigApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderConfigApplication.class, args);
}
}
然後先啓動microservice-config-server-4001項目,再啓動microservice-eureka-server-config-2004項目,最後再啓動microservice-student-provider-config項目,然後訪問2004註冊中心查看生產者的註冊情況:
以上結果說明生產者成功註冊到註冊中心了
總結
將springcloud config配置中心的用途與單體項目的配置切換進行對比,得到如下看法: