结构
如图所示,eureka 模块就是 eureka 服务端,另外 2 个模块都属于 eureka 客户端,无论是服务提供方还是服务发现方。它们的配置也是相同,不同的是使用方式。
依赖配置
<?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>com.xiangxue.jack</groupId>
<artifactId>micro-order</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>micro-order</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<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-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
</project>
bootstrap.properties 配置
spring.application.name=micro-order
server.port=8084
eureka.client.serviceUrl.defaultZone=http://localhost:8763/eureka/
#服务续约,心跳的时间间隔 单位 秒
eureka.instance.lease-renewal-interval-in-seconds=30
#如果从前一次发送心跳时间起,服务端90秒没接受到新的心跳,讲本剔除服务
eureka.instance.lease-expiration-duration-in-seconds=90
#间隔多久去拉取服务注册信息,默认为30秒 有可能还没剔除本问题服务,发现方请求错误
eureka.client.registry-fetch-interval-seconds=30
#健康检测 可以配合配置类做粒度更细的健康检测,如数据库连接失败就把服务连接状态置down
eureka.client.healthcheck.enabled=true
SpringBoot 启动类配置
//开启eureka客户端功能
@EnableEurekaClient
其它相关配置
健康检测相关配置
依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置类
@Configuration
public class MicroWebHealthIndicator implements HealthIndicator {
@Override
public Health health() {
//这个状态就是数据库是否连接OK,自己编写
if(UserController.canVisitDb) {
return new Health.Builder(Status.UP).build();
} else {
return new Health.Builder(Status.DOWN).build();
}
}
}
@RestController
@RequestMapping("/user")
public class UserController {
public static boolean canVisitDb = true;
/*
* 这个接口只为了检测db连接是否ok
* */
@RequestMapping("/db/{can}")
public void setDb(@PathVariable boolean can) {
canVisitDb = can;
}
}