In the previous article calls between micro-Services of service registration and service , we basically achieved between service calls, today we look at Eureka self-protection mechanisms and health checks.
-
Eureka self-protection mechanism
Then the project more than three articles establish the basis (eureka-server, uerreg, myweb ), Eureka is turned on by default to protect themselves. Let's do a test, we start the first of three projects, we have access to the registry HTTP: // localhost: 8761 / ,
We in the development process certainly do not want that, is not conducive to development. We can turn off the self-protection mechanism in Eureka (actual production environment is not recommended to close).
- eureka-server server
configuration file we add the following configuration
#关闭保护机制,以确保注册中心将不可用的实例正确剔除
eureka.server.enable-self-preservation=false
#(代表是5秒,单位是毫秒,清理失效服务的间隔 )
eureka.server.eviction-interval-timer-in-ms=5000
- userreg client
configuration file we add the following configuration
# 心跳检测检测与续约时间
# 测试时将值设置设置小些,保证服务关闭后注册中心能及时踢出服务
# 配置说明
# lease-renewal-interval-in-seconds 每间隔10s,向服务端发送一次心跳,证明自己依然”存活“
# lease-expiration-duration-in-seconds 告诉服务端,如果我20s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=20
We restart the service, and then close userreg client for testing.
At this point we find that a red warning became self-protection is turned off warnings, and examples of the registry excluded, indicating that self-protection mechanism is turned off.
-
health examination
People get sick, like people, service sometimes abnormal situation, we also need to know the health status of a service. We can add the following dependency to open a health check services. Userreg services, for example with
pom file and add the following dependence
...
<!--健康检查依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
...
ok,其他的什么都不变,我们来访问一下这个接口http://localhost:9001/health
我们看到了一个很简短的健康报告:{"description":"Spring Cloud Eureka Discovery Client","status":"UP"},类似的还有
info 显示任意的应用信息
metrics 展示当前应用的指标信息 true
mappings 显示所有@RequestMapping路径的整理列表
trace 显示trace信息(默认为最新的一些HTTP请求)
health 展示应用的健康信息
beans 显示一个应用中所有Spring Beans的完整列表
这其中有一些是敏感信息,出于安全考虑,如果不设置
#关掉认证(公网下的生产环境不建议,内网部署可以)
#management.security.enabled=false
默认是无法访问的。
如果我们要访问查看而且想保证一定的安全性,我们应该做什么呢?我们在userreg的pom文件中引入
...
<!--安全认证依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
...
此时我们访问/beans敏感信息时,弹出如下信息,需要我们进行身份认证
仅仅引入依赖其实是有问题的,因为我们请求正常的业务接口他也会要求进行认证,解决办法可以在userreg工程的配置文件中添加如下设置:
#(增加了访问路径)
management.context-path=/admin
security.user.name=root
security.user.password=123
#只对/admin进行安全认证
security.basic.path=/admin
重启服务,我们访问http://localhost:9001/admin/beans,注意哦,我们在配置文件中添加了相对路径,只对admin进行验证,此时我们输入正确的用户名和密码(已在配置文件中配置)会显示我们需要的信息。
-
实战健康检查
What health checks in the practical application scenarios it? For example, we configured userreg engineering data source
is introduced in the following dependency pom file
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
...
Then create a configuration class, the configuration data source DataSource
@Configuration
public class Myconfig {
@Bean
public DataSource dataSource()
{
org.apache.tomcat.jdbc.pool.DataSource dataSource=new org.apache.tomcat.jdbc.pool.DataSource();
dataSource.setUrl("jdbc:mysql://localhost/test?characterEncoding=UTF-8");
dataSource.setUsername("root");
dataSource.setPassword("mysql");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}
}
After this has been studied in springboot, I will follow-up the learning process in a blog springboot way to record, data source configuration is complete, we start the service, visit http: // localhost: 9001 / admin / health see the health