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
470d6abf
Commit
470d6abf
authored
Jan 09, 2017
by
Ryan Baxter
Committed by
GitHub
Jan 09, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1457 from ryanjbaxter/feign-retry
Use Spring Retry For Failed Requests Using Feign
parents
f3275c16
5cd39df5
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
324 additions
and
105 deletions
+324
-105
FeignClientsConfiguration.java
...mework/cloud/netflix/feign/FeignClientsConfiguration.java
+16
-11
CachingSpringLoadBalancerFactory.java
...etflix/feign/ribbon/CachingSpringLoadBalancerFactory.java
+13
-1
FeignLoadBalancer.java
...amework/cloud/netflix/feign/ribbon/FeignLoadBalancer.java
+87
-28
FeignRetryPolicy.java
...ramework/cloud/netflix/feign/ribbon/FeignRetryPolicy.java
+119
-0
FeignRibbonClientAutoConfiguration.java
...flix/feign/ribbon/FeignRibbonClientAutoConfiguration.java
+11
-2
RibbonLoadBalancerClient.java
...mework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
+3
-3
RibbonLoadBalancingHttpClient.java
.../netflix/ribbon/apache/RibbonLoadBalancingHttpClient.java
+7
-0
ZuulProperties.java
...gframework/cloud/netflix/zuul/filters/ZuulProperties.java
+4
-7
FeignClientOverrideDefaultsTests.java
...cloud/netflix/feign/FeignClientOverrideDefaultsTests.java
+1
-1
CachingSpringLoadBalancerFactoryTests.java
...x/feign/ribbon/CachingSpringLoadBalancerFactoryTests.java
+11
-6
FeignLoadBalancerTests.java
...rk/cloud/netflix/feign/ribbon/FeignLoadBalancerTests.java
+28
-26
FeignRibbonClientRetryTests.java
...oud/netflix/feign/ribbon/FeignRibbonClientRetryTests.java
+8
-9
FeignRibbonClientTests.java
...rk/cloud/netflix/feign/ribbon/FeignRibbonClientTests.java
+14
-11
FeignClientValidationTests.java
...cloud/netflix/feign/valid/FeignClientValidationTests.java
+2
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientsConfiguration.java
View file @
470d6abf
...
...
@@ -16,9 +16,16 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
;
import
feign.Contract
;
import
feign.Feign
;
import
feign.Logger
;
import
feign.Retryer
;
import
feign.codec.Decoder
;
import
feign.codec.Encoder
;
import
feign.hystrix.HystrixFeign
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.springframework.beans.factory.ObjectFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
...
...
@@ -35,16 +42,8 @@ import org.springframework.context.annotation.Scope;
import
org.springframework.core.convert.ConversionService
;
import
org.springframework.format.support.DefaultFormattingConversionService
;
import
org.springframework.format.support.FormattingConversionService
;
import
com.netflix.hystrix.HystrixCommand
;
import
feign.Contract
;
import
feign.Feign
;
import
feign.Logger
;
import
feign.codec.Decoder
;
import
feign.codec.Encoder
;
import
feign.hystrix.HystrixFeign
;
/**
* @author Dave Syer
* @author Venil Noronha
...
...
@@ -104,10 +103,16 @@ public class FeignClientsConfiguration {
}
@Bean
@ConditionalOnMissingBean
public
Retryer
feignRetryer
()
{
return
Retryer
.
NEVER_RETRY
;
}
@Bean
@Scope
(
"prototype"
)
@ConditionalOnMissingBean
public
Feign
.
Builder
feignBuilder
()
{
return
Feign
.
builder
();
public
Feign
.
Builder
feignBuilder
(
Retryer
retryer
)
{
return
Feign
.
builder
()
.
retryer
(
retryer
)
;
}
@Bean
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/ribbon/CachingSpringLoadBalancerFactory.java
View file @
470d6abf
...
...
@@ -18,8 +18,11 @@ package org.springframework.cloud.netflix.feign.ribbon;
import
java.util.Map
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicyFactory
;
import
org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryPolicyFactory
;
import
org.springframework.cloud.netflix.ribbon.ServerIntrospector
;
import
org.springframework.cloud.netflix.ribbon.SpringClientFactory
;
import
org.springframework.retry.support.RetryTemplate
;
import
org.springframework.util.ConcurrentReferenceHashMap
;
import
com.netflix.client.config.IClientConfig
;
...
...
@@ -34,11 +37,19 @@ import com.netflix.loadbalancer.ILoadBalancer;
public
class
CachingSpringLoadBalancerFactory
{
private
final
SpringClientFactory
factory
;
private
final
LoadBalancedRetryPolicyFactory
loadBalancedRetryPolicyFactory
;
private
volatile
Map
<
String
,
FeignLoadBalancer
>
cache
=
new
ConcurrentReferenceHashMap
<>();
public
CachingSpringLoadBalancerFactory
(
SpringClientFactory
factory
)
{
this
.
factory
=
factory
;
this
.
loadBalancedRetryPolicyFactory
=
new
RibbonLoadBalancedRetryPolicyFactory
(
factory
);
}
public
CachingSpringLoadBalancerFactory
(
SpringClientFactory
factory
,
LoadBalancedRetryPolicyFactory
loadBalancedRetryPolicyFactory
)
{
this
.
factory
=
factory
;
this
.
loadBalancedRetryPolicyFactory
=
loadBalancedRetryPolicyFactory
;
}
public
FeignLoadBalancer
create
(
String
clientName
)
{
...
...
@@ -48,7 +59,8 @@ public class CachingSpringLoadBalancerFactory {
IClientConfig
config
=
this
.
factory
.
getClientConfig
(
clientName
);
ILoadBalancer
lb
=
this
.
factory
.
getLoadBalancer
(
clientName
);
ServerIntrospector
serverIntrospector
=
this
.
factory
.
getInstance
(
clientName
,
ServerIntrospector
.
class
);
FeignLoadBalancer
client
=
new
FeignLoadBalancer
(
lb
,
config
,
serverIntrospector
);
FeignLoadBalancer
client
=
new
FeignLoadBalancer
(
lb
,
config
,
serverIntrospector
,
loadBalancedRetryPolicyFactory
);
this
.
cache
.
put
(
clientName
,
client
);
return
client
;
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/ribbon/FeignLoadBalancer.java
View file @
470d6abf
...
...
@@ -16,44 +16,61 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
ribbon
;
import
feign.Client
;
import
feign.Request
;
import
feign.Response
;
import
feign.Util
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicyFactory
;
import
org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser
;
import
org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient
;
import
org.springframework.cloud.netflix.ribbon.ServerIntrospector
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.retry.RetryCallback
;
import
org.springframework.retry.RetryContext
;
import
org.springframework.retry.policy.NeverRetryPolicy
;
import
org.springframework.retry.support.RetryTemplate
;
import
com.netflix.client.AbstractLoadBalancerAwareClient
;
import
com.netflix.client.ClientException
;
import
com.netflix.client.ClientRequest
;
import
com.netflix.client.DefaultLoadBalancerRetryHandler
;
import
com.netflix.client.IResponse
;
import
com.netflix.client.RequestSpecificRetryHandler
;
import
com.netflix.client.RetryHandler
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.Server
;
import
feign.Client
;
import
feign.Request
;
import
feign.Response
;
import
feign.Util
;
import
static
org
.
springframework
.
cloud
.
netflix
.
ribbon
.
RibbonUtils
.
updateToHttpsIfNeeded
;
public
class
FeignLoadBalancer
extends
AbstractLoadBalancerAwareClient
<
FeignLoadBalancer
.
RibbonRequest
,
FeignLoadBalancer
.
RibbonResponse
>
{
AbstractLoadBalancerAwareClient
<
FeignLoadBalancer
.
RibbonRequest
,
FeignLoadBalancer
.
RibbonResponse
>
implements
ServiceInstanceChooser
{
private
final
int
connectTimeout
;
private
final
int
readTimeout
;
private
final
IClientConfig
clientConfig
;
private
final
ServerIntrospector
serverIntrospector
;
private
final
LoadBalancedRetryPolicyFactory
loadBalancedRetryPolicyFactory
;
public
FeignLoadBalancer
(
ILoadBalancer
lb
,
IClientConfig
clientConfig
,
ServerIntrospector
serverIntrospector
)
{
ServerIntrospector
serverIntrospector
,
LoadBalancedRetryPolicyFactory
loadBalancedRetryPolicyFactory
)
{
super
(
lb
,
clientConfig
);
this
.
setRetryHandler
(
RetryHandler
.
DEFAULT
);
this
.
loadBalancedRetryPolicyFactory
=
loadBalancedRetryPolicyFactory
;
this
.
setRetryHandler
(
new
DefaultLoadBalancerRetryHandler
(
clientConfig
));
this
.
clientConfig
=
clientConfig
;
this
.
connectTimeout
=
clientConfig
.
get
(
CommonClientConfigKey
.
ConnectTimeout
);
this
.
readTimeout
=
clientConfig
.
get
(
CommonClientConfigKey
.
ReadTimeout
);
...
...
@@ -61,9 +78,9 @@ public class FeignLoadBalancer extends
}
@Override
public
RibbonResponse
execute
(
RibbonRequest
request
,
IClientConfig
configOverride
)
public
RibbonResponse
execute
(
final
RibbonRequest
request
,
IClientConfig
configOverride
)
throws
IOException
{
Request
.
Options
options
;
final
Request
.
Options
options
;
if
(
configOverride
!=
null
)
{
options
=
new
Request
.
Options
(
configOverride
.
get
(
CommonClientConfigKey
.
ConnectTimeout
,
...
...
@@ -74,26 +91,35 @@ public class FeignLoadBalancer extends
else
{
options
=
new
Request
.
Options
(
this
.
connectTimeout
,
this
.
readTimeout
);
}
Response
response
=
request
.
client
().
execute
(
request
.
toRequest
(),
options
);
return
new
RibbonResponse
(
request
.
getUri
(),
response
);
LoadBalancedRetryPolicy
retryPolicy
=
loadBalancedRetryPolicyFactory
.
create
(
this
.
getClientName
(),
this
);
RetryTemplate
retryTemplate
=
new
RetryTemplate
();
retryTemplate
.
setRetryPolicy
(
retryPolicy
==
null
?
new
NeverRetryPolicy
()
:
new
FeignRetryPolicy
(
request
.
toHttpRequest
(),
retryPolicy
,
this
,
this
.
getClientName
()));
return
retryTemplate
.
execute
(
new
RetryCallback
<
RibbonResponse
,
IOException
>()
{
@Override
public
RibbonResponse
doWithRetry
(
RetryContext
retryContext
)
throws
IOException
{
Request
feignRequest
=
null
;
//on retries the policy will choose the server and set it in the context
//extract the server and update the request being made
if
(
retryContext
instanceof
LoadBalancedRetryContext
)
{
ServiceInstance
service
=
((
LoadBalancedRetryContext
)
retryContext
).
getServiceInstance
();
if
(
service
!=
null
)
{
feignRequest
=
((
RibbonRequest
)
request
.
replaceUri
(
reconstructURIWithServer
(
new
Server
(
service
.
getHost
(),
service
.
getPort
()),
request
.
getUri
()))).
toRequest
();
}
}
if
(
feignRequest
==
null
)
{
feignRequest
=
request
.
toRequest
();
}
Response
response
=
request
.
client
().
execute
(
feignRequest
,
options
);
return
new
RibbonResponse
(
request
.
getUri
(),
response
);
}
});
}
@Override
public
RequestSpecificRetryHandler
getRequestSpecificRetryHandler
(
RibbonRequest
request
,
IClientConfig
requestConfig
)
{
if
(
this
.
clientConfig
.
get
(
CommonClientConfigKey
.
OkToRetryOnAllOperations
,
false
))
{
return
new
RequestSpecificRetryHandler
(
true
,
true
,
this
.
getRetryHandler
(),
requestConfig
);
}
if
(!
request
.
toRequest
().
method
().
equals
(
"GET"
))
{
return
new
RequestSpecificRetryHandler
(
true
,
false
,
this
.
getRetryHandler
(),
requestConfig
);
}
else
{
return
new
RequestSpecificRetryHandler
(
true
,
true
,
this
.
getRetryHandler
(),
requestConfig
);
}
return
new
RequestSpecificRetryHandler
(
false
,
false
,
this
.
getRetryHandler
(),
requestConfig
);
}
@Override
...
...
@@ -102,6 +128,12 @@ public class FeignLoadBalancer extends
return
super
.
reconstructURIWithServer
(
server
,
uri
);
}
@Override
public
ServiceInstance
choose
(
String
serviceId
)
{
return
new
RibbonLoadBalancerClient
.
RibbonServer
(
serviceId
,
this
.
getLoadBalancer
().
chooseServer
(
serviceId
));
}
static
class
RibbonRequest
extends
ClientRequest
implements
Cloneable
{
private
final
Request
request
;
...
...
@@ -129,6 +161,33 @@ public class FeignLoadBalancer extends
return
this
.
client
;
}
HttpRequest
toHttpRequest
()
{
return
new
HttpRequest
()
{
@Override
public
HttpMethod
getMethod
()
{
return
HttpMethod
.
resolve
(
RibbonRequest
.
this
.
toRequest
().
method
());
}
@Override
public
URI
getURI
()
{
return
RibbonRequest
.
this
.
getUri
();
}
@Override
public
HttpHeaders
getHeaders
()
{
Map
<
String
,
List
<
String
>>
headers
=
new
HashMap
<
String
,
List
<
String
>>();
Map
<
String
,
Collection
<
String
>>
feignHeaders
=
RibbonRequest
.
this
.
toRequest
().
headers
();
for
(
String
key
:
feignHeaders
.
keySet
())
{
headers
.
put
(
key
,
new
ArrayList
<
String
>(
feignHeaders
.
get
(
key
)));
}
HttpHeaders
httpHeaders
=
new
HttpHeaders
();
httpHeaders
.
putAll
(
headers
);
return
httpHeaders
;
}
};
}
@Override
public
Object
clone
()
{
return
new
RibbonRequest
(
this
.
client
,
this
.
request
,
getUri
());
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/ribbon/FeignRetryPolicy.java
0 → 100644
View file @
470d6abf
/*
*
* * Copyright 2013-2016 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
ribbon
;
import
java.net.URI
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.loadbalancer.InterceptorRetryPolicy
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy
;
import
org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.retry.RetryContext
;
/**
* @author Ryan Baxter
*/
public
class
FeignRetryPolicy
extends
InterceptorRetryPolicy
{
private
HttpRequest
request
;
private
String
serviceId
;
public
FeignRetryPolicy
(
HttpRequest
request
,
LoadBalancedRetryPolicy
policy
,
ServiceInstanceChooser
serviceInstanceChooser
,
String
serviceName
)
{
super
(
request
,
policy
,
serviceInstanceChooser
,
serviceName
);
this
.
request
=
request
;
this
.
serviceId
=
serviceName
;
}
@Override
public
boolean
canRetry
(
RetryContext
context
)
{
/*
* In InterceptorRetryPolicy.canRetry we ask the LoadBalancer to choose a server if one is not
* set in the retry context and then return true. RetryTemplat calls the canRetry method of
* the policy even on its first execution. So the fact that we didnt have a service instance set
* in the RetryContext signaled that it was the first execution and we should return true.
*
* In the Feign scenario, Feign as actually already queried the load balancer for a service instance
* and we set that service instance in the context when we call the open method of the policy. So in
* the Feign case we just return true if the retry count is 0 indicating we haven't yet made a failed
* request.
*/
if
(
context
.
getRetryCount
()
==
0
)
{
return
true
;
}
return
super
.
canRetry
(
context
);
}
@Override
public
RetryContext
open
(
RetryContext
parent
)
{
/*
* With Feign (unlike Ribbon) the request already has the URI for the service instance
* we are going to make the request to, so extract that information and set the service
* instance in the context. In the Ribbon scenario the URI in the request object still has
* the service id so we choose and set the service instance later on.
*/
LoadBalancedRetryContext
context
=
new
LoadBalancedRetryContext
(
parent
,
this
.
request
);
context
.
setServiceInstance
(
new
FeignRetryPolicyServiceInstance
(
serviceId
,
request
));
return
context
;
}
class
FeignRetryPolicyServiceInstance
implements
ServiceInstance
{
private
String
serviceId
;
private
HttpRequest
request
;
private
Map
<
String
,
String
>
metadata
;
FeignRetryPolicyServiceInstance
(
String
serviceId
,
HttpRequest
request
)
{
this
.
serviceId
=
serviceId
;
this
.
request
=
request
;
this
.
metadata
=
new
HashMap
<
String
,
String
>();
}
@Override
public
String
getServiceId
()
{
return
serviceId
;
}
@Override
public
String
getHost
()
{
return
request
.
getURI
().
getHost
();
}
@Override
public
int
getPort
()
{
return
request
.
getURI
().
getPort
();
}
@Override
public
boolean
isSecure
()
{
return
"https"
.
equals
(
request
.
getURI
().
getScheme
());
}
@Override
public
URI
getUri
()
{
return
request
.
getURI
();
}
@Override
public
Map
<
String
,
String
>
getMetadata
()
{
return
metadata
;
}
}
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/ribbon/FeignRibbonClientAutoConfiguration.java
View file @
470d6abf
...
...
@@ -22,11 +22,13 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicyFactory
;
import
org.springframework.cloud.netflix.feign.FeignAutoConfiguration
;
import
org.springframework.cloud.netflix.ribbon.SpringClientFactory
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.retry.support.RetryTemplate
;
import
com.netflix.loadbalancer.ILoadBalancer
;
...
...
@@ -50,8 +52,15 @@ public class FeignRibbonClientAutoConfiguration {
@Bean
@Primary
public
CachingSpringLoadBalancerFactory
cachingLBClientFactory
(
SpringClientFactory
factory
)
{
return
new
CachingSpringLoadBalancerFactory
(
factory
);
SpringClientFactory
factory
,
LoadBalancedRetryPolicyFactory
retryPolicyFactory
)
{
return
new
CachingSpringLoadBalancerFactory
(
factory
,
retryPolicyFactory
);
}
@Bean
public
RetryTemplate
retryTemplate
()
{
RetryTemplate
template
=
new
RetryTemplate
();
template
.
setThrowLastExceptionOnExhausted
(
true
);
return
template
;
}
@Bean
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
View file @
470d6abf
...
...
@@ -141,17 +141,17 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
return
this
.
clientFactory
.
getLoadBalancer
(
serviceId
);
}
p
rotected
static
class
RibbonServer
implements
ServiceInstance
{
p
ublic
static
class
RibbonServer
implements
ServiceInstance
{
private
final
String
serviceId
;
private
final
Server
server
;
private
final
boolean
secure
;
private
Map
<
String
,
String
>
metadata
;
p
rotected
RibbonServer
(
String
serviceId
,
Server
server
)
{
p
ublic
RibbonServer
(
String
serviceId
,
Server
server
)
{
this
(
serviceId
,
server
,
false
,
Collections
.<
String
,
String
>
emptyMap
());
}
p
rotected
RibbonServer
(
String
serviceId
,
Server
server
,
boolean
secure
,
p
ublic
RibbonServer
(
String
serviceId
,
Server
server
,
boolean
secure
,
Map
<
String
,
String
>
metadata
)
{
this
.
serviceId
=
serviceId
;
this
.
server
=
server
;
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/apache/RibbonLoadBalancingHttpClient.java
View file @
470d6abf
...
...
@@ -27,6 +27,8 @@ import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
import
org.springframework.cloud.netflix.ribbon.support.AbstractLoadBalancingClient
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
com.netflix.client.RequestSpecificRetryHandler
;
import
com.netflix.client.RetryHandler
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.ILoadBalancer
;
...
...
@@ -102,4 +104,9 @@ public class RibbonLoadBalancingHttpClient
return
super
.
reconstructURIWithServer
(
server
,
uri
);
}
@Override
public
RequestSpecificRetryHandler
getRequestSpecificRetryHandler
(
RibbonApacheHttpRequest
request
,
IClientConfig
requestConfig
)
{
return
new
RequestSpecificRetryHandler
(
false
,
false
,
RetryHandler
.
DEFAULT
,
null
);
}
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java
View file @
470d6abf
...
...
@@ -16,6 +16,10 @@
package
org
.
springframework
.
cloud
.
netflix
.
zuul
.
filters
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
...
...
@@ -24,19 +28,12 @@ import java.util.List;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Set
;
import
javax.annotation.PostConstruct
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.StringUtils
;
import
com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
static
com
.
netflix
.
hystrix
.
HystrixCommandProperties
.
ExecutionIsolationStrategy
.
SEMAPHORE
;
/**
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientOverrideDefaultsTests.java
View file @
470d6abf
...
...
@@ -108,7 +108,7 @@ public class FeignClientOverrideDefaultsTests {
@Test
public
void
overrideRetryer
()
{
assert
Null
(
this
.
context
.
getInstance
(
"foo"
,
Retryer
.
class
));
assert
Equals
(
Retryer
.
NEVER_RETRY
,
this
.
context
.
getInstance
(
"foo"
,
Retryer
.
class
));
Retryer
.
Default
.
class
.
cast
(
this
.
context
.
getInstance
(
"bar"
,
Retryer
.
class
));
}
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/ribbon/CachingSpringLoadBalancerFactoryTests.java
View file @
470d6abf
...
...
@@ -16,21 +16,23 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
ribbon
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryPolicyFactory
;
import
org.springframework.cloud.netflix.ribbon.SpringClientFactory
;
import
org.springframework.retry.support.RetryTemplate
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.DefaultClientConfigImpl
;
import
com.netflix.client.config.IClientConfig
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* @author Spencer Gibb
*/
...
...
@@ -39,6 +41,9 @@ public class CachingSpringLoadBalancerFactoryTests {
@Mock
private
SpringClientFactory
delegate
;
@Mock
private
RibbonLoadBalancedRetryPolicyFactory
loadBalancedRetryPolicyFactory
;
private
CachingSpringLoadBalancerFactory
factory
;
@Before
...
...
@@ -52,7 +57,7 @@ public class CachingSpringLoadBalancerFactoryTests {
when
(
this
.
delegate
.
getClientConfig
(
"client1"
)).
thenReturn
(
config
);
when
(
this
.
delegate
.
getClientConfig
(
"client2"
)).
thenReturn
(
config
);
this
.
factory
=
new
CachingSpringLoadBalancerFactory
(
this
.
delegate
);
this
.
factory
=
new
CachingSpringLoadBalancerFactory
(
this
.
delegate
,
loadBalancedRetryPolicyFactory
);
}
@Test
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/ribbon/FeignLoadBalancerTests.java
View file @
470d6abf
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
ribbon
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
ConnectTimeout
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
IsSecure
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
MaxAutoRetries
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
MaxAutoRetriesNextServer
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
OkToRetryOnAllOperations
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
ReadTimeout
;
import
static
com
.
netflix
.
client
.
config
.
DefaultClientConfigImpl
.
DEFAULT_MAX_AUTO_RETRIES
;
import
static
com
.
netflix
.
client
.
config
.
DefaultClientConfigImpl
.
DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
anyBoolean
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
feign.Client
;
import
feign.Request
;
import
feign.Request.Options
;
import
feign.RequestTemplate
;
import
feign.Response
;
import
lombok.SneakyThrows
;
import
java.net.URI
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.Mock
;
...
...
@@ -29,18 +20,27 @@ import org.mockito.MockitoAnnotations;
import
org.springframework.cloud.netflix.feign.ribbon.FeignLoadBalancer.RibbonRequest
;
import
org.springframework.cloud.netflix.feign.ribbon.FeignLoadBalancer.RibbonResponse
;
import
org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector
;
import
org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryPolicyFactory
;
import
org.springframework.cloud.netflix.ribbon.ServerIntrospector
;
import
org.springframework.retry.support.RetryTemplate
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.Server
;
import
feign.Client
;
import
feign.Request
;
import
feign.Request.Options
;
import
feign.RequestTemplate
;
import
feign.Response
;
import
lombok.SneakyThrows
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
ConnectTimeout
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
IsSecure
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
MaxAutoRetries
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
MaxAutoRetriesNextServer
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
OkToRetryOnAllOperations
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
ReadTimeout
;
import
static
com
.
netflix
.
client
.
config
.
DefaultClientConfigImpl
.
DEFAULT_MAX_AUTO_RETRIES
;
import
static
com
.
netflix
.
client
.
config
.
DefaultClientConfigImpl
.
DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
anyBoolean
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
class
FeignLoadBalancerTests
{
...
...
@@ -50,6 +50,8 @@ public class FeignLoadBalancerTests {
private
ILoadBalancer
lb
;
@Mock
private
IClientConfig
config
;
@Mock
private
RibbonLoadBalancedRetryPolicyFactory
loadBalancedRetryPolicyFactory
;
private
FeignLoadBalancer
feignLoadBalancer
;
...
...
@@ -75,7 +77,7 @@ public class FeignLoadBalancerTests {
public
void
testUriInsecure
()
{
when
(
this
.
config
.
get
(
IsSecure
)).
thenReturn
(
false
);
this
.
feignLoadBalancer
=
new
FeignLoadBalancer
(
this
.
lb
,
this
.
config
,
this
.
inspector
);
this
.
inspector
,
loadBalancedRetryPolicyFactory
);
Request
request
=
new
RequestTemplate
().
method
(
"GET"
).
append
(
"http://foo/"
)
.
request
();
RibbonRequest
ribbonRequest
=
new
RibbonRequest
(
this
.
delegate
,
request
,
...
...
@@ -96,7 +98,7 @@ public class FeignLoadBalancerTests {
public
void
testSecureUriFromClientConfig
()
{
when
(
this
.
config
.
get
(
IsSecure
)).
thenReturn
(
true
);
this
.
feignLoadBalancer
=
new
FeignLoadBalancer
(
this
.
lb
,
this
.
config
,
this
.
inspector
);
this
.
inspector
,
loadBalancedRetryPolicyFactory
);
Server
server
=
new
Server
(
"foo"
,
7777
);
URI
uri
=
this
.
feignLoadBalancer
.
reconstructURIWithServer
(
server
,
new
URI
(
"http://foo/"
));
...
...
@@ -118,7 +120,7 @@ public class FeignLoadBalancerTests {
public
Map
<
String
,
String
>
getMetadata
(
Server
server
)
{
return
null
;
}
});
}
,
loadBalancedRetryPolicyFactory
);
Server
server
=
new
Server
(
"foo"
,
7777
);
URI
uri
=
this
.
feignLoadBalancer
.
reconstructURIWithServer
(
server
,
new
URI
(
"http://foo/"
));
...
...
@@ -129,7 +131,7 @@ public class FeignLoadBalancerTests {
@SneakyThrows
public
void
testSecureUriFromClientConfigOverride
()
{
this
.
feignLoadBalancer
=
new
FeignLoadBalancer
(
this
.
lb
,
this
.
config
,
this
.
inspector
);
this
.
inspector
,
loadBalancedRetryPolicyFactory
);
Server
server
=
Mockito
.
mock
(
Server
.
class
);
when
(
server
.
getPort
()).
thenReturn
(
443
);
when
(
server
.
getHost
()).
thenReturn
(
"foo"
);
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/ribbon/FeignRibbonClientRetryTests.java
View file @
470d6abf
...
...
@@ -16,14 +16,13 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
ribbon
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
java.lang.reflect.InvocationHandler
;
import
java.lang.reflect.Proxy
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -43,13 +42,12 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ServerList
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* Tests the Feign Retryer, not ribbon retry.
...
...
@@ -58,7 +56,8 @@ import lombok.NoArgsConstructor;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
FeignRibbonClientRetryTests
.
Application
.
class
,
webEnvironment
=
WebEnvironment
.
RANDOM_PORT
,
value
=
{
"spring.application.name=feignclientretrytest"
,
"feign.okhttp.enabled=false"
,
"feign.httpclient.enabled=false"
,
"feign.hystrix.enabled=false"
,
})
"feign.httpclient.enabled=false"
,
"feign.hystrix.enabled=false"
,
"localapp.ribbon.MaxAutoRetries=2"
,
"localapp.ribbon.MaxAutoRetriesNextServer=3"
})
@DirtiesContext
public
class
FeignRibbonClientRetryTests
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/ribbon/FeignRibbonClientTests.java
View file @
470d6abf
...
...
@@ -16,19 +16,19 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
ribbon
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
argThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
feign.Client
;
import
feign.Request
;
import
feign.Request.Options
;
import
feign.RequestTemplate
;
import
org.hamcrest.CustomMatcher
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.cloud.netflix.ribbon.DefaultServerIntrospector
;
import
org.springframework.cloud.netflix.ribbon.RibbonLoadBalancedRetryPolicyFactory
;
import
org.springframework.cloud.netflix.ribbon.ServerIntrospector
;
import
org.springframework.cloud.netflix.ribbon.SpringClientFactory
;
import
org.springframework.retry.support.RetryTemplate
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.DefaultClientConfigImpl
;
import
com.netflix.client.config.IClientConfig
;
...
...
@@ -38,10 +38,11 @@ import com.netflix.loadbalancer.LoadBalancerStats;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ServerStats
;
import
feign.Client
;
import
feign.Request
;
import
feign.Request.Options
;
import
feign.RequestTemplate
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
argThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
/**
* @author Dave Syer
...
...
@@ -51,6 +52,7 @@ public class FeignRibbonClientTests {
private
AbstractLoadBalancer
loadBalancer
=
mock
(
AbstractLoadBalancer
.
class
);
private
Client
delegate
=
mock
(
Client
.
class
);
private
RibbonLoadBalancedRetryPolicyFactory
retryPolicyFactory
=
mock
(
RibbonLoadBalancedRetryPolicyFactory
.
class
);
private
SpringClientFactory
factory
=
new
SpringClientFactory
()
{
@Override
...
...
@@ -79,7 +81,8 @@ public class FeignRibbonClientTests {
// Even though we don't maintain FeignRibbonClient, keep these tests
// around to make sure the expected behaviour doesn't break
private
Client
client
=
new
LoadBalancerFeignClient
(
this
.
delegate
,
new
CachingSpringLoadBalancerFactory
(
this
.
factory
),
this
.
factory
);
private
Client
client
=
new
LoadBalancerFeignClient
(
this
.
delegate
,
new
CachingSpringLoadBalancerFactory
(
this
.
factory
,
retryPolicyFactory
),
this
.
factory
);
@Before
public
void
init
()
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientValidationTests.java
View file @
470d6abf
...
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
cloud
.
netflix
.
feign
.
valid
;
import
org.junit.Test
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration
;
import
org.springframework.cloud.netflix.feign.EnableFeignClients
;
import
org.springframework.cloud.netflix.feign.FeignAutoConfiguration
;
import
org.springframework.cloud.netflix.feign.FeignClient
;
...
...
@@ -82,6 +83,7 @@ public class FeignClientValidationTests {
@Test
public
void
validLoadBalanced
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
LoadBalancerAutoConfiguration
.
class
,
RibbonAutoConfiguration
.
class
,
FeignRibbonClientAutoConfiguration
.
class
,
GoodServiceIdConfiguration
.
class
);
...
...
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