17
17
package org .springframework .cloud .netflix .eureka .http ;
18
18
19
19
import java .util .Map ;
20
+ import java .util .Optional ;
20
21
21
22
import com .netflix .appinfo .InstanceInfo ;
22
23
import com .netflix .appinfo .InstanceInfo .InstanceStatus ;
@@ -52,28 +53,28 @@ public WebClientEurekaHttpClient(WebClient webClient) {
52
53
53
54
@ Override
54
55
public EurekaHttpResponse <Void > register (InstanceInfo info ) {
55
- return webClient .post ().uri ("apps/" + info . getAppName ()). body ( BodyInserters . fromValue ( info ))
56
- .header (HttpHeaders .ACCEPT_ENCODING , "gzip" )
56
+ return webClient .post ().uri (uriBuilder -> uriBuilder . path ( "apps/{appName}" ). build ( info . getAppName () ))
57
+ .body ( BodyInserters . fromValue ( info )). header (HttpHeaders .ACCEPT_ENCODING , "gzip" )
57
58
.header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE ).retrieve ()
58
59
.onStatus (HttpStatus ::isError , this ::ignoreError ).toBodilessEntity ().map (this ::eurekaHttpResponse )
59
60
.block ();
60
61
}
61
62
62
63
@ Override
63
64
public EurekaHttpResponse <Void > cancel (String appName , String id ) {
64
- return webClient .delete ().uri ("apps/" + appName + '/' + id ). retrieve ( )
65
- .onStatus (HttpStatus ::isError , this ::ignoreError ).toBodilessEntity (). map ( this :: eurekaHttpResponse )
66
- .block ();
65
+ return webClient .delete ().uri (uriBuilder -> uriBuilder . path ( "apps/{ appName}/{id}" ). build ( appName , id ) )
66
+ .retrieve (). onStatus (HttpStatus ::isError , this ::ignoreError ).toBodilessEntity ()
67
+ .map ( this :: eurekaHttpResponse ). block ();
67
68
}
68
69
69
70
@ Override
70
71
public EurekaHttpResponse <InstanceInfo > sendHeartBeat (String appName , String id , InstanceInfo info ,
71
72
InstanceStatus overriddenStatus ) {
72
- String urlPath = "apps/" + appName + '/' + id + "?status=" + info .getStatus ().toString ()
73
- + "&lastDirtyTimestamp=" + info .getLastDirtyTimestamp ().toString ()
74
- + (overriddenStatus != null ? "&overriddenstatus=" + overriddenStatus .name () : "" );
75
73
76
- ResponseEntity <InstanceInfo > response = webClient .put ().uri (urlPath )
74
+ ResponseEntity <InstanceInfo > response = webClient .put ()
75
+ .uri (uriBuilder -> uriBuilder .path ("apps/{appName}/{id}" )
76
+ .queryParam ("status" , info .getStatus ().toString ())
77
+ .queryParam ("lastDirtyTimestamp" , info .getLastDirtyTimestamp ().toString ()).build (appName , id ))
77
78
.header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
78
79
.header (HttpHeaders .ACCEPT , MediaType .APPLICATION_JSON_VALUE ).retrieve ()
79
80
.onStatus (HttpStatus ::isError , this ::ignoreError ).toEntity (InstanceInfo .class ).block ();
@@ -94,22 +95,22 @@ public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id,
94
95
@ Override
95
96
public EurekaHttpResponse <Void > statusUpdate (String appName , String id , InstanceStatus newStatus ,
96
97
InstanceInfo info ) {
97
- String urlPath = "apps/" + appName + '/' + id + "/status?value=" + newStatus . name () + "&lastDirtyTimestamp="
98
- + info . getLastDirtyTimestamp (). toString ();
99
-
100
- return webClient . put (). uri ( urlPath ). header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
101
- .retrieve (). onStatus (HttpStatus ::isError , this ::ignoreError ).toBodilessEntity ()
102
- .map ( this :: eurekaHttpResponse ). block ();
98
+ return webClient . put ()
99
+ . uri ( uriBuilder -> uriBuilder . path ( "apps/{appName}/{id}/status" ). queryParam ( "value" , newStatus . name ())
100
+ . queryParam ( "lastDirtyTimestamp" , info . getLastDirtyTimestamp (). toString ()). build ( appName , id ))
101
+ . header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE ). retrieve ( )
102
+ .onStatus (HttpStatus ::isError , this ::ignoreError ).toBodilessEntity (). map ( this :: eurekaHttpResponse )
103
+ .block ();
103
104
}
104
105
105
106
@ Override
106
107
public EurekaHttpResponse <Void > deleteStatusOverride (String appName , String id , InstanceInfo info ) {
107
- String urlPath = "apps/" + appName + '/' + id + "/status?lastDirtyTimestamp="
108
- + info . getLastDirtyTimestamp (). toString ();
109
-
110
- return webClient . delete (). uri ( urlPath ). header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
111
- .retrieve (). onStatus (HttpStatus ::isError , this ::ignoreError ).toBodilessEntity ()
112
- .map ( this :: eurekaHttpResponse ). block ();
108
+ return webClient . delete ()
109
+ . uri ( uriBuilder -> uriBuilder . path ( "apps/{appName}/{id}/status" )
110
+ . queryParam ( "lastDirtyTimestamp" , info . getLastDirtyTimestamp (). toString ()). build ( appName , id ))
111
+ . header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE ). retrieve ( )
112
+ .onStatus (HttpStatus ::isError , this ::ignoreError ).toBodilessEntity (). map ( this :: eurekaHttpResponse )
113
+ .block ();
113
114
}
114
115
115
116
@ Override
@@ -118,13 +119,11 @@ public EurekaHttpResponse<Applications> getApplications(String... regions) {
118
119
}
119
120
120
121
private EurekaHttpResponse <Applications > getApplicationsInternal (String urlPath , String [] regions ) {
121
- String url = urlPath ;
122
-
123
- if (regions != null && regions .length > 0 ) {
124
- url = url + (urlPath .contains ("?" ) ? "&" : "?" ) + "regions=" + StringUtil .join (regions );
125
- }
122
+ Optional <String > regionsParam = (regions != null && regions .length > 0 ) ? Optional .of (StringUtil .join (regions ))
123
+ : Optional .empty ();
126
124
127
- ResponseEntity <Applications > response = webClient .get ().uri (url )
125
+ ResponseEntity <Applications > response = webClient .get ()
126
+ .uri (uriBuilder -> uriBuilder .path (urlPath ).queryParamIfPresent ("regions" , regionsParam ).build ())
128
127
.header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
129
128
.header (HttpHeaders .ACCEPT , MediaType .APPLICATION_JSON_VALUE ).retrieve ()
130
129
.onStatus (HttpStatus ::isError , this ::ignoreError ).toEntity (Applications .class ).block ();
@@ -155,7 +154,9 @@ public EurekaHttpResponse<Applications> getSecureVip(String secureVipAddress, St
155
154
@ Override
156
155
public EurekaHttpResponse <Application > getApplication (String appName ) {
157
156
158
- ResponseEntity <Application > response = webClient .get ().uri ("apps/" + appName )
157
+ ResponseEntity <Application > response = webClient .get ()
158
+ .uri (uriBuilder -> uriBuilder .path ("apps/{appName}" )
159
+ .build (appName ))
159
160
.header (HttpHeaders .ACCEPT , MediaType .APPLICATION_JSON_VALUE ).retrieve ()
160
161
.onStatus (HttpStatus ::isError , this ::ignoreError ).toEntity (Application .class ).block ();
161
162
@@ -169,16 +170,17 @@ public EurekaHttpResponse<Application> getApplication(String appName) {
169
170
170
171
@ Override
171
172
public EurekaHttpResponse <InstanceInfo > getInstance (String appName , String id ) {
172
- return getInstanceInternal ("apps/" + appName + '/' + id );
173
+ return getInstanceInternal ("apps" , appName , id );
173
174
}
174
175
175
176
@ Override
176
177
public EurekaHttpResponse <InstanceInfo > getInstance (String id ) {
177
- return getInstanceInternal ("instances/" + id );
178
+ return getInstanceInternal ("instances" , id );
178
179
}
179
180
180
- private EurekaHttpResponse <InstanceInfo > getInstanceInternal (String urlPath ) {
181
- ResponseEntity <InstanceInfo > response = webClient .get ().uri (urlPath )
181
+ private EurekaHttpResponse <InstanceInfo > getInstanceInternal (String ... pathSegments ) {
182
+ ResponseEntity <InstanceInfo > response = webClient .get ()
183
+ .uri (uriBuilder -> uriBuilder .pathSegment (pathSegments ).build ())
182
184
.header (HttpHeaders .ACCEPT , MediaType .APPLICATION_JSON_VALUE ).retrieve ()
183
185
.onStatus (HttpStatus ::isError , this ::ignoreError ).toEntity (InstanceInfo .class ).block ();
184
186
0 commit comments