上篇文章講述了一個服務(wù)如何從配置中心讀取文件,配置中心如何從遠(yuǎn)程git讀取配置文件,當(dāng)服務(wù)實例很多時粮揉,都從配置中心讀取文件巡李,這時可以考慮將配置中心做成一個微服務(wù),將其集群化扶认,從而達(dá)到高可用侨拦,架構(gòu)圖如下:
一,準(zhǔn)備工作
繼續(xù)使用上一篇文章的工程辐宾,創(chuàng)建一個eureka-server工程狱从,用作服務(wù)注冊中心。
在其pom.xml文件引入Eureka的起步依賴spring-cloud-starter-netflix- eureka-server叠纹,代碼如下:
<?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.example</groupId>
<artifactId>demo2-springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml代碼
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在啟動類添加@EnableEurekaServer注解
package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
二季研,改造config-server
將其注冊微到服務(wù)注冊中心,作為Eureka客戶端,pom添加eureka依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
修改application.yml配置文件
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/forezp/SpringcloudConfig/
search-paths: respo
password:
username:
label: master
server:
port: 8888
eureka:
client:
service-url:
defaultZone: http://localhost:8889/eureka/
在啟動類添加注解
package com.example.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
三誉察,改造config-client
將其注冊微到服務(wù)注冊中心与涡,作為Eureka客戶端,pom添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在啟動類添加@EnableEurekaClient注解
package com.example.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@EnableEurekaClient
@RestController
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${democonfigclient.message}")
String message;
@RequestMapping("/hi")
public String sayHi(){
return message;
}
}
配置文件bootstrap.properties,注意是bootstrap!修改bootstrap.yml
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
uri: http://localhost:8888/eureka/
name: config-client
discovery:
enabled: true
service-id: config-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8889/eureka/
server:
port: 8881
- spring.cloud.config.discovery.enabled 是從配置中心讀取文件持偏。
- spring.cloud.config.discovery.serviceId 配置中心的servieId驼卖,即服務(wù)名。
這時發(fā)現(xiàn)鸿秆,在讀取配置文件不再寫ip地址酌畜,而是服務(wù)名,這時如果配置服務(wù)部署多份卿叽,通過負(fù)載均衡桥胞,從而高可用。
依次啟動eureka-servr,config-server,config-client
訪問網(wǎng)址:http://localhost:8889/
訪問http://localhost:8881/hi考婴,瀏覽器顯示:
四贩虾,易錯總結(jié)
1.config-client的配置文件修改是在bootstrap.yml上
2.注冊服務(wù)中心的路徑不能不一樣,不小心打錯了蕉扮,排查了幾個小時整胃。。喳钟。屁使。
3.啟動類添加的是@EnableEurekaClient不是@EnableEurekaServer