SpringClud一站式学习之Eureka服务治理(二)
SpringClud一站式学习之Eureka服务治理
- 引言
- 1. 搭建Eureka Server
- 1.1. 添加Eureka Server依赖
- 1.2. 添加 Eureka Server注解
- 1.3. 配置Eureka Server
- 1.4. 运行Eureka Server
- 2. 搭建Eureka Client 服务提供者
- 2.1. 添加依赖
- 2.2. 添加注解
- 2.3. 配置Eureka Client
- 2.4. 启动服务
- 3. 搭建Eureka Client 消费者
- 3.1. 添加依赖
- 3.2. 配置
- 3.3. 服务消费者获取服务提供者信息
- 3.4. 启动服务消费者
- 4 搭建集群
引言
Eureka是Netflix开源的一款服务发现框架,它主要用于在微服务架构中定位服务,是服务之间调用的枢纽和关键。在微服务架构中,服务实例可能会动态地增加或减少,Eureka提供了服务注册和发现的功能,使得服务实例可以相互发现对方,而不需要硬编码服务地址。
Eureka的两个主要组件:
Eureka Server:服务注册中心,用于维护各个微服务实例的注册信息,各个微服务实例在启动时会向Eureka Server注册自己的信息,并定期发送心跳以表明自己的存活状态。当服务实例关闭或者网络问题导致心跳丢失时,Eureka Server会从注册信息中移除该实例。
Eureka Client:服务提供者和消费者都会使用Eureka Client来与Eureka Server进行通信。服务提供者在启动时会向Eureka Server注册自己的服务地址和端口,服务消费者通过Eureka Server查询服务提供者的地址,然后直接调用。
Eureka是Spring Cloud体系中的核心组件之一,它与Spring Cloud的其他组件(如Ribbon、Feign、Hystrix等)协同工作,提供了一套完整的微服务解决方案。通过Eureka,开发者可以更容易地实现服务的注册与发现,从而构建和管理复杂的微服务系统。
1. 搭建Eureka Server
1.1. 添加Eureka Server依赖
新建立springboot工程,添加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 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.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eurakaservertrue</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurakaservertrue</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</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>
1.2. 添加 Eureka Server注解
在启动类上添加注解,表明为Eureka Server
@EnableEurekaServer
1.3. 配置Eureka Server
spring.application.name=eurakaservertrue
server.port=8761
#从注册表中获取信息
eureka.client.fetch-registry=false
#允许自己注册到服务中
eureka.client.register-with-eureka=false
1.4. 运行Eureka Server
访问http://localhost:8761/ 出现此界面表示已经搭建好了服务
2. 搭建Eureka Client 服务提供者
2.1. 添加依赖
新建立一个spirngboot工程,并添加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 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.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>erakaclientprovide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>erakaclientprovide</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</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.2. 添加注解
@EnableDiscoveryClient
2.3. 配置Eureka Client
spring.application.name=erakaclientprovide
server.port=8081
eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
#自定义实例ID
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
2.4. 启动服务
3. 搭建Eureka Client 消费者
3.1. 添加依赖
<?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.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>erakaclientconsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>erakaclientconsumer</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</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>
3.2. 配置
spring.application.name=erakaclientconsumer
server.port=8082
eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
#自定义实例ID
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
3.3. 服务消费者获取服务提供者信息
编写controller 方法获取
package com.example.erakaclientconsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Auther: lifang
* @Description
* @Date: $ $
* $
* $
**/
@RestController
public class EurekeController {
@Autowired
private DiscoveryClient discoveryClient;
//获取Eureka服务中所有的提供者信息
@GetMapping("/instance")
public List<ServiceInstance> getApplication()
{
List<ServiceInstance> instances=discoveryClient.getServices().stream().
map(sid->discoveryClient.getInstances(sid))
.collect(Collectors.toList())
.stream().flatMap(list->list.stream()).collect(Collectors.toList());
return instances;
}
}
3.4. 启动服务消费者
访问 http://localhost:8761/
访问 http://localhost:8082/instance
[
{
"metadata": {
"management.port": "8082"
},
"secure": false,
"uri": "http://localhost:8082",
"instanceInfo": {
"instanceId": "erakaclientconsumer:192.168.10.1��8082",
"app": "ERAKACLIENTCONSUMER",
"appGroupName": null,
"ipAddr": "192.168.10.1",
"sid": "na",
"homePageUrl": "http://localhost:8082/",
"statusPageUrl": "http://localhost:8082/actuator/info",
"healthCheckUrl": "http://localhost:8082/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "erakaclientconsumer",
"secureVipAddress": "erakaclientconsumer",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "localhost",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 30,
"durationInSecs": 90,
"registrationTimestamp": 1731075179095,
"lastRenewalTimestamp": 1731075329123,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1731075179095
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"management.port": "8082"
},
"lastUpdatedTimestamp": 1731075179095,
"lastDirtyTimestamp": 1731075179034,
"actionType": "ADDED",
"asgName": null
},
]
4 搭建集群
集群多个节点组成的服务器群,一个Eureka Server服务是一个节点,不同节点之间服务信息是相互复制的,对于Eureka Server不同的节点代码是一样的,不同的是地址不同,因此我们通过配置不同环境的方法启动2个Eureka Server,创建 application-A.properties 和application-B.properties,在application.properties,指定当前的配置文件,启动后更改下一个节点的配置文件;
server.port=8762
eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
server.port=8761
eureka.client.service-url.defaultZone: http://localhost:8762/eureka/
spring.profiles.active=B