Browse Source

Supports context path when using Ribbon LoadBalancingTarget (#433)

Before, `LoadBalancingTarget` stripped out the path and only used
`URI.getScheme()` and `URI.getHost()` to generate the `Request`.

Now, it will add `URI.getPath()` to the `Request` as well; this is
useful if you want to interact with endpoints with a context-path.

Update README.md Travis CI to point to correct git repository
pull/436/head
Mike Liu 8 years ago committed by Adrian Cole
parent
commit
30753bf250
  1. 3
      CHANGELOG.md
  2. 2
      README.md
  3. 31
      ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java
  4. 28
      ribbon/src/test/java/feign/ribbon/LoadBalancingTargetTest.java

3
CHANGELOG.md

@ -1,3 +1,6 @@ @@ -1,3 +1,6 @@
### Version 9.2
* Supports context path when using Ribbon `LoadBalancingTarget`
### Version 9.1
* Allows query parameters to match on a substring. Ex `q=body:{body}`

2
README.md

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
# Feign makes writing java http clients easier
[![Join the chat at https://gitter.im/Netflix/feign](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Netflix/feign?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://travis-ci.org/Netflix/feign.svg?branch=master)](https://travis-ci.org/Netflix/feign)
[![Build Status](https://travis-ci.org/Netflix/feign.svg?branch=master)](https://travis-ci.org/OpenFeign/feign)
Feign is a java to http client binder inspired by [Retrofit](https://github.com/square/retrofit), [JAXRS-2.0](https://jax-rs-spec.java.net/nonav/2.0/apidocs/index.html), and [WebSocket](http://www.oracle.com/technetwork/articles/java/jsr356-1937161.html). Feign's first goal was reducing the complexity of binding [Denominator](https://github.com/Netflix/Denominator) uniformly to http apis regardless of [restfulness](http://www.slideshare.net/adrianfcole/99problems).

31
ribbon/src/main/java/feign/ribbon/LoadBalancingTarget.java

@ -45,26 +45,41 @@ public class LoadBalancingTarget<T> implements Target<T> { @@ -45,26 +45,41 @@ public class LoadBalancingTarget<T> implements Target<T> {
private final String name;
private final String scheme;
private final String path;
private final Class<T> type;
private final AbstractLoadBalancer lb;
/**
* @Deprecated will be removed in Feign 10
*/
@Deprecated
protected LoadBalancingTarget(Class<T> type, String scheme, String name) {
this.type = checkNotNull(type, "type");
this.scheme = checkNotNull(scheme, "scheme");
this.name = checkNotNull(name, "name");
this.path = "";
this.lb = AbstractLoadBalancer.class.cast(getNamedLoadBalancer(name()));
}
protected LoadBalancingTarget(Class<T> type, String scheme, String name, String path) {
this.type = checkNotNull(type, "type");
this.scheme = checkNotNull(scheme, "scheme");
this.name = checkNotNull(name, "name");
this.path = checkNotNull(path, "path");
this.lb = AbstractLoadBalancer.class.cast(getNamedLoadBalancer(name()));
}
/**
* creates a target which dynamically derives urls from a {@link com.netflix.loadbalancer.ILoadBalancer
* Creates a target which dynamically derives urls from a {@link com.netflix.loadbalancer.ILoadBalancer
* loadbalancer}.
*
* @param type corresponds to {@link feign.Target#type()}
* @param schemeName naming convention is {@code https://name} or {@code http://name} where name
* @param url naming convention is {@code https://name} or {@code http://name/api/v2} where name
* corresponds to {@link com.netflix.client.ClientFactory#getNamedLoadBalancer(String)}
*/
public static <T> LoadBalancingTarget<T> create(Class<T> type, String schemeName) {
URI asUri = URI.create(schemeName);
return new LoadBalancingTarget<T>(type, asUri.getScheme(), asUri.getHost());
public static <T> LoadBalancingTarget<T> create(Class<T> type, String url) {
URI asUri = URI.create(url);
return new LoadBalancingTarget<T>(type, asUri.getScheme(), asUri.getHost(), asUri.getPath());
}
@Override
@ -79,7 +94,7 @@ public class LoadBalancingTarget<T> implements Target<T> { @@ -79,7 +94,7 @@ public class LoadBalancingTarget<T> implements Target<T> {
@Override
public String url() {
return name;
return String.format("%s://%s", scheme, path);
}
/**
@ -92,7 +107,7 @@ public class LoadBalancingTarget<T> implements Target<T> { @@ -92,7 +107,7 @@ public class LoadBalancingTarget<T> implements Target<T> {
@Override
public Request apply(RequestTemplate input) {
Server currentServer = lb.chooseServer(null);
String url = format("%s://%s", scheme, currentServer.getHostPort());
String url = format("%s://%s%s", scheme, currentServer.getHostPort(), path);
input.insert(0, url);
try {
return input.request();
@ -121,6 +136,6 @@ public class LoadBalancingTarget<T> implements Target<T> { @@ -121,6 +136,6 @@ public class LoadBalancingTarget<T> implements Target<T> {
@Override
public String toString() {
return "LoadBalancingTarget(type=" + type.getSimpleName() + ", name=" + name + ")";
return "LoadBalancingTarget(type=" + type.getSimpleName() + ", name=" + name + ", path=" + path + ")";
}
}

28
ribbon/src/test/java/feign/ribbon/LoadBalancingTargetTest.java

@ -71,10 +71,38 @@ public class LoadBalancingTargetTest { @@ -71,10 +71,38 @@ public class LoadBalancingTargetTest {
getConfigInstance().clearProperty(serverListKey);
}
}
@Test
public void loadBalancingTargetPath() throws InterruptedException {
String name = "LoadBalancingTargetTest-loadBalancingDefaultPolicyRoundRobin";
String serverListKey = name + ".ribbon.listOfServers";
server1.enqueue(new MockResponse().setBody("success!"));
getConfigInstance().setProperty(serverListKey,
hostAndPort(server1.url("").url()));
try {
LoadBalancingTarget<TestInterface>
target =
LoadBalancingTarget.create(TestInterface.class, "http://" + name + "/context-path");
TestInterface api = Feign.builder().target(target);
api.get();
assertEquals("http:///context-path", target.url());
assertEquals("/context-path/servers", server1.takeRequest().getPath());
} finally {
getConfigInstance().clearProperty(serverListKey);
}
}
interface TestInterface {
@RequestLine("POST /")
void post();
@RequestLine("GET /servers")
void get();
}
}

Loading…
Cancel
Save