1.1、什么是Eureka
和Consul、Zookeeper类似,Eureka是一个用于服务注册和发现的组件,主要分为Eureka Server(服务注册中心)和Eureka Client(客户端,提供接口服务)。
1.2、Eureka的基本架构
a、Register Service:服务注册中心,是一个Eureka Server,提供服务注册和发现功能;
b、Provider Service:服务提供者,是一个Eureka Client,可以提供服务;
c、Consumer Service:服务消费者,也是一个Eureka Client,服务消费者;
1.3、源码简单实现demo
demo将采用多个Spring Boot工程,使用Maven的多Module结构,项目结构如下:
eureka-memo工程中引入各个module共同需要的依赖,主Maven工程的pom文件代码(注意springboot版本和springcloud的版本兼容问题):
<?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>
<packaging>pom</packaging>
<groupId>com.hzlitai</groupId>
<artifactId>eureka-memo</artifactId>
<version>1.1-SNAPSHOT</version>
<modules>
<module>eureka-server</module>
<module>eureka-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</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>
</project>
创建一个命名为eureka-server的module,采用Spring Initializr的方式创建,并引入相关依赖,pom文件代码如下:
<?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.hzlitai</groupId>
<artifactId>eureka-memo</artifactId>
<version>1.1-SNAPSHOT</version>
</parent>
<groupId>com.hzlitai</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在application.yml文件中增加配置信息,代码如下:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
在EurekaServerApplication类中增加@EnableEurekaServer注解,支持EurekaServer
然后通过在浏览器访问:http://localhost:8761,如果显示Eureka Server的UI界面,即为成功。
和Server模块类似,建立Client模块,其中pom文件源码如下:
<?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.hzlitai</groupId>
<artifactId>eureka-memo</artifactId>
<version>1.1-SNAPSHOT</version>
</parent>
<groupId>com.hzlitai</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置yml文件:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: eureka-client
最后增加eureka client注解:
启动客户端,客户端自动注册到注册中心,如下图所示:
最后可以写个Controller进行接口测试,代码如下:
@RestController
public class HiController {
@Value("${server.port}")
String port;
@GetMapping("/hi")
public String home(@RequestParam String name){
return "hi "+name+" ,I am from port:"+port;
}
}
在浏览器访问:http://localhost:8764/hi?name-zhuheliang,则结果显示如下:
hi zhuheliang,I am from port:8764
2、构建高可用的Eureka Server集群
在项目中,实际上存在很多微服务实例,那么Server要承担非常高的负载,而Server必须保证要提供持续发服务,所以对Server进行高可用的集群处理。
在之前工程的基础上进行改造,修改eureka-server的配置文件application.yml,采用多profile格式进行配置,代码如下:
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
hostname: peer1
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8763/eureka/,http://localhost:8762/eureka/
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
hostname: peer2
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
---
spring:
profiles: peer3
server:
port: 8763
eureka:
instance:
hostname: peer3
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8762/eureka/,http://localhost:8761/eureka/
接下来使用idea启动server端的三个实例,配置如下:
进入配置页面进行配置:
启动三个实例后,最后启动客户端,访问:http://localhost:8761/、http://localhost:8762、http://localhost:8763,三个节点的主界面均可显示client都已经注册成功。