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
62bc2758
Commit
62bc2758
authored
Feb 14, 2017
by
Ryan Baxter
Committed by
GitHub
Feb 14, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1694 from ryanjbaxter/default-zuul-fallbackprovider
Add the ability to provide a default ZuulFallbackProvider
parents
d78da7b9
731c8bf0
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
168 additions
and
7 deletions
+168
-7
spring-cloud-netflix.adoc
docs/src/main/asciidoc/spring-cloud-netflix.adoc
+50
-0
AbstractRibbonCommandFactory.java
...l/filters/route/support/AbstractRibbonCommandFactory.java
+12
-2
HttpClientRibbonCommandFallbackTests.java
...rs/route/apache/HttpClientRibbonCommandFallbackTests.java
+1
-1
OkHttpRibbonCommandFallbackTests.java
...ilters/route/okhttp/OkHttpRibbonCommandFallbackTests.java
+1
-1
RestClientRibbonCommandFallbackTests.java
...oute/restclient/RestClientRibbonCommandFallbackTests.java
+1
-1
RibbonCommandFallbackTests.java
...uul/filters/route/support/RibbonCommandFallbackTests.java
+103
-2
No files found.
docs/src/main/asciidoc/spring-cloud-netflix.adoc
View file @
62bc2758
...
...
@@ -1755,6 +1755,56 @@ zuul:
customers
:
/
customers
/**
----
If
you
would
like
to
provide
a
default
fallback
for
all
routes
than
you
can
create
a
bean
of
type
`
ZuulFallbackProvider
`
and
have
the
`
getRoute
`
method
return
`*`
or
`
null
`.
[
source
,
java
]
----
class
MyFallbackProvider
implements
ZuulFallbackProvider
{
@
Override
public
String
getRoute
()
{
return
"*"
;
}
@
Override
public
ClientHttpResponse
fallbackResponse
()
{
return
new
ClientHttpResponse
()
{
@
Override
public
HttpStatus
getStatusCode
()
throws
IOException
{
return
HttpStatus
.
OK
;
}
@
Override
public
int
getRawStatusCode
()
throws
IOException
{
return
200
;
}
@
Override
public
String
getStatusText
()
throws
IOException
{
return
"OK"
;
}
@
Override
public
void
close
()
{
}
@
Override
public
InputStream
getBody
()
throws
IOException
{
return
new
ByteArrayInputStream
(
"fallback"
.
getBytes
());
}
@
Override
public
HttpHeaders
getHeaders
()
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
APPLICATION_JSON
);
return
headers
;
}
};
}
}
----
[[
zuul
-
developer
-
guide
]]
===
Zuul
Developer
Guide
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/support/AbstractRibbonCommandFactory.java
View file @
62bc2758
...
...
@@ -30,15 +30,25 @@ import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider
public
abstract
class
AbstractRibbonCommandFactory
implements
RibbonCommandFactory
{
private
Map
<
String
,
ZuulFallbackProvider
>
fallbackProviderCache
;
private
ZuulFallbackProvider
defaultFallbackProvider
=
null
;
public
AbstractRibbonCommandFactory
(
Set
<
ZuulFallbackProvider
>
fallbackProviders
){
this
.
fallbackProviderCache
=
new
HashMap
<>();
for
(
ZuulFallbackProvider
provider
:
fallbackProviders
)
{
fallbackProviderCache
.
put
(
provider
.
getRoute
(),
provider
);
String
route
=
provider
.
getRoute
();
if
(
"*"
.
equals
(
route
)
||
route
==
null
)
{
defaultFallbackProvider
=
provider
;
}
else
{
fallbackProviderCache
.
put
(
route
,
provider
);
}
}
}
protected
ZuulFallbackProvider
getFallbackProvider
(
String
route
)
{
return
fallbackProviderCache
.
get
(
route
);
ZuulFallbackProvider
provider
=
fallbackProviderCache
.
get
(
route
);
if
(
provider
==
null
)
{
provider
=
defaultFallbackProvider
;
}
return
provider
;
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/apache/HttpClientRibbonCommandFallbackTests.java
View file @
62bc2758
...
...
@@ -33,7 +33,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
* @author Ryan Baxter
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
HttpClientRibbonCommandIntegration
Tests
.
TestConfig
.
class
,
webEnvironment
=
RANDOM_PORT
,
@SpringBootTest
(
classes
=
RibbonCommandFallback
Tests
.
TestConfig
.
class
,
webEnvironment
=
RANDOM_PORT
,
properties
=
{
"zuul.routes.simple: /simple/**"
,
"zuul.routes.another: /another/twolevel/**"
,
"ribbon.ReadTimeout: 1"
})
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/okhttp/OkHttpRibbonCommandFallbackTests.java
View file @
62bc2758
...
...
@@ -31,7 +31,7 @@ import com.netflix.zuul.context.RequestContext;
* @author Ryan Baxter
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
OkHttpRibbonCommandIntegration
Tests
.
TestConfig
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
,
value
=
{
@SpringBootTest
(
classes
=
RibbonCommandFallback
Tests
.
TestConfig
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
,
value
=
{
"zuul.routes.simple: /simple/**"
,
"zuul.routes.another: /another/twolevel/**"
,
"ribbon.ReadTimeout: 1"
})
@DirtiesContext
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/restclient/RestClientRibbonCommandFallbackTests.java
View file @
62bc2758
...
...
@@ -31,7 +31,7 @@ import com.netflix.zuul.context.RequestContext;
* @author Ryan Baxter
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
R
estClientRibbonCommandIntegration
Tests
.
TestConfig
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
,
value
=
{
@SpringBootTest
(
classes
=
R
ibbonCommandFallback
Tests
.
TestConfig
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
,
value
=
{
"zuul.routes.simple: /simple/**"
,
"zuul.routes.another: /another/twolevel/**"
,
"ribbon.ReadTimeout: 1"
})
@DirtiesContext
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/support/RibbonCommandFallbackTests.java
View file @
62bc2758
...
...
@@ -18,13 +18,35 @@
package
org
.
springframework
.
cloud
.
netflix
.
zuul
.
filters
.
route
.
support
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.Collections
;
import
java.util.Set
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.ErrorAttributes
;
import
org.springframework.boot.context.embedded.LocalServerPort
;
import
org.springframework.boot.test.web.client.TestRestTemplate
;
import
org.springframework.cloud.netflix.ribbon.RibbonClient
;
import
org.springframework.cloud.netflix.ribbon.RibbonClients
;
import
org.springframework.cloud.netflix.ribbon.SpringClientFactory
;
import
org.springframework.cloud.netflix.zuul.EnableZuulProxy
;
import
org.springframework.cloud.netflix.zuul.filters.ZuulProperties
;
import
org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory
;
import
org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider
;
import
org.springframework.cloud.netflix.zuul.filters.route.apache.HttpClientRibbonCommandFactory
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.client.ClientHttpResponse
;
import
org.springframework.web.bind.annotation.RestController
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -47,11 +69,90 @@ public abstract class RibbonCommandFallbackTests {
}
@Test
public
void
no
Fallback
()
{
public
void
default
Fallback
()
{
String
uri
=
"/another/twolevel/slow"
;
ResponseEntity
<
String
>
result
=
new
TestRestTemplate
().
exchange
(
"http://localhost:"
+
this
.
port
+
uri
,
HttpMethod
.
GET
,
new
HttpEntity
<>((
Void
)
null
),
String
.
class
);
assertEquals
(
HttpStatus
.
INTERNAL_SERVER_ERROR
,
result
.
getStatusCode
());
assertEquals
(
HttpStatus
.
OK
,
result
.
getStatusCode
());
assertEquals
(
"default fallback"
,
result
.
getBody
());
}
// Don't use @SpringBootApplication because we don't want to component scan
@Configuration
@EnableAutoConfiguration
@RestController
@EnableZuulProxy
@RibbonClients
({
@RibbonClient
(
name
=
"simple"
,
configuration
=
ZuulProxyTestBase
.
SimpleRibbonClientConfiguration
.
class
),
@RibbonClient
(
name
=
"another"
,
configuration
=
ZuulProxyTestBase
.
AnotherRibbonClientConfiguration
.
class
)})
public
static
class
TestConfig
extends
ZuulProxyTestBase
.
AbstractZuulProxyApplication
{
@Autowired
(
required
=
false
)
private
Set
<
ZuulFallbackProvider
>
zuulFallbackProviders
=
Collections
.
emptySet
();
@Bean
public
RibbonCommandFactory
<?>
ribbonCommandFactory
(
final
SpringClientFactory
clientFactory
)
{
return
new
HttpClientRibbonCommandFactory
(
clientFactory
,
new
ZuulProperties
(),
zuulFallbackProviders
);
}
@Bean
public
ZuulProxyTestBase
.
MyErrorController
myErrorController
(
ErrorAttributes
errorAttributes
)
{
return
new
ZuulProxyTestBase
.
MyErrorController
(
errorAttributes
);
}
@Bean
public
ZuulFallbackProvider
defaultFallbackProvider
()
{
return
new
DefaultFallbackProvider
();
}
}
public
static
class
DefaultFallbackProvider
implements
ZuulFallbackProvider
{
@Override
public
String
getRoute
()
{
return
"*"
;
}
@Override
public
ClientHttpResponse
fallbackResponse
()
{
return
new
ClientHttpResponse
()
{
@Override
public
HttpStatus
getStatusCode
()
throws
IOException
{
return
HttpStatus
.
OK
;
}
@Override
public
int
getRawStatusCode
()
throws
IOException
{
return
200
;
}
@Override
public
String
getStatusText
()
throws
IOException
{
return
null
;
}
@Override
public
void
close
()
{
}
@Override
public
InputStream
getBody
()
throws
IOException
{
return
new
ByteArrayInputStream
(
"default fallback"
.
getBytes
());
}
@Override
public
HttpHeaders
getHeaders
()
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
MediaType
.
TEXT_HTML
);
return
headers
;
}
};
}
}
}
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