文章目錄
1. 前言
所有的微服務提供者都是Config Client,都需要從Config Server獲取配置,所以Config Server的訪問壓力會很大,需要集羣搭建;將所有的Config Server註冊到Eureka Server中,所有的Config Client 從Eureka Server獲取Config Server的地址做負載均衡。
2. Config Server集羣搭建
2.1 pom依賴
添加Eureka Client的依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--向註冊中心註冊-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2.2 yml配置
server:
port: 9800
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/wenrongyao/springcloudconfig/
search-paths: shared
username: # 公共倉庫可以不寫
password: # 公共倉庫可以不寫
###### 向Eureka Server 註冊自己
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server-7001:7001/eureka/,http://eureka-server-7002:7002/eureka/,http://eureka-server-7003:7003/eureka/
instance:
instance-id: config-server-9800 # 服務名稱
prefer-ip-address: true # 顯示ip地址
info: # 點擊註冊列表未服務出現的信息
app.name: springcloud
company.name: www.honor.com
build.artifactId: @project.artifactId@
build.version: @project.version@
2.3 主啓動類
@SpringBootApplication
@EnableConfigServer
public class ConfigServer9800Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServer9800Application.class, args);
}
}
2.4 測試
依次訪問三臺機的配置文件都正常,且三臺機都成功向Eureka Server註冊
http://localhost:9800/release1.0/config-client-dev.yml
http://localhost:9801/release1.0/config-client-dev.yml
http://localhost:9802/release1.0/config-client-dev.yml
3. Config Client搭建
3.1 pom依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--向註冊中心註冊-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3.2 bootstrap.yml配置
#####集羣版Config Server從Eureka Server獲取服務#######
spring:
application:
name: config-client
cloud:
config:
label: release1.0
discovery:
service-id: config-server
enabled: true
fail-fast: true
profile: dev
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server-7001:7001/eureka/,http://eureka-server-7002:7002/eureka/,http://eureka-server-7003:7003/eureka/
instance:
instance-id: config-client-9900 # 服務名稱
prefer-ip-address: true # 顯示ip地址
info: # 點擊註冊列表未服務出現的信息
app.name: springcloud
company.name: www.honor.com
build.artifactId: @project.artifactId@
build.version: @project.version@
3.3 主啓動類
打開EnableEurekaClient獲取服務註冊地址
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ConfigClient9900Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient9900Application.class, args);
}
// 配置文件注入
@Value("${foo}")
private String foo;
@Value("${label:master}")
private String label;
@GetMapping("/getconfig")
public String getConfig() {
return "Current label is " + label + ", and the foo content is " + foo + ".";
}
}
3.4 測試
啓動頁面日誌可以看到獲取的Config Server的地址
2020-04-06 15:29:24.250 INFO 20548 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Multiple Config Server Urls found listed.
2020-04-06 15:29:24.250 INFO 20548 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://10.127.127.1:9801/
2020-04-06 15:29:26.830 INFO 20548 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=release1.0, version=428cf9453635640ee6c9191a7ef4b384cbbfadcc, state=null
啓動成功以後訪問
http://localhost:9900/getconfig
可以獲取
Current label is release1.0, and the foo content is dev foo version.