Skip to content

Commit

Permalink
add ribbon example
Browse files Browse the repository at this point in the history
  • Loading branch information
landyking committed Mar 24, 2020
1 parent 31cb5c9 commit c252079
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
26 changes: 26 additions & 0 deletions spring/learn-ribbon/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:https://maven.apache.org/POM/4.0.0"
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>daydayup</artifactId>
<groupId>com.github.landyking</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../</relativePath>
</parent>
<groupId>com.github.landyking</groupId>
<artifactId>learn-ribbon</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.landyking.learn.ribbon;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import rx.Observable;

import com.google.common.collect.Lists;
import com.netflix.client.DefaultLoadBalancerRetryHandler;
import com.netflix.client.RetryHandler;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.LoadBalancerBuilder;
import com.netflix.loadbalancer.LoadBalancerStats;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.reactive.LoadBalancerCommand;
import com.netflix.loadbalancer.reactive.ServerOperation;

/**
*
* @author Allen Wang
*
*/
public class URLConnectionLoadBalancer {

private final ILoadBalancer loadBalancer;
// retry handler that does not retry on same server, but on a different server
private final RetryHandler retryHandler = new DefaultLoadBalancerRetryHandler(0, 1, true);

public URLConnectionLoadBalancer(List<Server> serverList) {
loadBalancer = LoadBalancerBuilder.newBuilder().buildFixedServerListLoadBalancer(serverList);
}

public String call(final String path) throws Exception {
return LoadBalancerCommand.<String>builder()
.withLoadBalancer(loadBalancer)
.build()
.submit(new ServerOperation<String>() {
@Override
public Observable<String> call(Server server) {
URL url;
try {
url = new URL("http:https://" + server.getHost() + ":" + server.getPort() + path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
return Observable.just(conn.getResponseMessage());
} catch (Exception e) {
return Observable.error(e);
}
}
}).toBlocking().first();
}

public LoadBalancerStats getLoadBalancerStats() {
return ((BaseLoadBalancer) loadBalancer).getLoadBalancerStats();
}

public static void main(String[] args) throws Exception {
URLConnectionLoadBalancer urlLoadBalancer = new URLConnectionLoadBalancer(Lists.newArrayList(
new Server("www.163.com", 80),
new Server("www.baidu.com", 80),
new Server("www.qq.com", 80)));
for (int i = 0; i < 6; i++) {
System.out.println(urlLoadBalancer.call("/"));
}
System.out.println("=== Load balancer stats ===");
System.out.println(urlLoadBalancer.getLoadBalancerStats());
}
}

0 comments on commit c252079

Please sign in to comment.