Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
948b176c
Commit
948b176c
authored
Oct 17, 2014
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add loadBalancerRequest so that LoadBalancerStats().incrementNumRequests can be called after
parent
07a66fe8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
19 deletions
+69
-19
LoadBalancerClient.java
...amework/cloud/client/loadbalancer/LoadBalancerClient.java
+8
-0
LoadBalancerRequest.java
...mework/cloud/client/loadbalancer/LoadBalancerRequest.java
+10
-0
RibbonInterceptor.java
...ringframework/cloud/netflix/ribbon/RibbonInterceptor.java
+12
-7
RibbonLoadBalancerClient.java
...mework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
+39
-12
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerClient.java
View file @
948b176c
...
...
@@ -12,4 +12,12 @@ public interface LoadBalancerClient {
* @return
*/
public
ServiceInstance
choose
(
String
serviceId
);
/**
* Choose a {@see ServiceInstance} from the LoadBalancer for the specified service
* @param serviceId The serviceId to use to look up the LoadBalancer
* @param request allows implementations to execute pre and post actions such as incrementing metrics
* @return
*/
public
<
T
>
T
choose
(
String
serviceId
,
LoadBalancerRequest
<
T
>
request
);
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerRequest.java
0 → 100644
View file @
948b176c
package
org
.
springframework
.
cloud
.
client
.
loadbalancer
;
import
org.springframework.cloud.client.ServiceInstance
;
/**
* @author Spencer Gibb
*/
public
interface
LoadBalancerRequest
<
T
>
{
public
T
apply
(
ServiceInstance
instance
);
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonInterceptor.java
View file @
948b176c
...
...
@@ -2,6 +2,7 @@ package org.springframework.cloud.netflix.ribbon;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerClient
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerRequest
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
...
...
@@ -28,14 +29,18 @@ public class RibbonInterceptor implements ClientHttpRequestInterceptor {
HttpRequestWrapper
wrapper
=
new
HttpRequestWrapper
(
request
)
{
@Override
public
URI
getURI
()
{
URI
originalUri
=
super
.
getURI
();
final
URI
originalUri
=
super
.
getURI
();
String
serviceName
=
originalUri
.
getHost
();
ServiceInstance
instance
=
loadBalancer
.
choose
(
serviceName
);
URI
uri
=
UriComponentsBuilder
.
fromUri
(
originalUri
)
.
host
(
instance
.
getHost
())
.
port
(
instance
.
getPort
())
.
build
()
.
toUri
();
URI
uri
=
loadBalancer
.
choose
(
serviceName
,
new
LoadBalancerRequest
<
URI
>()
{
@Override
public
URI
apply
(
ServiceInstance
instance
)
{
return
UriComponentsBuilder
.
fromUri
(
originalUri
)
.
host
(
instance
.
getHost
())
.
port
(
instance
.
getPort
())
.
build
()
.
toUri
();
}
});
return
uri
;
}
};
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
View file @
948b176c
...
...
@@ -4,6 +4,7 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Map
;
import
com.netflix.loadbalancer.AbstractLoadBalancer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerClient
;
...
...
@@ -11,6 +12,7 @@ import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import
com.netflix.loadbalancer.BaseLoadBalancer
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.Server
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerRequest
;
/**
* @author Spencer Gibb
...
...
@@ -34,20 +36,45 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
@Override
public
ServiceInstance
choose
(
String
serviceId
)
{
ribbonClientPreprocessor
.
preprocess
(
serviceId
);
ILoadBalancer
loadBalancer
=
this
.
balancers
.
get
(
serviceId
);
if
(
loadBalancer
==
null
)
{
loadBalancer
=
clientFactory
.
getNamedLoadBalancer
(
serviceId
);
}
Server
server
=
loadBalancer
.
chooseServer
(
"default"
);
if
(
server
==
null
)
{
throw
new
IllegalStateException
(
"Unable to locate ILoadBalancer for service: "
+
serviceId
);
}
return
new
RibbonServer
(
serviceId
,
server
);
return
new
RibbonServer
(
serviceId
,
getServer
(
serviceId
));
}
private
class
RibbonServer
implements
ServiceInstance
{
@Override
public
<
T
>
T
choose
(
String
serviceId
,
LoadBalancerRequest
<
T
>
request
)
{
ILoadBalancer
loadBalancer
=
getLoadBalancer
(
serviceId
);
Server
server
=
getServer
(
serviceId
,
loadBalancer
);
RibbonServer
ribbonServer
=
new
RibbonServer
(
serviceId
,
server
);
try
{
return
request
.
apply
(
ribbonServer
);
}
finally
{
if
(
loadBalancer
instanceof
AbstractLoadBalancer
)
{
AbstractLoadBalancer
.
class
.
cast
(
loadBalancer
).
getLoadBalancerStats
().
incrementNumRequests
(
server
);
}
}
}
protected
Server
getServer
(
String
serviceId
)
{
return
getServer
(
serviceId
,
getLoadBalancer
(
serviceId
));
}
protected
Server
getServer
(
String
serviceId
,
ILoadBalancer
loadBalancer
)
{
Server
server
=
loadBalancer
.
chooseServer
(
"default"
);
if
(
server
==
null
)
{
throw
new
IllegalStateException
(
"Unable to locate ILoadBalancer for service: "
+
serviceId
);
}
return
server
;
}
protected
ILoadBalancer
getLoadBalancer
(
String
serviceId
)
{
ribbonClientPreprocessor
.
preprocess
(
serviceId
);
ILoadBalancer
loadBalancer
=
this
.
balancers
.
get
(
serviceId
);
if
(
loadBalancer
==
null
)
{
loadBalancer
=
clientFactory
.
getNamedLoadBalancer
(
serviceId
);
}
return
loadBalancer
;
}
private
class
RibbonServer
implements
ServiceInstance
{
private
String
serviceId
;
private
Server
server
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment