SpringCloud之eureka简单实例(springboot2.2.2RELEASE)
程序员文章站
2022-06-13 15:32:11
...
一. Eureka简介
Eureka是Netflix开源的服务发现组件,本身是基于Rest的服务,它包含服务端和客户端两部分;在SpringCloud中将它集成在其中,从而实现了微服务的发现与注册;
- Eureka的GitHub:https://github.com/Netflix/Eureka
二. Eureka Server说明
- 服务端:服务实例需要执行服务注册、发送心跳去续约(服务续约)、服务下线等操作。
- 客户端:在内存中缓存着eureka的注册信息,因此不必每请求到eureka查找服务,需要执行获取注册服务列表,并消费服务等操。
三. 实例
服务端(github地址:https://github.com/dingkaige/eureka-service)
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.edu.springboot</groupId>
<artifactId>eureka-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类
加上@EnableEurekaServer
application.xml:
spring.application.name=eureka-server
server.port=8025
# logging 配置
logging.config=classpath:log4j2.xml
#运行环境
eureka.environment=dev
eureka.instance.hostname=127.0.0.1
#由于该应用是注册中心,false:代表不向注册中心注册自己;true:代表注册自己。多节点时打开
eureka.client.register-with-eureka=false
#是否启动检测服务,表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=false
#注册地址 多节点用,分隔
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#是否开启保护模式 测试时,需要关闭,线上打开
eureka.server.enable-self-preservation=false
运行结果
红字的意思是:自动保存模式关闭。在网络/其他问题的情况下,这可能无法保护实例失效。
关于Eureka自我保护机制可以参考:Eureka自我保护机制
客户端(github地址:https://github.com/dingkaige/eureka-client)
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.edu.springboot</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类
加上@EnableDiscoveryClientr
application.xml:
spring.application.name=eureka-client
server.port=8030
# logging 配置
logging.config=classpath:log4j2.xml
#ribbon 饥饿加载
#ribbon.eager-load.enabled=true
#指定需要饥饿加载的客户端名称、服务名
#ribbon.eager-load.clients=eureka-client,eureka-server
#ribbon.ConnectTimeout=90000
#ribbon.ReadTimeout=90000
#根据ip注册实例
eureka.instance.prefer-ip-address=true
#指定注册实例ID(默认是主机名:应用名:应用端口)
#eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
#指定注册实例主机名
#eureka.instance.hostname=127.0.0.1
#eureka.instance.hostname= ${spring.cloud.client.ip-address}
#注册地址 eureka服务端的地址 多节点用,分隔
eureka.client.service-url.defaultZone=http://127.0.0.1:8025/eureka/
运行结果
扩展
eureka的用户端和服务端要使用同一版本,具体版本如下(也可以去SpringCloud官网查询:https://spring.io/projects/spring-cloud):
查询到版本后可以到https://mvnrepository.com/搜索下载
上一篇: Mybatis入门配置