Skip to content

Commit cc75132

Browse files
author
Allen Wang
committed
Change rxnetty version to 0.3.6. Implementation of ResourceGroup. Change to ConfigurationBasedServerList to make the server list part of client configuration.
1 parent 0ea9da4 commit cc75132

File tree

20 files changed

+268
-173
lines changed

20 files changed

+268
-173
lines changed

build.gradle

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ project(':ribbon-transport') {
7272
compile project(':ribbon-core')
7373
compile project(':ribbon-loadbalancer')
7474
compile project(':ribbon-core')
75-
compile 'com.netflix.rxnetty:rx-netty:0.3.5'
76-
compile 'com.netflix.rxnetty:rx-netty-contexts:0.3.5'
75+
compile 'com.netflix.rxnetty:rx-netty:0.3.6'
76+
compile 'com.netflix.rxnetty:rx-netty-contexts:0.3.6'
7777
testCompile 'com.google.mockwebserver:mockwebserver:20130706'
7878
testCompile project(':ribbon-test')
7979
}
@@ -141,6 +141,7 @@ project(':ribbon-client-extensions') {
141141
compile 'com.netflix.hystrix:hystrix-core:1.4.0-RC4'
142142
compile 'com.netflix.evcache:evcache-client:1.0.5'
143143
compile project(':ribbon-transport')
144+
compile project(':ribbon-eureka')
144145
testCompile 'com.google.mockwebserver:mockwebserver:20130706'
145146

146147
}

ribbon-client-extensions/src/examples/java/com/netflix/ribbonclientextensions/RibbonExamples.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class RibbonExamples {
2121
public static void main(String[] args) {
2222
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient");
23-
HttpRequestTemplate<ByteBuf> template = group.requestTemplateBuilder().newRequestTemplate("GetUser")
23+
HttpRequestTemplate<ByteBuf> template = group.newRequestTemplate("GetUser")
2424
.withResponseValidator(new ResponseValidator<HttpClientResponse<ByteBuf>>() {
2525

2626
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.netflix.ribbonclientextensions;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
import com.netflix.client.config.CommonClientConfigKey;
8+
import com.netflix.client.config.IClientConfigKey;
9+
import com.netflix.loadbalancer.Server;
10+
import com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList;
11+
12+
public final class ClientOptions {
13+
14+
private Map<IClientConfigKey<?>, Object> options;
15+
16+
private ClientOptions() {
17+
options = new ConcurrentHashMap<IClientConfigKey<?>, Object>();
18+
}
19+
20+
public static ClientOptions create() {
21+
return new ClientOptions();
22+
}
23+
24+
public ClientOptions useEurekaDynamicServerList(String vipAddress) {
25+
options.put(IClientConfigKey.CommonKeys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
26+
options.put(IClientConfigKey.CommonKeys.DeploymentContextBasedVipAddresses, vipAddress);
27+
return this;
28+
}
29+
30+
public ClientOptions useConfigurationBasedServerList(String serverList) {
31+
options.put(IClientConfigKey.CommonKeys.ListOfServers, serverList);
32+
return this;
33+
}
34+
35+
public ClientOptions withMaxAutoRetries(int value) {
36+
options.put(IClientConfigKey.CommonKeys.MaxAutoRetries, value);
37+
return this;
38+
}
39+
40+
public ClientOptions withMaxAutoRetriesNextServer(int value) {
41+
options.put(IClientConfigKey.CommonKeys.MaxAutoRetriesNextServer, value);
42+
return this;
43+
}
44+
45+
public ClientOptions withRetryOnAllOperations(boolean value) {
46+
options.put(IClientConfigKey.CommonKeys.OkToRetryOnAllOperations, value);
47+
return this;
48+
}
49+
50+
public ClientOptions withMaxConnectionsPerHost(int value) {
51+
options.put(IClientConfigKey.CommonKeys.MaxConnectionsPerHost, value);
52+
return this;
53+
}
54+
55+
public ClientOptions withMaxTotalConnections(int value) {
56+
options.put(IClientConfigKey.CommonKeys.MaxTotalConnections, value);
57+
return this;
58+
}
59+
60+
public ClientOptions withConnectTimeout(int value) {
61+
options.put(IClientConfigKey.CommonKeys.ConnectTimeout, value);
62+
return this;
63+
}
64+
65+
public ClientOptions withReadTimeout(int value) {
66+
options.put(IClientConfigKey.CommonKeys.ReadTimeout, value);
67+
return this;
68+
}
69+
70+
public ClientOptions withFollowRedirects(boolean value) {
71+
options.put(IClientConfigKey.CommonKeys.FollowRedirects, value);
72+
return this;
73+
}
74+
75+
public ClientOptions withConnectionPoolIdleEvictTimeMilliseconds(int value) {
76+
options.put(IClientConfigKey.CommonKeys.ConnIdleEvictTimeMilliSeconds, value);
77+
return this;
78+
}
79+
80+
public ClientOptions withLoadBalancerEnabled(boolean value) {
81+
options.put(IClientConfigKey.CommonKeys.InitializeNFLoadBalancer, value);
82+
return this;
83+
}
84+
85+
Map<IClientConfigKey<?>, Object> getOptions() {
86+
return options;
87+
}
88+
89+
}

ribbon-client-extensions/src/main/java/com/netflix/ribbonclientextensions/RequestTemplate.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
/**
77
* @author awang
88
*
9-
* @param <I> request input entity type
10-
* @param <O> response entity type
9+
* @param <T> response entity type
1110
* @param <R> response meta data, e.g. HttpClientResponse
1211
*/
1312
public interface RequestTemplate<T, R> {
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
11
package com.netflix.ribbonclientextensions;
22

33
import com.netflix.client.config.IClientConfig;
4-
import com.netflix.loadbalancer.ILoadBalancer;
4+
import com.netflix.client.config.IClientConfigKey;
55

6-
public interface ResourceGroup {
7-
String name();
6+
public abstract class ResourceGroup<T extends RequestTemplate<?, ?>> {
7+
private String name;
8+
private IClientConfig clientConfig;
89

9-
ResourceGroup withLoadBalancer(ILoadBalancer loadBalancer);
10+
public ResourceGroup(String name) {
11+
this(name, null);
12+
}
13+
14+
public ResourceGroup(String name, ClientOptions options) {
15+
this.name = name;
16+
clientConfig = loadDefaultConfig(name);
17+
if (options != null) {
18+
for (IClientConfigKey key: options.getOptions().keySet()) {
19+
clientConfig.setPropertyWithType(key, options.getOptions().get(key));
20+
}
21+
}
22+
}
1023

11-
ResourceGroup withClientConfig(IClientConfig config);
24+
ResourceGroup(IClientConfig clientConfig) {
25+
this.clientConfig = clientConfig;
26+
}
1227

13-
<T extends RequestTemplate<?, ?>> RequestTemplateBuilder<T> requestTemplateBuilder();
28+
protected abstract IClientConfig loadDefaultConfig(String name);
1429

15-
public abstract class RequestTemplateBuilder<T extends RequestTemplate<?, ?>> {
16-
public abstract <S> T newRequestTemplate(String name, Class<? extends S> classType);
30+
protected final IClientConfig getClientConfig() {
31+
return clientConfig;
1732
}
33+
34+
public String name() {
35+
return name;
36+
}
37+
38+
public abstract <S> T newRequestTemplate(String name, Class<? extends S> classType);
1839
}

ribbon-client-extensions/src/main/java/com/netflix/ribbonclientextensions/Ribbon.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.netflix.ribbonclientextensions.http.HttpResourceGroup;
44

5-
import io.reactivex.netty.protocol.http.client.HttpClient;
6-
75
public final class Ribbon {
86

97
private Ribbon() {
@@ -12,8 +10,12 @@ private Ribbon() {
1210
public static HttpResourceGroup createHttpResourceGroup(String name) {
1311
return new HttpResourceGroup(name);
1412
}
15-
16-
public static <I, O, T> T from(Class<T> contract, HttpClient<I, O> transportClient) {
13+
14+
public static HttpResourceGroup createHttpResourceGroup(String name, ClientOptions options) {
15+
return new HttpResourceGroup(name, options);
16+
}
17+
18+
public static <T> T from(Class<T> contract) {
1719
return null;
1820
}
1921
}

ribbon-client-extensions/src/main/java/com/netflix/ribbonclientextensions/http/HttpResourceGroup.java

+16-54
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,38 @@
1010
import com.netflix.client.config.IClientConfigKey;
1111
import com.netflix.client.netty.RibbonTransport;
1212
import com.netflix.loadbalancer.ILoadBalancer;
13+
import com.netflix.ribbonclientextensions.ClientOptions;
14+
import com.netflix.ribbonclientextensions.RequestTemplate;
1315
import com.netflix.ribbonclientextensions.ResourceGroup;
1416

15-
public class HttpResourceGroup implements ResourceGroup {
16-
17-
private String groupName;
18-
private IClientConfig config;
19-
private ILoadBalancer loadBalancer;
20-
21-
public class HttpRequestTemplateBuilder extends RequestTemplateBuilder<HttpRequestTemplate<?>>{
22-
private HttpClient<ByteBuf, ByteBuf> client;
23-
HttpRequestTemplateBuilder() {
24-
if (loadBalancer == null) {
25-
client = RibbonTransport.newHttpClient(config);
26-
} else {
27-
client = RibbonTransport.newHttpClient(loadBalancer, config);
28-
}
29-
}
30-
@Override
31-
public <T> HttpRequestTemplate<T> newRequestTemplate(String name, Class<? extends T> type) {
32-
return new HttpRequestTemplate<T>(name, HttpResourceGroup.this, client, type);
33-
}
34-
35-
public HttpRequestTemplate<ByteBuf> newRequestTemplate(String name) {
36-
return newRequestTemplate(name, ByteBuf.class);
37-
}
38-
39-
}
17+
public class HttpResourceGroup extends ResourceGroup<HttpRequestTemplate<?>> {
18+
private final HttpClient<ByteBuf, ByteBuf> client;
4019

41-
@Override
42-
public String name() {
43-
return groupName;
20+
public HttpResourceGroup(String groupName) {
21+
this(groupName, null);
4422
}
4523

46-
public HttpResourceGroup(String groupName) {
47-
this.groupName = groupName;
48-
config = getDefaultConfig(groupName);
24+
public HttpResourceGroup(String groupName, ClientOptions options) {
25+
super(groupName, options);
26+
client = RibbonTransport.newHttpClient(getClientConfig());
4927
}
5028

51-
protected IClientConfig getDefaultConfig(String groupName) {
29+
protected IClientConfig loadDefaultConfig(String groupName) {
5230
return ClientConfigBuilder.newBuilderWithArchaiusProperties(groupName).build();
5331
}
5432

5533
public HttpResourceGroup withCommonHeader(String name, String value) {
5634
return this;
5735
}
58-
59-
@SuppressWarnings({"rawtypes", "unchecked"})
36+
6037
@Override
61-
public HttpResourceGroup withClientConfig(IClientConfig overrideConfig) {
62-
if (config instanceof DefaultClientConfigImpl) {
63-
((DefaultClientConfigImpl) config).applyOverride(overrideConfig);
64-
} else {
65-
for (IClientConfigKey key: CommonClientConfigKey.keys()) {
66-
Object value = overrideConfig.getPropertyWithType(key);
67-
if (value != null) {
68-
config.setPropertyWithType(key, value);
69-
}
70-
}
71-
}
72-
return this;
38+
public <T> HttpRequestTemplate<T> newRequestTemplate(String name,
39+
Class<? extends T> classType) {
40+
return new HttpRequestTemplate<T>(name, HttpResourceGroup.this, client, classType);
7341
}
7442

75-
public HttpResourceGroup withLoadBalancer(ILoadBalancer loadBalancer) {
76-
this.loadBalancer = loadBalancer;
77-
return this;
43+
public HttpRequestTemplate<ByteBuf> newRequestTemplate(String name) {
44+
return newRequestTemplate(name, ByteBuf.class);
7845
}
7946

80-
@SuppressWarnings("unchecked")
81-
@Override
82-
public HttpRequestTemplateBuilder requestTemplateBuilder() {
83-
return new HttpRequestTemplateBuilder();
84-
}
8547
}

ribbon-client-extensions/src/main/java/com/netflix/ribbonclientextensions/hystrix/FallbackHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* @author awang
1212
*
1313
* @param <T> Output entity type
14-
* @param <R> Response
1514
*/
1615
public interface FallbackHandler<T> {
1716
public Observable<T> getFallback(HystrixExecutableInfo<?> hystrixInfo, Map<String, Object> requestProperties);

0 commit comments

Comments
 (0)