一 、搭建Eureka
编写Eureka Server
由于有多个spring boot项目,采用maven多module的结构,项目结构如下:
新建一个maven主工程,在主maven的pom文件中引入spring boot和spring cloud的依赖
<?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.cralor</groupId> <artifactId>chap5-2-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.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>Finchley.RELEASE</spring-cloud.version> </properties> <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> <!--使用aliyun镜像--> <repositories> <repository> <id>alimaven</id> <name>Maven Aliyun Mirror</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
采用spring initializr 方式新建module工程eureka-server,作为服务注册中心。在pom文件中引入相关依赖包括继承主maven工程的pom文件,eureka server的起步依赖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> <groupId>com.cralor</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> <parent> <groupId>com.cralor</groupId> <artifactId>chap5-2-eureka</artifactId> <version>0.0.1-SNAPSHOT</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> </properties> <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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在配置文件application.yml中做相关配置,指定eureka server 的端口号为8761,默认eureka server会向自己注册,需要配置eureka.client.registerWithEureka和eureka.client.fetchRegistry为false,防止自己注册自己。
server: port: 8761 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在工程启动类加上注解@EnableEurekaServer,开启eureka server功能
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
启动程序,在浏览器访问eureka server 主界面http://localhost:8761,在上面的Instance currently registered with Eureka这一项还没有注册任何实例,因为还没有eureka client 客户端向注册中心注册服务。
编写Eureka Client
新建module工程eureka-client,该工程作为Eureka Client的工程会向服务注册中心Eureka Server注册。在pom文件引入相关依赖包括eureka client起步依赖spring-cloud-starter-netflix-eureka-client
<?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.cralor</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.cralor</groupId> <artifactId>chap5-2-eureka</artifactId> <version>0.0.1-SNAPSHOT</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> </properties> <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.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在配置文件中做相关配置,程序名为eureka-client,端口号8762,服务注册地址为http://localhost:8761/eureka/。
server: port: 8762 spring: application: name: eureka-client eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
启动类保持默认生成的即可, Finchley 这个版本的 Spring Cloud 已经无需添加@EnableDiscoveryClient
注解了。(那么如果我引入了相关的 jar 包又想禁用服务注册与发现怎么办?设置eureka.client.enabled=false
)
@EnableDiscoveryClient
is no longer required. You can put a DiscoveryClient
implementation on the classpath to cause the Spring Boot application to register with the service discovery server.
Spring Cloud - @EnableDiscoveryClient
@SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
启动eureka client工程,成功后会在控制台打印
:DiscoveryClient_EUREKA-CLIENT/DESKTOP-R9RM7VT:eureka-client:8762: registering service...
DiscoveryClient_EUREKA-CLIENT/DESKTOP-R9RM7VT:eureka-client:8762 - registration status: 204
说明eureka client已向eureka server注册了,访问http://localhost:8761,显示已有一个实例注册
二、构建高可用Eureka Server集群
1、在实际项目中,一个注册中心远远无法满足实际的生产环境,我们需要多个注册中心进行集群,达到真正的高可用。
首先更改eureka-server的配置文件application.yml,采用多配置文件格式,在启动时指定具体的配置文件
spring: profiles: active: native application: name: peer-cluster
application-peer1.yml
server: port: 8761 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8762/eureka/
application-peer2.yml
server: port: 8762 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8761/eureka/
定义了两个配置文件peer1和peer2,hostname分别是peer1和peer2,并且在application.yml定义了相同的实例名称peer-cluster。peer1的注册地址为http://peer2:8762/eureka/,peer2的注册地址为http://peer1:8761/eureka/,让它们互相注册,以达到同步的目的。
因为是在本地搭建eureka server集群,需要修改本地的host。Windows在C:\Windows\System32\drivers\etc\hosts中修改,mac电脑通过终端vim/etc/hosts进行编辑修改,修改如下:
127.0.0.1 peer1
127.0.0.1 peer2
使用maven的package命令生成eureka server的jar包,在target目录下会生成eureka-server-0.0.1-SNAPSHOT.jar。通过java -jar的方式分别启动peer1和peer2,命令如下:
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
访问http://peer1:8761和http://peer2:8762如图
可以看到peer1和peer2分别为对方的实例并且registered-replicas和available-replicas分别有了对方的地址
2、修改eureka-client的配置文件
server:
port: 8763
spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka
此时eureka-client的配置文件并没有指定向peer2的节点eureka server注册,访问peer2主界面http://peer2:8762/eureka。节点peer2的主界面显示eureka client已向peer2注册,可见peer1的注册信息列表已经同步到了peer2节点,这是因为eureka是通过在各个节点进行复制来达到高可用的目的。
ps:关于spring cloud组件的这几篇随笔文章是在我学习方志朋老师的《深入理解Spring Cloud与微服务构建》一书的过程中写的。微信号:walkingstory。
写的非常好的文章:https://windmt.com/2018/04/14/spring-cloud-1-services-governance/
案例代码地址:https://github.com/cralor7/springcloud