1. Eureka 基础知识
1.1 服务治理
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理。
在传统的 RPC 远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,所以需要使用服务治理,管理服务与服务之间的依赖关系,可以实现服务调用、负载均衡、容错等,实现服务的发现与注册。
1.2 服务注册
Eureka 采用了 CS 的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心,而系统中的其他微服务,使用 Eureka Client 客户端连接到 Eureka Server 并维持心跳,这样系统维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。
在服务发现与注册中,有一个注册中心,当服务器启动的时候,会把自己当前服务器的信息,比如 服务地址、通讯地址等以别名方式注册到注册中心上,另一方(消费者 | 服务提供者),以该别名的方式去注册中心获取到实际的服务通讯地址,然后再实现本地 RPC 调用、RPC远程调用。
1.3 Eureka 两组件
包含两个组件: Eureka Server 和 Eureka Client。
Eureka Server:提供服务注册服务;
各个微服务结点通过配置启动后,会在 Eureka Server 中进行注册,这样 Eureka Server 中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
Eureka Client:通过注册中心进行访问;
是一个 Java 客户端,用于简化 Eureka Server 的交互,客户端同时也具备内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向 Eureka Server发送心跳(默认周期为 30秒),如果 Eureka Server 在多个心跳周期内没有接收到某个节点的心跳,Eureka Server 会自动将这个节点从服务注册表中剔除掉(90秒)。
2. 单机 Eureka 构建
2.1 建 module
- cloud-eureka-server7001
2.2 改 pom.xml
<?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">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>com.lele.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server7001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.lele.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
2.3 application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost #Eureka 服务端实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己就是注册中心,职责就是未付服务实例,并不需要检索服务
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/
2.4 启动类
package com.lele.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author: lele
* @date: 2021/3/4 7:21
* @description:
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
2.5 测试
- 启动当前模块;
- 访问 localhost:7001,如图所示
3. cloud-provider-payment8001 修改
EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider。
- pom文件新增:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yml新增:
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
- 主启动类
package com.lele.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author: lele
* @date: 2021/2/28 20:52
* @description:
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
4. cloud-consumer-order80 修改
EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer。
- pom 文件新增:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yml 新增:
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
- 主启动:
package com.lele.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author: lele
* @date: 2021/3/1 21:19
* @description:
*/
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
5. 测试
启动Eureka 、provider、consumer模块:
浏览器访问:http://localhost/consumer/payment/get/1