spring eureka | 张洪铭的个人博客
张洪铭的个人博客

spring eureka

创建eureka-server 子项目

创建application.properties

1
2
3
4
5
6
# 是否将自己实例注册到Eureka Server中
eureka.client.register-with-eureka=false
# 是否应从Eureka Server获取Eureka注册表信息
eureka.client.fetch-registry=false
spring.profiles.active=master

创建application-master.properties

1
2
3
4
server.port=8761
# 注册到从节点
eureka.client.serviceUrl.defaultZone=http://localhost:8762/eureka/

创建application-slave.properties

1
2
3
4
server.port=8762
# 注册到主节点
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

创建EurekaServerApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Created by zhanghongming on 2020/6/27.
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

打开

http://localhost:8761/

改成spring.profiles.active=slave
启动
此处输入图片的描述

eureka client 如何注册?
创建子项目:eureka-client-provider

创建:application.properties

1
2
3
4
5
6
7
server.port=8081
spring.application.name=eureka-client-provider
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 自定义实例ID格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
#eureka.client.healthcheck.enabled=true

创建EurekaClientProviderApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Created by zhanghongming on 2020/6/27.
*/
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientProviderApplication.class, args);
}
}

创建CustomHealthIndicator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package spring.cloud.eureka;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
/**
* Created by zhanghongming on 2020/6/27.
*/
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {
private boolean status = true;
public void setStatus(boolean status) {
this.status = status;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
if (status) {
builder.up();
} else {
builder.down();
}
}
}

创建TestController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package spring.cloud.eureka;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zhanghongming on 2020/6/27.
*/
@RestController
public class TestController {
@Autowired
private CustomHealthIndicator customHealthIndicator;
@GetMapping("/updateStatus")
public String updateStatus(boolean status) {
customHealthIndicator.setStatus(status);
return "success";
}
}

启动
此处输入图片的描述

那么消费者如何消费呢?

创建子项目eureka-client-consumer

创建application.properties

1
2
3
4
5
6
server.port=8082
spring.application.name=eureka-client-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 自定义实例ID格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

创建EurekaConsumerApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by zhanghongming on 2020/6/27.
*/
@SpringBootApplication
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}

创建EurekaController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package spring.cloud.eureka.controller;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zhanghongming on 2020/6/27.
*/
@RestController
public class EurekaController {
@Autowired
private DiscoveryClient discoveryClient;
/**
* 获取Eureka Server中所有服务实例信息
* @return
*/
@GetMapping("/instances")
public List<ServiceInstance> getApplications() {
List<ServiceInstance> instances = discoveryClient.getServices().stream()
.map(sid -> discoveryClient.getInstances(sid))
.collect(Collectors.toList())
.stream().flatMap(list -> list.stream()).collect(Collectors.toList());
return instances;
}
}

启动
打开
http://localhost:8082/instances
此处输入图片的描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
[{
"host": "zhm-pc",
"port": 8081,
"metadata": {
"management.port": "8081"
},
"secure": false,
"serviceId": "EUREKA-CLIENT-PROVIDER",
"uri": "http://zhm-pc:8081",
"instanceInfo": {
"instanceId": "eureka-client-provider:192.168.1.3:8081",
"app": "EUREKA-CLIENT-PROVIDER",
"appGroupName": null,
"ipAddr": "192.168.1.3",
"sid": "na",
"homePageUrl": "http://zhm-pc:8081/",
"statusPageUrl": "http://zhm-pc:8081/actuator/info",
"healthCheckUrl": "http://zhm-pc:8081/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "eureka-client-provider",
"secureVipAddress": "eureka-client-provider",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "zhm-pc",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 30,
"durationInSecs": 90,
"registrationTimestamp": 1593256140359,
"lastRenewalTimestamp": 1593256620293,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1593256140360
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"management.port": "8081"
},
"lastUpdatedTimestamp": 1593256140360,
"lastDirtyTimestamp": 1593256140191,
"actionType": "ADDED",
"asgName": null
},
"instanceId": "eureka-client-provider:192.168.1.3:8081",
"scheme": null
}, {
"host": "zhm-pc",
"port": 8080,
"metadata": {
"management.port": "8080"
},
"secure": false,
"serviceId": "UNKNOWN",
"uri": "http://zhm-pc:8080",
"instanceInfo": {
"instanceId": "zhm-pc",
"app": "UNKNOWN",
"appGroupName": null,
"ipAddr": "192.168.1.3",
"sid": "na",
"homePageUrl": "http://zhm-pc:8080/",
"statusPageUrl": "http://zhm-pc:8080/actuator/info",
"healthCheckUrl": "http://zhm-pc:8080/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "unknown",
"secureVipAddress": "unknown",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "zhm-pc",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 30,
"durationInSecs": 90,
"registrationTimestamp": 1593255452890,
"lastRenewalTimestamp": 1593256622230,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1593255452891
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"management.port": "8080"
},
"lastUpdatedTimestamp": 1593255452892,
"lastDirtyTimestamp": 1593255452440,
"actionType": "ADDED",
"asgName": null
},
"instanceId": "zhm-pc",
"scheme": null
}, {
"host": "zhm-pc",
"port": 8082,
"metadata": {
"management.port": "8082"
},
"secure": false,
"serviceId": "EUREKA-CLIENT-CONSUMER",
"uri": "http://zhm-pc:8082",
"instanceInfo": {
"instanceId": "eureka-client-consumer:192.168.1.3:8082",
"app": "EUREKA-CLIENT-CONSUMER",
"appGroupName": null,
"ipAddr": "192.168.1.3",
"sid": "na",
"homePageUrl": "http://zhm-pc:8082/",
"statusPageUrl": "http://zhm-pc:8082/actuator/info",
"healthCheckUrl": "http://zhm-pc:8082/actuator/health",
"secureHealthCheckUrl": null,
"vipAddress": "eureka-client-consumer",
"secureVipAddress": "eureka-client-consumer",
"countryId": 1,
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
"hostName": "zhm-pc",
"status": "UP",
"overriddenStatus": "UNKNOWN",
"leaseInfo": {
"renewalIntervalInSecs": 30,
"durationInSecs": 90,
"registrationTimestamp": 1593256562280,
"lastRenewalTimestamp": 1593256562280,
"evictionTimestamp": 0,
"serviceUpTimestamp": 1593256562280
},
"isCoordinatingDiscoveryServer": false,
"metadata": {
"management.port": "8082"
},
"lastUpdatedTimestamp": 1593256562280,
"lastDirtyTimestamp": 1593256562192,
"actionType": "ADDED",
"asgName": null
},
"instanceId": "eureka-client-consumer:192.168.1.3:8082",
"scheme": null
}]

此处输入图片的描述

源码跟踪
AbstractInstanceRegistry

坚持原创技术分享,您的支持将鼓励我继续创作!