- @Override
- protected void onCacheRefreshed() {
- if (this.cacheRefreshedCount != null) { //might be called during construction and will be null
- long newCount = this.cacheRefreshedCount.incrementAndGet();
- log.trace("onCacheRefreshed called with count: " + newCount);
- //spring容器内广播,HeartbeatEvent实例在创建时收到两个参数:CloudEurekaClient实例和缓存刷新次数
- this.publisher.publishEvent(new HeartbeatEvent(this, newCount));
- }
- }
-
复制代码
- <?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.bolingcavalry</groupId>
- <artifactId>springcloudcustomizelistener</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
-
- <name>springcloudcustomizelistener</name>
- <description>Demo project for Spring Boot</description>
-
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.9.RELEASE</version>
- </parent>
-
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.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>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.28</version>
- </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>Edgware.RELEASE</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>
-
复制代码
- server:
- port: 8085
- spring:
- application:
- name: springcloud-customize-listener
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8081/eureka/
- instance:
- prefer-ip-address: true
-
复制代码
- @Component
- public class EurekaCacheRefreshListener implements ApplicationListener<HeartbeatEvent> {
-
-
- private static final Logger logger = LoggerFactory.getLogger(EurekaCacheRefreshListener.class);
-
-
- @Override
- public void onApplicationEvent(HeartbeatEvent event) {
- Object count = event.getValue();
- Object source = event.getSource();
-
-
- logger.info("start onApplicationEvent, count [{}], source :\n{}", count, JSON.toJSON(source));
- }
- }
-
复制代码
- {
- "instanceRemoteStatus": "UP",
- "lastSuccessfulHeartbeatTimePeriod": 316,
- "allKnownRegions": [
- "us-east-1"
- ],
- ...
- 此处省略部分内容
- ...
- "applications": {
- "appsHashCode": "UP_1_",
- "registeredApplications": [
- {
- "instances": [
- {
- "hostName": "192.168.31.104",
- "overriddenStatus": "UNKNOWN",
- "metadata": {
- "jmx.port": "64656",
- "management.port": "8085"
- },
- "statusPageUrl": "http://192.168.31.104:8085/info",
- "secureVipAddress": "springcloud-customize-listener",
- "leaseInfo": {
- "renewalIntervalInSecs": 30,
- "registrationTimestamp": 1537827838033,
- "evictionTimestamp": 0,
- "renewalTimestamp": 1537827987419,
- "durationInSecs": 90,
- "serviceUpTimestamp": 1537827837461
- },
- "homePageUrl": "http://192.168.31.104:8085/",
- "countryId": 1,
- "sID": "na",
- "securePort": 443,
- "dataCenterInfo": {
- "name": "MyOwn"
- },
- "instanceId": "DESKTOP-82CCEBN:springcloud-customize-listener:8085",
- "coordinatingDiscoveryServer": false,
- "id": "DESKTOP-82CCEBN:springcloud-customize-listener:8085",
- "vIPAddress": "springcloud-customize-listener",
- "dirty": false,
- "lastUpdatedTimestamp": 1537827838033,
- "healthCheckUrl": "http://192.168.31.104:8085/health",
- "appName": "SPRINGCLOUD-CUSTOMIZE-LISTENER",
- "lastDirtyTimestamp": 1537827837401,
- "iPAddr": "192.168.31.104",
- "version": "unknown",
- "actionType": "ADDED",
- "port": 8085,
- "healthCheckUrls": [
- "http://192.168.31.104:8085/health"
- ],
- "status": "UP"
- }
- ]
-
复制代码
- {
- "instances": [
- {
- "hostName": "192.168.119.1",
- "overriddenStatus": "UNKNOWN",
- "metadata": {
- "jmx.port": "58420",
- "management.port": "8082"
- },
- "statusPageUrl": "http://192.168.119.1:8082/info",
- "secureVipAddress": "springcloud-deep-provider",
- "leaseInfo": {
- "renewalIntervalInSecs": 30,
- "registrationTimestamp": 1537840715486,
- "evictionTimestamp": 0,
- "renewalTimestamp": 1537840715486,
- "durationInSecs": 90,
- "serviceUpTimestamp": 1537840715486
- },
- "homePageUrl": "http://192.168.119.1:8082/",
- "countryId": 1,
- "sID": "na",
- "securePort": 443,
- "dataCenterInfo": {
- "name": "MyOwn"
- },
- "instanceId": "localhost:springcloud-deep-provider:8082",
- "coordinatingDiscoveryServer": false,
- "id": "localhost:springcloud-deep-provider:8082",
- "vIPAddress": "springcloud-deep-provider",
- "dirty": false,
- "lastUpdatedTimestamp": 1537840715486,
- "healthCheckUrl": "http://192.168.119.1:8082/health",
- "appName": "SPRINGCLOUD-DEEP-PROVIDER",
- "lastDirtyTimestamp": 1537840715451,
- "iPAddr": "192.168.119.1",
- "version": "unknown",
- "actionType": "ADDED",
- "port": 8082,
- "healthCheckUrls": [
- "http://192.168.119.1:8082/health"
- ],
- "status": "UP"
- }
- ],
- "name": "SPRINGCLOUD-DEEP-PROVIDER",
- "instancesAsIsFromEureka": [
- {
- "hostName": "192.168.119.1",
- "overriddenStatus": "UNKNOWN",
- "metadata": {
- "jmx.port": "58420",
- "management.port": "8082"
- },
- "statusPageUrl": "http://192.168.119.1:8082/info",
- "secureVipAddress": "springcloud-deep-provider",
- "leaseInfo": {
- "renewalIntervalInSecs": 30,
- "registrationTimestamp": 1537840715486,
- "evictionTimestamp": 0,
- "renewalTimestamp": 1537840715486,
- "durationInSecs": 90,
- "serviceUpTimestamp": 1537840715486
- },
- "homePageUrl": "http://192.168.119.1:8082/",
- "countryId": 1,
- "sID": "na",
- "securePort": 443,
- "dataCenterInfo": {
- "name": "MyOwn"
- },
- "instanceId": "localhost:springcloud-deep-provider:8082",
- "coordinatingDiscoveryServer": false,
- "id": "localhost:springcloud-deep-provider:8082",
- "vIPAddress": "springcloud-deep-provider",
- "dirty": false,
- "lastUpdatedTimestamp": 1537840715486,
- "healthCheckUrl": "http://192.168.119.1:8082/health",
- "appName": "SPRINGCLOUD-DEEP-PROVIDER",
- "lastDirtyTimestamp": 1537840715451,
- "iPAddr": "192.168.119.1",
- "version": "unknown",
- "actionType": "ADDED",
- "port": 8082,
- "healthCheckUrls": [
- "http://192.168.119.1:8082/health"
- ],
- "status": "UP"
- }
- ]
- }
-
复制代码