前言:
必需學會SpringBoot基礎知識
簡介:
spring cloud 為開發人員提供了快速構建分布式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分布式會話等等。它運行環境簡單,可以在開發人員的電腦上跑。
工具:
JDK8
apache-maven-3.5.2
IntelliJ IDEA 2018.1 x64
上一篇文章講述了一個服務如何從配置中心讀取文件,配置中心如何從遠程git讀取配置文件,當服務實例很多時,都從配置中心讀取文件,這時可以考慮將配置中心做成一個微服務,將其集群化,從而達到高可用,架構圖如下:
一、准備工作
繼續使用上一篇文章的工程,創建一個 eureka-config-server-cluster Project,用作服務注冊中心。
在 pom.xml File 引入 Eureka 的起步依賴 spring-cloud-starter-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>
<groupId>com.lwc</groupId>
<artifactId>eureka-config-server-cluster</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-config-server-cluster</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在 application-dev.yml ,指定服務端口為8889,配置服務注冊中心,代碼如下:
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
啟動類:
package com.lwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author Eddie
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaConfigServerClusterApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConfigServerClusterApplication.class, args);
}
}
二、修改 eureka-config-server
在 pom.xml File 引入 Eureka 的起步依賴 spring-cloud-starter-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>
<groupId>com.lwc</groupId>
<artifactId>eureka-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-config-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件application-dev.yml,指定服務注冊地址為http://localhost:8889/eureka/,其他配置同上一篇文章,完整的配置如下:
server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8889/eureka/
spring:
cloud:
config:
server:
git:
uri: https://github.com/eddie-code/SpringCloudDemo
search-paths: config-repo
username:
passphrase:
label: master
並且啟動類添加@EnableConfigServer和@EnableEurekaClient的注解
三、修改 eureka-config-client
將其注冊微到服務注冊中心,作為Eureka客戶端,需要pom文件加上起步依賴spring-cloud-starter-eureka,代碼如下:
<?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>
<groupId>com.lwc</groupId>
<artifactId>eureka-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-config-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
配置文件bootstrap-local.yml,注意是bootstrap。加上服務注冊地址為 http://localhost:8889/eureka/
server:
port: 8881
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8889/eureka/
spring:
cloud:
config:
label: master
profile: local
discovery:
serviceId: eureka-config-server
enabled: true
spring.cloud.config.discovery.enabled 是從配置中心讀取文件。
spring.cloud.config.discovery.serviceId 配置中心的servieId,即服務名。
這時發現,在讀取配置文件不再寫ip地址,而是服務名,這時如果配置服務部署多份,通過負載均衡,從而高可用。
依次啟動
- eureka-config-server-cluster
- eureka-config-server
- eureka-config-client
訪問網址:http://localhost:8889/
訪問 http://localhost:8881/configs ,瀏覽器顯示:
This is local config Blog: http://www.cnblogs.com/EddieBlog/
四、源碼下載
標簽 7-1
https://github.com/eddie-code/SpringCloudDemo/tree/master/eureka-config-server-cluster
五、踩過的坑
留意注解
六、理解
如果有親手做過的同學應該會發現, 我們是新建一個注冊中心, 再將配置服務端和配置客戶端一起注冊去中心, 其余的和第一篇的差不多一樣, 具體理解還是看個人, 師傅帶入門, 修行靠自己!


