一、Eureka微服务发现
二、编写Eureka Server
三、编写Eureka Client
1、服务提供者
2、服务消费者
四、Eureka Server集群
五、将Eureka Client注册到Eureka Server集群中
六、为Eureka Server添加用户认证
七、Eureka的自我保护模式
八、多网卡服务器的IP选择
九、Eureka的健康检查
一、Eureka微服务发现
在微服务中主要有两类角色:服务提供者和服务消费者,连接两者的一个非常重要的组件就是服务发现组件。
他们三者之间的关系大致如下:
- 各微服务启动时将自身的网络地址等信息注册到服务发现组件中进行存储
- 服务消费者可以从服务发现组件查询服务提供者的网络地址,并使用该地址调用服务提供者的接口
- 各服务与服务发现组件使用一些机制进行通信(例如心跳包),若微服务与服务发现组件长时间无法通信则服务发现组件会注销该服务实例
- 微服务的网络地址变化是会重新注册到服务发现组件
Eureka是Netflix开源的服务发现组件,本身基于REST服务,包含了Server和Client两部分,如下图。
图中:
- Application Client相当于服务消费者
- Application Server相当于服务提供者
Eureka Server提供服务发现功能,各个微服务也就是Application Client或者说Eureka Client启动时会想Eureka Server注册自己的信息(网络地址、端口、微服务名称等等),多个Eureka Server之间互相通过复制的方式来实现服务注册表中的数据同步。当然默认情况下Eureka Server也是Eureka Client(不明白的伙伴可以暂时往下看)。
Eureka Client就是一个Java应用程序,一个微服务,它启动时就会注册到Eureka Server中,然后周期(默认30s)向Eureka Server发送心跳以在Eureka Server中保持实例的存在(长时间不通信Eureka Service会注销掉该微服务(默认90s))。Eureka Client会缓冲服务注册表中的信息,这样无须每次请求就去查询Eureka Server,从而降低Eureka Server的压力,其次一定程度避免Eureka Server全部宕机后服务消费者依然可以通过缓冲的信息进行接口调用。
二、编写Eureka Server
1、创建项目eureka-server的Spring Boot项目
(不清楚怎么创建Spring boot的伙伴可以查看这篇文章哦!)
项目结构
项目的pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springclouddemo</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>服务发现</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.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-server</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、在启动类上添加声明该应用为Eureka Server的注解@EnableEurekaServer
package com.springclouddemo.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3、编写配置文件applicaton.properties
新创建的Spring Boot项目中的applicaton.properties默认是空内容的,我们追加以下内容:
server.port=7000
eureka.client.service-url.default-zone=http://localhost:7000/eureka/
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
- server.port 设置应用启动端口
- eureka.client.service-url.default-zone 设置Eureka Server地址
- eureka.client.register-with-eureka 设置是否将自己注册到Eureka Server(默认为true)
- eureka.client.fetch-registry 设置是否从Eureka Server获取注册信息(默认为true),单Eureka Server不需要同步信息,而多Eureka Server则需要同步信息
注:该应用本身为Eureka Server所以不需要注册到Eureka Server(难不成自己注册到自己)
4、启动main类,访问测试
在浏览器输入http://localhost:7000就可以访问Eureka的web页面了
通过Eureka Web页面我们可以查看到当前注册实例(微服务)的很多信息。
三、编写Eureka Client
1、服务提供者
1、创建项目eureka-client-provider
以下是目录结构
以下是pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springclouddemo</groupId>
<artifactId>eureka-client-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-provider</name>
<description>Eureka client-服务提供者</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.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.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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、在启动类上添加声明该应用为Eureka Server的注解@EnableEurekaClient
package com.springclouddemo.eurekaclientprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientProviderApplication.class, args);
}
}
3、编写配置文件applicaton.properties
spring.application.name=eureka-client-provider
server.port=7100
eureka.client.service-url.defaultZone=http://localhost:7000/eureka/
eureka.instance.prefer-ip-address=true
- eureka.instance.prefer-ip-address 设置true表示将自己的IP注册到Eureka Server上,false表示将应用所在系统的hostname注册到Eureka Server上
- spring.application.name 设置注册到Eureka Server的应用名称
4、编写controller
编写controllers/DemoController.java
package com.springclouddemo.eurekaclientprovider.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 何昌杰
*/
@RestController
public class DemoController {
@GetMapping("/hello/{name}")
public String demo1(@PathVariable("name") String name){
return "hello "+name;
}
}
5、启动main类,访问测试
在浏览器输入http://localhost:7000就可以访问Eureka的web页面,可以看到刚才创建的应用已经注册到Eureka Server上了。
访问http://localhost:7100/hello/hcj接口正常
2、服务消费者
1、创建项目eureka-client-consumer
项目结构:
pom.xml文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springclouddemo</groupId>
<artifactId>eureka-client-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.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.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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、在启动类上添加声明该应用为Eureka Server的注解@EnableEurekaClient和RestTemplate
package com.springclouddemo.eurekaclientconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author 何昌杰
*/
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumerApplication.class, args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
3、编写applicaton配置文件
spring.application.name=eureka-client-consumer
server.port=7200
eureka.client.service-url.defaultZone=http://localhost:7000/eureka/
eureka.instance.prefer-ip-address=true
4、编写controllers/DemoController.java
package com.springclouddemo.eurekaclientconsumer.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author 何昌杰
*/
@RestController
public class DemoController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello/{name}")
public String demo1(@PathVariable("name") String name){
return this.restTemplate.getForObject("http://localhost:7100/hello/"+name,String.class);
}
}
5、启动main类,访问测试
在浏览器输入http://localhost:7000就可以访问Eureka的web页面,可以看到刚才创建的应用已经注册到Eureka Server上了。
访问http://localhost:7200/hello/hcj 接口正常
说明调用eureka-client-provider接口正常
四、Eureka Server集群
第二节讲述了如何进行单节点的Eureka Server搭建,但并不适合线上生产环境,通常在生产环境中会部署一个高可用的Eureka Server集群。集群中Eureka Server以相互注册的方式实现高可用部署,Eureka Server示例会彼此增量同步信息,从而确保所有节点的服务注册表信息一致。
一个Eureka Client会周期连接Eureka Server,获取服务注册表信息并缓存,当Eureka Server发生宕机时,也不会影响微服务之间的服务调用(接口调用),但如果Eureka Server长时间宕机,Eureka Client的缓存不被更新,就可能影响到微服务的调用。
复制工程eureka-server为eureka-server1和eureka-server2两个工程
两个工程的pom.xml,main类注解相同
1、修改eureka-server1的application配置文件:
spring.application.name=eureka-server
server.port=7001
eureka.instance.hostname=eureka1
eureka.client.service-url.defaultZone=http://eureka2:7002/eureka/
2、修改eureka-server2的application配置文件:
spring.application.name=eureka-server
server.port=7002
eureka.instance.hostname=eureka2
eureka.client.service-url.defaultZone=http://eureka1:7001/eureka/
- eureka.instance.hostname 设置当前实例的主机名称
这里我们让eureka-server与eureka-server2相互注册
注:Eureka-server集群中,每个Eureka Server实例名称需要一致
3、修改系统的hosts文件,添加127.0.0.1与实例主机名的映射
windows的hosts路径:C:\Windows\System32\drivers\etc\hosts,在hosts文件另起一行追加以下内容:
127.0.0.1 eureka1
127.0.0.1 eureka2
注:因为我们是在一个电脑上进行Eureka Server的集群部署所以需要通过修改hosts模拟集群
4、分别启动eureka-server和eureka-server2的mian类
在浏览器输入http://localhost:7001或http://localhost:7002就可以访问Eureka的web页面,可以看到刚才的两个Eureka Server以相互注册。
6、(拓展)使用同一个项目进行Eureka Server集群
通过上面布置我们可以发现,其实两个项目的绝大部分内容都是相同的,在生产环境进行Eureka Server集群部署时,则需要多个jar包,比较浪费存储,我们也可以在一个项目中通过配置多个Applicaton以达到这样的效果
对项目eureka-server的application配置文件修改如下:
spring.application.name=eureka-server
---
spring.profiles=eureka1
server.port=7001
eureka.instance.hostname=eureka1
eureka.client.service-url.defaultZone=http://eureka2:7002/eureka/
---
spring.profiles=eureka2
server.port=7002
eureka.instance.hostname=eureka2
eureka.client.service-url.defaultZone=http://eureka1:7001/eureka/
打包项目后分别使用以下命令启动eureka-server项目,依然可以达到同样的效果
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=eureka1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=eureka2
7、(拓展)使用同一个项目进行Eureka Server集群
除了在一个项目的application中使用连字符(---)将配置文件分段进行不同配置启动外,也可以创建多个配置文件启动时指定配置文件后缀的形式:
资源目录:
application-eureka1.properties内容:
spring.application.name=eureka-server
server.port=7001
eureka.instance.hostname=eureka1
eureka.client.service-url.defaultZone=http://eureka2:7002/eureka/
application-eureka2.properties内容:
spring.application.name=eureka-server
server.port=7002
eureka.instance.hostname=eureka2
eureka.client.service-url.defaultZone=http://eureka1:7001/eureka/
打包项目后同样以4.6节方式启动。
五、将Eureka Client注册到Eureka Server集群中
将Eureka Client(微服务)注册到Eureka Server集群中十分简单,只需要修改Eureka Client实例的application配置文件的eureka.client.service-url.defaultZone即可
将第3节项目eureka-client-provider的配置文件做一下修改:
#原来的配置eureka.client.service-url.defaultZone=http://localhost:7000/eureka/
eureka.client.service-url.defaultZone=http://eureka1:7001/eureka/,http://eureka2:7002/eureka/
启动eureka-client-provider的main类,然后我们查看Eureka Server和Eureka Server2的web页面发现eureka-client-provider实例已注册。
六、为Eureka Server添加用户认证
在前面几节的示例中,Eureka Server是运行匿名访问的,接下来我们就构架一个需要登录才能访问的Eureka Server。
1、复制项目eureka-server为eureka-server-authentication
以下是目录结构:
以下是pom.xml文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springclouddemo</groupId>
<artifactId>eureka-server-authentication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server-authentication</name>
<description>Eureka Server添加认证</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
重点是在原来的Eureka Server项目集成商添加以下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
2、在启动类上添加声明该应用为Eureka Server的注解@EnableEurekaServer
package com.com.springclouddemo.eurekaserverauthentication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerAuthenticationApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerAuthenticationApplication.class, args);
}
}
3、编写配置文件applicaton.properties
server.port=7002
spring.application.name=eureka-server-authentication
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.security.user.name=hcj
spring.security.user.password=123
注:这个使用单节点Eureka Server进行示例
4、启动main类
通过浏览器访问http://localhost:7002进行Eureka Server Web,可以看到以下界面
通过之前设置的用户名和密码即可进入Eureka Server。
5、最新版的Eureka Server需要添加以下配置才可以运行Eureka Client登录:
创建/config/WebSecurityConfig.java
package com.com.springclouddemo.eurekaserverauthentication.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
@Configuration
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable();
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
目录结构:
而后再启动main类
七、将微服务注册到需要认证的Eureka Server
将微服务注册到需要认证的Eureka Server十分简单,自需要将
eureka.client.service-url.defaultZone配置为http://user:password@eureka_hostname:port/eureka/的形式
将项目eureka-client-provider的applicaton配置文件修改如下:
spring.application.name=eureka-client-provider
server.port=7100
eureka.client.service-url.defaultZone=http://hcj:123@localhost:7002/eureka/
启动eureka-client-provider的main类就可以发现eureka-client-provider微服务已被注册到Eureka Server上了,登录Eureka Server Web页面也可以查看到
七、Eureka的自我保护模式
Eureka Server进入自我保护模式最直观的体现就是,Eureka Server Web页面有以下提示语:
默认情况下,如果Eureka Server在一定时间内没有接收到某个微服务实例的心跳,Eureka Server将会注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,这就可能变得非常危险了----因为微服务本身是健康的,此时本不应该注销这个微服务。
Eureka Server通过“自我保护模式”来解决这个问题----当Eureka Server节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,Eureka Server就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。
自我保护模式是一种对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康与不健康的微服务都保留),也不盲目注销任何一个微服务。使用自我保护模式,让Eureka集群更加的健壮、稳定。
可以通过eureka.server.enable-self-preservation = false 关闭Eureka Server的自我保护模式。
八、多网卡服务器的IP选择
对于多网卡的服务器,指定IP十分重要,例如某台服务器有网卡et1,et2两块网卡,但是只有et2可以被其他服务器访问;如果Eureka Client将et1注册到Eureka Server,他们其他微服务将无法通过这个IP调用该微服务的接口。
Spring Cloud提供了按需选择IP的功能,从而避免这个问题
1、忽略指定名称的网卡
在Eureka Client的配置中添加以下内容:
spring.cloud.inetutils.ignored-interfaces=docker0,venus.*
- 忽略docker0网卡和venus开头的网卡
2、正则表达式,指定网络地址
eureka.instance.prefer-ip-address=true
spring.cloud.inetutils.preferred-networks=192.168,10.51.53
- 使用192.168或者10.51.53的网卡
3、使用本地地址
eureka.instance.prefer-ip-address=true
spring.cloud.inetutils.use-only-site-local-interfaces=true
- 强制使用本地地址
4、手动指定IP地址
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=127.0.0.1
也可以动态获取本机IP
eureka.instance.prefer-ip-address=true
eureka.instance.hostname= ${spring.cloud.client.ip-address}
eureka.instance.instance-id= ${spring.cloud.client.ip-address}:${server.port}
不同Spring Cloud版本的${}属性名不同,各位可以移步这篇文件的2.1节查看
九、Eureka的健康检查
在Eureka Server 的web首页我们可以看到以下内容:
有图可见在Status下有一个UP,表示该微服务状态正常,状态还有DOWN、OUT_OF_SERVICE、UNKNOWN等,只有表示为UP的微服务才可以被请求。
1、微服务开启健康检查
在pom.xml添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
依赖添加后健康检查是默认开启的,也可以通过eureka.client.healthcheck.enabled=false 关闭
微服务开启健康检查以后就会将自身的健康状态传播到Eureka Server
启动微服务的main类,访问http://ip:port/actuator/info
Eureka 常用配置项
#服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean
#服务实例类配置 Bean类:org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean
#服务注册类配置 Bean类:org.springframework.cloud.netflix.eureka.EurekaClientConfigBean
#默认值false 关闭注册中心的保护机制,Eureka 会统计15分钟之内心跳失败的比例低于85%将会触发保护机制,不剔除服务提供者,如果关闭服务注册中心将不可用的实例正确剔除
eureka.server.enable-self-preservation=false
#默认值false 不使用主机名来定义注册中心的地址,而使用IP地址的形式,如果设置了
eureka.instance.prefer-ip-address=false
#属性,则使用该属性配置的IP,否则自动获取除环路IP外的第一个IP地址
eureka.instance.ip-address=
#设置当前实例的主机名称
eureka.instance.hostname=
#服务名,默认取 spring.application.name 配置值,如果没有则为 unknown
eureka.instance.appname=
#默认值30 定义服务续约任务(心跳)的调用间隔,单位:秒
eureka.instance.lease-renewal-interval-in-seconds=30
#默认值90 定义服务失效的时间,单位:秒
eureka.instance.lease-expiration-duration-in-seconds=90
#默认值/info 状态页面的URL,相对路径,默认使用 HTTP 访问,如果需要使用 HTTPS则需要使用绝对路径配置
eureka.instance.status-page-url-path=/info
#状态页面的URL,绝对路径
eureka.instance.status-page-url=
#默认值/health 健康检查页面的URL,相对路径,默认使用 HTTP 访问,如果需要使用 HTTPS则需要使用绝对路径配置
eureka.instance.health-check-url-path=/health
#健康检查页面的URL,绝对路径
eureka.instance.health-check-url=
#指定服务注册中心地址,类型为 HashMap,并设置有一组默认值,默认的Key为 defaultZone;默认的Value为 http://localhost:8761/eureka ,如果服务注册中心为高可用集群时,多个注册中心地址以逗号分隔。
#如果服务注册中心加入了安全验证,这里配置的地址格式为: http://<username>:<password>@localhost:8761/eureka 其中 <username> 为安全校验的用户名;<password> 为该用户的密码
eureka.client.service-url.
#默认值true 检索服务
eureka.client.fetch-registry=true
#默认值30 从Eureka服务器端获取注册信息的间隔时间,单位:秒
eureka.client.registry-fetch-interval-seconds=30
#默认值true 启动服务注册
eureka.client.register-with-eureka=true
#默认值5 连接 Eureka Server 的超时时间,单位:秒
eureka.client.eureka-server-connect-timeout-seconds=5
#8 读取 Eureka Server 信息的超时时间,单位:秒
eureka.client.eureka-server-read-timeout-seconds=8
#默认值true 获取实例时是否过滤,只保留UP状态的实例
eureka.client.filter-only-up-instances=true
#默认值30 Eureka 服务端连接空闲关闭时间,单位:秒
eureka.client.eureka-connection-idle-timeout-seconds=30
#默认值200 从Eureka 客户端到所有Eureka服务端的连接总数
eureka.client.eureka-server-total-connections=200
#默认值50 从Eureka客户端到每个Eureka服务主机的连接总数
eureka.client.eureka-server-total-connections-per-host=50