一、SpringCloud Eureka介绍
Eureka是SpringCloud中的一个负责服务注册与发现的组件,是由 Netfix开发的服务发现框架,本身是一个基于REST的服务,遵循CAP理论的AP原则,
一个Eureka可分为Eureka Server和Eureka Client,Eureka Server提供服务注册服务,各个几点启动后,会在Eureka Server中注册,这样Eureka Server中的服务注册列表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中清晰的看到。
Eureka采用C-S架构,EurekaClient是一个java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器,在应用启动后,会向EurekaServer发送心跳(默认周期为30秒),如果EurekaServer在多个心跳周期内没有收到心跳,EurekaServer会将这个服务移除出去。
服务生产者将信息注册到Eureka,从而能使消费者能够找到,服务消费者从Eureka获取注册列表,从而能找到消费服务,这里服务生产者与服务消费者都为ureka Client
二、Eureka Server
这里实现单个的Eureka Server,新建一个SpringBoot项目
pom.xml导入EurekaServer的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<?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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>sprincloud-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sprincloud-eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2020.0.3</spring-cloud.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> <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.yml
server: port: 8761 spring: application: name: springcloud-eureka eureka: instance: hostname: localhost client: register-with-eureka: false #false标识不向注册中心注册子级 fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类添加注解@EnableEurekaServer
启动项目,访问localhost:8761即可看到服务列表页面
第一次启动这个地方Application应该没有任何服务
三、Eureka Client
1.添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<?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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>springcloud-ekreka-client01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud-ekreka-client01</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2020.0.3</spring-cloud.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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>
2.application.yml,配置刚才注册中心的地址
server:
port: 8090
spring:
application:
name: eureka-client-01
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3.启动类添加注解@EnableEurekaClient
启动项目可以在服务列表中看到我们刚才新添加的服务