开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第4天,点击查看活动详情
什么是eureka
eureka 它是 spring cloud Netflix 中提供的一个子模块,也是spring cloud Netflix 中核心模块之一,它是一个基于 REST 的服务,主要的作用就是针对于服务的注册与发现,这个针对微服务来讲的话是非常重要的,它的功能类似于 zookeeper。
它是由什么组成的
eureka 它采用的是 C-S 的架构,并且也是基于 spring-boot 我们使用的时候直接可以通过maven进行构建,它主要包含以下两个组件:
1、Eureka Server
2、Eureka Client
Eureka Server
其中 Eureka Server 是作为一个服务注册功能的服务器,它也就是服务的注册中心,当我们各个节点启动后应注册到 Eureka Server 当中
Eureka Client
Eureka Client 则作为各种消费者注册到Eureka Server中。
搭建 Eureka Server
搭建 Eureka Server 首先需要创一个 spring-boot 项目,然后引入对应的 spring-cloud 依赖包、eureka 的依赖包,最后通过对应的配置就可以创建一个 Eureka Server 供微服务中的其他服务进行注册。
创建 spring boot 项目
首先创建一个spring boot 的项目,在创建的时候其实我们也可以选择对应的依赖包,但是这里不进行选择,直接创建一个空的spring boot项目。
注意:这里为什么不直接在创建的时候通过 idea 直接构建 spring-cloud 项目,因为之前我就是这样干的,然后发现每次创建的时候都会有问题,这里就直接创建一个新的空项目。
引入 spring-cloud-starter-netflix-eureka-server 包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<!-- <version>2.7.7-SNAPSHOT</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spring_cloud</groupId>
<artifactId>eureka_server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka_server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<!-- <spring-cloud.version>2021.0.5</spring-cloud.version>-->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.4.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <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-web</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>-->
注意:上面注释的部分就是通过 idea 创建项目的时候自己生成的。
添加配置文件信息
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka_server
其中 hostname: localhost 为 注册中心的注册名
register-with-eureka: false:配置为false,表示不将自己注册到注册中心。
fetch-registry: false:配置为false,表明该注册中心只负责管理实例,不负责去检索实例服务。
添加好配置文件后在启动类中添加 eureka 的 @EnableEurekaServer 注解,表示这个服务是 eureka 服务。
启动成功后访问配置中配置好的访问路径
到这里eureka_server 的构建就已经结束了,之后就运行着等待其他服务注册过来。