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