Spring Cloud-Eureka

Spring Cloud-Eureka

Eureka两大组件

  • Eureka Server 服务注册中心,用于提供服务注册功能,当一个客户端启动后,会把自己注册到服务注册中心,在其他微服务调用的时候,会通过服务注册中心拉取自己需要的服务
  • Eureka Client Eureka客户端,简单来说就是各个微服务,对于Eureka来说,Client会想服务注册中心周期性地发送心跳,若超过这个周期没有发送心跳,Server就会把该服务给移除

Eureka服务注册与服务发现

Eureka服务注册与服务发现原理图:

Eureka 服务注册与发现

  • 服务注册中心(Register Service):它是一个 Eureka Server,用于提供服务注册和发现功能。
  • 服务提供者(Provider Service):它是一个 Eureka Client,用于提供服务。它将自己提供的服务注册到服务注册中心,以供服务消费者发现。
  • 服务消费者(Consumer Service):它是一个 Eureka Client,用于消费服务。它可以从服务注册中心获取服务列表,调用所需的服务。

注意到提供者和消费者都是一个Client,在我刚开始看的时候,我误认为提供者就是一个Eureka Server。我觉得可以这样理解,提供者就是一个函数,消费者就是调用这个函数的函数。

Eureka具体搭建流程为:

image-20220807142755028

Eureka搭建

创建主工程

创建一个空的maven工程

image-20220807143454628

pom文件配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?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>org.example</groupId>
<artifactId>spring-cloud-demo1</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath/>
</parent>

<!--声明一系列插件版本-->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
</properties>


<dependencyManagement>
<dependencies>
<!--在主工程中使用 dependencyManagement 声明 Spring Cloud 的版本,
这样工程内的 Module 中引入 Spring Cloud 组件依赖时,就不必在声明组件的版本信息
保证 Spring Cloud 各个组件一致性-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>

</project>

创建公共子模块

image-20220807143649437

子模块全部使用SpringBoot进行创建

在pom文件中添加parent属性,该属性为主工程的信息

1
2
3
4
5
<parent>
<artifactId>spring-cloud-demo1</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

micro-service-cloud-api 是整个工程的公共子模块,它包含了一些其他子模块共有的内容,例如实体类、公共工具类、公共依赖项等。当其他子模块需要使用公共子模块中的内容时,只需要在其 pom.xml 引入公共子模块的依赖即可。

在子模块中创建实体类

image-20220807144143133

Accessors属性具体见:

  • 写Accessors属性详情

创建Eureka Server

  • 创建子模块 micro-service-cloud-eureka-7001

  • 同样在pom中添加父工程属性

  • 引入服务依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  • 在application.yml文件中做相关配置
1
2
3
4
5
6
7
8
9
10
11
server:
port: 7001

eureka:
instance:
hostname: localhost #实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #单机版服务注册中心
  • 在主启动类上添加 @EnableEurekaServer 注解,接受其他服务的注册

  • 启动注册中心,访问localhost:7001

    image-20220807145203246

创建服务提供者(Eureka Client)

  • 其他的都和单个开发SpringBoot一样,主要是pom文件
1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.example</groupId>
<artifactId>micro-service-cloud-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

第一个dependency是引入之前的公共子模块

第二个dependency导入Eureka Client

  • 配置Application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
server:
port: 8001
spring:
application:
name: microServiceCloudProviderDept #微服务名称,对外暴漏的微服务名称
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://XXX.XX.XX.XX:3306/tbl_dept?useSSL=false
username: XXXX
password: XXX
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml # 指定 mapper.xml 的位置
type-aliases-package: com.example.pojo #扫描实体类的位置,在此处指明扫描实体类的包,在 mapper.xml 中就可以不写实体类的全路径名
configuration:
map-underscore-to-camel-case: true

eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #这个地址是 7001注册中心在 application.yml 中暴露出来的注册地址
instance:
instance-id: spring-cloud-provide-8001 #对应的服务名称
prefer-ip-address: true

########################################## spring cloud 使用 Spring Boot actuator 监控完善信息###################################
# Spring Boot 2.50对 actuator 监控屏蔽了大多数的节点,只暴露了 heath 节点,本段配置(*)就是为了开启所有的节点
management:
endpoints:
web:
exposure:
include: "*" # * 在yaml 文件属于关键字,所以需要加引号
info:
app.name: micro-service-cloud-provider-dept
company.name: com.example
build.aetifactId: @project.artifactId@
build.version: @project.version@
  • 常规的写接口