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

- 服务注册中心(Register Service):它是一个 Eureka Server,用于提供服务注册和发现功能。
- 服务提供者(Provider Service):它是一个 Eureka Client,用于提供服务。它将自己提供的服务注册到服务注册中心,以供服务消费者发现。
- 服务消费者(Consumer Service):它是一个 Eureka Client,用于消费服务。它可以从服务注册中心获取服务列表,调用所需的服务。
注意到提供者和消费者都是一个Client,在我刚开始看的时候,我误认为提供者就是一个Eureka Server。我觉得可以这样理解,提供者就是一个函数,消费者就是调用这个函数的函数。
Eureka具体搭建流程为:

Eureka搭建
创建主工程
创建一个空的maven工程

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>
<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>
|
创建公共子模块

子模块全部使用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 引入公共子模块的依赖即可。
在子模块中创建实体类

Accessors属性具体见:
创建Eureka Server
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11
| server: port: 7001
eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
|
创建服务提供者(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
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 type-aliases-package: com.example.pojo configuration: map-underscore-to-camel-case: true
eureka: client: service-url: defaultZone: http://localhost:7001/eureka instance: instance-id: spring-cloud-provide-8001 prefer-ip-address: true
management: endpoints: web: exposure: include: "*" info: app.name: micro-service-cloud-provider-dept company.name: com.example build.aetifactId: @project.artifactId@ build.version: @project.version@
|