Compare commits
219
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
48bb0cdba5 | ||
|
|
dc5eef0125 | ||
|
|
343f436b71 | ||
|
|
d3da7e16e8 | ||
|
|
f184c3f293 | ||
|
|
4e89dd1523 | ||
|
|
f600140004 | ||
|
|
63ba4d40ff | ||
|
|
d07c229b0e | ||
|
|
06862ab090 | ||
|
|
84cb74a898 | ||
|
|
72784920d8 | ||
|
|
9677353c27 | ||
|
|
4766d95def | ||
|
|
bba3f2e92c | ||
|
|
c4fd4eb42e | ||
|
|
b56a4a1259 | ||
|
|
4a4da0e2d1 | ||
|
|
bf13ae95c9 | ||
|
|
efca091fda | ||
|
|
af06086c30 | ||
|
|
5497f5a37c | ||
|
|
2e2987e787 | ||
|
|
12084de3a9 | ||
|
|
7cd7ed5c70 | ||
|
|
5a6a75d22b | ||
|
|
a790386732 | ||
|
|
84cf97b391 | ||
|
|
fa5162c2fc | ||
|
|
6d3181e718 | ||
|
|
3002643ee7 | ||
|
|
b1ec55cb01 | ||
|
|
f0e0e7ea64 | ||
|
|
d0278f63b1 | ||
|
|
39e915e568 | ||
|
|
7311e4abc8 | ||
|
|
ad776a43b5 | ||
|
|
e03d7da36d | ||
|
|
dcd5a56c86 | ||
|
|
d492fe0742 | ||
|
|
c9fe3af3ef | ||
|
|
afb0b436c0 | ||
|
|
c85853ac9d | ||
|
|
21986f5e92 | ||
|
|
f857cca5b3 | ||
|
|
1929dae2d2 | ||
|
|
94ad7066a6 | ||
|
|
fc9b47c3b3 | ||
|
|
e7932a6aae | ||
|
|
1b66594ed4 | ||
|
|
25a988e1bf | ||
|
|
af103e0137 | ||
|
|
06a55b8bcd | ||
|
|
090c85c7ed | ||
|
|
88633c7f54 | ||
|
|
bb499ea823 | ||
|
|
0e1a579de5 | ||
|
|
b538d7f88c | ||
|
|
525bb603f7 | ||
|
|
ea3fb86690 | ||
|
|
9a335536bc | ||
|
|
f6cf14f257 | ||
|
|
6ac428b21c | ||
|
|
30eafcd02d | ||
|
|
147136b85b | ||
|
|
45589b7977 | ||
|
|
f4b49d5542 | ||
|
|
a6b5663c69 | ||
|
|
59ba6b1bc6 | ||
|
|
8f421ced21 | ||
|
|
dd91dcdf8d | ||
|
|
78653c3e8e | ||
|
|
1f3ee5cfec | ||
|
|
e4a8b0a6a6 | ||
|
|
c87e5f2dbf | ||
|
|
397130a935 | ||
|
|
f49db7233a | ||
|
|
0afb60a72a | ||
|
|
a77bd2bdf1 | ||
|
|
56bee946d8 | ||
|
|
c281800ccd | ||
|
|
1de832e472 | ||
|
|
90181f34c0 | ||
|
|
6b9be3f513 | ||
|
|
db2e84ef06 | ||
|
|
cd1cead7fd | ||
|
|
1ad9a8a24f | ||
|
|
06769c4705 | ||
|
|
f3c3b9073b | ||
|
|
df5d1d5079 | ||
|
|
8666006bf4 | ||
|
|
b5c8e6619e | ||
|
|
6f09ae0824 | ||
|
|
efe264d2bb | ||
|
|
ffa18c4346 | ||
|
|
d6e1a9f6a1 | ||
|
|
c82b577d00 | ||
|
|
a2e968465a | ||
|
|
eca40256d8 | ||
|
|
a85294a835 | ||
|
|
6b656195ce | ||
|
|
db778b5c74 | ||
|
|
73a75fd8b2 | ||
|
|
186711914a | ||
|
|
569dd54ccc | ||
|
|
91895278bf | ||
|
|
8683a04048 | ||
|
|
7c531d9dae | ||
|
|
487390fc20 | ||
|
|
c187ff8792 | ||
|
|
892e65e303 | ||
|
|
826c9e9699 | ||
|
|
87ebb3cbdf | ||
|
|
14a43f976d | ||
|
|
08f0fe4ae1 | ||
|
|
422dda0a75 | ||
|
|
3eabb0237e | ||
|
|
ada2cb7913 | ||
|
|
05b58e087e | ||
|
|
80ecc9ac09 | ||
|
|
bee97b8611 | ||
|
|
8a08e1ec4b | ||
|
|
bf1a5e6d33 | ||
|
|
5560bc19be | ||
|
|
36fd5d7755 | ||
|
|
3b191bcb9e | ||
|
|
01d05227c8 | ||
|
|
90400b9e43 | ||
|
|
b1e38db1e2 | ||
|
|
d77337bc79 | ||
|
|
80d2067134 | ||
|
|
887ceefa60 | ||
|
|
2b364367cb | ||
|
|
8aa4729537 | ||
|
|
41e9b55869 | ||
|
|
8a25b8bd63 | ||
|
|
774017db85 | ||
|
|
d1b50ec2ae | ||
|
|
bde804c6a4 | ||
|
|
0888d9ce8d | ||
|
|
1bc7542dc5 | ||
|
|
68689182be | ||
|
|
d5c176c5f4 | ||
|
|
38733043ae | ||
|
|
1b21e3b639 | ||
|
|
14e6806c1c | ||
|
|
dd95dae352 | ||
|
|
c1c211efdb | ||
|
|
ea69c30496 | ||
|
|
f71b419d8d | ||
|
|
35bbb32e03 | ||
|
|
f97cbb9a73 | ||
|
|
de39e67e18 | ||
|
|
78037a1161 | ||
|
|
1ce5ca1ba8 | ||
|
|
59b34b23e9 | ||
|
|
f01c46ab2e | ||
|
|
6dc78c99c2 | ||
|
|
3046af2b42 | ||
|
|
8973f4286b | ||
|
|
ef5f9d6858 | ||
|
|
f661105e63 | ||
|
|
bc10583bf3 | ||
|
|
cb1ea20f3f | ||
|
|
6bfc684967 | ||
|
|
63a9245f13 | ||
|
|
64d63cbb39 | ||
|
|
d963ebd0f0 | ||
|
|
1e1562b24a | ||
|
|
7eb7067752 | ||
|
|
bc4bfb5b7a | ||
|
|
94977691b8 | ||
|
|
b2791605b7 | ||
|
|
3e5d7f84aa | ||
|
|
eecc51e695 | ||
|
|
9dfc820306 | ||
|
|
c3fc93268c | ||
|
|
ecfd1b4cee | ||
|
|
5ce9fcf9f8 | ||
|
|
4e6c1b2d04 | ||
|
|
d8cdade30f | ||
|
|
21a2a59461 | ||
|
|
39dfa0ef7f | ||
|
|
37fd2d4448 | ||
|
|
6493eab98a | ||
|
|
caacee575c | ||
|
|
8abbf0a343 | ||
|
|
c32b8568cf | ||
|
|
654479a696 | ||
|
|
35b9de7f6e | ||
|
|
2294dee616 | ||
|
|
dabbfb7220 | ||
|
|
3e069b41af | ||
|
|
ead4c47fcb | ||
|
|
eda905684d | ||
|
|
ebd80b5115 | ||
|
|
866d20d976 | ||
|
|
0d891d284a | ||
|
|
9b8a3c6c1b | ||
|
|
2e8a5833a9 | ||
|
|
f76a81fe6b | ||
|
|
3a60921794 | ||
|
|
e531539e77 | ||
|
|
7afd62dd32 | ||
|
|
2e21175087 | ||
|
|
10dda1601e | ||
|
|
84b0d0ca40 | ||
|
|
50c799a4a0 | ||
|
|
aba2abba8a | ||
|
|
f6fee55411 | ||
|
|
4092cb10cc | ||
|
|
0169a3c3f3 | ||
|
|
fff4975c1e | ||
|
|
75faca6c17 | ||
|
|
a81c53a55c | ||
|
|
43c53739cf | ||
|
|
115e87b6ce | ||
|
|
33fa5b1b6b | ||
|
|
ca55e95c8f |
@@ -9,7 +9,6 @@ jobs:
|
|||||||
branches:
|
branches:
|
||||||
ignore:
|
ignore:
|
||||||
- gh-pages # list of branches to ignore
|
- gh-pages # list of branches to ignore
|
||||||
resource_class: large
|
|
||||||
steps:
|
steps:
|
||||||
- checkout
|
- checkout
|
||||||
- restore_cache:
|
- restore_cache:
|
||||||
@@ -36,4 +35,4 @@ jobs:
|
|||||||
destination: artifacts
|
destination: artifacts
|
||||||
- store_test_results:
|
- store_test_results:
|
||||||
path: /junit/
|
path: /junit/
|
||||||
destination: testartifacts
|
destination: testartifacts
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
---
|
||||||
|
name: Bug report
|
||||||
|
about: Create a report to help us improve
|
||||||
|
title: ''
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Describe the bug**
|
||||||
|
Please provide details of the problem, including the version of Spring Cloud that you
|
||||||
|
are using.
|
||||||
|
|
||||||
|
**Sample**
|
||||||
|
If possible, please provide a test case or sample application that reproduces
|
||||||
|
the problem. This makes it much easier for us to diagnose the problem and to verify that
|
||||||
|
we have fixed it.
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
---
|
||||||
|
name: Feature request
|
||||||
|
about: Suggest an idea for this project
|
||||||
|
title: ''
|
||||||
|
labels: ''
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Is your feature request related to a problem? Please describe.**
|
||||||
|
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||||
|
|
||||||
|
**Describe the solution you'd like**
|
||||||
|
A clear and concise description of what you want to happen.
|
||||||
|
|
||||||
|
**Describe alternatives you've considered**
|
||||||
|
A clear and concise description of any alternative solutions or features you've considered.
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context or screenshots about the feature request here.
|
||||||
@@ -18,3 +18,6 @@ _site/
|
|||||||
.shelf
|
.shelf
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
.vscode/
|
||||||
|
.flattened-pom.xml
|
||||||
|
|
||||||
|
|||||||
+25
-2
@@ -4,12 +4,30 @@ Manual changes to this file will be lost when it is generated again.
|
|||||||
Edit the files in the src/main/asciidoc/ directory instead.
|
Edit the files in the src/main/asciidoc/ directory instead.
|
||||||
////
|
////
|
||||||
|
|
||||||
|
|
||||||
image:https://circleci.com/gh/spring-cloud/spring-cloud-openfeign.svg?style=svg["CircleCI", link="https://circleci.com/gh/spring-cloud/spring-cloud-openfeign"]
|
image:https://circleci.com/gh/spring-cloud/spring-cloud-openfeign.svg?style=svg["CircleCI", link="https://circleci.com/gh/spring-cloud/spring-cloud-openfeign"]
|
||||||
|
|
||||||
image:https://codecov.io/gh/spring-cloud/spring-cloud-openfeign/branch/master/graph/badge.svg["Codecov", link="https://codecov.io/gh/spring-cloud/spring-cloud-openfeign"]
|
image:https://codecov.io/gh/spring-cloud/spring-cloud-openfeign/branch/master/graph/badge.svg["Codecov", link="https://codecov.io/gh/spring-cloud/spring-cloud-openfeign"]
|
||||||
|
|
||||||
image:https://api.codacy.com/project/badge/Grade/97b04c4e609c4b4f86b415e4437a6484["Codacy code quality", link="https://www.codacy.com/app/Spring-Cloud/spring-cloud-openfeign?utm_source=github.com&utm_medium=referral&utm_content=spring-cloud/spring-cloud-openfeign&utm_campaign=Badge_Grade"]
|
image:https://api.codacy.com/project/badge/Grade/97b04c4e609c4b4f86b415e4437a6484["Codacy code quality", link="https://www.codacy.com/app/Spring-Cloud/spring-cloud-openfeign?utm_source=github.com&utm_medium=referral&utm_content=spring-cloud/spring-cloud-openfeign&utm_campaign=Badge_Grade"]
|
||||||
|
|
||||||
|
:doctype: book
|
||||||
|
:idprefix:
|
||||||
|
:idseparator: -
|
||||||
|
:toc: left
|
||||||
|
:toclevels: 4
|
||||||
|
:tabsize: 4
|
||||||
|
:numbered:
|
||||||
|
:sectanchors:
|
||||||
|
:sectnums:
|
||||||
|
:icons: font
|
||||||
|
:hide-uri-scheme:
|
||||||
|
:docinfo: shared,private
|
||||||
|
|
||||||
|
:sc-ext: java
|
||||||
|
:project-full-name: Spring Cloud OpenFeign
|
||||||
|
:all: {asterisk}{asterisk}
|
||||||
|
|
||||||
This project provides OpenFeign integrations for Spring Boot apps through autoconfiguration
|
This project provides OpenFeign integrations for Spring Boot apps through autoconfiguration
|
||||||
and binding to the Spring Environment and other Spring programming model idioms.
|
and binding to the Spring Environment and other Spring programming model idioms.
|
||||||
|
|
||||||
@@ -82,6 +100,11 @@ https://eclipse.org[Eclipse] when working with the code. We use the
|
|||||||
https://eclipse.org/m2e/[m2eclipse] eclipse plugin for maven support. Other IDEs and tools
|
https://eclipse.org/m2e/[m2eclipse] eclipse plugin for maven support. Other IDEs and tools
|
||||||
should also work without issue as long as they use Maven 3.3.3 or better.
|
should also work without issue as long as they use Maven 3.3.3 or better.
|
||||||
|
|
||||||
|
==== Activate the Spring Maven profile
|
||||||
|
Spring Cloud projects require the 'spring' Maven profile to be activated to resolve
|
||||||
|
the spring milestone and snapshot repositories. Use your preferred IDE to set this
|
||||||
|
profile to be active, or you may experience build errors.
|
||||||
|
|
||||||
==== Importing into eclipse with m2eclipse
|
==== Importing into eclipse with m2eclipse
|
||||||
We recommend the https://eclipse.org/m2e/[m2eclipse] eclipse plugin when working with
|
We recommend the https://eclipse.org/m2e/[m2eclipse] eclipse plugin when working with
|
||||||
eclipse. If you don't already have m2eclipse installed it is available from the "eclipse
|
eclipse. If you don't already have m2eclipse installed it is available from the "eclipse
|
||||||
@@ -286,8 +309,8 @@ image::https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/{spring
|
|||||||
|
|
||||||
Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on the `+` icon in the `Configuration file` section. There, you'll have to define where the checkstyle rules should be picked from. In the image above, we've picked the rules from the cloned Spring Cloud Build repository. However, you can point to the Spring Cloud Build's GitHub repository (e.g. for the `checkstyle.xml` : `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle.xml`). We need to provide the following variables:
|
Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on the `+` icon in the `Configuration file` section. There, you'll have to define where the checkstyle rules should be picked from. In the image above, we've picked the rules from the cloned Spring Cloud Build repository. However, you can point to the Spring Cloud Build's GitHub repository (e.g. for the `checkstyle.xml` : `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle.xml`). We need to provide the following variables:
|
||||||
|
|
||||||
- `checkstyle.header.file` - please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/main/resources/checkstyle/checkstyle-header.txt` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` URL.
|
- `checkstyle.header.file` - please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/main/resources/checkstyle-header.txt` URL.
|
||||||
- `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL.
|
- `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL.
|
||||||
- `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`.
|
- `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`.
|
||||||
|
|
||||||
IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.
|
IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources.
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# Security Policy
|
||||||
|
|
||||||
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
To report security vulnerabilities, please go to https://pivotal.io/security.
|
||||||
+24
-20
@@ -6,54 +6,58 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-openfeign</artifactId>
|
<artifactId>spring-cloud-openfeign</artifactId>
|
||||||
<version>2.1.3.RELEASE</version>
|
<version>3.0.0-M3</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>spring-cloud-openfeign-docs</artifactId>
|
<artifactId>spring-cloud-openfeign-docs</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>jar</packaging>
|
||||||
<name>Spring Cloud OpenFeign Docs</name>
|
<name>Spring Cloud OpenFeign Docs</name>
|
||||||
<description>Spring Cloud Docs</description>
|
<description>Spring Cloud Docs</description>
|
||||||
<properties>
|
<properties>
|
||||||
<docs.main>spring-cloud-openfeign</docs.main>
|
<docs.main>spring-cloud-openfeign</docs.main>
|
||||||
<main.basedir>${basedir}/..</main.basedir>
|
<main.basedir>${basedir}/..</main.basedir>
|
||||||
<docs.whitelisted.branches>2.0.x,2.1.x</docs.whitelisted.branches>
|
<configprops.inclusionPattern>feign.*</configprops.inclusionPattern>
|
||||||
|
<upload-docs-zip.phase>deploy</upload-docs-zip.phase>
|
||||||
</properties>
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>${project.groupId}</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<sourceDirectory>src/main/asciidoc</sourceDirectory>
|
||||||
<plugin>
|
|
||||||
<artifactId>maven-deploy-plugin</artifactId>
|
|
||||||
<configuration>
|
|
||||||
<skip>true</skip>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
</build>
|
||||||
<profiles>
|
<profiles>
|
||||||
<profile>
|
<profile>
|
||||||
<id>docs</id>
|
<id>docs</id>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>pl.project13.maven</groupId>
|
||||||
|
<artifactId>git-commit-id-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-dependency-plugin</artifactId>
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.asciidoctor</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>asciidoctor-maven-plugin</artifactId>
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
<inherited>false</inherited>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>com.agilejava.docbkx</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
<artifactId>docbkx-maven-plugin</artifactId>
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.asciidoctor</groupId>
|
||||||
|
<artifactId>asciidoctor-maven-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-antrun-plugin</artifactId>
|
<artifactId>maven-antrun-plugin</artifactId>
|
||||||
<inherited>false</inherited>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<artifactId>maven-deploy-plugin</artifactId>
|
||||||
<artifactId>build-helper-maven-plugin</artifactId>
|
|
||||||
<inherited>false</inherited>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ image:https://codecov.io/gh/spring-cloud/spring-cloud-openfeign/branch/master/gr
|
|||||||
|
|
||||||
image:https://api.codacy.com/project/badge/Grade/97b04c4e609c4b4f86b415e4437a6484["Codacy code quality", link="https://www.codacy.com/app/Spring-Cloud/spring-cloud-openfeign?utm_source=github.com&utm_medium=referral&utm_content=spring-cloud/spring-cloud-openfeign&utm_campaign=Badge_Grade"]
|
image:https://api.codacy.com/project/badge/Grade/97b04c4e609c4b4f86b415e4437a6484["Codacy code quality", link="https://www.codacy.com/app/Spring-Cloud/spring-cloud-openfeign?utm_source=github.com&utm_medium=referral&utm_content=spring-cloud/spring-cloud-openfeign&utm_campaign=Badge_Grade"]
|
||||||
|
|
||||||
|
include::_attributes.adoc[]
|
||||||
|
|
||||||
include::intro.adoc[]
|
include::intro.adoc[]
|
||||||
|
|
||||||
== Features
|
== Features
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
:doctype: book
|
||||||
|
:idprefix:
|
||||||
|
:idseparator: -
|
||||||
|
:toc: left
|
||||||
|
:toclevels: 4
|
||||||
|
:tabsize: 4
|
||||||
|
:numbered:
|
||||||
|
:sectanchors:
|
||||||
|
:sectnums:
|
||||||
|
:icons: font
|
||||||
|
:hide-uri-scheme:
|
||||||
|
:docinfo: shared,private
|
||||||
|
|
||||||
|
:sc-ext: java
|
||||||
|
:project-full-name: Spring Cloud OpenFeign
|
||||||
|
:all: {asterisk}{asterisk}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
|===
|
||||||
|
|Name | Default | Description
|
||||||
|
|
||||||
|
|feign.client.config | |
|
||||||
|
|feign.client.default-config | default |
|
||||||
|
|feign.client.default-to-properties | true |
|
||||||
|
|feign.compression.request.enabled | false | Enables the request sent by Feign to be compressed.
|
||||||
|
|feign.compression.request.mime-types | [text/xml, application/xml, application/json] | The list of supported mime types.
|
||||||
|
|feign.compression.request.min-request-size | 2048 | The minimum threshold content size.
|
||||||
|
|feign.compression.response.enabled | false | Enables the response from Feign to be compressed.
|
||||||
|
|feign.compression.response.useGzipDecoder | false | Enables the default gzip decoder to be used.
|
||||||
|
|feign.httpclient.connection-timeout | 2000 |
|
||||||
|
|feign.httpclient.connection-timer-repeat | 3000 |
|
||||||
|
|feign.httpclient.disable-ssl-validation | false |
|
||||||
|
|feign.httpclient.enabled | true | Enables the use of the Apache HTTP Client by Feign.
|
||||||
|
|feign.httpclient.follow-redirects | true |
|
||||||
|
|feign.httpclient.max-connections | 200 |
|
||||||
|
|feign.httpclient.max-connections-per-route | 50 |
|
||||||
|
|feign.httpclient.time-to-live | 900 |
|
||||||
|
|feign.httpclient.time-to-live-unit | |
|
||||||
|
|feign.hystrix.enabled | false | If true, an OpenFeign client will be wrapped with a Hystrix circuit breaker.
|
||||||
|
|feign.okhttp.enabled | false | Enables the use of the OK HTTP Client by Feign.
|
||||||
|
|
||||||
|
|===
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
:numbered!:
|
||||||
|
[appendix]
|
||||||
|
[[common-application-properties]]
|
||||||
|
== Common application properties
|
||||||
|
|
||||||
|
include::_attributes.adoc[]
|
||||||
|
|
||||||
|
Various properties can be specified inside your `application.properties` file, inside your `application.yml` file, or as command line switches.
|
||||||
|
This appendix provides a list of common {project-full-name} properties and references to the underlying classes that consume them.
|
||||||
|
|
||||||
|
NOTE: Property contributions can come from additional jar files on your classpath, so you should not consider this an exhaustive list.
|
||||||
|
Also, you can define your own properties.
|
||||||
|
|
||||||
|
include::_configprops.adoc[]
|
||||||
@@ -40,7 +40,7 @@ function check_if_anything_to_sync() {
|
|||||||
|
|
||||||
function retrieve_current_branch() {
|
function retrieve_current_branch() {
|
||||||
# Code getting the name of the current branch. For master we want to publish as we did until now
|
# Code getting the name of the current branch. For master we want to publish as we did until now
|
||||||
# http://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch
|
# https://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch
|
||||||
# If there is a branch already passed will reuse it - otherwise will try to find it
|
# If there is a branch already passed will reuse it - otherwise will try to find it
|
||||||
CURRENT_BRANCH=${BRANCH}
|
CURRENT_BRANCH=${BRANCH}
|
||||||
if [[ -z "${CURRENT_BRANCH}" ]] ; then
|
if [[ -z "${CURRENT_BRANCH}" ]] ; then
|
||||||
@@ -147,7 +147,7 @@ function copy_docs_for_current_version() {
|
|||||||
COMMIT_CHANGES="yes"
|
COMMIT_CHANGES="yes"
|
||||||
else
|
else
|
||||||
echo -e "Current branch is [${CURRENT_BRANCH}]"
|
echo -e "Current branch is [${CURRENT_BRANCH}]"
|
||||||
# http://stackoverflow.com/questions/29300806/a-bash-script-to-check-if-a-string-is-present-in-a-comma-separated-list-of-strin
|
# https://stackoverflow.com/questions/29300806/a-bash-script-to-check-if-a-string-is-present-in-a-comma-separated-list-of-strin
|
||||||
if [[ ",${WHITELISTED_BRANCHES_VALUE}," = *",${CURRENT_BRANCH},"* ]] ; then
|
if [[ ",${WHITELISTED_BRANCHES_VALUE}," = *",${CURRENT_BRANCH},"* ]] ; then
|
||||||
mkdir -p ${ROOT_FOLDER}/${CURRENT_BRANCH}
|
mkdir -p ${ROOT_FOLDER}/${CURRENT_BRANCH}
|
||||||
echo -e "Branch [${CURRENT_BRANCH}] is whitelisted! Will copy the current docs to the [${CURRENT_BRANCH}] folder"
|
echo -e "Branch [${CURRENT_BRANCH}] is whitelisted! Will copy the current docs to the [${CURRENT_BRANCH}] folder"
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
This project provides https://github.com/OpenFeign/feign[OpenFeign] integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
* Declarative REST Client: Feign creates a dynamic implementation of an interface decorated with JAX-RS or Spring MVC annotations
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
|
||||||
|
```java
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
|
public class WebApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(WebApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient("name")
|
||||||
|
static interface NameService {
|
||||||
|
@RequestMapping("/")
|
||||||
|
public String getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
@@ -1,11 +1,5 @@
|
|||||||
:github-tag: master
|
|
||||||
:github-repo: spring-cloud/spring-cloud-openfeign
|
|
||||||
:github-raw: https://raw.github.com/{github-repo}/{github-tag}
|
|
||||||
:github-code: https://github.com/{github-repo}/tree/{github-tag}
|
|
||||||
:all: {asterisk}{asterisk}
|
|
||||||
:nofooter:
|
|
||||||
:branch: master
|
|
||||||
= Spring Cloud OpenFeign
|
= Spring Cloud OpenFeign
|
||||||
|
include::_attributes.adoc[]
|
||||||
|
|
||||||
*{spring-cloud-version}*
|
*{spring-cloud-version}*
|
||||||
|
|
||||||
@@ -15,7 +9,13 @@ include::intro.adoc[]
|
|||||||
[[spring-cloud-feign]]
|
[[spring-cloud-feign]]
|
||||||
== Declarative REST Client: Feign
|
== Declarative REST Client: Feign
|
||||||
|
|
||||||
https://github.com/Netflix/feign[Feign] is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same `HttpMessageConverters` used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
|
https://github.com/OpenFeign/feign[Feign] is a declarative web service client.
|
||||||
|
It makes writing web service clients easier.
|
||||||
|
To use Feign create an interface and annotate it.
|
||||||
|
It has pluggable annotation support including Feign annotations and JAX-RS annotations.
|
||||||
|
Feign also supports pluggable encoders and decoders.
|
||||||
|
Spring Cloud adds support for Spring MVC annotations and for using the same `HttpMessageConverters` used by default in Spring Web.
|
||||||
|
Spring Cloud integrates Eureka, as well as Spring Cloud LoadBalancer to provide a load-balanced http client when using Feign.
|
||||||
|
|
||||||
[[netflix-feign-starter]]
|
[[netflix-feign-starter]]
|
||||||
=== How to Include Feign
|
=== How to Include Feign
|
||||||
@@ -47,26 +47,26 @@ public interface StoreClient {
|
|||||||
@RequestMapping(method = RequestMethod.GET, value = "/stores")
|
@RequestMapping(method = RequestMethod.GET, value = "/stores")
|
||||||
List<Store> getStores();
|
List<Store> getStores();
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, value = "/stores")
|
||||||
|
Page<Store> getStores(Pageable pageable);
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
|
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
|
||||||
Store update(@PathVariable("storeId") Long storeId, Store store);
|
Store update(@PathVariable("storeId") Long storeId, Store store);
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
In the `@FeignClient` annotation the String value ("stores" above) is
|
In the `@FeignClient` annotation the String value ("stores" above) is an arbitrary client name, which is used to create a https://github.com/spring-cloud/spring-cloud-commons/blob/master/spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/blocking/client/BlockingLoadBalancerClient.java[Spring Cloud LoadBalancer client].
|
||||||
an arbitrary client name, which is used to create a Ribbon load
|
You can also specify a URL using the `url` attribute
|
||||||
balancer (see <<spring-cloud-ribbon,below for details of Ribbon
|
|
||||||
support>>). You can also specify a URL using the `url` attribute
|
|
||||||
(absolute value or just a hostname). The name of the bean in the
|
(absolute value or just a hostname). The name of the bean in the
|
||||||
application context is the fully qualified name of the interface.
|
application context is the fully qualified name of the interface.
|
||||||
To specify your own alias value you can use the `qualifier` value
|
To specify your own alias value you can use the `qualifier` value
|
||||||
of the `@FeignClient` annotation.
|
of the `@FeignClient` annotation.
|
||||||
|
|
||||||
The Ribbon client above will want to discover the physical addresses
|
The load-balancer client above will want to discover the physical addresses
|
||||||
for the "stores" service. If your application is a Eureka client then
|
for the "stores" service. If your application is a Eureka client then
|
||||||
it will resolve the service in the Eureka service registry. If you
|
it will resolve the service in the Eureka service registry. If you
|
||||||
don't want to use Eureka, you can simply configure a list of servers
|
don't want to use Eureka, you can simply configure a list of servers
|
||||||
in your external configuration (see
|
in your external configuration using https://cloud.spring.io/spring-cloud-static/spring-cloud-commons/current/reference/html/#simplediscoveryclient[`SimpleDiscoveryClient`].
|
||||||
<<spring-cloud-ribbon-without-eureka,above for example>>).
|
|
||||||
|
|
||||||
[[spring-cloud-feign-overriding-defaults]]
|
[[spring-cloud-feign-overriding-defaults]]
|
||||||
=== Overriding Feign Defaults
|
=== Overriding Feign Defaults
|
||||||
@@ -108,19 +108,22 @@ public interface StoreClient {
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
Spring Cloud Netflix provides the following beans by default for feign (`BeanType` beanName: `ClassName`):
|
Spring Cloud OpenFeign provides the following beans by default for feign (`BeanType` beanName: `ClassName`):
|
||||||
|
|
||||||
* `Decoder` feignDecoder: `ResponseEntityDecoder` (which wraps a `SpringDecoder`)
|
* `Decoder` feignDecoder: `ResponseEntityDecoder` (which wraps a `SpringDecoder`)
|
||||||
* `Encoder` feignEncoder: `SpringEncoder`
|
* `Encoder` feignEncoder: `SpringEncoder`
|
||||||
* `Logger` feignLogger: `Slf4jLogger`
|
* `Logger` feignLogger: `Slf4jLogger`
|
||||||
* `Contract` feignContract: `SpringMvcContract`
|
* `Contract` feignContract: `SpringMvcContract`
|
||||||
* `Feign.Builder` feignBuilder: `HystrixFeign.Builder`
|
* `Feign.Builder` feignBuilder: `HystrixFeign.Builder`
|
||||||
* `Client` feignClient: if Ribbon is enabled it is a `LoadBalancerFeignClient`, otherwise the default feign client is used.
|
* `Client` feignClient: if Spring Cloud LoadBalancer is in the classpath, `FeignBlockingLoadBalancerClient` is used.
|
||||||
|
If none of them is in the classpath, the default feign client is used.
|
||||||
|
|
||||||
|
NOTE: `spring-cloud-starter-openfeign` supports `spring-cloud-starter-loadbalancer`. However, as is an optional dependency, you need to make sure it been added to your project if you want to use it.
|
||||||
|
|
||||||
The OkHttpClient and ApacheHttpClient feign clients can be used by setting `feign.okhttp.enabled` or `feign.httpclient.enabled` to `true`, respectively, and having them on the classpath.
|
The OkHttpClient and ApacheHttpClient feign clients can be used by setting `feign.okhttp.enabled` or `feign.httpclient.enabled` to `true`, respectively, and having them on the classpath.
|
||||||
You can customize the HTTP client used by providing a bean of either `ClosableHttpClient` when using Apache or `OkHttpClient` when using OK HTTP.
|
You can customize the HTTP client used by providing a bean of either `org.apache.http.impl.client.CloseableHttpClient` when using Apache or `okhttp3.OkHttpClient` when using OK HTTP.
|
||||||
|
|
||||||
Spring Cloud Netflix _does not_ provide the following beans by default for feign, but still looks up beans of these types from the application context to create the feign client:
|
Spring Cloud OpenFeign _does not_ provide the following beans by default for feign, but still looks up beans of these types from the application context to create the feign client:
|
||||||
|
|
||||||
* `Logger.Level`
|
* `Logger.Level`
|
||||||
* `Retryer`
|
* `Retryer`
|
||||||
@@ -128,6 +131,11 @@ Spring Cloud Netflix _does not_ provide the following beans by default for feign
|
|||||||
* `Request.Options`
|
* `Request.Options`
|
||||||
* `Collection<RequestInterceptor>`
|
* `Collection<RequestInterceptor>`
|
||||||
* `SetterFactory`
|
* `SetterFactory`
|
||||||
|
* `QueryMapEncoder`
|
||||||
|
|
||||||
|
A bean of `Retryer.NEVER_RETRY` with the type `Retryer` is created by default, which will disable retrying.
|
||||||
|
Notice this retrying behavior is different from the Feign default one, where it will automatically retry IOExceptions,
|
||||||
|
treating them as transient network related exceptions, and any RetryableException thrown from an ErrorDecoder.
|
||||||
|
|
||||||
Creating a bean of one of those type and placing it in a `@FeignClient` configuration (such as `FooConfiguration` above) allows you to override each one of the beans described. Example:
|
Creating a bean of one of those type and placing it in a `@FeignClient` configuration (such as `FooConfiguration` above) allows you to override each one of the beans described. Example:
|
||||||
|
|
||||||
@@ -233,6 +241,29 @@ public interface BarClient {
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
It is also possible to configure FeignClient not to inherit beans from the parent context.
|
||||||
|
You can do this by overriding the `inheritParentConfiguration()` in a `FeignClientConfigurer`
|
||||||
|
bean to return `false`:
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
@Configuration
|
||||||
|
public class CustomConfiguration{
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FeignClientConfigurer feignClientConfigurer() {
|
||||||
|
return new FeignClientConfigurer() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean inheritParentConfiguration() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
=== Creating Feign Clients Manually
|
=== Creating Feign Clients Manually
|
||||||
|
|
||||||
In some cases it might be necessary to customize your Feign Clients in a way that is not
|
In some cases it might be necessary to customize your Feign Clients in a way that is not
|
||||||
@@ -257,14 +288,14 @@ class FooController {
|
|||||||
.decoder(decoder)
|
.decoder(decoder)
|
||||||
.contract(contract)
|
.contract(contract)
|
||||||
.requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
|
.requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
|
||||||
.target(FooClient.class, "http://PROD-SVC");
|
.target(FooClient.class, "https://PROD-SVC");
|
||||||
|
|
||||||
this.adminClient = Feign.builder().client(client)
|
this.adminClient = Feign.builder().client(client)
|
||||||
.encoder(encoder)
|
.encoder(encoder)
|
||||||
.decoder(decoder)
|
.decoder(decoder)
|
||||||
.contract(contract)
|
.contract(contract)
|
||||||
.requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
|
.requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
|
||||||
.target(FooClient.class, "http://PROD-SVC");
|
.target(FooClient.class, "https://PROD-SVC");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
@@ -278,6 +309,9 @@ NOTE: The Feign `Contract` object defines what annotations and values are valid
|
|||||||
autowired `Contract` bean provides supports for SpringMVC annotations, instead of
|
autowired `Contract` bean provides supports for SpringMVC annotations, instead of
|
||||||
the default Feign native annotations.
|
the default Feign native annotations.
|
||||||
|
|
||||||
|
You can also use the `Builder`to configure FeignClient not to inherit beans from the parent context.
|
||||||
|
You can do this by overriding calling `inheritParentContext(false)` on the `Builder`.
|
||||||
|
|
||||||
[[spring-cloud-feign-hystrix]]
|
[[spring-cloud-feign-hystrix]]
|
||||||
=== Feign Hystrix Support
|
=== Feign Hystrix Support
|
||||||
|
|
||||||
@@ -423,6 +457,14 @@ feign.compression.request.min-request-size=2048
|
|||||||
|
|
||||||
These properties allow you to be selective about the compressed media types and minimum request threshold length.
|
These properties allow you to be selective about the compressed media types and minimum request threshold length.
|
||||||
|
|
||||||
|
For http clients except OkHttpClient, default gzip decoder can be enabled to decode gzip response in UTF-8 encoding:
|
||||||
|
|
||||||
|
[source,java]
|
||||||
|
----
|
||||||
|
feign.compression.response.enabled=true
|
||||||
|
feign.compression.response.useGzipDecoder=true
|
||||||
|
----
|
||||||
|
|
||||||
=== Feign logging
|
=== Feign logging
|
||||||
|
|
||||||
A logger is created for each Feign client created. By default the name of the logger is the full class name of the interface used to create the Feign client. Feign logging only responds to the `DEBUG` level.
|
A logger is created for each Feign client created. By default the name of the logger is the full class name of the interface used to create the Feign client. Feign logging only responds to the `DEBUG` level.
|
||||||
@@ -481,9 +523,100 @@ The following feign client uses the `Params` class by using the `@SpringQueryMap
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
@FeignClient("demo")
|
@FeignClient("demo")
|
||||||
public class DemoTemplate {
|
public interface DemoTemplate {
|
||||||
|
|
||||||
@GetMapping(path = "/demo")
|
@GetMapping(path = "/demo")
|
||||||
String demoEndpoint(@SpringQueryMap Params params);
|
String demoEndpoint(@SpringQueryMap Params params);
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
If you need more control over the generated query parameter map, you can implement a custom `QueryMapEncoder` bean.
|
||||||
|
|
||||||
|
=== HATEOAS support
|
||||||
|
|
||||||
|
Spring provides some APIs to create REST representations that follow the https://en.wikipedia.org/wiki/HATEOAS[HATEOAS] principle, https://spring.io/projects/spring-hateoas[Spring Hateoas] and https://spring.io/projects/spring-data-rest[Spring Data REST].
|
||||||
|
|
||||||
|
If your project use the `org.springframework.boot:spring-boot-starter-hateoas` starter
|
||||||
|
or the `org.springframework.boot:spring-boot-starter-data-rest` starter, Feign HATEOAS support is enabled by default.
|
||||||
|
|
||||||
|
When HATEOAS support is enabled, Feign clients are allowed to serialize
|
||||||
|
and deserialize HATEOAS representation models: https://docs.spring.io/spring-hateoas/docs/1.0.0.M1/apidocs/org/springframework/hateoas/EntityModel.html[EntityModel], https://docs.spring.io/spring-hateoas/docs/1.0.0.M1/apidocs/org/springframework/hateoas/CollectionModel.html[CollectionModel] and https://docs.spring.io/spring-hateoas/docs/1.0.0.M1/apidocs/org/springframework/hateoas/PagedModel.html[PagedModel].
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
@FeignClient("demo")
|
||||||
|
public interface DemoTemplate {
|
||||||
|
|
||||||
|
@GetMapping(path = "/stores")
|
||||||
|
CollectionModel<Store> getStores();
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
=== Spring @MatrixVariable Support
|
||||||
|
|
||||||
|
Spring Cloud OpenFeign provides support for the Spring `@MatrixVariable` annotation.
|
||||||
|
|
||||||
|
If a map is passed as the method argument, the `@MatrixVariable` path segment is created by joining key-value pairs from the map with a `=`.
|
||||||
|
|
||||||
|
If a different object is passed, either the `name` provided in the `@MatrixVariable` annotation (if defined) or the annotated variable name is
|
||||||
|
joined with the provided method argument using `=`.
|
||||||
|
|
||||||
|
IMPORTANT:: Even though, on the server side, Spring does not require the users to name the path segment placeholder same as the matrix variable name, since it would be too ambiguous on the client side, Sprig Cloud OpenFeign requires that you add a path segment placeholder with a name matching either the `name` provided in the `@MatrixVariable` annotation (if defined) or the annotated variable name.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
@GetMapping("/objects/links/{matrixVars}")
|
||||||
|
Map<String, List<String>> getObjects(@MatrixVariable Map<String, List<String>> matrixVars);
|
||||||
|
----
|
||||||
|
Note that both variable name and the path segment placeholder are called `matrixVars`.
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
@FeignClient("demo")
|
||||||
|
public interface DemoTemplate {
|
||||||
|
|
||||||
|
@GetMapping(path = "/stores")
|
||||||
|
CollectionModel<Store> getStores();
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
=== Feign `CollectionFormat` support
|
||||||
|
We support `feign.CollectionFormat` by providing the `@CollectionFormat` annotation. You can annotate a Feign client method with it by passing the desired `feign.CollectionFormat` as annotation value.
|
||||||
|
|
||||||
|
In the following example, the `CSV` format is used instead of the default `EXPLODED` to process the method.
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
@FeignClient(name = "demo")
|
||||||
|
protected interface PageableFeignClient {
|
||||||
|
|
||||||
|
@CollectionFormat(feign.CollectionFormat.CSV)
|
||||||
|
@GetMapping(path = "/page")
|
||||||
|
ResponseEntity performRequest(Pageable page);
|
||||||
|
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
TIP: Set the `CSV` format while sending `Pageable` as a query parameter in order for it to be encoded correctly.
|
||||||
|
|
||||||
|
=== Reactive Support
|
||||||
|
As the https://github.com/OpenFeign/feign[OpenFeign project] does not currently support reactive clients, such as https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/reactive/function/client/WebClient.html[Spring WebClient], neither does Spring Cloud OpenFeign. We will add support for it here as soon as it becomes available in the core project.
|
||||||
|
|
||||||
|
Until that is done, we recommend using https://github.com/Playtika/feign-reactive[feign-reactive] for Spring WebClient support.
|
||||||
|
|
||||||
|
==== Early Initialization Errors
|
||||||
|
|
||||||
|
Depending on how you are using your Feign clients you may see initialization errors when starting your application.
|
||||||
|
To work around this problem you can use an `ObjectProvider` when autowiring your client.
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
@Autowired
|
||||||
|
ObjectProvider<TestFeginClient> testFeginClient;
|
||||||
|
----
|
||||||
|
|
||||||
|
== Configuration properties
|
||||||
|
|
||||||
|
To see the list of all Sleuth related configuration properties please check link:appendix.html[the Appendix page].
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
# "License"); you may not use this file except in compliance
|
# "License"); you may not use this file except in compliance
|
||||||
# with the License. You may obtain a copy of the License at
|
# with the License. You may obtain a copy of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law or agreed to in writing,
|
# Unless required by applicable law or agreed to in writing,
|
||||||
# software distributed under the License is distributed on an
|
# software distributed under the License is distributed on an
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
@REM "License"); you may not use this file except in compliance
|
@REM "License"); you may not use this file except in compliance
|
||||||
@REM with the License. You may obtain a copy of the License at
|
@REM with the License. You may obtain a copy of the License at
|
||||||
@REM
|
@REM
|
||||||
@REM http://www.apache.org/licenses/LICENSE-2.0
|
@REM https://www.apache.org/licenses/LICENSE-2.0
|
||||||
@REM
|
@REM
|
||||||
@REM Unless required by applicable law or agreed to in writing,
|
@REM Unless required by applicable law or agreed to in writing,
|
||||||
@REM software distributed under the License is distributed on an
|
@REM software distributed under the License is distributed on an
|
||||||
|
|||||||
@@ -4,14 +4,14 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>spring-cloud-openfeign</artifactId>
|
<artifactId>spring-cloud-openfeign</artifactId>
|
||||||
<version>2.1.3.RELEASE</version>
|
<version>3.0.0-M3</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<name>Spring Cloud OpenFeign</name>
|
<name>Spring Cloud OpenFeign</name>
|
||||||
<description>Spring Cloud OpenFeign</description>
|
<description>Spring Cloud OpenFeign</description>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-build</artifactId>
|
<artifactId>spring-cloud-build</artifactId>
|
||||||
<version>2.1.7.RELEASE</version>
|
<version>3.0.0-M3</version>
|
||||||
<relativePath/>
|
<relativePath/>
|
||||||
</parent>
|
</parent>
|
||||||
<scm>
|
<scm>
|
||||||
@@ -26,8 +26,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<main.basedir>${basedir}</main.basedir>
|
<main.basedir>${basedir}</main.basedir>
|
||||||
<jackson.version>2.7.3</jackson.version>
|
<jackson.version>2.7.3</jackson.version>
|
||||||
<spring-cloud-commons.version>2.1.3.RELEASE</spring-cloud-commons.version>
|
<spring-cloud-commons.version>3.0.0-M3</spring-cloud-commons.version>
|
||||||
<spring-cloud-netflix.version>2.1.3.RELEASE</spring-cloud-netflix.version>
|
|
||||||
|
|
||||||
<!-- Plugin versions -->
|
<!-- Plugin versions -->
|
||||||
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
|
||||||
@@ -39,15 +38,13 @@
|
|||||||
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec
|
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec
|
||||||
</sonar.jacoco.reportPath>
|
</sonar.jacoco.reportPath>
|
||||||
<sonar.language>java</sonar.language>
|
<sonar.language>java</sonar.language>
|
||||||
|
|
||||||
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError>
|
|
||||||
<maven-checkstyle-plugin.failsOnViolation>true
|
|
||||||
</maven-checkstyle-plugin.failsOnViolation>
|
|
||||||
<maven-checkstyle-plugin.includeTestSourceDirectory>true
|
|
||||||
</maven-checkstyle-plugin.includeTestSourceDirectory>
|
|
||||||
</properties>
|
</properties>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>flatten-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-eclipse-plugin</artifactId>
|
<artifactId>maven-eclipse-plugin</artifactId>
|
||||||
@@ -98,13 +95,6 @@
|
|||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-netflix-dependencies</artifactId>
|
|
||||||
<version>${spring-cloud-netflix.version}</version>
|
|
||||||
<type>pom</type>
|
|
||||||
<scope>import</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-commons-dependencies</artifactId>
|
<artifactId>spring-cloud-commons-dependencies</artifactId>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-openfeign</artifactId>
|
<artifactId>spring-cloud-openfeign</artifactId>
|
||||||
<version>2.1.3.RELEASE</version>
|
<version>3.0.0-M3</version>
|
||||||
<relativePath>..</relativePath> <!-- lookup parent from repository -->
|
<relativePath>..</relativePath> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>spring-cloud-openfeign-core</artifactId>
|
<artifactId>spring-cloud-openfeign-core</artifactId>
|
||||||
@@ -21,10 +21,6 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-netflix-ribbon</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
@@ -50,6 +46,11 @@
|
|||||||
<artifactId>spring-boot-starter-reactor-netty</artifactId>
|
<artifactId>spring-boot-starter-reactor-netty</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.netty</groupId>
|
<groupId>io.netty</groupId>
|
||||||
<artifactId>netty-codec-http</artifactId>
|
<artifactId>netty-codec-http</artifactId>
|
||||||
@@ -84,11 +85,6 @@
|
|||||||
<artifactId>spring-cloud-context</artifactId>
|
<artifactId>spring-cloud-context</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.netflix.ribbon</groupId>
|
|
||||||
<artifactId>ribbon-loadbalancer</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.github.openfeign</groupId>
|
<groupId>io.github.openfeign</groupId>
|
||||||
<artifactId>feign-core</artifactId>
|
<artifactId>feign-core</artifactId>
|
||||||
@@ -108,51 +104,11 @@
|
|||||||
<artifactId>feign-httpclient</artifactId>
|
<artifactId>feign-httpclient</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>io.github.openfeign</groupId>
|
|
||||||
<artifactId>feign-hystrix</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.github.openfeign</groupId>
|
<groupId>io.github.openfeign</groupId>
|
||||||
<artifactId>feign-okhttp</artifactId>
|
<artifactId>feign-okhttp</artifactId>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>io.github.openfeign</groupId>
|
|
||||||
<artifactId>feign-java8</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.netflix.hystrix</groupId>
|
|
||||||
<artifactId>hystrix-core</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.netflix.hystrix</groupId>
|
|
||||||
<artifactId>hystrix-serialization</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.netflix.hystrix</groupId>
|
|
||||||
<artifactId>hystrix-metrics-event-stream</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.netflix.hystrix</groupId>
|
|
||||||
<artifactId>hystrix-javanica</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.netflix.ribbon</groupId>
|
|
||||||
<artifactId>ribbon-core</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.netflix.ribbon</groupId>
|
|
||||||
<artifactId>ribbon-httpclient</artifactId>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.reactivex</groupId>
|
<groupId>io.reactivex</groupId>
|
||||||
<artifactId>rxjava</artifactId>
|
<artifactId>rxjava</artifactId>
|
||||||
@@ -161,7 +117,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.okhttp3</groupId>
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
<artifactId>okhttp</artifactId>
|
<artifactId>okhttp</artifactId>
|
||||||
<version>4.1.0</version>
|
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -179,6 +134,11 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-test-support</artifactId>
|
<artifactId>spring-cloud-test-support</artifactId>
|
||||||
@@ -195,13 +155,8 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.netflix.ribbon</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>ribbon</artifactId>
|
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
|
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -222,6 +177,17 @@
|
|||||||
<version>0.10.0</version>
|
<version>0.10.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-loadbalancer</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>pl.pragmatists</groupId>
|
||||||
|
<artifactId>JUnitParams</artifactId>
|
||||||
|
<version>1.1.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<profiles>
|
<profiles>
|
||||||
<profile>
|
<profile>
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+41
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates which collection format should be used while processing the annotated method.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @see feign.CollectionFormat
|
||||||
|
*/
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface CollectionFormat {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows setting the {@link feign.CollectionFormat} to be used while processing the
|
||||||
|
* annotated method.
|
||||||
|
* @return the {@link feign.CollectionFormat} to be used
|
||||||
|
*/
|
||||||
|
feign.CollectionFormat value();
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016-2019 the original author or authors.
|
* Copyright 2016-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+10
-23
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -38,7 +38,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cloud.client.actuator.HasFeatures;
|
import org.springframework.cloud.client.actuator.HasFeatures;
|
||||||
@@ -46,18 +45,21 @@ import org.springframework.cloud.commons.httpclient.ApacheHttpClientConnectionMa
|
|||||||
import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory;
|
import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory;
|
||||||
import org.springframework.cloud.commons.httpclient.OkHttpClientConnectionPoolFactory;
|
import org.springframework.cloud.commons.httpclient.OkHttpClientConnectionPoolFactory;
|
||||||
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
|
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
|
||||||
|
import org.springframework.cloud.openfeign.support.DefaultGzipDecoderConfiguration;
|
||||||
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Spencer Gibb
|
* @author Spencer Gibb
|
||||||
* @author Julien Roy
|
* @author Julien Roy
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnClass(Feign.class)
|
@ConditionalOnClass(Feign.class)
|
||||||
@EnableConfigurationProperties({ FeignClientProperties.class,
|
@EnableConfigurationProperties({ FeignClientProperties.class,
|
||||||
FeignHttpClientProperties.class })
|
FeignHttpClientProperties.class })
|
||||||
|
@Import(DefaultGzipDecoderConfiguration.class)
|
||||||
public class FeignAutoConfiguration {
|
public class FeignAutoConfiguration {
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
@@ -75,20 +77,7 @@ public class FeignAutoConfiguration {
|
|||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnClass(name = "feign.hystrix.HystrixFeign")
|
|
||||||
protected static class HystrixFeignTargeterConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
public Targeter feignTargeter() {
|
|
||||||
return new HystrixTargeter();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnMissingClass("feign.hystrix.HystrixFeign")
|
|
||||||
protected static class DefaultFeignTargeterConfiguration {
|
protected static class DefaultFeignTargeterConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -100,12 +89,11 @@ public class FeignAutoConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the following configuration is for alternate feign clients if
|
// the following configuration is for alternate feign clients if
|
||||||
// ribbon is not on the class path.
|
// SC loadbalancer is not on the class path.
|
||||||
// see corresponding configurations in FeignRibbonClientAutoConfiguration
|
// see corresponding configurations in FeignRibbonClientAutoConfiguration
|
||||||
// for load balanced ribbon clients.
|
// for load-balanced clients.
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnClass(ApacheHttpClient.class)
|
@ConditionalOnClass(ApacheHttpClient.class)
|
||||||
@ConditionalOnMissingClass("com.netflix.loadbalancer.ILoadBalancer")
|
|
||||||
@ConditionalOnMissingBean(CloseableHttpClient.class)
|
@ConditionalOnMissingBean(CloseableHttpClient.class)
|
||||||
@ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
|
@ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
|
||||||
protected static class HttpClientFeignConfiguration {
|
protected static class HttpClientFeignConfiguration {
|
||||||
@@ -169,9 +157,8 @@ public class FeignAutoConfiguration {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnClass(OkHttpClient.class)
|
@ConditionalOnClass(OkHttpClient.class)
|
||||||
@ConditionalOnMissingClass("com.netflix.loadbalancer.ILoadBalancer")
|
|
||||||
@ConditionalOnMissingBean(okhttp3.OkHttpClient.class)
|
@ConditionalOnMissingBean(okhttp3.OkHttpClient.class)
|
||||||
@ConditionalOnProperty("feign.okhttp.enabled")
|
@ConditionalOnProperty("feign.okhttp.enabled")
|
||||||
protected static class OkHttpFeignConfiguration {
|
protected static class OkHttpFeignConfiguration {
|
||||||
|
|||||||
+9
-9
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,18 +14,18 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.cloud.openfeign.hystrix.security.app;
|
package org.springframework.cloud.openfeign;
|
||||||
|
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import feign.Feign;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Daniel Lavoie
|
* Allows application to customize the Feign builder.
|
||||||
|
*
|
||||||
|
* @author Matt King
|
||||||
*/
|
*/
|
||||||
@FeignClient("username")
|
@FunctionalInterface
|
||||||
public interface UsernameClient {
|
public interface FeignBuilderCustomizer {
|
||||||
|
|
||||||
@RequestMapping("/username")
|
void customize(Feign.Builder builder);
|
||||||
String getUsername();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+8
-7
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,6 +18,7 @@ package org.springframework.cloud.openfeign;
|
|||||||
|
|
||||||
import java.lang.annotation.Documented;
|
import java.lang.annotation.Documented;
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Inherited;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
@@ -26,9 +27,9 @@ import org.springframework.core.annotation.AliasFor;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Annotation for interfaces declaring that a REST client with that interface should be
|
* Annotation for interfaces declaring that a REST client with that interface should be
|
||||||
* created (e.g. for autowiring into another component). If ribbon is available it will be
|
* created (e.g. for autowiring into another component). If SC LoadBalancer is available
|
||||||
* used to load balance the backend requests, and the load balancer can be configured
|
* it will be used to load balance the backend requests, and the load balancer can be
|
||||||
* using a <code>@RibbonClient</code> with the same name (i.e. value) as the feign client.
|
* configured using the same name (i.e. value) as the feign client.
|
||||||
*
|
*
|
||||||
* @author Spencer Gibb
|
* @author Spencer Gibb
|
||||||
* @author Venil Noronha
|
* @author Venil Noronha
|
||||||
@@ -36,6 +37,7 @@ import org.springframework.core.annotation.AliasFor;
|
|||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
|
@Inherited
|
||||||
public @interface FeignClient {
|
public @interface FeignClient {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -85,7 +87,7 @@ public @interface FeignClient {
|
|||||||
boolean decode404() default false;
|
boolean decode404() default false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A custom <code>@Configuration</code> for the feign client. Can contain override
|
* A custom configuration class for the feign client. Can contain override
|
||||||
* <code>@Bean</code> definition for the pieces that make up the client, for instance
|
* <code>@Bean</code> definition for the pieces that make up the client, for instance
|
||||||
* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
|
* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, {@link feign.Contract}.
|
||||||
*
|
*
|
||||||
@@ -112,8 +114,7 @@ public @interface FeignClient {
|
|||||||
Class<?> fallbackFactory() default void.class;
|
Class<?> fallbackFactory() default void.class;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return path prefix to be used by all method-level mappings. Can be used with or
|
* @return path prefix to be used by all method-level mappings.
|
||||||
* without <code>@RibbonClient</code>.
|
|
||||||
*/
|
*/
|
||||||
String path() default "";
|
String path() default "";
|
||||||
|
|
||||||
|
|||||||
+27
-17
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -25,6 +25,7 @@ import org.springframework.context.ApplicationContext;
|
|||||||
* {@link FeignClient} annotation.
|
* {@link FeignClient} annotation.
|
||||||
*
|
*
|
||||||
* @author Sven Döring
|
* @author Sven Döring
|
||||||
|
* @author Matt King
|
||||||
*/
|
*/
|
||||||
public class FeignClientBuilder {
|
public class FeignClientBuilder {
|
||||||
|
|
||||||
@@ -38,6 +39,11 @@ public class FeignClientBuilder {
|
|||||||
return new Builder<>(this.applicationContext, type, name);
|
return new Builder<>(this.applicationContext, type, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> Builder<T> forType(final Class<T> type,
|
||||||
|
final FeignClientFactoryBean clientFactoryBean, final String name) {
|
||||||
|
return new Builder<>(this.applicationContext, clientFactoryBean, type, name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder of feign targets.
|
* Builder of feign targets.
|
||||||
*
|
*
|
||||||
@@ -49,55 +55,59 @@ public class FeignClientBuilder {
|
|||||||
|
|
||||||
private Builder(final ApplicationContext applicationContext, final Class<T> type,
|
private Builder(final ApplicationContext applicationContext, final Class<T> type,
|
||||||
final String name) {
|
final String name) {
|
||||||
this.feignClientFactoryBean = new FeignClientFactoryBean();
|
this(applicationContext, new FeignClientFactoryBean(), type, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Builder(final ApplicationContext applicationContext,
|
||||||
|
final FeignClientFactoryBean clientFactoryBean, final Class<T> type,
|
||||||
|
final String name) {
|
||||||
|
this.feignClientFactoryBean = clientFactoryBean;
|
||||||
|
|
||||||
this.feignClientFactoryBean.setApplicationContext(applicationContext);
|
this.feignClientFactoryBean.setApplicationContext(applicationContext);
|
||||||
this.feignClientFactoryBean.setType(type);
|
this.feignClientFactoryBean.setType(type);
|
||||||
this.feignClientFactoryBean.setName(FeignClientsRegistrar.getName(name));
|
this.feignClientFactoryBean.setName(FeignClientsRegistrar.getName(name));
|
||||||
this.feignClientFactoryBean.setContextId(FeignClientsRegistrar.getName(name));
|
this.feignClientFactoryBean.setContextId(FeignClientsRegistrar.getName(name));
|
||||||
|
this.feignClientFactoryBean.setInheritParentContext(true);
|
||||||
// preset default values - these values resemble the default values on the
|
// preset default values - these values resemble the default values on the
|
||||||
// FeignClient annotation
|
// FeignClient annotation
|
||||||
this.url("").path("").decode404(false).fallback(void.class)
|
this.url("").path("").decode404(false);
|
||||||
.fallbackFactory(void.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder url(final String url) {
|
public Builder<T> url(final String url) {
|
||||||
this.feignClientFactoryBean.setUrl(FeignClientsRegistrar.getUrl(url));
|
this.feignClientFactoryBean.setUrl(FeignClientsRegistrar.getUrl(url));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder contextId(final String contextId) {
|
public Builder<T> contextId(final String contextId) {
|
||||||
this.feignClientFactoryBean.setContextId(contextId);
|
this.feignClientFactoryBean.setContextId(contextId);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder path(final String path) {
|
public Builder<T> path(final String path) {
|
||||||
this.feignClientFactoryBean.setPath(FeignClientsRegistrar.getPath(path));
|
this.feignClientFactoryBean.setPath(FeignClientsRegistrar.getPath(path));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder decode404(final boolean decode404) {
|
public Builder<T> decode404(final boolean decode404) {
|
||||||
this.feignClientFactoryBean.setDecode404(decode404);
|
this.feignClientFactoryBean.setDecode404(decode404);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder fallback(final Class<T> fallback) {
|
public Builder<T> inheritParentContext(final boolean inheritParentContext) {
|
||||||
|
this.feignClientFactoryBean.setInheritParentContext(inheritParentContext);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder<T> fallback(final Class<? extends T> fallback) {
|
||||||
FeignClientsRegistrar.validateFallback(fallback);
|
FeignClientsRegistrar.validateFallback(fallback);
|
||||||
this.feignClientFactoryBean.setFallback(fallback);
|
this.feignClientFactoryBean.setFallback(fallback);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder fallbackFactory(final Class<T> fallbackFactory) {
|
|
||||||
FeignClientsRegistrar.validateFallbackFactory(fallbackFactory);
|
|
||||||
this.feignClientFactoryBean.setFallbackFactory(fallbackFactory);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param <T> the target type of the Feign client to be created
|
|
||||||
* @return the created Feign client
|
* @return the created Feign client
|
||||||
*/
|
*/
|
||||||
public <T> T build() {
|
public T build() {
|
||||||
return this.feignClientFactoryBean.getTarget();
|
return this.feignClientFactoryBean.getTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+156
-72
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,11 +18,14 @@ package org.springframework.cloud.openfeign;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import feign.Client;
|
import feign.Client;
|
||||||
import feign.Contract;
|
import feign.Contract;
|
||||||
|
import feign.ExceptionPropagationPolicy;
|
||||||
import feign.Feign;
|
import feign.Feign;
|
||||||
import feign.Logger;
|
import feign.Logger;
|
||||||
|
import feign.QueryMapEncoder;
|
||||||
import feign.Request;
|
import feign.Request;
|
||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
import feign.Retryer;
|
import feign.Retryer;
|
||||||
@@ -36,9 +39,11 @@ import org.springframework.beans.BeansException;
|
|||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer;
|
||||||
|
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@@ -47,8 +52,10 @@ import org.springframework.util.StringUtils;
|
|||||||
* @author Venil Noronha
|
* @author Venil Noronha
|
||||||
* @author Eko Kurniawan Khannedy
|
* @author Eko Kurniawan Khannedy
|
||||||
* @author Gregor Zurowski
|
* @author Gregor Zurowski
|
||||||
|
* @author Matt King
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
*/
|
*/
|
||||||
class FeignClientFactoryBean
|
public class FeignClientFactoryBean
|
||||||
implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {
|
implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {
|
||||||
|
|
||||||
/***********************************
|
/***********************************
|
||||||
@@ -68,21 +75,27 @@ class FeignClientFactoryBean
|
|||||||
|
|
||||||
private boolean decode404;
|
private boolean decode404;
|
||||||
|
|
||||||
|
private boolean inheritParentContext = true;
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
private Class<?> fallback = void.class;
|
private Class<?> fallback = void.class;
|
||||||
|
|
||||||
private Class<?> fallbackFactory = void.class;
|
private Class<?> fallbackFactory = void.class;
|
||||||
|
|
||||||
|
private int readTimeoutMillis = new Request.Options().readTimeoutMillis();
|
||||||
|
|
||||||
|
private int connectTimeoutMillis = new Request.Options().connectTimeoutMillis();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() {
|
||||||
Assert.hasText(this.contextId, "Context id must be set");
|
Assert.hasText(contextId, "Context id must be set");
|
||||||
Assert.hasText(this.name, "Name must be set");
|
Assert.hasText(name, "Name must be set");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Feign.Builder feign(FeignContext context) {
|
protected Feign.Builder feign(FeignContext context) {
|
||||||
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
|
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
|
||||||
Logger logger = loggerFactory.create(this.type);
|
Logger logger = loggerFactory.create(type);
|
||||||
|
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
Feign.Builder builder = get(context, Feign.Builder.class)
|
Feign.Builder builder = get(context, Feign.Builder.class)
|
||||||
@@ -94,28 +107,44 @@ class FeignClientFactoryBean
|
|||||||
// @formatter:on
|
// @formatter:on
|
||||||
|
|
||||||
configureFeign(context, builder);
|
configureFeign(context, builder);
|
||||||
|
applyBuildCustomizers(context, builder);
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void applyBuildCustomizers(FeignContext context, Feign.Builder builder) {
|
||||||
|
Map<String, FeignBuilderCustomizer> customizerMap = context
|
||||||
|
.getInstances(contextId, FeignBuilderCustomizer.class);
|
||||||
|
|
||||||
|
if (customizerMap != null) {
|
||||||
|
customizerMap.values().stream()
|
||||||
|
.sorted(AnnotationAwareOrderComparator.INSTANCE)
|
||||||
|
.forEach(feignBuilderCustomizer -> feignBuilderCustomizer
|
||||||
|
.customize(builder));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void configureFeign(FeignContext context, Feign.Builder builder) {
|
protected void configureFeign(FeignContext context, Feign.Builder builder) {
|
||||||
FeignClientProperties properties = this.applicationContext
|
FeignClientProperties properties = applicationContext
|
||||||
.getBean(FeignClientProperties.class);
|
.getBean(FeignClientProperties.class);
|
||||||
if (properties != null) {
|
|
||||||
|
FeignClientConfigurer feignClientConfigurer = getOptional(context,
|
||||||
|
FeignClientConfigurer.class);
|
||||||
|
setInheritParentContext(feignClientConfigurer.inheritParentConfiguration());
|
||||||
|
|
||||||
|
if (properties != null && inheritParentContext) {
|
||||||
if (properties.isDefaultToProperties()) {
|
if (properties.isDefaultToProperties()) {
|
||||||
configureUsingConfiguration(context, builder);
|
configureUsingConfiguration(context, builder);
|
||||||
configureUsingProperties(
|
configureUsingProperties(
|
||||||
properties.getConfig().get(properties.getDefaultConfig()),
|
properties.getConfig().get(properties.getDefaultConfig()),
|
||||||
builder);
|
builder);
|
||||||
configureUsingProperties(properties.getConfig().get(this.contextId),
|
configureUsingProperties(properties.getConfig().get(contextId), builder);
|
||||||
builder);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
configureUsingProperties(
|
configureUsingProperties(
|
||||||
properties.getConfig().get(properties.getDefaultConfig()),
|
properties.getConfig().get(properties.getDefaultConfig()),
|
||||||
builder);
|
builder);
|
||||||
configureUsingProperties(properties.getConfig().get(this.contextId),
|
configureUsingProperties(properties.getConfig().get(contextId), builder);
|
||||||
builder);
|
|
||||||
configureUsingConfiguration(context, builder);
|
configureUsingConfiguration(context, builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,31 +155,52 @@ class FeignClientFactoryBean
|
|||||||
|
|
||||||
protected void configureUsingConfiguration(FeignContext context,
|
protected void configureUsingConfiguration(FeignContext context,
|
||||||
Feign.Builder builder) {
|
Feign.Builder builder) {
|
||||||
Logger.Level level = getOptional(context, Logger.Level.class);
|
Logger.Level level = getInheritedAwareOptional(context, Logger.Level.class);
|
||||||
if (level != null) {
|
if (level != null) {
|
||||||
builder.logLevel(level);
|
builder.logLevel(level);
|
||||||
}
|
}
|
||||||
Retryer retryer = getOptional(context, Retryer.class);
|
Retryer retryer = getInheritedAwareOptional(context, Retryer.class);
|
||||||
if (retryer != null) {
|
if (retryer != null) {
|
||||||
builder.retryer(retryer);
|
builder.retryer(retryer);
|
||||||
}
|
}
|
||||||
ErrorDecoder errorDecoder = getOptional(context, ErrorDecoder.class);
|
ErrorDecoder errorDecoder = getInheritedAwareOptional(context,
|
||||||
|
ErrorDecoder.class);
|
||||||
if (errorDecoder != null) {
|
if (errorDecoder != null) {
|
||||||
builder.errorDecoder(errorDecoder);
|
builder.errorDecoder(errorDecoder);
|
||||||
}
|
}
|
||||||
Request.Options options = getOptional(context, Request.Options.class);
|
else {
|
||||||
|
FeignErrorDecoderFactory errorDecoderFactory = getOptional(context,
|
||||||
|
FeignErrorDecoderFactory.class);
|
||||||
|
if (errorDecoderFactory != null) {
|
||||||
|
ErrorDecoder factoryErrorDecoder = errorDecoderFactory.create(type);
|
||||||
|
builder.errorDecoder(factoryErrorDecoder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Request.Options options = getInheritedAwareOptional(context,
|
||||||
|
Request.Options.class);
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
builder.options(options);
|
builder.options(options);
|
||||||
|
readTimeoutMillis = options.readTimeoutMillis();
|
||||||
|
connectTimeoutMillis = options.connectTimeoutMillis();
|
||||||
}
|
}
|
||||||
Map<String, RequestInterceptor> requestInterceptors = context
|
Map<String, RequestInterceptor> requestInterceptors = getInheritedAwareInstances(
|
||||||
.getInstances(this.contextId, RequestInterceptor.class);
|
context, RequestInterceptor.class);
|
||||||
if (requestInterceptors != null) {
|
if (requestInterceptors != null) {
|
||||||
builder.requestInterceptors(requestInterceptors.values());
|
builder.requestInterceptors(requestInterceptors.values());
|
||||||
}
|
}
|
||||||
|
QueryMapEncoder queryMapEncoder = getInheritedAwareOptional(context,
|
||||||
if (this.decode404) {
|
QueryMapEncoder.class);
|
||||||
|
if (queryMapEncoder != null) {
|
||||||
|
builder.queryMapEncoder(queryMapEncoder);
|
||||||
|
}
|
||||||
|
if (decode404) {
|
||||||
builder.decode404();
|
builder.decode404();
|
||||||
}
|
}
|
||||||
|
ExceptionPropagationPolicy exceptionPropagationPolicy = getInheritedAwareOptional(
|
||||||
|
context, ExceptionPropagationPolicy.class);
|
||||||
|
if (exceptionPropagationPolicy != null) {
|
||||||
|
builder.exceptionPropagationPolicy(exceptionPropagationPolicy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void configureUsingProperties(
|
protected void configureUsingProperties(
|
||||||
@@ -164,10 +214,13 @@ class FeignClientFactoryBean
|
|||||||
builder.logLevel(config.getLoggerLevel());
|
builder.logLevel(config.getLoggerLevel());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.getConnectTimeout() != null && config.getReadTimeout() != null) {
|
connectTimeoutMillis = config.getConnectTimeout() != null
|
||||||
builder.options(new Request.Options(config.getConnectTimeout(),
|
? config.getConnectTimeout() : connectTimeoutMillis;
|
||||||
config.getReadTimeout()));
|
readTimeoutMillis = config.getReadTimeout() != null ? config.getReadTimeout()
|
||||||
}
|
: readTimeoutMillis;
|
||||||
|
|
||||||
|
builder.options(new Request.Options(connectTimeoutMillis, TimeUnit.MILLISECONDS,
|
||||||
|
readTimeoutMillis, TimeUnit.MILLISECONDS, true));
|
||||||
|
|
||||||
if (config.getRetryer() != null) {
|
if (config.getRetryer() != null) {
|
||||||
Retryer retryer = getOrInstantiate(config.getRetryer());
|
Retryer retryer = getOrInstantiate(config.getRetryer());
|
||||||
@@ -205,11 +258,15 @@ class FeignClientFactoryBean
|
|||||||
if (Objects.nonNull(config.getContract())) {
|
if (Objects.nonNull(config.getContract())) {
|
||||||
builder.contract(getOrInstantiate(config.getContract()));
|
builder.contract(getOrInstantiate(config.getContract()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Objects.nonNull(config.getExceptionPropagationPolicy())) {
|
||||||
|
builder.exceptionPropagationPolicy(config.getExceptionPropagationPolicy());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> T getOrInstantiate(Class<T> tClass) {
|
private <T> T getOrInstantiate(Class<T> tClass) {
|
||||||
try {
|
try {
|
||||||
return this.applicationContext.getBean(tClass);
|
return applicationContext.getBean(tClass);
|
||||||
}
|
}
|
||||||
catch (NoSuchBeanDefinitionException e) {
|
catch (NoSuchBeanDefinitionException e) {
|
||||||
return BeanUtils.instantiateClass(tClass);
|
return BeanUtils.instantiateClass(tClass);
|
||||||
@@ -217,16 +274,35 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected <T> T get(FeignContext context, Class<T> type) {
|
protected <T> T get(FeignContext context, Class<T> type) {
|
||||||
T instance = context.getInstance(this.contextId, type);
|
T instance = context.getInstance(contextId, type);
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"No bean found of type " + type + " for " + this.contextId);
|
"No bean found of type " + type + " for " + contextId);
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> T getOptional(FeignContext context, Class<T> type) {
|
protected <T> T getOptional(FeignContext context, Class<T> type) {
|
||||||
return context.getInstance(this.contextId, type);
|
return context.getInstance(contextId, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <T> T getInheritedAwareOptional(FeignContext context, Class<T> type) {
|
||||||
|
if (inheritParentContext) {
|
||||||
|
return getOptional(context, type);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return context.getInstanceWithoutAncestors(contextId, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <T> Map<String, T> getInheritedAwareInstances(FeignContext context,
|
||||||
|
Class<T> type) {
|
||||||
|
if (inheritParentContext) {
|
||||||
|
return context.getInstances(contextId, type);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return context.getInstancesWithoutAncestors(contextId, type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> T loadBalance(Feign.Builder builder, FeignContext context,
|
protected <T> T loadBalance(Feign.Builder builder, FeignContext context,
|
||||||
@@ -239,11 +315,11 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netflix-ribbon?");
|
"No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getObject() throws Exception {
|
public Object getObject() {
|
||||||
return getTarget();
|
return getTarget();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,36 +329,36 @@ class FeignClientFactoryBean
|
|||||||
* information
|
* information
|
||||||
*/
|
*/
|
||||||
<T> T getTarget() {
|
<T> T getTarget() {
|
||||||
FeignContext context = this.applicationContext.getBean(FeignContext.class);
|
FeignContext context = applicationContext.getBean(FeignContext.class);
|
||||||
Feign.Builder builder = feign(context);
|
Feign.Builder builder = feign(context);
|
||||||
|
|
||||||
if (!StringUtils.hasText(this.url)) {
|
if (!StringUtils.hasText(url)) {
|
||||||
if (!this.name.startsWith("http")) {
|
if (!name.startsWith("http")) {
|
||||||
this.url = "http://" + this.name;
|
url = "http://" + name;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.url = this.name;
|
url = name;
|
||||||
}
|
}
|
||||||
this.url += cleanPath();
|
url += cleanPath();
|
||||||
return (T) loadBalance(builder, context,
|
return (T) loadBalance(builder, context,
|
||||||
new HardCodedTarget<>(this.type, this.name, this.url));
|
new HardCodedTarget<>(type, name, url));
|
||||||
}
|
}
|
||||||
if (StringUtils.hasText(this.url) && !this.url.startsWith("http")) {
|
if (StringUtils.hasText(url) && !url.startsWith("http")) {
|
||||||
this.url = "http://" + this.url;
|
url = "http://" + url;
|
||||||
}
|
}
|
||||||
String url = this.url + cleanPath();
|
String url = this.url + cleanPath();
|
||||||
Client client = getOptional(context, Client.class);
|
Client client = getOptional(context, Client.class);
|
||||||
if (client != null) {
|
if (client != null) {
|
||||||
if (client instanceof LoadBalancerFeignClient) {
|
if (client instanceof FeignBlockingLoadBalancerClient) {
|
||||||
// not load balancing because we have a url,
|
// not load balancing because we have a url,
|
||||||
// but ribbon is on the classpath, so unwrap
|
// but Spring Cloud LoadBalancer is on the classpath, so unwrap
|
||||||
client = ((LoadBalancerFeignClient) client).getDelegate();
|
client = ((FeignBlockingLoadBalancerClient) client).getDelegate();
|
||||||
}
|
}
|
||||||
builder.client(client);
|
builder.client(client);
|
||||||
}
|
}
|
||||||
Targeter targeter = get(context, Targeter.class);
|
Targeter targeter = get(context, Targeter.class);
|
||||||
return (T) targeter.target(this, builder, context,
|
return (T) targeter.target(this, builder, context,
|
||||||
new HardCodedTarget<>(this.type, this.name, url));
|
new HardCodedTarget<>(type, name, url));
|
||||||
}
|
}
|
||||||
|
|
||||||
private String cleanPath() {
|
private String cleanPath() {
|
||||||
@@ -300,7 +376,7 @@ class FeignClientFactoryBean
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> getObjectType() {
|
public Class<?> getObjectType() {
|
||||||
return this.type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -309,7 +385,7 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Class<?> getType() {
|
public Class<?> getType() {
|
||||||
return this.type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(Class<?> type) {
|
public void setType(Class<?> type) {
|
||||||
@@ -317,7 +393,7 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
@@ -325,7 +401,7 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getContextId() {
|
public String getContextId() {
|
||||||
return this.contextId;
|
return contextId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContextId(String contextId) {
|
public void setContextId(String contextId) {
|
||||||
@@ -333,7 +409,7 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
return this.url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUrl(String url) {
|
public void setUrl(String url) {
|
||||||
@@ -341,7 +417,7 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getPath() {
|
public String getPath() {
|
||||||
return this.path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPath(String path) {
|
public void setPath(String path) {
|
||||||
@@ -349,15 +425,23 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDecode404() {
|
public boolean isDecode404() {
|
||||||
return this.decode404;
|
return decode404;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDecode404(boolean decode404) {
|
public void setDecode404(boolean decode404) {
|
||||||
this.decode404 = decode404;
|
this.decode404 = decode404;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isInheritParentContext() {
|
||||||
|
return inheritParentContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInheritParentContext(boolean inheritParentContext) {
|
||||||
|
this.inheritParentContext = inheritParentContext;
|
||||||
|
}
|
||||||
|
|
||||||
public ApplicationContext getApplicationContext() {
|
public ApplicationContext getApplicationContext() {
|
||||||
return this.applicationContext;
|
return applicationContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -366,7 +450,7 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Class<?> getFallback() {
|
public Class<?> getFallback() {
|
||||||
return this.fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFallback(Class<?> fallback) {
|
public void setFallback(Class<?> fallback) {
|
||||||
@@ -374,7 +458,7 @@ class FeignClientFactoryBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Class<?> getFallbackFactory() {
|
public Class<?> getFallbackFactory() {
|
||||||
return this.fallbackFactory;
|
return fallbackFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFallbackFactory(Class<?> fallbackFactory) {
|
public void setFallbackFactory(Class<?> fallbackFactory) {
|
||||||
@@ -390,32 +474,32 @@ class FeignClientFactoryBean
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
FeignClientFactoryBean that = (FeignClientFactoryBean) o;
|
FeignClientFactoryBean that = (FeignClientFactoryBean) o;
|
||||||
return Objects.equals(this.applicationContext, that.applicationContext)
|
return Objects.equals(applicationContext, that.applicationContext)
|
||||||
&& this.decode404 == that.decode404
|
&& decode404 == that.decode404
|
||||||
&& Objects.equals(this.fallback, that.fallback)
|
&& inheritParentContext == that.inheritParentContext
|
||||||
&& Objects.equals(this.fallbackFactory, that.fallbackFactory)
|
&& Objects.equals(fallback, that.fallback)
|
||||||
&& Objects.equals(this.name, that.name)
|
&& Objects.equals(fallbackFactory, that.fallbackFactory)
|
||||||
&& Objects.equals(this.path, that.path)
|
&& Objects.equals(name, that.name) && Objects.equals(path, that.path)
|
||||||
&& Objects.equals(this.type, that.type)
|
&& Objects.equals(type, that.type) && Objects.equals(url, that.url);
|
||||||
&& Objects.equals(this.url, that.url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(this.applicationContext, this.decode404, this.fallback,
|
return Objects.hash(applicationContext, decode404, inheritParentContext, fallback,
|
||||||
this.fallbackFactory, this.name, this.path, this.type, this.url);
|
fallbackFactory, name, path, type, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new StringBuilder("FeignClientFactoryBean{").append("type=")
|
return new StringBuilder("FeignClientFactoryBean{").append("type=").append(type)
|
||||||
.append(this.type).append(", ").append("name='").append(this.name)
|
.append(", ").append("name='").append(name).append("', ").append("url='")
|
||||||
.append("', ").append("url='").append(this.url).append("', ")
|
.append(url).append("', ").append("path='").append(path).append("', ")
|
||||||
.append("path='").append(this.path).append("', ").append("decode404=")
|
.append("decode404=").append(decode404).append(", ")
|
||||||
.append(this.decode404).append(", ").append("applicationContext=")
|
.append("inheritParentContext=").append(inheritParentContext).append(", ")
|
||||||
.append(this.applicationContext).append(", ").append("fallback=")
|
.append("applicationContext=").append(applicationContext).append(", ")
|
||||||
.append(this.fallback).append(", ").append("fallbackFactory=")
|
.append("fallback=").append(fallback).append(", ")
|
||||||
.append(this.fallbackFactory).append("}").toString();
|
.append("fallbackFactory=").append(fallbackFactory).append("}")
|
||||||
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -22,6 +22,7 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import feign.Contract;
|
import feign.Contract;
|
||||||
|
import feign.ExceptionPropagationPolicy;
|
||||||
import feign.Logger;
|
import feign.Logger;
|
||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
import feign.Retryer;
|
import feign.Retryer;
|
||||||
@@ -111,6 +112,8 @@ public class FeignClientProperties {
|
|||||||
|
|
||||||
private Class<Contract> contract;
|
private Class<Contract> contract;
|
||||||
|
|
||||||
|
private ExceptionPropagationPolicy exceptionPropagationPolicy;
|
||||||
|
|
||||||
public Logger.Level getLoggerLevel() {
|
public Logger.Level getLoggerLevel() {
|
||||||
return this.loggerLevel;
|
return this.loggerLevel;
|
||||||
}
|
}
|
||||||
@@ -192,6 +195,15 @@ public class FeignClientProperties {
|
|||||||
this.contract = contract;
|
this.contract = contract;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ExceptionPropagationPolicy getExceptionPropagationPolicy() {
|
||||||
|
return exceptionPropagationPolicy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExceptionPropagationPolicy(
|
||||||
|
ExceptionPropagationPolicy exceptionPropagationPolicy) {
|
||||||
|
this.exceptionPropagationPolicy = exceptionPropagationPolicy;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
@@ -210,14 +222,17 @@ public class FeignClientProperties {
|
|||||||
&& Objects.equals(this.decode404, that.decode404)
|
&& Objects.equals(this.decode404, that.decode404)
|
||||||
&& Objects.equals(this.encoder, that.encoder)
|
&& Objects.equals(this.encoder, that.encoder)
|
||||||
&& Objects.equals(this.decoder, that.decoder)
|
&& Objects.equals(this.decoder, that.decoder)
|
||||||
&& Objects.equals(this.contract, that.contract);
|
&& Objects.equals(this.contract, that.contract)
|
||||||
|
&& Objects.equals(this.exceptionPropagationPolicy,
|
||||||
|
that.exceptionPropagationPolicy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(this.loggerLevel, this.connectTimeout, this.readTimeout,
|
return Objects.hash(this.loggerLevel, this.connectTimeout, this.readTimeout,
|
||||||
this.retryer, this.errorDecoder, this.requestInterceptors,
|
this.retryer, this.errorDecoder, this.requestInterceptors,
|
||||||
this.decode404, this.encoder, this.decoder, this.contract);
|
this.decode404, this.encoder, this.decoder, this.contract,
|
||||||
|
this.exceptionPropagationPolicy);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+63
-18
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -20,26 +20,30 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.Module;
|
import com.fasterxml.jackson.databind.Module;
|
||||||
import com.netflix.hystrix.HystrixCommand;
|
|
||||||
import feign.Contract;
|
import feign.Contract;
|
||||||
import feign.Feign;
|
import feign.Feign;
|
||||||
import feign.Logger;
|
import feign.Logger;
|
||||||
import feign.Retryer;
|
import feign.Retryer;
|
||||||
import feign.codec.Decoder;
|
import feign.codec.Decoder;
|
||||||
import feign.codec.Encoder;
|
import feign.codec.Encoder;
|
||||||
import feign.hystrix.HystrixFeign;
|
import feign.form.MultipartFormContentProcessor;
|
||||||
|
import feign.form.spring.SpringFormEncoder;
|
||||||
import feign.optionals.OptionalDecoder;
|
import feign.optionals.OptionalDecoder;
|
||||||
|
|
||||||
import org.springframework.beans.factory.ObjectFactory;
|
import org.springframework.beans.factory.ObjectFactory;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
|
||||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||||
|
import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer;
|
||||||
|
import org.springframework.cloud.openfeign.support.AbstractFormWriter;
|
||||||
import org.springframework.cloud.openfeign.support.PageJacksonModule;
|
import org.springframework.cloud.openfeign.support.PageJacksonModule;
|
||||||
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
|
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
|
||||||
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
|
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
|
||||||
|
import org.springframework.cloud.openfeign.support.SortJacksonModule;
|
||||||
import org.springframework.cloud.openfeign.support.SpringDecoder;
|
import org.springframework.cloud.openfeign.support.SpringDecoder;
|
||||||
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
||||||
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
||||||
@@ -50,11 +54,14 @@ import org.springframework.core.convert.ConversionService;
|
|||||||
import org.springframework.format.support.DefaultFormattingConversionService;
|
import org.springframework.format.support.DefaultFormattingConversionService;
|
||||||
import org.springframework.format.support.FormattingConversionService;
|
import org.springframework.format.support.FormattingConversionService;
|
||||||
|
|
||||||
|
import static feign.form.ContentType.MULTIPART;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Venil Noronha
|
* @author Venil Noronha
|
||||||
|
* @author Darren Foong
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
public class FeignClientsConfiguration {
|
public class FeignClientsConfiguration {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -69,6 +76,9 @@ public class FeignClientsConfiguration {
|
|||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private SpringDataWebProperties springDataWebProperties;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public Decoder feignDecoder() {
|
public Decoder feignDecoder() {
|
||||||
@@ -79,15 +89,27 @@ public class FeignClientsConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@ConditionalOnMissingClass("org.springframework.data.domain.Pageable")
|
@ConditionalOnMissingClass("org.springframework.data.domain.Pageable")
|
||||||
public Encoder feignEncoder() {
|
public Encoder feignEncoder(ObjectProvider<AbstractFormWriter> formWriterProvider) {
|
||||||
return new SpringEncoder(this.messageConverters);
|
return springEncoder(formWriterProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnClass(name = "org.springframework.data.domain.Pageable")
|
@ConditionalOnClass(name = "org.springframework.data.domain.Pageable")
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public Encoder feignEncoderPageable() {
|
public Encoder feignEncoderPageable(
|
||||||
return new PageableSpringEncoder(new SpringEncoder(this.messageConverters));
|
ObjectProvider<AbstractFormWriter> formWriterProvider) {
|
||||||
|
PageableSpringEncoder encoder = new PageableSpringEncoder(
|
||||||
|
springEncoder(formWriterProvider));
|
||||||
|
|
||||||
|
if (springDataWebProperties != null) {
|
||||||
|
encoder.setPageParameter(
|
||||||
|
springDataWebProperties.getPageable().getPageParameter());
|
||||||
|
encoder.setSizeParameter(
|
||||||
|
springDataWebProperties.getPageable().getSizeParameter());
|
||||||
|
encoder.setSortParameter(
|
||||||
|
springDataWebProperties.getSort().getSortParameter());
|
||||||
|
}
|
||||||
|
return encoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -130,16 +152,39 @@ public class FeignClientsConfiguration {
|
|||||||
return new PageJacksonModule();
|
return new PageJacksonModule();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Bean
|
||||||
@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
|
@ConditionalOnClass(name = "org.springframework.data.domain.Page")
|
||||||
protected static class HystrixFeignConfiguration {
|
public Module sortModule() {
|
||||||
|
return new SortJacksonModule();
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Scope("prototype")
|
@ConditionalOnMissingBean(FeignClientConfigurer.class)
|
||||||
@ConditionalOnMissingBean
|
public FeignClientConfigurer feignClientConfigurer() {
|
||||||
@ConditionalOnProperty(name = "feign.hystrix.enabled")
|
return new FeignClientConfigurer() {
|
||||||
public Feign.Builder feignHystrixBuilder() {
|
};
|
||||||
return HystrixFeign.builder();
|
}
|
||||||
|
|
||||||
|
private Encoder springEncoder(ObjectProvider<AbstractFormWriter> formWriterProvider) {
|
||||||
|
AbstractFormWriter formWriter = formWriterProvider.getIfAvailable();
|
||||||
|
|
||||||
|
if (formWriter != null) {
|
||||||
|
return new SpringEncoder(new SpringPojoFormEncoder(formWriter),
|
||||||
|
this.messageConverters);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return new SpringEncoder(new SpringFormEncoder(), this.messageConverters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SpringPojoFormEncoder extends SpringFormEncoder {
|
||||||
|
|
||||||
|
SpringPojoFormEncoder(AbstractFormWriter formWriter) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(
|
||||||
|
MULTIPART);
|
||||||
|
processor.addFirstWriter(formWriter);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -27,6 +27,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||||
@@ -242,9 +243,10 @@ class FeignClientsRegistrar
|
|||||||
|
|
||||||
String alias = contextId + "FeignClient";
|
String alias = contextId + "FeignClient";
|
||||||
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
|
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
|
||||||
|
beanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, className);
|
||||||
|
|
||||||
boolean primary = (Boolean) attributes.get("primary"); // has a default, won't be
|
// has a default, won't be null
|
||||||
// null
|
boolean primary = (Boolean) attributes.get("primary");
|
||||||
|
|
||||||
beanDefinition.setPrimary(primary);
|
beanDefinition.setPrimary(primary);
|
||||||
|
|
||||||
|
|||||||
+22
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,7 +16,12 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.openfeign;
|
package org.springframework.cloud.openfeign;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||||
import org.springframework.cloud.context.named.NamedContextFactory;
|
import org.springframework.cloud.context.named.NamedContextFactory;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A factory that creates instances of feign classes. It creates a Spring
|
* A factory that creates instances of feign classes. It creates a Spring
|
||||||
@@ -24,6 +29,7 @@ import org.springframework.cloud.context.named.NamedContextFactory;
|
|||||||
*
|
*
|
||||||
* @author Spencer Gibb
|
* @author Spencer Gibb
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
|
* @author Matt King
|
||||||
*/
|
*/
|
||||||
public class FeignContext extends NamedContextFactory<FeignClientSpecification> {
|
public class FeignContext extends NamedContextFactory<FeignClientSpecification> {
|
||||||
|
|
||||||
@@ -31,4 +37,19 @@ public class FeignContext extends NamedContextFactory<FeignClientSpecification>
|
|||||||
super(FeignClientsConfiguration.class, "feign", "feign.client.name");
|
super(FeignClientsConfiguration.class, "feign", "feign.client.name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public <T> T getInstanceWithoutAncestors(String name, Class<T> type) {
|
||||||
|
try {
|
||||||
|
return BeanFactoryUtils.beanOfType(getContext(name), type);
|
||||||
|
}
|
||||||
|
catch (BeansException ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public <T> Map<String, T> getInstancesWithoutAncestors(String name, Class<T> type) {
|
||||||
|
return getContext(name).getBeansOfType(type);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign;
|
||||||
|
|
||||||
|
import feign.codec.ErrorDecoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows an application to use a custom Feign {@link feign.codec.ErrorDecoder}.
|
||||||
|
*
|
||||||
|
* @author Michael Cramer
|
||||||
|
*/
|
||||||
|
public interface FeignErrorDecoderFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory method to provide a {@link feign.codec.ErrorDecoder} for a given
|
||||||
|
* {@link Class}.
|
||||||
|
* @param type the {@link Class} for which a {@link feign.codec.ErrorDecoder} instance
|
||||||
|
* is to be created
|
||||||
|
* @return a {@link feign.codec.ErrorDecoder} instance
|
||||||
|
*/
|
||||||
|
ErrorDecoder create(Class<?> type);
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2016-2019 the original author or authors.
|
* Copyright 2016-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
-99
@@ -1,99 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign;
|
|
||||||
|
|
||||||
import feign.Feign;
|
|
||||||
import feign.Target;
|
|
||||||
import feign.hystrix.FallbackFactory;
|
|
||||||
import feign.hystrix.HystrixFeign;
|
|
||||||
import feign.hystrix.SetterFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Spencer Gibb
|
|
||||||
* @author Erik Kringen
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
class HystrixTargeter implements Targeter {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T target(FeignClientFactoryBean factory, Feign.Builder feign,
|
|
||||||
FeignContext context, Target.HardCodedTarget<T> target) {
|
|
||||||
if (!(feign instanceof feign.hystrix.HystrixFeign.Builder)) {
|
|
||||||
return feign.target(target);
|
|
||||||
}
|
|
||||||
feign.hystrix.HystrixFeign.Builder builder = (feign.hystrix.HystrixFeign.Builder) feign;
|
|
||||||
SetterFactory setterFactory = getOptional(factory.getName(), context,
|
|
||||||
SetterFactory.class);
|
|
||||||
if (setterFactory != null) {
|
|
||||||
builder.setterFactory(setterFactory);
|
|
||||||
}
|
|
||||||
Class<?> fallback = factory.getFallback();
|
|
||||||
if (fallback != void.class) {
|
|
||||||
return targetWithFallback(factory.getName(), context, target, builder,
|
|
||||||
fallback);
|
|
||||||
}
|
|
||||||
Class<?> fallbackFactory = factory.getFallbackFactory();
|
|
||||||
if (fallbackFactory != void.class) {
|
|
||||||
return targetWithFallbackFactory(factory.getName(), context, target, builder,
|
|
||||||
fallbackFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
return feign.target(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> T targetWithFallbackFactory(String feignClientName, FeignContext context,
|
|
||||||
Target.HardCodedTarget<T> target, HystrixFeign.Builder builder,
|
|
||||||
Class<?> fallbackFactoryClass) {
|
|
||||||
FallbackFactory<? extends T> fallbackFactory = (FallbackFactory<? extends T>) getFromContext(
|
|
||||||
"fallbackFactory", feignClientName, context, fallbackFactoryClass,
|
|
||||||
FallbackFactory.class);
|
|
||||||
return builder.target(target, fallbackFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> T targetWithFallback(String feignClientName, FeignContext context,
|
|
||||||
Target.HardCodedTarget<T> target, HystrixFeign.Builder builder,
|
|
||||||
Class<?> fallback) {
|
|
||||||
T fallbackInstance = getFromContext("fallback", feignClientName, context,
|
|
||||||
fallback, target.type());
|
|
||||||
return builder.target(target, fallbackInstance);
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> T getFromContext(String fallbackMechanism, String feignClientName,
|
|
||||||
FeignContext context, Class<?> beanType, Class<T> targetType) {
|
|
||||||
Object fallbackInstance = context.getInstance(feignClientName, beanType);
|
|
||||||
if (fallbackInstance == null) {
|
|
||||||
throw new IllegalStateException(String.format(
|
|
||||||
"No " + fallbackMechanism
|
|
||||||
+ " instance of type %s found for feign client %s",
|
|
||||||
beanType, feignClientName));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!targetType.isAssignableFrom(beanType)) {
|
|
||||||
throw new IllegalStateException(String.format("Incompatible "
|
|
||||||
+ fallbackMechanism
|
|
||||||
+ " instance. Fallback/fallbackFactory of type %s is not assignable to %s for feign client %s",
|
|
||||||
beanType, targetType, feignClientName));
|
|
||||||
}
|
|
||||||
return (T) fallbackInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> T getOptional(String feignClientName, FeignContext context,
|
|
||||||
Class<T> beanType) {
|
|
||||||
return context.getInstance(feignClientName, beanType);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -22,7 +22,7 @@ import feign.Target;
|
|||||||
/**
|
/**
|
||||||
* @author Spencer Gibb
|
* @author Spencer Gibb
|
||||||
*/
|
*/
|
||||||
interface Targeter {
|
public interface Targeter {
|
||||||
|
|
||||||
<T> T target(FeignClientFactoryBean factory, Feign.Builder feign,
|
<T> T target(FeignClientFactoryBean factory, Feign.Builder feign,
|
||||||
FeignContext context, Target.HardCodedTarget<T> target);
|
FeignContext context, Target.HardCodedTarget<T> target);
|
||||||
|
|||||||
+83
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import feign.MethodMetadata;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
|
||||||
|
import org.springframework.web.bind.annotation.MatrixVariable;
|
||||||
|
|
||||||
|
import static feign.Util.checkState;
|
||||||
|
import static feign.Util.emptyToNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link MatrixVariable} annotation processor.
|
||||||
|
*
|
||||||
|
* Can expand maps or single objects. Values are assigned from the objects
|
||||||
|
* {@code toString()} method.
|
||||||
|
*
|
||||||
|
* @author Matt King
|
||||||
|
* @see AnnotatedParameterProcessor
|
||||||
|
*/
|
||||||
|
public class MatrixVariableParameterProcessor implements AnnotatedParameterProcessor {
|
||||||
|
|
||||||
|
private static final Class<MatrixVariable> ANNOTATION = MatrixVariable.class;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends Annotation> getAnnotationType() {
|
||||||
|
return ANNOTATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean processArgument(AnnotatedParameterContext context,
|
||||||
|
Annotation annotation, Method method) {
|
||||||
|
int parameterIndex = context.getParameterIndex();
|
||||||
|
Class<?> parameterType = method.getParameterTypes()[parameterIndex];
|
||||||
|
MethodMetadata data = context.getMethodMetadata();
|
||||||
|
String name = ANNOTATION.cast(annotation).value();
|
||||||
|
|
||||||
|
checkState(emptyToNull(name) != null,
|
||||||
|
"MatrixVariable annotation was empty on param %s.",
|
||||||
|
context.getParameterIndex());
|
||||||
|
|
||||||
|
context.setParameterName(name);
|
||||||
|
|
||||||
|
if (Map.class.isAssignableFrom(parameterType)) {
|
||||||
|
data.indexToExpander().put(parameterIndex, this::expandMap);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
data.indexToExpander().put(parameterIndex,
|
||||||
|
object -> ";" + name + "=" + object.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String expandMap(Object object) {
|
||||||
|
Map<String, Object> paramMap = (Map) object;
|
||||||
|
|
||||||
|
return paramMap.keySet().stream()
|
||||||
|
.map(key -> ";" + key + "=" + paramMap.get(key).toString())
|
||||||
|
.collect(Collectors.joining());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import feign.MethodMetadata;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
|
||||||
|
import org.springframework.web.bind.annotation.RequestPart;
|
||||||
|
|
||||||
|
import static feign.Util.checkState;
|
||||||
|
import static feign.Util.emptyToNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link RequestPart} parameter processor.
|
||||||
|
*
|
||||||
|
* @author Aaron Whiteside
|
||||||
|
* @see AnnotatedParameterProcessor
|
||||||
|
*/
|
||||||
|
public class RequestPartParameterProcessor implements AnnotatedParameterProcessor {
|
||||||
|
|
||||||
|
private static final Class<RequestPart> ANNOTATION = RequestPart.class;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends Annotation> getAnnotationType() {
|
||||||
|
return ANNOTATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean processArgument(AnnotatedParameterContext context,
|
||||||
|
Annotation annotation, Method method) {
|
||||||
|
int parameterIndex = context.getParameterIndex();
|
||||||
|
MethodMetadata data = context.getMethodMetadata();
|
||||||
|
|
||||||
|
String name = ANNOTATION.cast(annotation).value();
|
||||||
|
checkState(emptyToNull(name) != null,
|
||||||
|
"RequestPart.value() was empty on parameter %s", parameterIndex);
|
||||||
|
context.setParameterName(name);
|
||||||
|
|
||||||
|
data.formParams().add(name);
|
||||||
|
Collection<String> names = context.setTemplateParameter(name,
|
||||||
|
data.indexToName().get(parameterIndex));
|
||||||
|
data.indexToName().put(parameterIndex, names);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2019 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.clientconfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Additional Feign Client configuration that are not included in
|
||||||
|
* {@link org.springframework.cloud.openfeign.FeignClient}.
|
||||||
|
*
|
||||||
|
* @author Matt King
|
||||||
|
*/
|
||||||
|
public interface FeignClientConfigurer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return whether to mark the feign proxy as a primary bean. Defaults to true.
|
||||||
|
*/
|
||||||
|
default boolean primary() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FALSE will only apply configurations from classes listed in
|
||||||
|
* <code>configuration()</code>. Will still use parent instance of
|
||||||
|
* {@link feign.codec.Decoder}, {@link feign.codec.Encoder}, and
|
||||||
|
* {@link feign.Contract} if none are provided.
|
||||||
|
* @return weather to inherit parent context for client configuration.
|
||||||
|
*/
|
||||||
|
default boolean inheritParentConfiguration() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+123
@@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.clientconfig;
|
||||||
|
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
|
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.config.RegistryBuilder;
|
||||||
|
import org.apache.http.conn.HttpClientConnectionManager;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.cloud.commons.httpclient.ApacheHttpClientConnectionManagerFactory;
|
||||||
|
import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory;
|
||||||
|
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default configuration for {@link CloseableHttpClient}.
|
||||||
|
*
|
||||||
|
* @author Ryan Baxter
|
||||||
|
* @author Marcin Grzejszczak
|
||||||
|
* @author Spencer Gibb
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnMissingBean(CloseableHttpClient.class)
|
||||||
|
public class HttpClientFeignConfiguration {
|
||||||
|
|
||||||
|
private final Timer connectionManagerTimer = new Timer(
|
||||||
|
"FeignApacheHttpClientConfiguration.connectionManagerTimer", true);
|
||||||
|
|
||||||
|
private CloseableHttpClient httpClient;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private RegistryBuilder registryBuilder;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean(HttpClientConnectionManager.class)
|
||||||
|
public HttpClientConnectionManager connectionManager(
|
||||||
|
ApacheHttpClientConnectionManagerFactory connectionManagerFactory,
|
||||||
|
FeignHttpClientProperties httpClientProperties) {
|
||||||
|
final HttpClientConnectionManager connectionManager = connectionManagerFactory
|
||||||
|
.newConnectionManager(httpClientProperties.isDisableSslValidation(),
|
||||||
|
httpClientProperties.getMaxConnections(),
|
||||||
|
httpClientProperties.getMaxConnectionsPerRoute(),
|
||||||
|
httpClientProperties.getTimeToLive(),
|
||||||
|
httpClientProperties.getTimeToLiveUnit(), this.registryBuilder);
|
||||||
|
this.connectionManagerTimer.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
connectionManager.closeExpiredConnections();
|
||||||
|
}
|
||||||
|
}, 30000, httpClientProperties.getConnectionTimerRepeat());
|
||||||
|
return connectionManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnProperty(value = "feign.compression.response.enabled",
|
||||||
|
havingValue = "true")
|
||||||
|
public CloseableHttpClient customHttpClient(
|
||||||
|
HttpClientConnectionManager httpClientConnectionManager,
|
||||||
|
FeignHttpClientProperties httpClientProperties) {
|
||||||
|
HttpClientBuilder builder = HttpClientBuilder.create().disableCookieManagement()
|
||||||
|
.useSystemProperties();
|
||||||
|
this.httpClient = createClient(builder, httpClientConnectionManager,
|
||||||
|
httpClientProperties);
|
||||||
|
return this.httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnProperty(value = "feign.compression.response.enabled",
|
||||||
|
havingValue = "false", matchIfMissing = true)
|
||||||
|
public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory,
|
||||||
|
HttpClientConnectionManager httpClientConnectionManager,
|
||||||
|
FeignHttpClientProperties httpClientProperties) {
|
||||||
|
this.httpClient = createClient(httpClientFactory.createBuilder(),
|
||||||
|
httpClientConnectionManager, httpClientProperties);
|
||||||
|
return this.httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
private CloseableHttpClient createClient(HttpClientBuilder builder,
|
||||||
|
HttpClientConnectionManager httpClientConnectionManager,
|
||||||
|
FeignHttpClientProperties httpClientProperties) {
|
||||||
|
RequestConfig defaultRequestConfig = RequestConfig.custom()
|
||||||
|
.setConnectTimeout(httpClientProperties.getConnectionTimeout())
|
||||||
|
.setRedirectsEnabled(httpClientProperties.isFollowRedirects()).build();
|
||||||
|
CloseableHttpClient httpClient = builder
|
||||||
|
.setDefaultRequestConfig(defaultRequestConfig)
|
||||||
|
.setConnectionManager(httpClientConnectionManager).build();
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void destroy() throws Exception {
|
||||||
|
this.connectionManagerTimer.cancel();
|
||||||
|
if (this.httpClient != null) {
|
||||||
|
this.httpClient.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+79
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.clientconfig;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
|
|
||||||
|
import okhttp3.ConnectionPool;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.cloud.commons.httpclient.OkHttpClientConnectionPoolFactory;
|
||||||
|
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
|
||||||
|
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defualt configuration for {@link OkHttpClient}.
|
||||||
|
*
|
||||||
|
* @author Ryan Baxter
|
||||||
|
* @author Marcin Grzejszczak
|
||||||
|
* @author Spencer Gibb
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnMissingBean(okhttp3.OkHttpClient.class)
|
||||||
|
public class OkHttpFeignConfiguration {
|
||||||
|
|
||||||
|
private okhttp3.OkHttpClient okHttpClient;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean(ConnectionPool.class)
|
||||||
|
public ConnectionPool httpClientConnectionPool(
|
||||||
|
FeignHttpClientProperties httpClientProperties,
|
||||||
|
OkHttpClientConnectionPoolFactory connectionPoolFactory) {
|
||||||
|
Integer maxTotalConnections = httpClientProperties.getMaxConnections();
|
||||||
|
Long timeToLive = httpClientProperties.getTimeToLive();
|
||||||
|
TimeUnit ttlUnit = httpClientProperties.getTimeToLiveUnit();
|
||||||
|
return connectionPoolFactory.create(maxTotalConnections, timeToLive, ttlUnit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public okhttp3.OkHttpClient client(OkHttpClientFactory httpClientFactory,
|
||||||
|
ConnectionPool connectionPool,
|
||||||
|
FeignHttpClientProperties httpClientProperties) {
|
||||||
|
Boolean followRedirects = httpClientProperties.isFollowRedirects();
|
||||||
|
Integer connectTimeout = httpClientProperties.getConnectionTimeout();
|
||||||
|
this.okHttpClient = httpClientFactory
|
||||||
|
.createBuilder(httpClientProperties.isDisableSslValidation())
|
||||||
|
.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
|
||||||
|
.followRedirects(followRedirects).connectionPool(connectionPool).build();
|
||||||
|
return this.okHttpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void destroy() {
|
||||||
|
if (this.okHttpClient != null) {
|
||||||
|
this.okHttpClient.dispatcher().executorService().shutdown();
|
||||||
|
this.okHttpClient.connectionPool().evictAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+4
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -35,11 +35,12 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
* @author Jakub Narloch
|
* @author Jakub Narloch
|
||||||
* @see FeignAcceptGzipEncodingInterceptor
|
* @see FeignAcceptGzipEncodingInterceptor
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
|
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
|
||||||
@ConditionalOnClass(Feign.class)
|
@ConditionalOnClass(Feign.class)
|
||||||
@ConditionalOnBean(Client.class)
|
@ConditionalOnBean(Client.class)
|
||||||
@ConditionalOnProperty(value = "feign.compression.response.enabled", matchIfMissing = false)
|
@ConditionalOnProperty(value = "feign.compression.response.enabled",
|
||||||
|
matchIfMissing = false)
|
||||||
// The OK HTTP client uses "transparent" compression.
|
// The OK HTTP client uses "transparent" compression.
|
||||||
// If the accept-encoding header is present it disable transparent compression
|
// If the accept-encoding header is present it disable transparent compression
|
||||||
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
|
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+3
-6
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,11 +16,9 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.openfeign.encoding;
|
package org.springframework.cloud.openfeign.encoding;
|
||||||
|
|
||||||
import feign.Client;
|
|
||||||
import feign.Feign;
|
import feign.Feign;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
@@ -35,14 +33,13 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
* @author Jakub Narloch
|
* @author Jakub Narloch
|
||||||
* @see FeignContentGzipEncodingInterceptor
|
* @see FeignContentGzipEncodingInterceptor
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
|
@EnableConfigurationProperties(FeignClientEncodingProperties.class)
|
||||||
@ConditionalOnClass(Feign.class)
|
@ConditionalOnClass(Feign.class)
|
||||||
@ConditionalOnBean(Client.class)
|
|
||||||
// The OK HTTP client uses "transparent" compression.
|
// The OK HTTP client uses "transparent" compression.
|
||||||
// If the content-encoding header is present it disable transparent compression
|
// If the content-encoding header is present it disable transparent compression
|
||||||
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
|
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
|
||||||
@ConditionalOnProperty(value = "feign.compression.request.enabled", matchIfMissing = false)
|
@ConditionalOnProperty("feign.compression.request.enabled")
|
||||||
@AutoConfigureAfter(FeignAutoConfiguration.class)
|
@AutoConfigureAfter(FeignAutoConfiguration.class)
|
||||||
public class FeignContentGzipEncodingAutoConfiguration {
|
public class FeignContentGzipEncodingAutoConfiguration {
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+96
@@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.hateoas;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
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.ConditionalOnWebApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.hateoas.RepresentationModel;
|
||||||
|
import org.springframework.hateoas.mediatype.MessageResolver;
|
||||||
|
import org.springframework.hateoas.mediatype.hal.CurieProvider;
|
||||||
|
import org.springframework.hateoas.mediatype.hal.DefaultCurieProvider;
|
||||||
|
import org.springframework.hateoas.mediatype.hal.HalConfiguration;
|
||||||
|
import org.springframework.hateoas.mediatype.hal.Jackson2HalModule;
|
||||||
|
import org.springframework.hateoas.server.LinkRelationProvider;
|
||||||
|
import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
|
||||||
|
|
||||||
|
import static org.springframework.hateoas.MediaTypes.HAL_JSON;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Hector Espert
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnWebApplication
|
||||||
|
@ConditionalOnClass(RepresentationModel.class)
|
||||||
|
@AutoConfigureAfter({ JacksonAutoConfiguration.class,
|
||||||
|
HttpMessageConvertersAutoConfiguration.class,
|
||||||
|
RepositoryRestMvcAutoConfiguration.class })
|
||||||
|
@AutoConfigureBefore(HypermediaAutoConfiguration.class)
|
||||||
|
public class FeignHalAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public TypeConstrainedMappingJackson2HttpMessageConverter halJacksonHttpMessageConverter(
|
||||||
|
ObjectProvider<ObjectMapper> objectMapper,
|
||||||
|
ObjectProvider<HalConfiguration> halConfiguration,
|
||||||
|
ObjectProvider<MessageResolver> messageResolver,
|
||||||
|
ObjectProvider<CurieProvider> curieProvider,
|
||||||
|
ObjectProvider<LinkRelationProvider> linkRelationProvider) {
|
||||||
|
|
||||||
|
ObjectMapper mapper = objectMapper.getIfAvailable(ObjectMapper::new).copy();
|
||||||
|
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||||
|
|
||||||
|
HalConfiguration configuration = halConfiguration
|
||||||
|
.getIfAvailable(HalConfiguration::new);
|
||||||
|
|
||||||
|
CurieProvider curieProviderInstance = curieProvider
|
||||||
|
.getIfAvailable(() -> new DefaultCurieProvider(Collections.emptyMap()));
|
||||||
|
|
||||||
|
Jackson2HalModule.HalHandlerInstantiator halHandlerInstantiator = new Jackson2HalModule.HalHandlerInstantiator(
|
||||||
|
linkRelationProvider.getIfAvailable(), curieProviderInstance,
|
||||||
|
messageResolver.getIfAvailable(), configuration);
|
||||||
|
|
||||||
|
mapper.setHandlerInstantiator(halHandlerInstantiator);
|
||||||
|
|
||||||
|
if (!Jackson2HalModule.isAlreadyRegisteredIn(mapper)) {
|
||||||
|
Jackson2HalModule halModule = new Jackson2HalModule();
|
||||||
|
mapper.registerModule(halModule);
|
||||||
|
}
|
||||||
|
|
||||||
|
TypeConstrainedMappingJackson2HttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter(
|
||||||
|
RepresentationModel.class);
|
||||||
|
converter.setSupportedMediaTypes(Arrays.asList(HAL_JSON));
|
||||||
|
converter.setObjectMapper(mapper);
|
||||||
|
return converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+13
-10
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,27 +14,30 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.cloud.openfeign.ribbon;
|
package org.springframework.cloud.openfeign.loadbalancer;
|
||||||
|
|
||||||
import feign.Client;
|
import feign.Client;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Spencer Gibb
|
* Configuration instantiating a {@link LoadBalancerClient}-based {@link Client} object
|
||||||
|
* that uses {@link Client.Default} under the hood.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @since 2.2.0
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
class DefaultFeignLoadBalancedConfiguration {
|
class DefaultFeignLoadBalancerConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
|
public Client feignClient(LoadBalancerClient loadBalancerClient) {
|
||||||
SpringClientFactory clientFactory) {
|
return new FeignBlockingLoadBalancerClient(new Client.Default(null, null),
|
||||||
return new LoadBalancerFeignClient(new Client.Default(null, null), cachingFactory,
|
loadBalancerClient);
|
||||||
clientFactory);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+86
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.loadbalancer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import feign.Client;
|
||||||
|
import feign.Request;
|
||||||
|
import feign.Response;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link Client} implementation that uses {@link LoadBalancerClient} to select a
|
||||||
|
* {@link ServiceInstance} to use while resolving the request host.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
public class FeignBlockingLoadBalancerClient implements Client {
|
||||||
|
|
||||||
|
private static final Log LOG = LogFactory
|
||||||
|
.getLog(FeignBlockingLoadBalancerClient.class);
|
||||||
|
|
||||||
|
private final Client delegate;
|
||||||
|
|
||||||
|
private final LoadBalancerClient loadBalancerClient;
|
||||||
|
|
||||||
|
public FeignBlockingLoadBalancerClient(Client delegate,
|
||||||
|
LoadBalancerClient loadBalancerClient) {
|
||||||
|
this.delegate = delegate;
|
||||||
|
this.loadBalancerClient = loadBalancerClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response execute(Request request, Request.Options options) throws IOException {
|
||||||
|
final URI originalUri = URI.create(request.url());
|
||||||
|
String serviceId = originalUri.getHost();
|
||||||
|
Assert.state(serviceId != null,
|
||||||
|
"Request URI does not contain a valid hostname: " + originalUri);
|
||||||
|
ServiceInstance instance = loadBalancerClient.choose(serviceId);
|
||||||
|
if (instance == null) {
|
||||||
|
String message = "Load balancer does not contain an instance for the service "
|
||||||
|
+ serviceId;
|
||||||
|
if (LOG.isWarnEnabled()) {
|
||||||
|
LOG.warn(message);
|
||||||
|
}
|
||||||
|
return Response.builder().request(request)
|
||||||
|
.status(HttpStatus.SERVICE_UNAVAILABLE.value())
|
||||||
|
.body(message, StandardCharsets.UTF_8).build();
|
||||||
|
}
|
||||||
|
String reconstructedUrl = loadBalancerClient.reconstructURI(instance, originalUri)
|
||||||
|
.toString();
|
||||||
|
Request newRequest = Request.create(request.httpMethod(), reconstructedUrl,
|
||||||
|
request.headers(), request.body(), request.charset(),
|
||||||
|
request.requestTemplate());
|
||||||
|
return delegate.execute(newRequest, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Visible for Sleuth instrumentation
|
||||||
|
public Client getDelegate() {
|
||||||
|
return delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.loadbalancer;
|
||||||
|
|
||||||
|
import feign.Client;
|
||||||
|
import feign.Feign;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
|
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||||
|
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An autoconfiguration that instantiates {@link LoadBalancerClient}-based implementations
|
||||||
|
* of {@link Client}.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
@ConditionalOnClass(Feign.class)
|
||||||
|
@ConditionalOnBean(LoadBalancerClient.class)
|
||||||
|
@AutoConfigureBefore(FeignAutoConfiguration.class)
|
||||||
|
@EnableConfigurationProperties(FeignHttpClientProperties.class)
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
// Order is important here, last should be the default, first should be optional
|
||||||
|
// see
|
||||||
|
// https://github.com/spring-cloud/spring-cloud-netflix/issues/2086#issuecomment-316281653
|
||||||
|
@Import({ HttpClientFeignLoadBalancerConfiguration.class,
|
||||||
|
OkHttpFeignLoadBalancerConfiguration.class,
|
||||||
|
DefaultFeignLoadBalancerConfiguration.class })
|
||||||
|
public class FeignLoadBalancerAutoConfiguration {
|
||||||
|
|
||||||
|
}
|
||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.loadbalancer;
|
||||||
|
|
||||||
|
import feign.Client;
|
||||||
|
import feign.httpclient.ApacheHttpClient;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
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.LoadBalancerClient;
|
||||||
|
import org.springframework.cloud.openfeign.clientconfig.HttpClientFeignConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration instantiating a {@link LoadBalancerClient}-based {@link Client} object
|
||||||
|
* that uses {@link ApacheHttpClient} under the hood.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnClass(ApacheHttpClient.class)
|
||||||
|
@ConditionalOnBean(LoadBalancerClient.class)
|
||||||
|
@ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
|
||||||
|
@Import(HttpClientFeignConfiguration.class)
|
||||||
|
class HttpClientFeignLoadBalancerConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public Client feignClient(LoadBalancerClient loadBalancerClient,
|
||||||
|
HttpClient httpClient) {
|
||||||
|
ApacheHttpClient delegate = new ApacheHttpClient(httpClient);
|
||||||
|
return new FeignBlockingLoadBalancerClient(delegate, loadBalancerClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+54
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.loadbalancer;
|
||||||
|
|
||||||
|
import feign.Client;
|
||||||
|
import feign.okhttp.OkHttpClient;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
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.LoadBalancerClient;
|
||||||
|
import org.springframework.cloud.openfeign.clientconfig.OkHttpFeignConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration instantiating a {@link LoadBalancerClient}-based {@link Client} object
|
||||||
|
* that uses {@link OkHttpClient} under the hood.
|
||||||
|
*
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnClass(OkHttpClient.class)
|
||||||
|
@ConditionalOnProperty("feign.okhttp.enabled")
|
||||||
|
@ConditionalOnBean(LoadBalancerClient.class)
|
||||||
|
@Import(OkHttpFeignConfiguration.class)
|
||||||
|
class OkHttpFeignLoadBalancerConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public Client feignClient(okhttp3.OkHttpClient okHttpClient,
|
||||||
|
LoadBalancerClient loadBalancerClient) {
|
||||||
|
OkHttpClient delegate = new OkHttpClient(okHttpClient);
|
||||||
|
return new FeignBlockingLoadBalancerClient(delegate, loadBalancerClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
-72
@@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
|
||||||
import com.netflix.loadbalancer.ILoadBalancer;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
|
||||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Factory for SpringLoadBalancer instances that caches the entries created.
|
|
||||||
*
|
|
||||||
* @author Spencer Gibb
|
|
||||||
* @author Dave Syer
|
|
||||||
* @author Ryan Baxter
|
|
||||||
* @author Gang Li
|
|
||||||
*/
|
|
||||||
public class CachingSpringLoadBalancerFactory {
|
|
||||||
|
|
||||||
protected final SpringClientFactory factory;
|
|
||||||
|
|
||||||
protected LoadBalancedRetryFactory loadBalancedRetryFactory = null;
|
|
||||||
|
|
||||||
private volatile Map<String, FeignLoadBalancer> cache = new ConcurrentReferenceHashMap<>();
|
|
||||||
|
|
||||||
public CachingSpringLoadBalancerFactory(SpringClientFactory factory) {
|
|
||||||
this.factory = factory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CachingSpringLoadBalancerFactory(SpringClientFactory factory,
|
|
||||||
LoadBalancedRetryFactory loadBalancedRetryPolicyFactory) {
|
|
||||||
this.factory = factory;
|
|
||||||
this.loadBalancedRetryFactory = loadBalancedRetryPolicyFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FeignLoadBalancer create(String clientName) {
|
|
||||||
FeignLoadBalancer client = this.cache.get(clientName);
|
|
||||||
if (client != null) {
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
IClientConfig config = this.factory.getClientConfig(clientName);
|
|
||||||
ILoadBalancer lb = this.factory.getLoadBalancer(clientName);
|
|
||||||
ServerIntrospector serverIntrospector = this.factory.getInstance(clientName,
|
|
||||||
ServerIntrospector.class);
|
|
||||||
client = this.loadBalancedRetryFactory != null
|
|
||||||
? new RetryableFeignLoadBalancer(lb, config, serverIntrospector,
|
|
||||||
this.loadBalancedRetryFactory)
|
|
||||||
: new FeignLoadBalancer(lb, config, serverIntrospector);
|
|
||||||
this.cache.put(clientName, client);
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-246
@@ -1,246 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
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 com.netflix.client.AbstractLoadBalancerAwareClient;
|
|
||||||
import com.netflix.client.ClientException;
|
|
||||||
import com.netflix.client.ClientRequest;
|
|
||||||
import com.netflix.client.IResponse;
|
|
||||||
import com.netflix.client.RequestSpecificRetryHandler;
|
|
||||||
import com.netflix.client.RetryHandler;
|
|
||||||
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 org.springframework.cloud.netflix.ribbon.RibbonProperties;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.HttpRequest;
|
|
||||||
|
|
||||||
import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToSecureConnectionIfNeeded;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Dave Syer
|
|
||||||
* @author Spencer Gibb
|
|
||||||
* @author Ryan Baxter
|
|
||||||
* @author Tim Ysewyn
|
|
||||||
* @author Olga Maciaszek-Sharma
|
|
||||||
*/
|
|
||||||
public class FeignLoadBalancer extends
|
|
||||||
AbstractLoadBalancerAwareClient<FeignLoadBalancer.RibbonRequest, FeignLoadBalancer.RibbonResponse> {
|
|
||||||
|
|
||||||
private final RibbonProperties ribbon;
|
|
||||||
|
|
||||||
protected int connectTimeout;
|
|
||||||
|
|
||||||
protected int readTimeout;
|
|
||||||
|
|
||||||
protected IClientConfig clientConfig;
|
|
||||||
|
|
||||||
protected ServerIntrospector serverIntrospector;
|
|
||||||
|
|
||||||
public FeignLoadBalancer(ILoadBalancer lb, IClientConfig clientConfig,
|
|
||||||
ServerIntrospector serverIntrospector) {
|
|
||||||
super(lb, clientConfig);
|
|
||||||
this.setRetryHandler(RetryHandler.DEFAULT);
|
|
||||||
this.clientConfig = clientConfig;
|
|
||||||
this.ribbon = RibbonProperties.from(clientConfig);
|
|
||||||
RibbonProperties ribbon = this.ribbon;
|
|
||||||
this.connectTimeout = ribbon.getConnectTimeout();
|
|
||||||
this.readTimeout = ribbon.getReadTimeout();
|
|
||||||
this.serverIntrospector = serverIntrospector;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride)
|
|
||||||
throws IOException {
|
|
||||||
Request.Options options;
|
|
||||||
if (configOverride != null) {
|
|
||||||
RibbonProperties override = RibbonProperties.from(configOverride);
|
|
||||||
options = new Request.Options(override.connectTimeout(this.connectTimeout),
|
|
||||||
override.readTimeout(this.readTimeout));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
options = new Request.Options(this.connectTimeout, this.readTimeout);
|
|
||||||
}
|
|
||||||
Response response = request.client().execute(request.toRequest(), options);
|
|
||||||
return new RibbonResponse(request.getUri(), response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RequestSpecificRetryHandler getRequestSpecificRetryHandler(
|
|
||||||
RibbonRequest request, IClientConfig requestConfig) {
|
|
||||||
if (this.ribbon.isOkToRetryOnAllOperations()) {
|
|
||||||
return new RequestSpecificRetryHandler(true, true, this.getRetryHandler(),
|
|
||||||
requestConfig);
|
|
||||||
}
|
|
||||||
if (!request.toRequest().httpMethod().name().equals("GET")) {
|
|
||||||
return new RequestSpecificRetryHandler(true, false, this.getRetryHandler(),
|
|
||||||
requestConfig);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return new RequestSpecificRetryHandler(true, true, this.getRetryHandler(),
|
|
||||||
requestConfig);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public URI reconstructURIWithServer(Server server, URI original) {
|
|
||||||
URI uri = updateToSecureConnectionIfNeeded(original, this.clientConfig,
|
|
||||||
this.serverIntrospector, server);
|
|
||||||
return super.reconstructURIWithServer(server, uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class RibbonRequest extends ClientRequest implements Cloneable {
|
|
||||||
|
|
||||||
private final Request request;
|
|
||||||
|
|
||||||
private final Client client;
|
|
||||||
|
|
||||||
protected RibbonRequest(Client client, Request request, URI uri) {
|
|
||||||
this.client = client;
|
|
||||||
setUri(uri);
|
|
||||||
this.request = toRequest(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Request toRequest(Request request) {
|
|
||||||
Map<String, Collection<String>> headers = new LinkedHashMap<>(
|
|
||||||
request.headers());
|
|
||||||
return Request.create(request.httpMethod(), getUri().toASCIIString(), headers,
|
|
||||||
request.requestBody());
|
|
||||||
}
|
|
||||||
|
|
||||||
Request toRequest() {
|
|
||||||
return toRequest(this.request);
|
|
||||||
}
|
|
||||||
|
|
||||||
Client client() {
|
|
||||||
return this.client;
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpRequest toHttpRequest() {
|
|
||||||
return new HttpRequest() {
|
|
||||||
@Override
|
|
||||||
public HttpMethod getMethod() {
|
|
||||||
return HttpMethod
|
|
||||||
.resolve(RibbonRequest.this.toRequest().httpMethod().name());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMethodValue() {
|
|
||||||
return getMethod().name();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public URI getURI() {
|
|
||||||
return RibbonRequest.this.getUri();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public HttpHeaders getHeaders() {
|
|
||||||
Map<String, List<String>> headers = new HashMap<>();
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public Request getRequest() {
|
|
||||||
return this.request;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Client getClient() {
|
|
||||||
return this.client;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object clone() {
|
|
||||||
return new RibbonRequest(this.client, this.request, getUri());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class RibbonResponse implements IResponse {
|
|
||||||
|
|
||||||
private final URI uri;
|
|
||||||
|
|
||||||
private final Response response;
|
|
||||||
|
|
||||||
protected RibbonResponse(URI uri, Response response) {
|
|
||||||
this.uri = uri;
|
|
||||||
this.response = response;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getPayload() throws ClientException {
|
|
||||||
return this.response.body();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasPayload() {
|
|
||||||
return this.response.body() != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isSuccess() {
|
|
||||||
return this.response.status() == 200;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public URI getRequestedURI() {
|
|
||||||
return this.uri;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Collection<String>> getHeaders() {
|
|
||||||
return this.response.headers();
|
|
||||||
}
|
|
||||||
|
|
||||||
Response toResponse() {
|
|
||||||
return this.response;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {
|
|
||||||
if (this.response != null && this.response.body() != null) {
|
|
||||||
this.response.body().close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-129
@@ -1,129 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.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(this.serviceId, this.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<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getServiceId() {
|
|
||||||
return this.serviceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getHost() {
|
|
||||||
return this.request.getURI().getHost();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getPort() {
|
|
||||||
return this.request.getURI().getPort();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isSecure() {
|
|
||||||
return "https".equals(this.request.getURI().getScheme());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public URI getUri() {
|
|
||||||
return this.request.getURI();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, String> getMetadata() {
|
|
||||||
return this.metadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-79
@@ -1,79 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
import com.netflix.loadbalancer.ILoadBalancer;
|
|
||||||
import feign.Feign;
|
|
||||||
import feign.Request;
|
|
||||||
|
|
||||||
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.ConditionalOnMissingClass;
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
|
||||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
|
||||||
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
import org.springframework.context.annotation.Primary;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Autoconfiguration to be activated if Feign is in use and needs to be use Ribbon as a
|
|
||||||
* load balancer.
|
|
||||||
*
|
|
||||||
* @author Dave Syer
|
|
||||||
*/
|
|
||||||
@ConditionalOnClass({ ILoadBalancer.class, Feign.class })
|
|
||||||
@Configuration
|
|
||||||
@AutoConfigureBefore(FeignAutoConfiguration.class)
|
|
||||||
@EnableConfigurationProperties({ FeignHttpClientProperties.class })
|
|
||||||
// Order is important here, last should be the default, first should be optional
|
|
||||||
// see
|
|
||||||
// https://github.com/spring-cloud/spring-cloud-netflix/issues/2086#issuecomment-316281653
|
|
||||||
@Import({ HttpClientFeignLoadBalancedConfiguration.class,
|
|
||||||
OkHttpFeignLoadBalancedConfiguration.class,
|
|
||||||
DefaultFeignLoadBalancedConfiguration.class })
|
|
||||||
public class FeignRibbonClientAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
@ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
|
|
||||||
public CachingSpringLoadBalancerFactory cachingLBClientFactory(
|
|
||||||
SpringClientFactory factory) {
|
|
||||||
return new CachingSpringLoadBalancerFactory(factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@Primary
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
@ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate")
|
|
||||||
public CachingSpringLoadBalancerFactory retryabeCachingLBClientFactory(
|
|
||||||
SpringClientFactory factory, LoadBalancedRetryFactory retryFactory) {
|
|
||||||
return new CachingSpringLoadBalancerFactory(factory, retryFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
public Request.Options feignRequestOptions() {
|
|
||||||
return LoadBalancerFeignClient.DEFAULT_OPTIONS;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-138
@@ -1,138 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
|
||||||
|
|
||||||
import feign.Client;
|
|
||||||
import feign.httpclient.ApacheHttpClient;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.config.RequestConfig;
|
|
||||||
import org.apache.http.config.RegistryBuilder;
|
|
||||||
import org.apache.http.conn.HttpClientConnectionManager;
|
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
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.commons.httpclient.ApacheHttpClientConnectionManagerFactory;
|
|
||||||
import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
|
||||||
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Spencer Gibb
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnClass(ApacheHttpClient.class)
|
|
||||||
@ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
|
|
||||||
class HttpClientFeignLoadBalancedConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(Client.class)
|
|
||||||
public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
|
|
||||||
SpringClientFactory clientFactory, HttpClient httpClient) {
|
|
||||||
ApacheHttpClient delegate = new ApacheHttpClient(httpClient);
|
|
||||||
return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnMissingBean(CloseableHttpClient.class)
|
|
||||||
protected static class HttpClientFeignConfiguration {
|
|
||||||
|
|
||||||
private final Timer connectionManagerTimer = new Timer(
|
|
||||||
"FeignApacheHttpClientConfiguration.connectionManagerTimer", true);
|
|
||||||
|
|
||||||
private CloseableHttpClient httpClient;
|
|
||||||
|
|
||||||
@Autowired(required = false)
|
|
||||||
private RegistryBuilder registryBuilder;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(HttpClientConnectionManager.class)
|
|
||||||
public HttpClientConnectionManager connectionManager(
|
|
||||||
ApacheHttpClientConnectionManagerFactory connectionManagerFactory,
|
|
||||||
FeignHttpClientProperties httpClientProperties) {
|
|
||||||
final HttpClientConnectionManager connectionManager = connectionManagerFactory
|
|
||||||
.newConnectionManager(httpClientProperties.isDisableSslValidation(),
|
|
||||||
httpClientProperties.getMaxConnections(),
|
|
||||||
httpClientProperties.getMaxConnectionsPerRoute(),
|
|
||||||
httpClientProperties.getTimeToLive(),
|
|
||||||
httpClientProperties.getTimeToLiveUnit(),
|
|
||||||
this.registryBuilder);
|
|
||||||
this.connectionManagerTimer.schedule(new TimerTask() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
connectionManager.closeExpiredConnections();
|
|
||||||
}
|
|
||||||
}, 30000, httpClientProperties.getConnectionTimerRepeat());
|
|
||||||
return connectionManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnProperty(value = "feign.compression.response.enabled", havingValue = "true")
|
|
||||||
public CloseableHttpClient customHttpClient(
|
|
||||||
HttpClientConnectionManager httpClientConnectionManager,
|
|
||||||
FeignHttpClientProperties httpClientProperties) {
|
|
||||||
HttpClientBuilder builder = HttpClientBuilder.create()
|
|
||||||
.disableCookieManagement().useSystemProperties();
|
|
||||||
this.httpClient = createClient(builder, httpClientConnectionManager,
|
|
||||||
httpClientProperties);
|
|
||||||
return this.httpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnProperty(value = "feign.compression.response.enabled", havingValue = "false", matchIfMissing = true)
|
|
||||||
public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory,
|
|
||||||
HttpClientConnectionManager httpClientConnectionManager,
|
|
||||||
FeignHttpClientProperties httpClientProperties) {
|
|
||||||
this.httpClient = createClient(httpClientFactory.createBuilder(),
|
|
||||||
httpClientConnectionManager, httpClientProperties);
|
|
||||||
return this.httpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
private CloseableHttpClient createClient(HttpClientBuilder builder,
|
|
||||||
HttpClientConnectionManager httpClientConnectionManager,
|
|
||||||
FeignHttpClientProperties httpClientProperties) {
|
|
||||||
RequestConfig defaultRequestConfig = RequestConfig.custom()
|
|
||||||
.setConnectTimeout(httpClientProperties.getConnectionTimeout())
|
|
||||||
.setRedirectsEnabled(httpClientProperties.isFollowRedirects())
|
|
||||||
.build();
|
|
||||||
CloseableHttpClient httpClient = builder
|
|
||||||
.setDefaultRequestConfig(defaultRequestConfig)
|
|
||||||
.setConnectionManager(httpClientConnectionManager).build();
|
|
||||||
return httpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PreDestroy
|
|
||||||
public void destroy() throws Exception {
|
|
||||||
this.connectionManagerTimer.cancel();
|
|
||||||
if (this.httpClient != null) {
|
|
||||||
this.httpClient.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-143
@@ -1,143 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import com.netflix.client.ClientException;
|
|
||||||
import com.netflix.client.config.CommonClientConfigKey;
|
|
||||||
import com.netflix.client.config.DefaultClientConfigImpl;
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
|
||||||
import feign.Client;
|
|
||||||
import feign.Request;
|
|
||||||
import feign.Response;
|
|
||||||
|
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Dave Syer
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class LoadBalancerFeignClient implements Client {
|
|
||||||
|
|
||||||
static final Request.Options DEFAULT_OPTIONS = new Request.Options();
|
|
||||||
|
|
||||||
private final Client delegate;
|
|
||||||
|
|
||||||
private CachingSpringLoadBalancerFactory lbClientFactory;
|
|
||||||
|
|
||||||
private SpringClientFactory clientFactory;
|
|
||||||
|
|
||||||
public LoadBalancerFeignClient(Client delegate,
|
|
||||||
CachingSpringLoadBalancerFactory lbClientFactory,
|
|
||||||
SpringClientFactory clientFactory) {
|
|
||||||
this.delegate = delegate;
|
|
||||||
this.lbClientFactory = lbClientFactory;
|
|
||||||
this.clientFactory = clientFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
static URI cleanUrl(String originalUrl, String host) {
|
|
||||||
String newUrl = originalUrl;
|
|
||||||
if (originalUrl.startsWith("https://")) {
|
|
||||||
newUrl = originalUrl.substring(0, 8)
|
|
||||||
+ originalUrl.substring(8 + host.length());
|
|
||||||
}
|
|
||||||
else if (originalUrl.startsWith("http")) {
|
|
||||||
newUrl = originalUrl.substring(0, 7)
|
|
||||||
+ originalUrl.substring(7 + host.length());
|
|
||||||
}
|
|
||||||
StringBuffer buffer = new StringBuffer(newUrl);
|
|
||||||
if ((newUrl.startsWith("https://") && newUrl.length() == 8)
|
|
||||||
|| (newUrl.startsWith("http://") && newUrl.length() == 7)) {
|
|
||||||
buffer.append("/");
|
|
||||||
}
|
|
||||||
return URI.create(buffer.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Response execute(Request request, Request.Options options) throws IOException {
|
|
||||||
try {
|
|
||||||
URI asUri = URI.create(request.url());
|
|
||||||
String clientName = asUri.getHost();
|
|
||||||
URI uriWithoutHost = cleanUrl(request.url(), clientName);
|
|
||||||
FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(
|
|
||||||
this.delegate, request, uriWithoutHost);
|
|
||||||
|
|
||||||
IClientConfig requestConfig = getClientConfig(options, clientName);
|
|
||||||
return lbClient(clientName)
|
|
||||||
.executeWithLoadBalancer(ribbonRequest, requestConfig).toResponse();
|
|
||||||
}
|
|
||||||
catch (ClientException e) {
|
|
||||||
IOException io = findIOException(e);
|
|
||||||
if (io != null) {
|
|
||||||
throw io;
|
|
||||||
}
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
IClientConfig getClientConfig(Request.Options options, String clientName) {
|
|
||||||
IClientConfig requestConfig;
|
|
||||||
if (options == DEFAULT_OPTIONS) {
|
|
||||||
requestConfig = this.clientFactory.getClientConfig(clientName);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
requestConfig = new FeignOptionsClientConfig(options);
|
|
||||||
}
|
|
||||||
return requestConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected IOException findIOException(Throwable t) {
|
|
||||||
if (t == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (t instanceof IOException) {
|
|
||||||
return (IOException) t;
|
|
||||||
}
|
|
||||||
return findIOException(t.getCause());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Client getDelegate() {
|
|
||||||
return this.delegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
private FeignLoadBalancer lbClient(String clientName) {
|
|
||||||
return this.lbClientFactory.create(clientName);
|
|
||||||
}
|
|
||||||
|
|
||||||
static class FeignOptionsClientConfig extends DefaultClientConfigImpl {
|
|
||||||
|
|
||||||
FeignOptionsClientConfig(Request.Options options) {
|
|
||||||
setProperty(CommonClientConfigKey.ConnectTimeout,
|
|
||||||
options.connectTimeoutMillis());
|
|
||||||
setProperty(CommonClientConfigKey.ReadTimeout, options.readTimeoutMillis());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void loadProperties(String clientName) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void loadDefaultValues() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-94
@@ -1,94 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
|
||||||
|
|
||||||
import feign.Client;
|
|
||||||
import feign.okhttp.OkHttpClient;
|
|
||||||
import okhttp3.ConnectionPool;
|
|
||||||
|
|
||||||
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.commons.httpclient.OkHttpClientConnectionPoolFactory;
|
|
||||||
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
|
||||||
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Spencer Gibb
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnClass(OkHttpClient.class)
|
|
||||||
@ConditionalOnProperty("feign.okhttp.enabled")
|
|
||||||
class OkHttpFeignLoadBalancedConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(Client.class)
|
|
||||||
public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
|
|
||||||
SpringClientFactory clientFactory, okhttp3.OkHttpClient okHttpClient) {
|
|
||||||
OkHttpClient delegate = new OkHttpClient(okHttpClient);
|
|
||||||
return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@ConditionalOnMissingBean(okhttp3.OkHttpClient.class)
|
|
||||||
protected static class OkHttpFeignConfiguration {
|
|
||||||
|
|
||||||
private okhttp3.OkHttpClient okHttpClient;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(ConnectionPool.class)
|
|
||||||
public ConnectionPool httpClientConnectionPool(
|
|
||||||
FeignHttpClientProperties httpClientProperties,
|
|
||||||
OkHttpClientConnectionPoolFactory connectionPoolFactory) {
|
|
||||||
Integer maxTotalConnections = httpClientProperties.getMaxConnections();
|
|
||||||
Long timeToLive = httpClientProperties.getTimeToLive();
|
|
||||||
TimeUnit ttlUnit = httpClientProperties.getTimeToLiveUnit();
|
|
||||||
return connectionPoolFactory.create(maxTotalConnections, timeToLive, ttlUnit);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public okhttp3.OkHttpClient client(OkHttpClientFactory httpClientFactory,
|
|
||||||
ConnectionPool connectionPool,
|
|
||||||
FeignHttpClientProperties httpClientProperties) {
|
|
||||||
Boolean followRedirects = httpClientProperties.isFollowRedirects();
|
|
||||||
Integer connectTimeout = httpClientProperties.getConnectionTimeout();
|
|
||||||
this.okHttpClient = httpClientFactory
|
|
||||||
.createBuilder(httpClientProperties.isDisableSslValidation())
|
|
||||||
.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
|
|
||||||
.followRedirects(followRedirects).connectionPool(connectionPool)
|
|
||||||
.build();
|
|
||||||
return this.okHttpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PreDestroy
|
|
||||||
public void destroy() {
|
|
||||||
if (this.okHttpClient != null) {
|
|
||||||
this.okHttpClient.dispatcher().executorService().shutdown();
|
|
||||||
this.okHttpClient.connectionPool().evictAll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-148
@@ -1,148 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import com.netflix.client.DefaultLoadBalancerRetryHandler;
|
|
||||||
import com.netflix.client.RequestSpecificRetryHandler;
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
|
||||||
import com.netflix.loadbalancer.ILoadBalancer;
|
|
||||||
import com.netflix.loadbalancer.Server;
|
|
||||||
import feign.Request;
|
|
||||||
import feign.Response;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRecoveryCallback;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.ServiceInstanceChooser;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.RibbonProperties;
|
|
||||||
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
|
|
||||||
import org.springframework.retry.RetryCallback;
|
|
||||||
import org.springframework.retry.RetryContext;
|
|
||||||
import org.springframework.retry.RetryListener;
|
|
||||||
import org.springframework.retry.backoff.BackOffPolicy;
|
|
||||||
import org.springframework.retry.backoff.NoBackOffPolicy;
|
|
||||||
import org.springframework.retry.policy.NeverRetryPolicy;
|
|
||||||
import org.springframework.retry.support.RetryTemplate;
|
|
||||||
import org.springframework.util.StreamUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link FeignLoadBalancer} that leverages Spring Retry to retry failed requests.
|
|
||||||
*
|
|
||||||
* @author Ryan Baxter
|
|
||||||
* @author Gang Li
|
|
||||||
*/
|
|
||||||
public class RetryableFeignLoadBalancer extends FeignLoadBalancer
|
|
||||||
implements ServiceInstanceChooser {
|
|
||||||
|
|
||||||
private final LoadBalancedRetryFactory loadBalancedRetryFactory;
|
|
||||||
|
|
||||||
public RetryableFeignLoadBalancer(ILoadBalancer lb, IClientConfig clientConfig,
|
|
||||||
ServerIntrospector serverIntrospector,
|
|
||||||
LoadBalancedRetryFactory loadBalancedRetryFactory) {
|
|
||||||
super(lb, clientConfig, serverIntrospector);
|
|
||||||
this.loadBalancedRetryFactory = loadBalancedRetryFactory;
|
|
||||||
this.setRetryHandler(new DefaultLoadBalancerRetryHandler(clientConfig));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RibbonResponse execute(final RibbonRequest request,
|
|
||||||
IClientConfig configOverride) throws IOException {
|
|
||||||
final Request.Options options;
|
|
||||||
if (configOverride != null) {
|
|
||||||
RibbonProperties ribbon = RibbonProperties.from(configOverride);
|
|
||||||
options = new Request.Options(ribbon.connectTimeout(this.connectTimeout),
|
|
||||||
ribbon.readTimeout(this.readTimeout));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
options = new Request.Options(this.connectTimeout, this.readTimeout);
|
|
||||||
}
|
|
||||||
final LoadBalancedRetryPolicy retryPolicy = this.loadBalancedRetryFactory
|
|
||||||
.createRetryPolicy(this.getClientName(), this);
|
|
||||||
RetryTemplate retryTemplate = new RetryTemplate();
|
|
||||||
BackOffPolicy backOffPolicy = this.loadBalancedRetryFactory
|
|
||||||
.createBackOffPolicy(this.getClientName());
|
|
||||||
retryTemplate.setBackOffPolicy(
|
|
||||||
backOffPolicy == null ? new NoBackOffPolicy() : backOffPolicy);
|
|
||||||
RetryListener[] retryListeners = this.loadBalancedRetryFactory
|
|
||||||
.createRetryListeners(this.getClientName());
|
|
||||||
if (retryListeners != null && retryListeners.length != 0) {
|
|
||||||
retryTemplate.setListeners(retryListeners);
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
if (retryPolicy != null
|
|
||||||
&& retryPolicy.retryableStatusCode(response.status())) {
|
|
||||||
byte[] byteArray = response.body() == null ? new byte[] {}
|
|
||||||
: StreamUtils
|
|
||||||
.copyToByteArray(response.body().asInputStream());
|
|
||||||
response.close();
|
|
||||||
throw new RibbonResponseStatusCodeException(
|
|
||||||
RetryableFeignLoadBalancer.this.clientName, response,
|
|
||||||
byteArray, request.getUri());
|
|
||||||
}
|
|
||||||
return new RibbonResponse(request.getUri(), response);
|
|
||||||
}
|
|
||||||
}, new LoadBalancedRecoveryCallback<RibbonResponse, Response>() {
|
|
||||||
@Override
|
|
||||||
protected RibbonResponse createResponse(Response response, URI uri) {
|
|
||||||
return new RibbonResponse(uri, response);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public RequestSpecificRetryHandler getRequestSpecificRetryHandler(
|
|
||||||
FeignLoadBalancer.RibbonRequest request, IClientConfig requestConfig) {
|
|
||||||
return new RequestSpecificRetryHandler(false, false, this.getRetryHandler(),
|
|
||||||
requestConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ServiceInstance choose(String serviceId) {
|
|
||||||
return new RibbonLoadBalancerClient.RibbonServer(serviceId,
|
|
||||||
this.getLoadBalancer().chooseServer(serviceId));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
-49
@@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.ribbon;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import feign.Response;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.loadbalancer.RetryableStatusCodeException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link RetryableStatusCodeException} for {@link Response}s.
|
|
||||||
*
|
|
||||||
* @author Ryan Baxter
|
|
||||||
*/
|
|
||||||
public class RibbonResponseStatusCodeException extends RetryableStatusCodeException {
|
|
||||||
|
|
||||||
private final Response response;
|
|
||||||
|
|
||||||
public RibbonResponseStatusCodeException(String serviceId, Response response,
|
|
||||||
byte[] body, URI uri) {
|
|
||||||
super(serviceId, response.status(), response, uri);
|
|
||||||
this.response = Response.builder()
|
|
||||||
.body(new ByteArrayInputStream(body), body.length)
|
|
||||||
.headers(response.headers()).reason(response.reason())
|
|
||||||
.status(response.status()).request(response.request()).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Response getResponse() {
|
|
||||||
return this.response;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+82
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
import feign.codec.EncodeException;
|
||||||
|
import feign.form.multipart.AbstractWriter;
|
||||||
|
import feign.form.multipart.Output;
|
||||||
|
import feign.form.util.PojoUtil;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import static feign.form.ContentProcessor.CRLF;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Darren Foong
|
||||||
|
*/
|
||||||
|
public abstract class AbstractFormWriter extends AbstractWriter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isApplicable(Object object) {
|
||||||
|
return !isTypeOrCollection(object, o -> o instanceof MultipartFile)
|
||||||
|
&& isTypeOrCollection(object, PojoUtil::isUserPojo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(Output output, String key, Object object) throws EncodeException {
|
||||||
|
try {
|
||||||
|
String string = new StringBuilder()
|
||||||
|
.append("Content-Disposition: form-data; name=\"").append(key)
|
||||||
|
.append('"').append(CRLF).append("Content-Type: ")
|
||||||
|
.append(getContentType()).append("; charset=")
|
||||||
|
.append(output.getCharset().name()).append(CRLF).append(CRLF)
|
||||||
|
.append(writeAsString(object)).toString();
|
||||||
|
|
||||||
|
output.write(string);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new EncodeException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract MediaType getContentType();
|
||||||
|
|
||||||
|
protected abstract String writeAsString(Object object) throws IOException;
|
||||||
|
|
||||||
|
private boolean isTypeOrCollection(Object object, Predicate<Object> isType) {
|
||||||
|
if (object.getClass().isArray()) {
|
||||||
|
Object[] array = (Object[]) object;
|
||||||
|
|
||||||
|
return array.length > 1 && isType.test(array[0]);
|
||||||
|
}
|
||||||
|
else if (object instanceof Iterable) {
|
||||||
|
Iterable<?> iterable = (Iterable<?>) object;
|
||||||
|
Iterator<?> iterator = iterable.iterator();
|
||||||
|
|
||||||
|
return iterator.hasNext() && isType.test(iterator.next());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return isType.test(object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+85
@@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.support;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
|
import feign.FeignException;
|
||||||
|
import feign.Response;
|
||||||
|
import feign.codec.Decoder;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.encoding.HttpEncoding;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When response is compressed as gzip, this decompresses and uses {@link SpringDecoder}
|
||||||
|
* to decode.
|
||||||
|
*
|
||||||
|
* @author Jaesik Kim
|
||||||
|
*/
|
||||||
|
public class DefaultGzipDecoder implements Decoder {
|
||||||
|
|
||||||
|
private Decoder decoder;
|
||||||
|
|
||||||
|
public DefaultGzipDecoder(Decoder decoder) {
|
||||||
|
this.decoder = decoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object decode(final Response response, Type type)
|
||||||
|
throws IOException, FeignException {
|
||||||
|
Collection<String> encoding = response.headers()
|
||||||
|
.containsKey(HttpEncoding.CONTENT_ENCODING_HEADER)
|
||||||
|
? response.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (encoding != null) {
|
||||||
|
if (encoding.contains(HttpEncoding.GZIP_ENCODING)) {
|
||||||
|
String decompressedBody = decompress(response);
|
||||||
|
if (decompressedBody != null) {
|
||||||
|
Response decompressedResponse = response.toBuilder()
|
||||||
|
.body(decompressedBody.getBytes()).build();
|
||||||
|
return decoder.decode(decompressedResponse, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return decoder.decode(response, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String decompress(Response response) throws IOException {
|
||||||
|
if (response.body() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try (GZIPInputStream gzipInputStream = new GZIPInputStream(
|
||||||
|
response.body().asInputStream());
|
||||||
|
BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8))) {
|
||||||
|
String outputString = "";
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
outputString += line;
|
||||||
|
}
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+59
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.support;
|
||||||
|
|
||||||
|
import feign.codec.Decoder;
|
||||||
|
import feign.optionals.OptionalDecoder;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectFactory;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||||
|
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures Default Gzip Decoder.
|
||||||
|
*
|
||||||
|
* @author Jaesik Kim
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnProperty("feign.compression.response.enabled")
|
||||||
|
// The OK HTTP client uses "transparent" compression.
|
||||||
|
// If the accept-encoding header is present, it disables transparent compression
|
||||||
|
@ConditionalOnMissingBean(type = "okhttp3.OkHttpClient")
|
||||||
|
@AutoConfigureAfter(FeignAutoConfiguration.class)
|
||||||
|
public class DefaultGzipDecoderConfiguration {
|
||||||
|
|
||||||
|
private ObjectFactory<HttpMessageConverters> messageConverters;
|
||||||
|
|
||||||
|
public DefaultGzipDecoderConfiguration(
|
||||||
|
ObjectFactory<HttpMessageConverters> messageConverters) {
|
||||||
|
this.messageConverters = messageConverters;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnProperty("feign.compression.response.useGzipDecoder")
|
||||||
|
public Decoder defaultGzipDecoder() {
|
||||||
|
return new OptionalDecoder(new ResponseEntityDecoder(
|
||||||
|
new DefaultGzipDecoder(new SpringDecoder(messageConverters))));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
-79
@@ -1,79 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2013-2019 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
|
|
||||||
*
|
|
||||||
* https://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.openfeign.support;
|
|
||||||
|
|
||||||
import com.netflix.hystrix.HystrixCommand;
|
|
||||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
|
||||||
import com.netflix.hystrix.HystrixThreadPoolKey;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convenience class for implementing feign fallbacks that return {@link HystrixCommand}.
|
|
||||||
* Also useful for return types of {@link rx.Observable} and
|
|
||||||
* {@link java.util.concurrent.Future}. For those return types, just call
|
|
||||||
* {@link FallbackCommand#observe()} or {@link FallbackCommand#queue()} respectively.
|
|
||||||
*
|
|
||||||
* @param <T> result type
|
|
||||||
* @author Spencer Gibb
|
|
||||||
*/
|
|
||||||
public class FallbackCommand<T> extends HystrixCommand<T> {
|
|
||||||
|
|
||||||
private T result;
|
|
||||||
|
|
||||||
public FallbackCommand(T result) {
|
|
||||||
this(result, "fallback");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected FallbackCommand(T result, String groupname) {
|
|
||||||
super(HystrixCommandGroupKey.Factory.asKey(groupname));
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FallbackCommand(T result, HystrixCommandGroupKey group) {
|
|
||||||
super(group);
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FallbackCommand(T result, HystrixCommandGroupKey group,
|
|
||||||
int executionIsolationThreadTimeoutInMilliseconds) {
|
|
||||||
super(group, executionIsolationThreadTimeoutInMilliseconds);
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FallbackCommand(T result, HystrixCommandGroupKey group,
|
|
||||||
HystrixThreadPoolKey threadPool) {
|
|
||||||
super(group, threadPool);
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FallbackCommand(T result, HystrixCommandGroupKey group,
|
|
||||||
HystrixThreadPoolKey threadPool,
|
|
||||||
int executionIsolationThreadTimeoutInMilliseconds) {
|
|
||||||
super(group, threadPool, executionIsolationThreadTimeoutInMilliseconds);
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public FallbackCommand(T result, Setter setter) {
|
|
||||||
super(setter);
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected T run() throws Exception {
|
|
||||||
return this.result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+20
-12
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -14,25 +14,33 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.cloud.openfeign.hystrix.security.app;
|
package org.springframework.cloud.openfeign.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Daniel Lavoie
|
* @author Darren Foong
|
||||||
*/
|
*/
|
||||||
@RestController
|
@Component
|
||||||
@RequestMapping("/proxy-username")
|
public class JsonFormWriter extends AbstractFormWriter {
|
||||||
public class ProxyUsernameController {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UsernameClient usernameClient;
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
@RequestMapping
|
@Override
|
||||||
public String getUsername() {
|
protected MediaType getContentType() {
|
||||||
return this.usernameClient.getUsername();
|
return MediaType.APPLICATION_JSON;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String writeAsString(Object object) throws IOException {
|
||||||
|
return objectMapper.writeValueAsString(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+15
-7
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2018 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -33,7 +33,7 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This jackson module provides support to deserialize spring {@link Page} objects.
|
* This Jackson module provides support to deserialize Spring {@link Page} objects.
|
||||||
*
|
*
|
||||||
* @author Pascal Büttiker
|
* @author Pascal Büttiker
|
||||||
*/
|
*/
|
||||||
@@ -64,10 +64,18 @@ public class PageJacksonModule extends Module {
|
|||||||
private final Page<T> delegate;
|
private final Page<T> delegate;
|
||||||
|
|
||||||
SimplePageImpl(@JsonProperty("content") List<T> content,
|
SimplePageImpl(@JsonProperty("content") List<T> content,
|
||||||
@JsonProperty("page") int number, @JsonProperty("size") int size,
|
@JsonProperty("number") int number, @JsonProperty("size") int size,
|
||||||
@JsonProperty("totalElements") long totalElements) {
|
@JsonProperty("totalElements") long totalElements,
|
||||||
delegate = new PageImpl<>(content, PageRequest.of(number, size),
|
@JsonProperty("sort") Sort sort) {
|
||||||
totalElements);
|
PageRequest pageRequest;
|
||||||
|
if (sort != null) {
|
||||||
|
pageRequest = PageRequest.of(number, size, sort);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pageRequest = PageRequest.of(number, size);
|
||||||
|
}
|
||||||
|
delegate = new PageImpl<>(content, pageRequest, totalElements);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
@@ -82,7 +90,7 @@ public class PageJacksonModule extends Module {
|
|||||||
return delegate.getTotalElements();
|
return delegate.getTotalElements();
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonProperty("page")
|
@JsonProperty
|
||||||
@Override
|
@Override
|
||||||
public int getNumber() {
|
public int getNumber() {
|
||||||
return delegate.getNumber();
|
return delegate.getNumber();
|
||||||
|
|||||||
+13
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2018 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -62,6 +62,18 @@ public class PageableSpringEncoder implements Encoder {
|
|||||||
this.delegate = delegate;
|
this.delegate = delegate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPageParameter(String pageParameter) {
|
||||||
|
this.pageParameter = pageParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSizeParameter(String sizeParameter) {
|
||||||
|
this.sizeParameter = sizeParameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSortParameter(String sortParameter) {
|
||||||
|
this.sortParameter = sortParameter;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void encode(Object object, Type bodyType, RequestTemplate template)
|
public void encode(Object object, Type bodyType, RequestTemplate template)
|
||||||
throws EncodeException {
|
throws EncodeException {
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.support;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.Version;
|
||||||
|
import com.fasterxml.jackson.databind.Module;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleSerializers;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Jackson module provides support for serializing and deserializing for Spring
|
||||||
|
* {@link Sort} object.
|
||||||
|
*
|
||||||
|
* @author Can Bezmen
|
||||||
|
*/
|
||||||
|
public class SortJacksonModule extends Module {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getModuleName() {
|
||||||
|
return "SortModule";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Version version() {
|
||||||
|
return new Version(0, 1, 0, "", null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setupModule(SetupContext context) {
|
||||||
|
SimpleSerializers serializers = new SimpleSerializers();
|
||||||
|
serializers.addSerializer(Sort.class, new SortJsonComponent.SortSerializer());
|
||||||
|
context.addSerializers(serializers);
|
||||||
|
|
||||||
|
SimpleDeserializers deserializers = new SimpleDeserializers();
|
||||||
|
deserializers.addDeserializer(Sort.class,
|
||||||
|
new SortJsonComponent.SortDeserializer());
|
||||||
|
context.addDeserializers(deserializers);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+95
@@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign.support;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.core.TreeNode;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
|
import feign.codec.EncodeException;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides provides support for serializing and deserializing for Spring
|
||||||
|
* {@link Sort} object.
|
||||||
|
*
|
||||||
|
* @author Can Bezmen
|
||||||
|
*/
|
||||||
|
public class SortJsonComponent {
|
||||||
|
|
||||||
|
public static class SortSerializer extends JsonSerializer<Sort> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(Sort value, JsonGenerator gen,
|
||||||
|
SerializerProvider serializers) throws IOException {
|
||||||
|
gen.writeStartArray();
|
||||||
|
value.iterator().forEachRemaining(v -> {
|
||||||
|
try {
|
||||||
|
gen.writeObject(v);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new EncodeException("Couldn't serialize object " + v);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
gen.writeEndArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<Sort> handledType() {
|
||||||
|
return Sort.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SortDeserializer extends JsonDeserializer<Sort> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Sort deserialize(JsonParser jsonParser,
|
||||||
|
DeserializationContext deserializationContext) throws IOException {
|
||||||
|
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
|
||||||
|
if (treeNode.isArray()) {
|
||||||
|
ArrayNode arrayNode = (ArrayNode) treeNode;
|
||||||
|
List<Sort.Order> orders = new ArrayList<>();
|
||||||
|
for (JsonNode jsonNode : arrayNode) {
|
||||||
|
Sort.Order order = new Sort.Order(
|
||||||
|
Sort.Direction.valueOf(jsonNode.get("direction").textValue()),
|
||||||
|
jsonNode.get("property").textValue());
|
||||||
|
orders.add(order);
|
||||||
|
}
|
||||||
|
return Sort.by(orders);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<Sort> handledType() {
|
||||||
|
return Sort.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+3
-2
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -59,7 +59,8 @@ public class SpringDecoder implements Decoder {
|
|||||||
return extractor.extractData(new FeignResponseAdapter(response));
|
return extractor.extractData(new FeignResponseAdapter(response));
|
||||||
}
|
}
|
||||||
throw new DecodeException(response.status(),
|
throw new DecodeException(response.status(),
|
||||||
"type is not an instance of Class or ParameterizedType: " + type);
|
"type is not an instance of Class or ParameterizedType: " + type,
|
||||||
|
response.request());
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class FeignResponseAdapter implements ClientHttpResponse {
|
private final class FeignResponseAdapter implements ClientHttpResponse {
|
||||||
|
|||||||
+86
-38
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -35,10 +35,13 @@ import org.apache.commons.logging.LogFactory;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.ObjectFactory;
|
import org.springframework.beans.factory.ObjectFactory;
|
||||||
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
||||||
|
import org.springframework.cloud.openfeign.encoding.HttpEncoding;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpOutputMessage;
|
import org.springframework.http.HttpOutputMessage;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.GenericHttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.HttpMessageConversionException;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
|
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@@ -49,16 +52,26 @@ import static org.springframework.cloud.openfeign.support.FeignUtils.getHttpHead
|
|||||||
/**
|
/**
|
||||||
* @author Spencer Gibb
|
* @author Spencer Gibb
|
||||||
* @author Scien Jus
|
* @author Scien Jus
|
||||||
|
* @author Ahmad Mozafarnia
|
||||||
|
* @author Aaron Whiteside
|
||||||
|
* @author Darren Foong
|
||||||
*/
|
*/
|
||||||
public class SpringEncoder implements Encoder {
|
public class SpringEncoder implements Encoder {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(SpringEncoder.class);
|
private static final Log log = LogFactory.getLog(SpringEncoder.class);
|
||||||
|
|
||||||
private final SpringFormEncoder springFormEncoder = new SpringFormEncoder();
|
private final SpringFormEncoder springFormEncoder;
|
||||||
|
|
||||||
private ObjectFactory<HttpMessageConverters> messageConverters;
|
private final ObjectFactory<HttpMessageConverters> messageConverters;
|
||||||
|
|
||||||
public SpringEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
|
public SpringEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
|
||||||
|
this.springFormEncoder = new SpringFormEncoder();
|
||||||
|
this.messageConverters = messageConverters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SpringEncoder(SpringFormEncoder springFormEncoder,
|
||||||
|
ObjectFactory<HttpMessageConverters> messageConverters) {
|
||||||
|
this.springFormEncoder = springFormEncoder;
|
||||||
this.messageConverters = messageConverters;
|
this.messageConverters = messageConverters;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +80,8 @@ public class SpringEncoder implements Encoder {
|
|||||||
throws EncodeException {
|
throws EncodeException {
|
||||||
// template.body(conversionService.convert(object, String.class));
|
// template.body(conversionService.convert(object, String.class));
|
||||||
if (requestBody != null) {
|
if (requestBody != null) {
|
||||||
Class<?> requestType = requestBody.getClass();
|
Collection<String> contentTypes = request.headers()
|
||||||
Collection<String> contentTypes = request.headers().get("Content-Type");
|
.get(HttpEncoding.CONTENT_TYPE);
|
||||||
|
|
||||||
MediaType requestContentType = null;
|
MediaType requestContentType = null;
|
||||||
if (contentTypes != null && !contentTypes.isEmpty()) {
|
if (contentTypes != null && !contentTypes.isEmpty()) {
|
||||||
@@ -76,44 +89,36 @@ public class SpringEncoder implements Encoder {
|
|||||||
requestContentType = MediaType.valueOf(type);
|
requestContentType = MediaType.valueOf(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bodyType != null && bodyType.equals(MultipartFile.class)) {
|
if (Objects.equals(requestContentType, MediaType.MULTIPART_FORM_DATA)) {
|
||||||
if (Objects.equals(requestContentType, MediaType.MULTIPART_FORM_DATA)) {
|
this.springFormEncoder.encode(requestBody, bodyType, request);
|
||||||
this.springFormEncoder.encode(requestBody, bodyType, request);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
else {
|
||||||
else {
|
if (bodyType == MultipartFile.class) {
|
||||||
String message = "Content-Type \"" + MediaType.MULTIPART_FORM_DATA
|
log.warn(
|
||||||
+ "\" not set for request body of type "
|
"For MultipartFile to be handled correctly, the 'consumes' parameter of @RequestMapping "
|
||||||
+ requestBody.getClass().getSimpleName();
|
+ "should be specified as MediaType.MULTIPART_FORM_DATA_VALUE");
|
||||||
throw new EncodeException(message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (HttpMessageConverter<?> messageConverter : this.messageConverters
|
for (HttpMessageConverter messageConverter : this.messageConverters
|
||||||
.getObject().getConverters()) {
|
.getObject().getConverters()) {
|
||||||
if (messageConverter.canWrite(requestType, requestContentType)) {
|
FeignOutputMessage outputMessage;
|
||||||
if (log.isDebugEnabled()) {
|
try {
|
||||||
if (requestContentType != null) {
|
if (messageConverter instanceof GenericHttpMessageConverter) {
|
||||||
log.debug("Writing [" + requestBody + "] as \""
|
outputMessage = checkAndWrite(requestBody, bodyType,
|
||||||
+ requestContentType + "\" using [" + messageConverter
|
requestContentType,
|
||||||
+ "]");
|
(GenericHttpMessageConverter) messageConverter, request);
|
||||||
}
|
|
||||||
else {
|
|
||||||
log.debug("Writing [" + requestBody + "] using ["
|
|
||||||
+ messageConverter + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
FeignOutputMessage outputMessage = new FeignOutputMessage(request);
|
outputMessage = checkAndWrite(requestBody, requestContentType,
|
||||||
try {
|
messageConverter, request);
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
HttpMessageConverter<Object> copy = (HttpMessageConverter<Object>) messageConverter;
|
|
||||||
copy.write(requestBody, requestContentType, outputMessage);
|
|
||||||
}
|
|
||||||
catch (IOException ex) {
|
|
||||||
throw new EncodeException("Error converting request body", ex);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (IOException | HttpMessageConversionException ex) {
|
||||||
|
throw new EncodeException("Error converting request body", ex);
|
||||||
|
}
|
||||||
|
if (outputMessage != null) {
|
||||||
// clear headers
|
// clear headers
|
||||||
request.headers(null);
|
request.headers(null);
|
||||||
// converters can modify headers, so update the request
|
// converters can modify headers, so update the request
|
||||||
@@ -139,7 +144,7 @@ public class SpringEncoder implements Encoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
String message = "Could not write request: no suitable HttpMessageConverter "
|
String message = "Could not write request: no suitable HttpMessageConverter "
|
||||||
+ "found for request type [" + requestType.getName() + "]";
|
+ "found for request type [" + requestBody.getClass().getName() + "]";
|
||||||
if (requestContentType != null) {
|
if (requestContentType != null) {
|
||||||
message += " and content type [" + requestContentType + "]";
|
message += " and content type [" + requestContentType + "]";
|
||||||
}
|
}
|
||||||
@@ -147,6 +152,49 @@ public class SpringEncoder implements Encoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private FeignOutputMessage checkAndWrite(Object body, MediaType contentType,
|
||||||
|
HttpMessageConverter converter, RequestTemplate request) throws IOException {
|
||||||
|
if (converter.canWrite(body.getClass(), contentType)) {
|
||||||
|
logBeforeWrite(body, contentType, converter);
|
||||||
|
FeignOutputMessage outputMessage = new FeignOutputMessage(request);
|
||||||
|
converter.write(body, contentType, outputMessage);
|
||||||
|
return outputMessage;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private FeignOutputMessage checkAndWrite(Object body, Type genericType,
|
||||||
|
MediaType contentType, GenericHttpMessageConverter converter,
|
||||||
|
RequestTemplate request) throws IOException {
|
||||||
|
if (converter.canWrite(genericType, body.getClass(), contentType)) {
|
||||||
|
logBeforeWrite(body, contentType, converter);
|
||||||
|
FeignOutputMessage outputMessage = new FeignOutputMessage(request);
|
||||||
|
converter.write(body, genericType, contentType, outputMessage);
|
||||||
|
return outputMessage;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logBeforeWrite(Object requestBody, MediaType requestContentType,
|
||||||
|
HttpMessageConverter messageConverter) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
if (requestContentType != null) {
|
||||||
|
log.debug("Writing [" + requestBody + "] as \"" + requestContentType
|
||||||
|
+ "\" using [" + messageConverter + "]");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
log.debug(
|
||||||
|
"Writing [" + requestBody + "] using [" + messageConverter + "]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private final class FeignOutputMessage implements HttpOutputMessage {
|
private final class FeignOutputMessage implements HttpOutputMessage {
|
||||||
|
|
||||||
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
|||||||
+57
-35
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -28,6 +28,7 @@ import java.util.HashMap;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import feign.Contract;
|
import feign.Contract;
|
||||||
import feign.Feign;
|
import feign.Feign;
|
||||||
@@ -36,10 +37,14 @@ import feign.Param;
|
|||||||
import feign.Request;
|
import feign.Request;
|
||||||
|
|
||||||
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
|
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
|
||||||
|
import org.springframework.cloud.openfeign.CollectionFormat;
|
||||||
|
import org.springframework.cloud.openfeign.annotation.MatrixVariableParameterProcessor;
|
||||||
import org.springframework.cloud.openfeign.annotation.PathVariableParameterProcessor;
|
import org.springframework.cloud.openfeign.annotation.PathVariableParameterProcessor;
|
||||||
import org.springframework.cloud.openfeign.annotation.QueryMapParameterProcessor;
|
import org.springframework.cloud.openfeign.annotation.QueryMapParameterProcessor;
|
||||||
import org.springframework.cloud.openfeign.annotation.RequestHeaderParameterProcessor;
|
import org.springframework.cloud.openfeign.annotation.RequestHeaderParameterProcessor;
|
||||||
import org.springframework.cloud.openfeign.annotation.RequestParamParameterProcessor;
|
import org.springframework.cloud.openfeign.annotation.RequestParamParameterProcessor;
|
||||||
|
import org.springframework.cloud.openfeign.annotation.RequestPartParameterProcessor;
|
||||||
|
import org.springframework.cloud.openfeign.encoding.HttpEncoding;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.ResourceLoaderAware;
|
import org.springframework.context.ResourceLoaderAware;
|
||||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||||
@@ -52,6 +57,7 @@ import org.springframework.core.convert.TypeDescriptor;
|
|||||||
import org.springframework.core.convert.support.DefaultConversionService;
|
import org.springframework.core.convert.support.DefaultConversionService;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -68,6 +74,9 @@ import static org.springframework.core.annotation.AnnotatedElementUtils.findMerg
|
|||||||
* @author Halvdan Hoem Grelland
|
* @author Halvdan Hoem Grelland
|
||||||
* @author Aram Peres
|
* @author Aram Peres
|
||||||
* @author Olga Maciaszek-Sharma
|
* @author Olga Maciaszek-Sharma
|
||||||
|
* @author Aaron Whiteside
|
||||||
|
* @author Artyom Romanenko
|
||||||
|
* @author Darren Foong
|
||||||
*/
|
*/
|
||||||
public class SpringMvcContract extends Contract.BaseContract
|
public class SpringMvcContract extends Contract.BaseContract
|
||||||
implements ResourceLoaderAware {
|
implements ResourceLoaderAware {
|
||||||
@@ -110,16 +119,12 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
"Parameter processors can not be null.");
|
"Parameter processors can not be null.");
|
||||||
Assert.notNull(conversionService, "ConversionService can not be null.");
|
Assert.notNull(conversionService, "ConversionService can not be null.");
|
||||||
|
|
||||||
List<AnnotatedParameterProcessor> processors;
|
List<AnnotatedParameterProcessor> processors = getDefaultAnnotatedArgumentsProcessors();
|
||||||
if (!annotatedParameterProcessors.isEmpty()) {
|
processors.addAll(annotatedParameterProcessors);
|
||||||
processors = new ArrayList<>(annotatedParameterProcessors);
|
|
||||||
}
|
annotatedArgumentProcessors = toAnnotatedArgumentProcessorMap(processors);
|
||||||
else {
|
|
||||||
processors = getDefaultAnnotatedArgumentsProcessors();
|
|
||||||
}
|
|
||||||
this.annotatedArgumentProcessors = toAnnotatedArgumentProcessorMap(processors);
|
|
||||||
this.conversionService = conversionService;
|
this.conversionService = conversionService;
|
||||||
this.convertingExpanderFactory = new ConvertingExpanderFactory(conversionService);
|
convertingExpanderFactory = new ConvertingExpanderFactory(conversionService);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TypeDescriptor createTypeDescriptor(Method method, int paramIndex) {
|
private static TypeDescriptor createTypeDescriptor(Method method, int paramIndex) {
|
||||||
@@ -168,15 +173,14 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
if (clz.getInterfaces().length == 0) {
|
if (clz.getInterfaces().length == 0) {
|
||||||
RequestMapping classAnnotation = findMergedAnnotation(clz,
|
RequestMapping classAnnotation = findMergedAnnotation(clz,
|
||||||
RequestMapping.class);
|
RequestMapping.class);
|
||||||
if (classAnnotation != null) {
|
if (classAnnotation != null && classAnnotation.value().length > 0) {
|
||||||
// Prepend path from class annotation if specified
|
// Prepend path from class annotation if specified
|
||||||
if (classAnnotation.value().length > 0) {
|
String pathValue = emptyToNull(classAnnotation.value()[0]);
|
||||||
String pathValue = emptyToNull(classAnnotation.value()[0]);
|
if (pathValue != null) {
|
||||||
pathValue = resolve(pathValue);
|
pathValue = resolve(pathValue);
|
||||||
if (!pathValue.startsWith("/")) {
|
if (!pathValue.equals("/")) {
|
||||||
pathValue = "/" + pathValue;
|
data.template().uri(pathValue);
|
||||||
}
|
}
|
||||||
data.template().uri(pathValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,7 +188,7 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
|
public MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
|
||||||
this.processedMethods.put(Feign.configKey(targetType, method), method);
|
processedMethods.put(Feign.configKey(targetType, method), method);
|
||||||
MethodMetadata md = super.parseAndValidateMetadata(targetType, method);
|
MethodMetadata md = super.parseAndValidateMetadata(targetType, method);
|
||||||
|
|
||||||
RequestMapping classAnnotation = findMergedAnnotation(targetType,
|
RequestMapping classAnnotation = findMergedAnnotation(targetType,
|
||||||
@@ -210,6 +214,12 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
@Override
|
@Override
|
||||||
protected void processAnnotationOnMethod(MethodMetadata data,
|
protected void processAnnotationOnMethod(MethodMetadata data,
|
||||||
Annotation methodAnnotation, Method method) {
|
Annotation methodAnnotation, Method method) {
|
||||||
|
if (CollectionFormat.class.isInstance(methodAnnotation)) {
|
||||||
|
CollectionFormat collectionFormat = findMergedAnnotation(method,
|
||||||
|
CollectionFormat.class);
|
||||||
|
data.template().collectionFormat(collectionFormat.value());
|
||||||
|
}
|
||||||
|
|
||||||
if (!RequestMapping.class.isInstance(methodAnnotation) && !methodAnnotation
|
if (!RequestMapping.class.isInstance(methodAnnotation) && !methodAnnotation
|
||||||
.annotationType().isAnnotationPresent(RequestMapping.class)) {
|
.annotationType().isAnnotationPresent(RequestMapping.class)) {
|
||||||
return;
|
return;
|
||||||
@@ -230,11 +240,9 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
String pathValue = emptyToNull(methodMapping.value()[0]);
|
String pathValue = emptyToNull(methodMapping.value()[0]);
|
||||||
if (pathValue != null) {
|
if (pathValue != null) {
|
||||||
pathValue = resolve(pathValue);
|
pathValue = resolve(pathValue);
|
||||||
// Append path from @RequestMapping if value is present on method
|
if (!pathValue.equals("/")) {
|
||||||
if (!pathValue.startsWith("/") && !data.template().path().endsWith("/")) {
|
data.template().uri(pathValue, true);
|
||||||
pathValue = "/" + pathValue;
|
|
||||||
}
|
}
|
||||||
data.template().uri(pathValue, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,13 +255,13 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
// headers
|
// headers
|
||||||
parseHeaders(data, method, methodMapping);
|
parseHeaders(data, method, methodMapping);
|
||||||
|
|
||||||
data.indexToExpander(new LinkedHashMap<Integer, Param.Expander>());
|
data.indexToExpander(new LinkedHashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String resolve(String value) {
|
private String resolve(String value) {
|
||||||
if (StringUtils.hasText(value)
|
if (StringUtils.hasText(value)
|
||||||
&& this.resourceLoader instanceof ConfigurableApplicationContext) {
|
&& resourceLoader instanceof ConfigurableApplicationContext) {
|
||||||
return ((ConfigurableApplicationContext) this.resourceLoader).getEnvironment()
|
return ((ConfigurableApplicationContext) resourceLoader).getEnvironment()
|
||||||
.resolvePlaceholders(value);
|
.resolvePlaceholders(value);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
@@ -279,9 +287,9 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
|
|
||||||
AnnotatedParameterProcessor.AnnotatedParameterContext context = new SimpleAnnotatedParameterContext(
|
AnnotatedParameterProcessor.AnnotatedParameterContext context = new SimpleAnnotatedParameterContext(
|
||||||
data, paramIndex);
|
data, paramIndex);
|
||||||
Method method = this.processedMethods.get(data.configKey());
|
Method method = processedMethods.get(data.configKey());
|
||||||
for (Annotation parameterAnnotation : annotations) {
|
for (Annotation parameterAnnotation : annotations) {
|
||||||
AnnotatedParameterProcessor processor = this.annotatedArgumentProcessors
|
AnnotatedParameterProcessor processor = annotatedArgumentProcessors
|
||||||
.get(parameterAnnotation.annotationType());
|
.get(parameterAnnotation.annotationType());
|
||||||
if (processor != null) {
|
if (processor != null) {
|
||||||
Annotation processParameterAnnotation;
|
Annotation processParameterAnnotation;
|
||||||
@@ -294,11 +302,11 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isHttpAnnotation && data.indexToExpander().get(paramIndex) == null) {
|
if (!isMultipartFormData(data) && isHttpAnnotation
|
||||||
|
&& data.indexToExpander().get(paramIndex) == null) {
|
||||||
TypeDescriptor typeDescriptor = createTypeDescriptor(method, paramIndex);
|
TypeDescriptor typeDescriptor = createTypeDescriptor(method, paramIndex);
|
||||||
if (this.conversionService.canConvert(typeDescriptor,
|
if (conversionService.canConvert(typeDescriptor, STRING_TYPE_DESCRIPTOR)) {
|
||||||
STRING_TYPE_DESCRIPTOR)) {
|
Param.Expander expander = convertingExpanderFactory
|
||||||
Param.Expander expander = this.convertingExpanderFactory
|
|
||||||
.getExpander(typeDescriptor);
|
.getExpander(typeDescriptor);
|
||||||
if (expander != null) {
|
if (expander != null) {
|
||||||
data.indexToExpander().put(paramIndex, expander);
|
data.indexToExpander().put(paramIndex, expander);
|
||||||
@@ -355,10 +363,12 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
|
|
||||||
List<AnnotatedParameterProcessor> annotatedArgumentResolvers = new ArrayList<>();
|
List<AnnotatedParameterProcessor> annotatedArgumentResolvers = new ArrayList<>();
|
||||||
|
|
||||||
|
annotatedArgumentResolvers.add(new MatrixVariableParameterProcessor());
|
||||||
annotatedArgumentResolvers.add(new PathVariableParameterProcessor());
|
annotatedArgumentResolvers.add(new PathVariableParameterProcessor());
|
||||||
annotatedArgumentResolvers.add(new RequestParamParameterProcessor());
|
annotatedArgumentResolvers.add(new RequestParamParameterProcessor());
|
||||||
annotatedArgumentResolvers.add(new RequestHeaderParameterProcessor());
|
annotatedArgumentResolvers.add(new RequestHeaderParameterProcessor());
|
||||||
annotatedArgumentResolvers.add(new QueryMapParameterProcessor());
|
annotatedArgumentResolvers.add(new QueryMapParameterProcessor());
|
||||||
|
annotatedArgumentResolvers.add(new RequestPartParameterProcessor());
|
||||||
|
|
||||||
return annotatedArgumentResolvers;
|
return annotatedArgumentResolvers;
|
||||||
}
|
}
|
||||||
@@ -389,6 +399,18 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
&& parameterTypes != null && parameterTypes.length > parameterIndex;
|
&& parameterTypes != null && parameterTypes.length > parameterIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isMultipartFormData(MethodMetadata data) {
|
||||||
|
Collection<String> contentTypes = data.template().headers()
|
||||||
|
.get(HttpEncoding.CONTENT_TYPE);
|
||||||
|
|
||||||
|
if (contentTypes != null && !contentTypes.isEmpty()) {
|
||||||
|
String type = contentTypes.iterator().next();
|
||||||
|
return Objects.equals(MediaType.valueOf(type), MediaType.MULTIPART_FORM_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Not used internally anymore. Will be removed in the future.
|
* @deprecated Not used internally anymore. Will be removed in the future.
|
||||||
*/
|
*/
|
||||||
@@ -403,7 +425,7 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String expand(Object value) {
|
public String expand(Object value) {
|
||||||
return this.conversionService.convert(value, String.class);
|
return conversionService.convert(value, String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -418,7 +440,7 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
|
|
||||||
Param.Expander getExpander(TypeDescriptor typeDescriptor) {
|
Param.Expander getExpander(TypeDescriptor typeDescriptor) {
|
||||||
return value -> {
|
return value -> {
|
||||||
Object converted = this.conversionService.convert(value, typeDescriptor,
|
Object converted = conversionService.convert(value, typeDescriptor,
|
||||||
STRING_TYPE_DESCRIPTOR);
|
STRING_TYPE_DESCRIPTOR);
|
||||||
return (String) converted;
|
return (String) converted;
|
||||||
};
|
};
|
||||||
@@ -441,17 +463,17 @@ public class SpringMvcContract extends Contract.BaseContract
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MethodMetadata getMethodMetadata() {
|
public MethodMetadata getMethodMetadata() {
|
||||||
return this.methodMetadata;
|
return methodMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getParameterIndex() {
|
public int getParameterIndex() {
|
||||||
return this.parameterIndex;
|
return parameterIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setParameterName(String name) {
|
public void setParameterName(String name) {
|
||||||
nameParam(this.methodMetadata, name, this.parameterIndex);
|
nameParam(methodMetadata, name, parameterIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+6
@@ -26,6 +26,12 @@
|
|||||||
"description": "Enables the response from Feign to be compressed.",
|
"description": "Enables the response from Feign to be compressed.",
|
||||||
"defaultValue": "false"
|
"defaultValue": "false"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "feign.compression.response.useGzipDecoder",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"description": "Enables the default gzip decoder to be used.",
|
||||||
|
"defaultValue": "false"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "feign.compression.request.enabled",
|
"name": "feign.compression.request.enabled",
|
||||||
"type": "java.lang.Boolean",
|
"type": "java.lang.Boolean",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
org.springframework.cloud.openfeign.ribbon.FeignRibbonClientAutoConfiguration,\
|
org.springframework.cloud.openfeign.hateoas.FeignHalAutoConfiguration,\
|
||||||
org.springframework.cloud.openfeign.FeignAutoConfiguration,\
|
org.springframework.cloud.openfeign.FeignAutoConfiguration,\
|
||||||
org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingAutoConfiguration,\
|
org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingAutoConfiguration,\
|
||||||
org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingAutoConfiguration
|
org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingAutoConfiguration,\
|
||||||
|
org.springframework.cloud.openfeign.loadbalancer.FeignLoadBalancerAutoConfiguration
|
||||||
|
|||||||
+179
@@ -0,0 +1,179 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.openfeign.test.NoSecurityConfiguration;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jaesik Kim
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringBootTest(classes = DefaultGzipDecoderTests.Application.class,
|
||||||
|
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||||
|
value = { "spring.application.name=defaultGzipDecoderTests",
|
||||||
|
"feign.compression.response.enabled=true",
|
||||||
|
"feign.compression.response.useGzipDecoder=true",
|
||||||
|
"feign.client.config.default.loggerLevel=full",
|
||||||
|
"logging.level.org.springframework.cloud.openfeign=DEBUG" })
|
||||||
|
@DirtiesContext
|
||||||
|
public class DefaultGzipDecoderTests extends FeignClientFactoryBean {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
FeignContext context;
|
||||||
|
|
||||||
|
@Value("${local.server.port}")
|
||||||
|
private int port = 0;
|
||||||
|
|
||||||
|
public DefaultGzipDecoderTests() {
|
||||||
|
setName("tests");
|
||||||
|
setContextId("test");
|
||||||
|
}
|
||||||
|
|
||||||
|
public TestClient testClient() {
|
||||||
|
setType(this.getClass());
|
||||||
|
return feign(context).target(TestClient.class, "http://localhost:" + this.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBodyDecompress() {
|
||||||
|
ResponseEntity<Hello> response = testClient().getGzipResponse();
|
||||||
|
assertThat(response).as("response was null").isNotNull();
|
||||||
|
assertThat(response.getStatusCode()).as("wrong status code")
|
||||||
|
.isEqualTo(HttpStatus.OK);
|
||||||
|
Hello hello = response.getBody();
|
||||||
|
assertThat(hello).as("hello was null").isNotNull();
|
||||||
|
assertThat(hello).as("first hello didn't match")
|
||||||
|
.isEqualTo(new Hello("hello world via response"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNullBodyDecompress() {
|
||||||
|
ResponseEntity<Hello> response = testClient().getNullResponse();
|
||||||
|
assertThat(response).as("response was null").isNotNull();
|
||||||
|
assertThat(response.getStatusCode()).as("wrong status code")
|
||||||
|
.isEqualTo(HttpStatus.OK);
|
||||||
|
Hello hello = response.getBody();
|
||||||
|
assertThat(hello).as("hello was not null").isNull();
|
||||||
|
assertThat(hello).as("null hello didn't match").isEqualTo(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCharsetDecompress() {
|
||||||
|
ResponseEntity<Hello> response = testClient().getUtf8Response();
|
||||||
|
assertThat(response).as("response was null").isNotNull();
|
||||||
|
assertThat(response.getStatusCode()).as("wrong status code")
|
||||||
|
.isEqualTo(HttpStatus.OK);
|
||||||
|
Hello hello = response.getBody();
|
||||||
|
assertThat(hello).as("hello was null").isNotNull();
|
||||||
|
assertThat(hello).as("utf8 hello didn't match")
|
||||||
|
.isEqualTo(new Hello("안녕하세요 means Hello in Korean"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Hello {
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
Hello() {
|
||||||
|
}
|
||||||
|
|
||||||
|
Hello(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Hello that = (Hello) o;
|
||||||
|
return Objects.equals(message, that.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected interface TestClient {
|
||||||
|
|
||||||
|
@GetMapping("/helloGzipResponse")
|
||||||
|
ResponseEntity<Hello> getGzipResponse();
|
||||||
|
|
||||||
|
@GetMapping("/nullGzipResponse")
|
||||||
|
ResponseEntity<Hello> getNullResponse();
|
||||||
|
|
||||||
|
@GetMapping("/utf8Response")
|
||||||
|
ResponseEntity<Hello> getUtf8Response();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@RestController
|
||||||
|
@Import(NoSecurityConfiguration.class)
|
||||||
|
protected static class Application implements TestClient {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResponseEntity<Hello> getGzipResponse() {
|
||||||
|
return ResponseEntity.ok(new Hello("hello world via response"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResponseEntity<Hello> getNullResponse() {
|
||||||
|
return ResponseEntity.ok(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResponseEntity<Hello> getUtf8Response() {
|
||||||
|
return ResponseEntity.ok(new Hello("안녕하세요 means Hello in Korean"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+4
-7
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2015 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -21,12 +21,10 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
|
|
||||||
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
|
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
@@ -48,9 +46,8 @@ public class EnableFeignClientsSpringDataTests {
|
|||||||
.cast(this.feignContext.getInstance("foo", Encoder.class));
|
.cast(this.feignContext.getInstance("foo", Encoder.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@Import({ PropertyPlaceholderAutoConfiguration.class, ArchaiusAutoConfiguration.class,
|
@EnableAutoConfiguration
|
||||||
FeignAutoConfiguration.class })
|
|
||||||
protected static class PlainConfiguration {
|
protected static class PlainConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-4
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2015 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -30,7 +30,6 @@ import org.junit.runner.RunWith;
|
|||||||
|
|
||||||
import org.springframework.boot.WebApplicationType;
|
import org.springframework.boot.WebApplicationType;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
|
|
||||||
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
import org.springframework.cloud.openfeign.support.SpringEncoder;
|
||||||
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
||||||
import org.springframework.cloud.test.ClassPathExclusions;
|
import org.springframework.cloud.test.ClassPathExclusions;
|
||||||
@@ -87,8 +86,8 @@ public class EnableFeignClientsTests {
|
|||||||
Feign.Builder.class.cast(this.context.getBeansOfType(Feign.Builder.class).get(0));
|
Feign.Builder.class.cast(this.context.getBeansOfType(Feign.Builder.class).get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@Import({ ArchaiusAutoConfiguration.class, FeignAutoConfiguration.class })
|
@Import({ FeignAutoConfiguration.class })
|
||||||
protected static class PlainConfiguration {
|
protected static class PlainConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+160
@@ -0,0 +1,160 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import feign.Feign;
|
||||||
|
import feign.Logger;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Matt King
|
||||||
|
*/
|
||||||
|
public class FeignBuilderCustomizerTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuilderCustomizer() {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||||
|
FeignBuilderCustomizerTests.SampleConfiguration2.class);
|
||||||
|
|
||||||
|
FeignClientFactoryBean clientFactoryBean = context
|
||||||
|
.getBean(FeignClientFactoryBean.class);
|
||||||
|
FeignContext feignContext = context.getBean(FeignContext.class);
|
||||||
|
|
||||||
|
Feign.Builder builder = clientFactoryBean.feign(feignContext);
|
||||||
|
assertFeignBuilderField(builder, "logLevel", Logger.Level.HEADERS);
|
||||||
|
assertFeignBuilderField(builder, "decode404", true);
|
||||||
|
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertFeignBuilderField(Feign.Builder builder, String fieldName,
|
||||||
|
Object expectedValue) {
|
||||||
|
Field builderField = ReflectionUtils.findField(Feign.Builder.class, fieldName);
|
||||||
|
ReflectionUtils.makeAccessible(builderField);
|
||||||
|
|
||||||
|
Object value = ReflectionUtils.getField(builderField, builder);
|
||||||
|
assertThat(value).as("Expected value for the field '" + fieldName + "':")
|
||||||
|
.isEqualTo(expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildCustomizerOrdered() {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||||
|
FeignBuilderCustomizerTests.SampleConfiguration3.class);
|
||||||
|
|
||||||
|
FeignClientFactoryBean clientFactoryBean = context
|
||||||
|
.getBean(FeignClientFactoryBean.class);
|
||||||
|
FeignContext feignContext = context.getBean(FeignContext.class);
|
||||||
|
|
||||||
|
Feign.Builder builder = clientFactoryBean.feign(feignContext);
|
||||||
|
assertFeignBuilderField(builder, "logLevel", Logger.Level.FULL);
|
||||||
|
assertFeignBuilderField(builder, "decode404", true);
|
||||||
|
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FeignClientFactoryBean defaultFeignClientFactoryBean() {
|
||||||
|
FeignClientFactoryBean feignClientFactoryBean = new FeignClientFactoryBean();
|
||||||
|
feignClientFactoryBean.setContextId("test");
|
||||||
|
feignClientFactoryBean.setName("test");
|
||||||
|
feignClientFactoryBean.setType(FeignClientFactoryTests.TestType.class);
|
||||||
|
feignClientFactoryBean.setPath("");
|
||||||
|
feignClientFactoryBean.setUrl("http://some.absolute.url");
|
||||||
|
return feignClientFactoryBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@Import(FeignClientsConfiguration.class)
|
||||||
|
protected static class SampleConfiguration2 {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignContext feignContext() {
|
||||||
|
return new FeignContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignClientProperties feignClientProperties() {
|
||||||
|
return new FeignClientProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignBuilderCustomizer feignBuilderCustomizer() {
|
||||||
|
return builder -> builder.logLevel(Logger.Level.HEADERS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignBuilderCustomizer feignBuilderCustomizer2() {
|
||||||
|
return Feign.Builder::decode404;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignClientFactoryBean feignClientFactoryBean() {
|
||||||
|
return defaultFeignClientFactoryBean();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@Import(FeignClientsConfiguration.class)
|
||||||
|
protected static class SampleConfiguration3 {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignContext feignContext() {
|
||||||
|
return new FeignContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignClientProperties feignClientProperties() {
|
||||||
|
return new FeignClientProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Order(1)
|
||||||
|
FeignBuilderCustomizer feignBuilderCustomizer() {
|
||||||
|
return builder -> builder.logLevel(Logger.Level.HEADERS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Order(2)
|
||||||
|
FeignBuilderCustomizer feignBuilderCustomizer1() {
|
||||||
|
return builder -> builder.logLevel(Logger.Level.FULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignBuilderCustomizer feignBuilderCustomizer2() {
|
||||||
|
return Feign.Builder::decode404;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignClientFactoryBean feignClientFactoryBean() {
|
||||||
|
return defaultFeignClientFactoryBean();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+32
-9
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -101,11 +101,11 @@ public class FeignClientBuilderTests {
|
|||||||
public void forType_preinitializedBuilder() {
|
public void forType_preinitializedBuilder() {
|
||||||
// when:
|
// when:
|
||||||
final FeignClientBuilder.Builder builder = this.feignClientBuilder
|
final FeignClientBuilder.Builder builder = this.feignClientBuilder
|
||||||
.forType(FeignClientBuilderTests.class, "TestClient");
|
.forType(TestFeignClient.class, "TestClient");
|
||||||
|
|
||||||
// then:
|
// then:
|
||||||
assertFactoryBeanField(builder, "applicationContext", this.applicationContext);
|
assertFactoryBeanField(builder, "applicationContext", this.applicationContext);
|
||||||
assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class);
|
assertFactoryBeanField(builder, "type", TestFeignClient.class);
|
||||||
assertFactoryBeanField(builder, "name", "TestClient");
|
assertFactoryBeanField(builder, "name", "TestClient");
|
||||||
assertFactoryBeanField(builder, "contextId", "TestClient");
|
assertFactoryBeanField(builder, "contextId", "TestClient");
|
||||||
|
|
||||||
@@ -126,21 +126,40 @@ public class FeignClientBuilderTests {
|
|||||||
public void forType_allFieldsSetOnBuilder() {
|
public void forType_allFieldsSetOnBuilder() {
|
||||||
// when:
|
// when:
|
||||||
final FeignClientBuilder.Builder builder = this.feignClientBuilder
|
final FeignClientBuilder.Builder builder = this.feignClientBuilder
|
||||||
.forType(FeignClientBuilderTests.class, "TestClient").decode404(true)
|
.forType(TestFeignClient.class, "TestClient").decode404(true).url("Url/")
|
||||||
.fallback(Object.class).fallbackFactory(Object.class).path("Path/")
|
.path("/Path").contextId("TestContext");
|
||||||
.url("Url/");
|
|
||||||
|
|
||||||
// then:
|
// then:
|
||||||
assertFactoryBeanField(builder, "applicationContext", this.applicationContext);
|
assertFactoryBeanField(builder, "applicationContext", this.applicationContext);
|
||||||
assertFactoryBeanField(builder, "type", FeignClientBuilderTests.class);
|
assertFactoryBeanField(builder, "type", TestFeignClient.class);
|
||||||
assertFactoryBeanField(builder, "name", "TestClient");
|
assertFactoryBeanField(builder, "name", "TestClient");
|
||||||
|
assertFactoryBeanField(builder, "contextId", "TestContext");
|
||||||
|
|
||||||
|
// and:
|
||||||
|
assertFactoryBeanField(builder, "url", "http://Url/");
|
||||||
|
assertFactoryBeanField(builder, "path", "/Path");
|
||||||
|
assertFactoryBeanField(builder, "decode404", true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void forType_clientFactoryBeanProvided() {
|
||||||
|
// when:
|
||||||
|
final FeignClientBuilder.Builder builder = this.feignClientBuilder
|
||||||
|
.forType(TestFeignClient.class, new FeignClientFactoryBean(),
|
||||||
|
"TestClient")
|
||||||
|
.decode404(true).path("Path/").url("Url/").contextId("TestContext");
|
||||||
|
|
||||||
|
// then:
|
||||||
|
assertFactoryBeanField(builder, "applicationContext", this.applicationContext);
|
||||||
|
assertFactoryBeanField(builder, "type", TestFeignClient.class);
|
||||||
|
assertFactoryBeanField(builder, "name", "TestClient");
|
||||||
|
assertFactoryBeanField(builder, "contextId", "TestContext");
|
||||||
|
|
||||||
// and:
|
// and:
|
||||||
assertFactoryBeanField(builder, "url", "http://Url/");
|
assertFactoryBeanField(builder, "url", "http://Url/");
|
||||||
assertFactoryBeanField(builder, "path", "/Path");
|
assertFactoryBeanField(builder, "path", "/Path");
|
||||||
assertFactoryBeanField(builder, "decode404", true);
|
assertFactoryBeanField(builder, "decode404", true);
|
||||||
assertFactoryBeanField(builder, "fallback", Object.class);
|
|
||||||
assertFactoryBeanField(builder, "fallbackFactory", Object.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -159,4 +178,8 @@ public class FeignClientBuilderTests {
|
|||||||
builder.build();
|
builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private interface TestFeignClient {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+172
@@ -0,0 +1,172 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2020-2020 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import feign.Contract;
|
||||||
|
import feign.InvocationHandlerFactory;
|
||||||
|
import feign.RequestLine;
|
||||||
|
import feign.Response;
|
||||||
|
import feign.codec.ErrorDecoder;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Michael Cramer
|
||||||
|
*/
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringBootTest(classes = FeignClientErrorDecoderTests.TestConfiguration.class)
|
||||||
|
@DirtiesContext
|
||||||
|
public class FeignClientErrorDecoderTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FeignContext context;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FooClient foo;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BarClient bar;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void clientsAvailable() {
|
||||||
|
assertThat(this.foo).isNotNull();
|
||||||
|
assertThat(this.bar).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void errorDecoderInConfiguration() {
|
||||||
|
assertThat(this.context.getInstance("foo", ErrorDecoder.class))
|
||||||
|
.isInstanceOf(ErrorDecoder.Default.class);
|
||||||
|
assertThat(this.context.getInstance("bar", ErrorDecoder.class)).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void useConfiguredErrorDecoderWhenAlsoErrorDecoderFactoryIsAvailable() {
|
||||||
|
Object errorDecoder = getErrorDecoderFromClient(this.foo);
|
||||||
|
assertThat(errorDecoder).isInstanceOf(ErrorDecoder.Default.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void useErrorDecoderFromErrorDecoderFactory() {
|
||||||
|
Object errorDecoder = getErrorDecoderFromClient(this.bar);
|
||||||
|
assertThat(errorDecoder).isInstanceOf(ErrorDecoderImpl.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "unchecked", "ConstantConditions" })
|
||||||
|
private Object getErrorDecoderFromClient(final Object client) {
|
||||||
|
Object invocationHandler = ReflectionTestUtils.getField(client, "h");
|
||||||
|
Map<Method, InvocationHandlerFactory.MethodHandler> dispatch = (Map<Method, InvocationHandlerFactory.MethodHandler>) ReflectionTestUtils
|
||||||
|
.getField(invocationHandler, "dispatch");
|
||||||
|
Method key = new ArrayList<>(dispatch.keySet()).get(0);
|
||||||
|
return ReflectionTestUtils.getField(
|
||||||
|
ReflectionTestUtils.getField(dispatch.get(key), "asyncResponseHandler"),
|
||||||
|
"errorDecoder");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@EnableFeignClients(clients = { FooClient.class, BarClient.class })
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
protected static class TestConfiguration {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient(name = "foo", url = "http://foo", configuration = FooConfiguration.class)
|
||||||
|
interface FooClient {
|
||||||
|
|
||||||
|
@RequestLine("GET /")
|
||||||
|
String get();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class FooConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
Contract feignContract() {
|
||||||
|
return new Contract.Default();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignErrorDecoderFactory errorDecoderFactory() {
|
||||||
|
return new FeignErrorDecoderFactoryImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ErrorDecoder feignErrorDecoder() {
|
||||||
|
return new ErrorDecoder.Default();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient(name = "bar", url = "http://bar", configuration = BarConfiguration.class)
|
||||||
|
interface BarClient {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/", method = RequestMethod.GET)
|
||||||
|
String get();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class BarConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
Contract feignContract() {
|
||||||
|
return new SpringMvcContract();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignErrorDecoderFactory errorDecoderFactory() {
|
||||||
|
return new FeignErrorDecoderFactoryImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class FeignErrorDecoderFactoryImpl implements FeignErrorDecoderFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ErrorDecoder create(final Class<?> type) {
|
||||||
|
return new ErrorDecoderImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ErrorDecoderImpl implements ErrorDecoder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Exception decode(final String methodKey, final Response response) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+83
-1
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,14 +16,31 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.openfeign;
|
package org.springframework.cloud.openfeign;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import feign.Client;
|
||||||
|
import feign.InvocationHandlerFactory;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerProperties;
|
||||||
|
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||||
|
import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.springframework.web.bind.annotation.RequestMethod.GET;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Spencer Gibb
|
* @author Spencer Gibb
|
||||||
@@ -49,10 +66,75 @@ public class FeignClientFactoryTests {
|
|||||||
assertThat(foobar).as("bar was not null").isNull();
|
assertThat(foobar).as("bar was not null").isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRedirectToDelegateWhenUrlSet() {
|
||||||
|
new ApplicationContextRunner().withUserConfiguration(TestConfig.class)
|
||||||
|
.run(this::defaultClientUsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "unchecked", "ConstantConditions" })
|
||||||
|
private void defaultClientUsed(AssertableApplicationContext context) {
|
||||||
|
Proxy target = context.getBean(FeignClientFactoryBean.class).getTarget();
|
||||||
|
Object invocationHandler = ReflectionTestUtils.getField(target, "h");
|
||||||
|
Map<Method, InvocationHandlerFactory.MethodHandler> dispatch = (Map<Method, InvocationHandlerFactory.MethodHandler>) ReflectionTestUtils
|
||||||
|
.getField(invocationHandler, "dispatch");
|
||||||
|
Method key = new ArrayList<>(dispatch.keySet()).get(0);
|
||||||
|
Object client = ReflectionTestUtils.getField(dispatch.get(key), "client");
|
||||||
|
assertThat(client).isInstanceOf(Client.Default.class);
|
||||||
|
}
|
||||||
|
|
||||||
private FeignClientSpecification getSpec(String name, Class<?> configClass) {
|
private FeignClientSpecification getSpec(String name, Class<?> configClass) {
|
||||||
return new FeignClientSpecification(name, new Class[] { configClass });
|
return new FeignClientSpecification(name, new Class[] { configClass });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TestType {
|
||||||
|
|
||||||
|
@RequestMapping(value = "/", method = GET)
|
||||||
|
String hello();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class TestConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
BlockingLoadBalancerClient loadBalancerClient() {
|
||||||
|
return new BlockingLoadBalancerClient(new LoadBalancerClientFactory(),
|
||||||
|
new LoadBalancerProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignContext feignContext() {
|
||||||
|
FeignContext feignContext = new FeignContext();
|
||||||
|
feignContext.setConfigurations(
|
||||||
|
Collections.singletonList(new FeignClientSpecification("test",
|
||||||
|
new Class[] { LoadBalancerAutoConfiguration.class })));
|
||||||
|
return feignContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignClientProperties feignClientProperties() {
|
||||||
|
return new FeignClientProperties();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
Targeter targeter() {
|
||||||
|
return new DefaultTargeter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
FeignClientFactoryBean feignClientFactoryBean() {
|
||||||
|
FeignClientFactoryBean feignClientFactoryBean = new FeignClientFactoryBean();
|
||||||
|
feignClientFactoryBean.setContextId("test");
|
||||||
|
feignClientFactoryBean.setName("test");
|
||||||
|
feignClientFactoryBean.setType(TestType.class);
|
||||||
|
feignClientFactoryBean.setPath("");
|
||||||
|
feignClientFactoryBean.setUrl("http://some.absolute.url");
|
||||||
|
return feignClientFactoryBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static class FooConfig {
|
static class FooConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
+39
-20
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,8 +17,9 @@
|
|||||||
package org.springframework.cloud.openfeign;
|
package org.springframework.cloud.openfeign;
|
||||||
|
|
||||||
import feign.Contract;
|
import feign.Contract;
|
||||||
import feign.Feign;
|
import feign.ExceptionPropagationPolicy;
|
||||||
import feign.Logger;
|
import feign.Logger;
|
||||||
|
import feign.QueryMapEncoder;
|
||||||
import feign.Request;
|
import feign.Request;
|
||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
import feign.RequestLine;
|
import feign.RequestLine;
|
||||||
@@ -28,21 +29,19 @@ import feign.auth.BasicAuthRequestInterceptor;
|
|||||||
import feign.codec.Decoder;
|
import feign.codec.Decoder;
|
||||||
import feign.codec.Encoder;
|
import feign.codec.Encoder;
|
||||||
import feign.codec.ErrorDecoder;
|
import feign.codec.ErrorDecoder;
|
||||||
import feign.hystrix.HystrixFeign;
|
|
||||||
import feign.optionals.OptionalDecoder;
|
import feign.optionals.OptionalDecoder;
|
||||||
|
import feign.querymap.BeanQueryMapEncoder;
|
||||||
import feign.slf4j.Slf4jLogger;
|
import feign.slf4j.Slf4jLogger;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
|
|
||||||
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
|
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
|
||||||
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
import org.springframework.cloud.openfeign.support.SpringMvcContract;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -118,13 +117,6 @@ public class FeignClientOverrideDefaultsTests {
|
|||||||
.cast(this.context.getInstance("bar", ErrorDecoder.class));
|
.cast(this.context.getInstance("bar", ErrorDecoder.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void overrideBuilder() {
|
|
||||||
HystrixFeign.Builder.class
|
|
||||||
.cast(this.context.getInstance("foo", Feign.Builder.class));
|
|
||||||
Feign.Builder.class.cast(this.context.getInstance("bar", Feign.Builder.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void overrideRequestOptions() {
|
public void overrideRequestOptions() {
|
||||||
assertThat(this.context.getInstance("foo", Request.Options.class)).isNull();
|
assertThat(this.context.getInstance("foo", Request.Options.class)).isNull();
|
||||||
@@ -133,6 +125,14 @@ public class FeignClientOverrideDefaultsTests {
|
|||||||
assertThat(options.readTimeoutMillis()).isEqualTo(1);
|
assertThat(options.readTimeoutMillis()).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void overrideQueryMapEncoder() {
|
||||||
|
QueryMapEncoder.Default.class
|
||||||
|
.cast(this.context.getInstance("foo", QueryMapEncoder.class));
|
||||||
|
BeanQueryMapEncoder.class
|
||||||
|
.cast(this.context.getInstance("bar", QueryMapEncoder.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addRequestInterceptor() {
|
public void addRequestInterceptor() {
|
||||||
assertThat(this.context.getInstances("foo", RequestInterceptor.class).size())
|
assertThat(this.context.getInstances("foo", RequestInterceptor.class).size())
|
||||||
@@ -141,7 +141,16 @@ public class FeignClientOverrideDefaultsTests {
|
|||||||
.isEqualTo(2);
|
.isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@FeignClient(name = "foo", url = "http://foo", configuration = FooConfiguration.class)
|
@Test
|
||||||
|
public void exceptionPropagationPolicy() {
|
||||||
|
assertThat(this.context.getInstances("foo", ExceptionPropagationPolicy.class))
|
||||||
|
.isNull();
|
||||||
|
assertThat(this.context.getInstances("bar", ExceptionPropagationPolicy.class))
|
||||||
|
.containsValues(ExceptionPropagationPolicy.UNWRAP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient(name = "foo", url = "https://foo",
|
||||||
|
configuration = FooConfiguration.class)
|
||||||
interface FooClient {
|
interface FooClient {
|
||||||
|
|
||||||
@RequestLine("GET /")
|
@RequestLine("GET /")
|
||||||
@@ -149,7 +158,8 @@ public class FeignClientOverrideDefaultsTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FeignClient(name = "bar", url = "http://bar", configuration = BarConfiguration.class)
|
@FeignClient(name = "bar", url = "https://bar",
|
||||||
|
configuration = BarConfiguration.class)
|
||||||
interface BarClient {
|
interface BarClient {
|
||||||
|
|
||||||
@RequestMapping(value = "/", method = RequestMethod.GET)
|
@RequestMapping(value = "/", method = RequestMethod.GET)
|
||||||
@@ -157,10 +167,9 @@ public class FeignClientOverrideDefaultsTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableFeignClients(clients = { FooClient.class, BarClient.class })
|
@EnableFeignClients(clients = { FooClient.class, BarClient.class })
|
||||||
@Import({ PropertyPlaceholderAutoConfiguration.class, ArchaiusAutoConfiguration.class,
|
@EnableAutoConfiguration
|
||||||
FeignAutoConfiguration.class })
|
|
||||||
protected static class TestConfiguration {
|
protected static class TestConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@@ -197,8 +206,8 @@ public class FeignClientOverrideDefaultsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Feign.Builder feignBuilder() {
|
public QueryMapEncoder queryMapEncoder() {
|
||||||
return HystrixFeign.builder();
|
return new feign.QueryMapEncoder.Default();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -230,6 +239,16 @@ public class FeignClientOverrideDefaultsTests {
|
|||||||
return new BasicAuthRequestInterceptor("user", "pass");
|
return new BasicAuthRequestInterceptor("user", "pass");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public QueryMapEncoder queryMapEncoder() {
|
||||||
|
return new BeanQueryMapEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ExceptionPropagationPolicy exceptionPropagationPolicy() {
|
||||||
|
return ExceptionPropagationPolicy.UNWRAP;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+149
@@ -0,0 +1,149 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2013-2019 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
|
||||||
|
*
|
||||||
|
* https://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.openfeign;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import feign.Feign;
|
||||||
|
import feign.Logger;
|
||||||
|
import feign.RequestInterceptor;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author matt king
|
||||||
|
*/
|
||||||
|
@DirtiesContext
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@SpringBootTest(classes = FeignClientUsingConfigurerTest.Application.class, value = {
|
||||||
|
"feign.client.config.default.loggerLevel=full",
|
||||||
|
"feign.client.config.default.requestInterceptors[0]=org.springframework.cloud.openfeign.FeignClientUsingPropertiesTests.FooRequestInterceptor",
|
||||||
|
"feign.client.config.default.requestInterceptors[1]=org.springframework.cloud.openfeign.FeignClientUsingPropertiesTests.BarRequestInterceptor" })
|
||||||
|
public class FeignClientUsingConfigurerTest {
|
||||||
|
|
||||||
|
private static final String BEAN_NAME_PREFIX = "&org.springframework.cloud.openfeign.FeignClientUsingConfigurerTest$";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FeignContext context;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFeignClient() {
|
||||||
|
FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) applicationContext
|
||||||
|
.getBean(BEAN_NAME_PREFIX + "TestFeignClient");
|
||||||
|
Feign.Builder builder = factoryBean.feign(context);
|
||||||
|
|
||||||
|
List<RequestInterceptor> interceptors = (List) getBuilderValue(builder,
|
||||||
|
"requestInterceptors");
|
||||||
|
assertThat(interceptors.size()).as("interceptors not set").isEqualTo(3);
|
||||||
|
assertThat(getBuilderValue(builder, "logLevel")).as("log level not set")
|
||||||
|
.isEqualTo(Logger.Level.FULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getBuilderValue(Feign.Builder builder, String member) {
|
||||||
|
Field builderField = ReflectionUtils.findField(Feign.Builder.class, member);
|
||||||
|
ReflectionUtils.makeAccessible(builderField);
|
||||||
|
|
||||||
|
return ReflectionUtils.getField(builderField, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoInheritFeignClient() {
|
||||||
|
FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) applicationContext
|
||||||
|
.getBean(BEAN_NAME_PREFIX + "NoInheritFeignClient");
|
||||||
|
Feign.Builder builder = factoryBean.feign(context);
|
||||||
|
|
||||||
|
List<RequestInterceptor> interceptors = (List) getBuilderValue(builder,
|
||||||
|
"requestInterceptors");
|
||||||
|
|
||||||
|
assertThat(interceptors).as("interceptors not set").isEmpty();
|
||||||
|
assertThat(factoryBean.isInheritParentContext())
|
||||||
|
.as("is inheriting from parent configuration").isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoInheritFeignClient_ignoreProperties() {
|
||||||
|
FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) applicationContext
|
||||||
|
.getBean(BEAN_NAME_PREFIX + "NoInheritFeignClient");
|
||||||
|
Feign.Builder builder = factoryBean.feign(context);
|
||||||
|
|
||||||
|
assertThat(getBuilderValue(builder, "logLevel")).as("log level not set")
|
||||||
|
.isEqualTo(Logger.Level.HEADERS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@EnableFeignClients(clients = { TestFeignClient.class, NoInheritFeignClient.class })
|
||||||
|
protected static class Application {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RequestInterceptor requestInterceptor() {
|
||||||
|
return requestTemplate -> {
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class NoInheritConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Logger.Level logLevel() {
|
||||||
|
return Logger.Level.HEADERS;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FeignClientConfigurer feignClientConfigurer() {
|
||||||
|
return new FeignClientConfigurer() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean inheritParentConfiguration() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient("testFeignClient")
|
||||||
|
interface TestFeignClient {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@FeignClient(name = "noInheritFeignClient",
|
||||||
|
configuration = NoInheritConfiguration.class)
|
||||||
|
interface NoInheritFeignClient {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+111
-25
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,12 +16,19 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.openfeign;
|
package org.springframework.cloud.openfeign;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import feign.InvocationHandlerFactory;
|
||||||
import feign.Request;
|
import feign.Request;
|
||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
import feign.RequestTemplate;
|
import feign.RequestTemplate;
|
||||||
@@ -46,20 +53,24 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.fail;
|
import static org.assertj.core.api.Assertions.fail;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Eko Kurniawan Khannedy
|
* @author Eko Kurniawan Khannedy
|
||||||
|
* @author Olga Maciaszek-Sharma
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("FieldMayBeFinal")
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest(classes = FeignClientUsingPropertiesTests.Application.class, webEnvironment = RANDOM_PORT)
|
@SpringBootTest(classes = FeignClientUsingPropertiesTests.Application.class,
|
||||||
|
webEnvironment = RANDOM_PORT)
|
||||||
@TestPropertySource("classpath:feign-properties.properties")
|
@TestPropertySource("classpath:feign-properties.properties")
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
public class FeignClientUsingPropertiesTests {
|
public class FeignClientUsingPropertiesTests {
|
||||||
@@ -77,38 +88,50 @@ public class FeignClientUsingPropertiesTests {
|
|||||||
|
|
||||||
private FeignClientFactoryBean barFactoryBean;
|
private FeignClientFactoryBean barFactoryBean;
|
||||||
|
|
||||||
|
private FeignClientFactoryBean unwrapFactoryBean;
|
||||||
|
|
||||||
private FeignClientFactoryBean formFactoryBean;
|
private FeignClientFactoryBean formFactoryBean;
|
||||||
|
|
||||||
public FeignClientUsingPropertiesTests() {
|
public FeignClientUsingPropertiesTests() {
|
||||||
this.fooFactoryBean = new FeignClientFactoryBean();
|
fooFactoryBean = new FeignClientFactoryBean();
|
||||||
this.fooFactoryBean.setContextId("foo");
|
fooFactoryBean.setContextId("foo");
|
||||||
this.fooFactoryBean.setType(FeignClientFactoryBean.class);
|
fooFactoryBean.setType(FeignClientFactoryBean.class);
|
||||||
|
|
||||||
this.barFactoryBean = new FeignClientFactoryBean();
|
barFactoryBean = new FeignClientFactoryBean();
|
||||||
this.barFactoryBean.setContextId("bar");
|
barFactoryBean.setContextId("bar");
|
||||||
this.barFactoryBean.setType(FeignClientFactoryBean.class);
|
barFactoryBean.setType(FeignClientFactoryBean.class);
|
||||||
|
|
||||||
this.formFactoryBean = new FeignClientFactoryBean();
|
unwrapFactoryBean = new FeignClientFactoryBean();
|
||||||
this.formFactoryBean.setContextId("form");
|
unwrapFactoryBean.setContextId("unwrap");
|
||||||
this.formFactoryBean.setType(FeignClientFactoryBean.class);
|
unwrapFactoryBean.setType(FeignClientFactoryBean.class);
|
||||||
|
|
||||||
|
formFactoryBean = new FeignClientFactoryBean();
|
||||||
|
formFactoryBean.setContextId("form");
|
||||||
|
formFactoryBean.setType(FeignClientFactoryBean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FooClient fooClient() {
|
public FooClient fooClient() {
|
||||||
this.fooFactoryBean.setApplicationContext(this.applicationContext);
|
fooFactoryBean.setApplicationContext(applicationContext);
|
||||||
return this.fooFactoryBean.feign(this.context).target(FooClient.class,
|
return fooFactoryBean.feign(context).target(FooClient.class,
|
||||||
"http://localhost:" + this.port);
|
"http://localhost:" + port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BarClient barClient() {
|
public BarClient barClient() {
|
||||||
this.barFactoryBean.setApplicationContext(this.applicationContext);
|
barFactoryBean.setApplicationContext(applicationContext);
|
||||||
return this.barFactoryBean.feign(this.context).target(BarClient.class,
|
return barFactoryBean.feign(context).target(BarClient.class,
|
||||||
"http://localhost:" + this.port);
|
"http://localhost:" + port);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnwrapClient unwrapClient() {
|
||||||
|
unwrapFactoryBean.setApplicationContext(applicationContext);
|
||||||
|
return unwrapFactoryBean.feign(context).target(UnwrapClient.class,
|
||||||
|
"http://localhost:" + port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FormClient formClient() {
|
public FormClient formClient() {
|
||||||
this.formFactoryBean.setApplicationContext(this.applicationContext);
|
formFactoryBean.setApplicationContext(applicationContext);
|
||||||
return this.formFactoryBean.feign(this.context).target(FormClient.class,
|
return formFactoryBean.feign(context).target(FormClient.class,
|
||||||
"http://localhost:" + this.port);
|
"http://localhost:" + port);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -123,6 +146,12 @@ public class FeignClientUsingPropertiesTests {
|
|||||||
fail("it should timeout");
|
fail("it should timeout");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = SocketTimeoutException.class)
|
||||||
|
public void testUnwrap() throws Exception {
|
||||||
|
unwrapClient().unwrap();
|
||||||
|
fail("it should timeout");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testForm() {
|
public void testForm() {
|
||||||
Map<String, String> request = Collections.singletonMap("form", "Data");
|
Map<String, String> request = Collections.singletonMap("form", "Data");
|
||||||
@@ -130,6 +159,47 @@ public class FeignClientUsingPropertiesTests {
|
|||||||
assertThat(response).isEqualTo("Data");
|
assertThat(response).isEqualTo("Data");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void readTimeoutShouldWorkWhenConnectTimeoutNotSet() {
|
||||||
|
FeignClientFactoryBean readTimeoutFactoryBean = new FeignClientFactoryBean();
|
||||||
|
readTimeoutFactoryBean.setContextId("readTimeout");
|
||||||
|
readTimeoutFactoryBean.setType(FeignClientFactoryBean.class);
|
||||||
|
readTimeoutFactoryBean.setApplicationContext(applicationContext);
|
||||||
|
|
||||||
|
TimeoutClient client = readTimeoutFactoryBean.feign(context)
|
||||||
|
.target(TimeoutClient.class, "http://localhost:" + port);
|
||||||
|
|
||||||
|
Request.Options options = getRequestOptions((Proxy) client);
|
||||||
|
|
||||||
|
assertThat(options.readTimeoutMillis()).isEqualTo(1000);
|
||||||
|
assertThat(options.connectTimeoutMillis()).isEqualTo(5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectTimeoutShouldWorkWhenReadTimeoutNotSet() {
|
||||||
|
FeignClientFactoryBean readTimeoutFactoryBean = new FeignClientFactoryBean();
|
||||||
|
readTimeoutFactoryBean.setContextId("connectTimeout");
|
||||||
|
readTimeoutFactoryBean.setType(FeignClientFactoryBean.class);
|
||||||
|
readTimeoutFactoryBean.setApplicationContext(applicationContext);
|
||||||
|
|
||||||
|
TimeoutClient client = readTimeoutFactoryBean.feign(context)
|
||||||
|
.target(TimeoutClient.class, "http://localhost:" + port);
|
||||||
|
|
||||||
|
Request.Options options = getRequestOptions((Proxy) client);
|
||||||
|
|
||||||
|
assertThat(options.connectTimeoutMillis()).isEqualTo(1000);
|
||||||
|
assertThat(options.readTimeoutMillis()).isEqualTo(5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Request.Options getRequestOptions(Proxy client) {
|
||||||
|
Object invocationHandler = ReflectionTestUtils.getField(client, "h");
|
||||||
|
Map<Method, InvocationHandlerFactory.MethodHandler> dispatch = (Map<Method, InvocationHandlerFactory.MethodHandler>) ReflectionTestUtils
|
||||||
|
.getField(Objects.requireNonNull(invocationHandler), "dispatch");
|
||||||
|
Method key = new ArrayList<>(dispatch.keySet()).get(0);
|
||||||
|
return (Request.Options) ReflectionTestUtils.getField(dispatch.get(key),
|
||||||
|
"options");
|
||||||
|
}
|
||||||
|
|
||||||
protected interface FooClient {
|
protected interface FooClient {
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/foo")
|
@RequestMapping(method = RequestMethod.GET, value = "/foo")
|
||||||
@@ -144,14 +214,29 @@ public class FeignClientUsingPropertiesTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected interface UnwrapClient {
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, value = "/bar") // intentionally /bar
|
||||||
|
String unwrap() throws IOException;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected interface FormClient {
|
protected interface FormClient {
|
||||||
|
|
||||||
@RequestMapping(value = "/form", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
@RequestMapping(value = "/form", method = RequestMethod.POST,
|
||||||
|
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||||
String form(Map<String, String> form);
|
String form(Map<String, String> form);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
protected interface TimeoutClient {
|
||||||
|
|
||||||
|
@GetMapping("/timeouts")
|
||||||
|
String timeouts();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@RestController
|
@RestController
|
||||||
@Import(NoSecurityConfiguration.class)
|
@Import(NoSecurityConfiguration.class)
|
||||||
@@ -174,7 +259,8 @@ public class FeignClientUsingPropertiesTests {
|
|||||||
return "OK";
|
return "OK";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/form", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
@RequestMapping(value = "/form", method = RequestMethod.POST,
|
||||||
|
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|
||||||
public String form(HttpServletRequest request) {
|
public String form(HttpServletRequest request) {
|
||||||
return request.getParameter("form");
|
return request.getParameter("form");
|
||||||
}
|
}
|
||||||
@@ -230,7 +316,7 @@ public class FeignClientUsingPropertiesTests {
|
|||||||
|
|
||||||
requestTemplate.header(HttpHeaders.CONTENT_TYPE,
|
requestTemplate.header(HttpHeaders.CONTENT_TYPE,
|
||||||
MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
||||||
requestTemplate.body(Request.Body.bodyTemplate(builder.toString(), UTF_8));
|
requestTemplate.body(builder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-10
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -37,7 +37,7 @@ public class FeignClientsRegistrarTests {
|
|||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void badNameHttpPrefix() {
|
public void badNameHttpPrefix() {
|
||||||
testGetName("http://bad_hostname");
|
testGetName("https://bad_hostname");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
@@ -63,8 +63,8 @@ public class FeignClientsRegistrarTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void goodNameHttpPrefix() {
|
public void goodNameHttpPrefix() {
|
||||||
String name = testGetName("http://good-name");
|
String name = testGetName("https://good-name");
|
||||||
assertThat(name).as("name was wrong").isEqualTo("http://good-name");
|
assertThat(name).as("name was wrong").isEqualTo("https://good-name");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -89,7 +89,8 @@ public class FeignClientsRegistrarTests {
|
|||||||
new AnnotationConfigApplicationContext(FallbackFactoryTestConfig.class);
|
new AnnotationConfigApplicationContext(FallbackFactoryTestConfig.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@FeignClient(name = "fallbackTestClient", url = "http://localhost:8080/", fallback = FallbackClient.class)
|
@FeignClient(name = "fallbackTestClient", url = "http://localhost:8080/",
|
||||||
|
fallback = FallbackClient.class)
|
||||||
protected interface FallbackClient {
|
protected interface FallbackClient {
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/hello")
|
@RequestMapping(method = RequestMethod.GET, value = "/hello")
|
||||||
@@ -97,7 +98,8 @@ public class FeignClientsRegistrarTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@FeignClient(name = "fallbackFactoryTestClient", url = "http://localhost:8081/", fallbackFactory = FallbackFactoryClient.class)
|
@FeignClient(name = "fallbackFactoryTestClient", url = "http://localhost:8081/",
|
||||||
|
fallbackFactory = FallbackFactoryClient.class)
|
||||||
protected interface FallbackFactoryClient {
|
protected interface FallbackFactoryClient {
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/hello")
|
@RequestMapping(method = RequestMethod.GET, value = "/hello")
|
||||||
@@ -105,17 +107,17 @@ public class FeignClientsRegistrarTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@EnableFeignClients(clients = { FeignClientsRegistrarTests.FallbackClient.class })
|
@EnableFeignClients(clients = { FeignClientsRegistrarTests.FallbackClient.class })
|
||||||
protected static class FallbackTestConfig {
|
protected static class FallbackTestConfig {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@EnableFeignClients(clients = {
|
@EnableFeignClients(
|
||||||
FeignClientsRegistrarTests.FallbackFactoryClient.class })
|
clients = { FeignClientsRegistrarTests.FallbackFactoryClient.class })
|
||||||
protected static class FallbackFactoryTestConfig {
|
protected static class FallbackFactoryTestConfig {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-5
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2013-2019 the original author or authors.
|
* Copyright 2013-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -28,7 +28,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.cloud.commons.httpclient.HttpClientConfiguration;
|
import org.springframework.cloud.commons.httpclient.HttpClientConfiguration;
|
||||||
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
|
|
||||||
import org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingAutoConfiguration;
|
import org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingAutoConfiguration;
|
||||||
import org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingInterceptor;
|
import org.springframework.cloud.openfeign.encoding.FeignAcceptGzipEncodingInterceptor;
|
||||||
import org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingAutoConfiguration;
|
import org.springframework.cloud.openfeign.encoding.FeignContentGzipEncodingAutoConfiguration;
|
||||||
@@ -54,8 +53,7 @@ public class FeignCompressionTests {
|
|||||||
.withPropertyValues("feign.compression.response.enabled=true",
|
.withPropertyValues("feign.compression.response.enabled=true",
|
||||||
"feign.compression.request.enabled=true",
|
"feign.compression.request.enabled=true",
|
||||||
"feign.okhttp.enabled=false")
|
"feign.okhttp.enabled=false")
|
||||||
.withConfiguration(AutoConfigurations.of(ArchaiusAutoConfiguration.class,
|
.withConfiguration(AutoConfigurations.of(FeignAutoConfiguration.class,
|
||||||
FeignAutoConfiguration.class,
|
|
||||||
FeignContentGzipEncodingAutoConfiguration.class,
|
FeignContentGzipEncodingAutoConfiguration.class,
|
||||||
FeignAcceptGzipEncodingAutoConfiguration.class,
|
FeignAcceptGzipEncodingAutoConfiguration.class,
|
||||||
HttpClientConfiguration.class, PlainConfig.class))
|
HttpClientConfiguration.class, PlainConfig.class))
|
||||||
@@ -71,7 +69,7 @@ public class FeignCompressionTests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
protected static class PlainConfig {
|
protected static class PlainConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user