Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify HttpRequestTemplate and HttpResourceGroup to be DI friendly #138

Merged
merged 27 commits into from
Jul 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5655c76
Modify HttpRequestTemplate and HttpResourceGroup to be DI friendly
elandau Jul 17, 2014
5d39d8d
Added ClientConfigFactory and RibbonTransportFactory
Jul 18, 2014
d98a139
More updates.
Jul 18, 2014
24298b4
Make RibbonTransportFactory and ClientConfigFactory injected to HttpR…
Jul 18, 2014
9c62f05
Merge branch 'master' of https://github.com/Netflix/ribbon into di
Jul 18, 2014
5c67188
Rename HttpResourceGroupFactory to RibbonResourceFactory.
Jul 22, 2014
c93a707
Initial pass at Guicified RibbonResourceProvider
elandau Jul 22, 2014
1291c35
Merge with allen's fixes
elandau Jul 22, 2014
b4c5cc0
Merge branch 'allenxwang-eran-di' into pr/1
elandau Jul 22, 2014
c02144b
Merge branch 'allenxwang-eran-di'
elandau Jul 22, 2014
1361628
Replace RibbonResourceFactory with HttpResourceGroupFactory
elandau Jul 22, 2014
f3e8cdf
Update to rxjava 0.19+
elandau Jul 22, 2014
4e85836
Merge branch 'master' of github.com:elandau/ribbon into eran-di
Jul 23, 2014
fa0921b
Rename HttpResourceGroupFactory to RibbonResourceFactory. Remove Http…
Jul 23, 2014
9537655
Merge pull request #2 from allenxwang/eran-di
elandau Jul 23, 2014
4d0ced0
Merge branch 'pr/1'
elandau Jul 23, 2014
1db9e91
Merge branch 'master' of https://github.com/elandau/ribbon
elandau Jul 23, 2014
0695314
Provide example for RxMovieProxy using Guice
elandau Jul 23, 2014
51ba17b
Add RxMovieClientTestBase from examples:test
elandau Jul 24, 2014
b566eb4
Use local mocked HTTP server to test redirecting.
Jul 25, 2014
a2f9c87
Merge pull request #3 from allenxwang/eran-di
elandau Jul 25, 2014
3f12bcd
Merge branch 'master' of https://github.com/Netflix/ribbon into eran-di
Jul 28, 2014
06abf85
Make RibbonResourceFactory and RibbonTransportFactory abstract class …
Jul 29, 2014
b020c1b
Minor changes.
Jul 29, 2014
0d3aac8
Merge pull request #4 from allenxwang/eran-di
elandau Jul 29, 2014
df8bed4
Added test for custom binding of ClientConfigFactory.
Jul 29, 2014
5c11e05
Merge pull request #5 from allenxwang/eran-di
elandau Jul 29, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ apply from: file('gradle/maven.gradle')
apply from: file('gradle/check.gradle')
apply from: file('gradle/license.gradle')
apply from: file('gradle/release.gradle')
apply plugin: 'idea'

subprojects {
group = "com.netflix.${githubProjectName}" // TEMPLATE: Set to organization of project
Expand All @@ -46,7 +47,7 @@ project(':ribbon-loadbalancer') {
dependencies {
compile project(':ribbon-core')
compile 'com.netflix.netflix-commons:netflix-statistics:0.1.1'
compile 'com.netflix.rxjava:rxjava-core:0.18+'
compile 'com.netflix.rxjava:rxjava-core:0.19+'
}
}

Expand Down Expand Up @@ -105,14 +106,28 @@ project(':ribbon-test') {
compile project(':ribbon-eureka')
compile 'org.powermock:powermock-easymock-release-full:1.5.4'
compile 'org.easymock:easymock:3.2'
compile 'com.netflix.rxjava:rxjava-core:0.18+'
compile 'com.netflix.rxjava:rxjava-core:0.19+'
compile 'com.sun.jersey:jersey-server:1.11'
compile 'javax.ws.rs:jsr311-api:1.1.1'
compile 'com.sun.jersey:jersey-core:1.11'
compile 'junit:junit:4.10'
}
}

project(':ribbon-guice') {
dependencies {
compile project(':ribbon')
compile project(':ribbon-core')
compile project(':ribbon-httpclient')
compile project(':ribbon-loadbalancer')

compile 'com.google.inject:guice:3.0'
compile 'com.google.inject.extensions:guice-multibindings:3.0'

testCompile project(':ribbon-examples')
}
}

project(':ribbon') {
configurations {
// add new configuration
Expand Down Expand Up @@ -152,6 +167,15 @@ project(':ribbon') {
}
}

project(':ribbon-guice') {
dependencies {
compile project(':ribbon')
compile 'com.google.inject:guice:3.0'
compile 'com.google.inject.extensions:guice-multibindings:3.0'
testCompile project(':ribbon-examples')
}
}

task wrapper(type: Wrapper) {
gradleVersion = '1.12'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.netflix.client.config;


/**
* Created by awang on 7/18/14.
*/
public interface ClientConfigFactory {
IClientConfig newConfig();

public static class DefaultClientConfigFactory implements ClientConfigFactory {
@Override
public IClientConfig newConfig() {
IClientConfig config = new DefaultClientConfigImpl();
config.loadDefaultValues();
return config;

}
}

public static final ClientConfigFactory DEFAULT = new DefaultClientConfigFactory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void main(String[] args) throws Exception {

final CountDownLatch latch = new CountDownLatch(1);
client.submit("www.google.com", 80, request)
.toBlockingObservable()
.toBlocking()
.forEach(new Action1<HttpClientResponse<ByteBuf>>() {
@Override
public void call(HttpClientResponse<ByteBuf> t1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.netflix.ribbon.examples.rx.proxy;

import com.netflix.ribbon.ClientOptions;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.config.ConfigurationManager;
import com.netflix.ribbon.Ribbon;
import com.netflix.ribbon.examples.rx.AbstractRxMovieClient;
import com.netflix.ribbon.examples.rx.RxMovieServer;
import com.netflix.ribbon.examples.rx.common.Movie;
import com.netflix.ribbon.http.HttpResourceGroup;
import com.netflix.ribbon.proxy.ProxyLifeCycle;
import io.netty.buffer.ByteBuf;
import rx.Observable;
Expand All @@ -36,11 +36,9 @@ public class RxMovieProxyExample extends AbstractRxMovieClient {
private final MovieService movieService;

public RxMovieProxyExample(int port) {
HttpResourceGroup httpResourceGroup = Ribbon.createHttpResourceGroup("movieServiceClient",
ClientOptions.create()
.withMaxAutoRetriesNextServer(3)
.withConfigurationBasedServerList("localhost:" + port));
movieService = Ribbon.from(MovieService.class, httpResourceGroup);
ConfigurationManager.getConfigInstance().setProperty("MovieService.ribbon." + CommonClientConfigKey.MaxAutoRetriesNextServer, "3");
ConfigurationManager.getConfigInstance().setProperty("MovieService.ribbon." + CommonClientConfigKey.ListOfServers, "localhost:" + port);
movieService = Ribbon.from(MovieService.class);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
/*
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.ribbon.examples.rx;

import io.netty.buffer.ByteBuf;
import io.reactivex.netty.protocol.http.server.HttpServer;

import org.junit.After;
import org.junit.Before;

import java.util.Random;

/**
* @author Tomasz Bak
*/
public class RxMovieClientTestBase {
private static final Random RANDOM = new Random();
protected int port = RANDOM.nextInt(1000) + 8000;
protected int port = 0;

private RxMovieServer movieServer;

Expand All @@ -22,6 +35,7 @@ public class RxMovieClientTestBase {
public void setUp() throws Exception {
movieServer = new RxMovieServer(port);
httpServer = movieServer.createServer().start();
port = httpServer.getServerPort();
}

@After
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.ribbon.examples.rx.proxy;

import com.netflix.ribbon.examples.rx.RxMovieClientTestBase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.ribbon.examples.rx.template;

import com.netflix.ribbon.examples.rx.RxMovieClientTestBase;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.ribbon.guice;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.netflix.client.config.ClientConfigFactory;
import com.netflix.client.config.ClientConfigFactory.DefaultClientConfigFactory;
import com.netflix.ribbon.DefaultResourceFactory;
import com.netflix.ribbon.RibbonResourceFactory;
import com.netflix.ribbon.RibbonTransportFactory;
import com.netflix.ribbon.RibbonTransportFactory.DefaultRibbonTransportFactory;

/**
* Default bindings for Ribbon
*
* @author elandau
*
*/
public class RibbonModule extends AbstractModule {
@Override
protected void configure() {
bind(RibbonResourceFactory.class).to(DefaultResourceFactory.class).in(Scopes.SINGLETON);
bind(RibbonTransportFactory.class).to(DefaultRibbonTransportFactory.class).in(Scopes.SINGLETON);
bind(ClientConfigFactory.class).to(DefaultClientConfigFactory.class).in(Scopes.SINGLETON);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.ribbon.guice;

import com.google.inject.Inject;
import com.google.inject.spi.BindingTargetVisitor;
import com.google.inject.spi.ProviderInstanceBinding;
import com.google.inject.spi.ProviderWithExtensionVisitor;
import com.google.inject.spi.Toolable;
import com.netflix.ribbon.RibbonResourceFactory;

/**
* Guice Provider extension to create a ribbon resource from an injectable
* RibbonResourceFactory.
*
* @author elandau
*
* @param <T> Type of the client API interface class
*/
public class RibbonResourceProvider<T> implements ProviderWithExtensionVisitor<T> {

private RibbonResourceFactory factory;
private final Class<T> contract;

public RibbonResourceProvider(Class<T> contract) {
this.contract = contract;
}

@Override
public T get() {
// TODO: Get name from annotations (not only class name of contract)
return factory.from(contract);
}

/**
* This is needed for 'initialize(injector)' below to be called so the provider
* can get the injector after it is instantiated.
*/
@Override
public <B, V> V acceptExtensionVisitor(
BindingTargetVisitor<B, V> visitor,
ProviderInstanceBinding<? extends B> binding) {
return visitor.visit(binding);
}

@Inject
@Toolable
void initialize(RibbonResourceFactory factory) {
this.factory = factory;
}
}
Loading