-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5.1 v1.5 将负载均衡策略通过注解注入,同时创建了随机负载均衡策略
- Loading branch information
1 parent
a2b9a22
commit 0c7b51c
Showing
8 changed files
with
131 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
zyt-rpc-common/src/main/java/annotation/LoadBalanceMethodImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
//注解的参数直接是要传入什么类 | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface LoadBalanceMethodImpl { | ||
Class chosenMethod(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
zyt-rpc-common/src/main/java/loadbalance/AccessBalance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package loadbalance; | ||
|
||
import org.apache.zookeeper.KeeperException; | ||
import org.apache.zookeeper.ZooKeeper; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class AccessBalance implements LoadBalance{ | ||
@Override | ||
public String loadBalance(ZooKeeper zooKeeper, String path) throws InterruptedException, KeeperException { | ||
List<String> children = zooKeeper.getChildren(path, false, null); | ||
if (children.isEmpty()) | ||
{ | ||
System.out.println("当前没有服务器提供该服务 请联系工作人员"); | ||
} | ||
//进行排序 根据每个节点的访问次数 从小到大进行排序 然后选用最小的 | ||
Collections.sort(children, new Comparator<String>() { | ||
@Override | ||
public int compare(String o1, String o2) { | ||
|
||
try { | ||
return Integer.valueOf(new String(zooKeeper.getData(path+"/"+o1,false,null))) | ||
- | ||
Integer.valueOf(new String(zooKeeper.getData(path+"/"+o2,false,null))); | ||
} catch (KeeperException e) { | ||
e.printStackTrace(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return 0; | ||
} | ||
}); | ||
//对选用的对象的访问量加1 todo 暂时不知道怎么让数据直接+1 | ||
// 获取节点数据+1,然后修改对应节点, | ||
String chooseNode = children.get(0); | ||
byte[] data = zooKeeper.getData(path+"/"+chooseNode, false, null); | ||
int visitCount = Integer.valueOf(new String(data)); | ||
++visitCount; | ||
//version参数用于指定节点的数据版本,表名本次更新操作是针对指定的数据版本进行的。 cas | ||
zooKeeper.setData(path+"/"+chooseNode,String.valueOf(visitCount).getBytes(StandardCharsets.UTF_8),-1); | ||
String address = new String(children.get(0)); | ||
return address; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package loadbalance; | ||
|
||
|
||
import annotation.LoadBalanceMethodImpl; | ||
import org.apache.zookeeper.KeeperException; | ||
import org.apache.zookeeper.ZooKeeper; | ||
|
||
//实现不同的负载均衡策略 | ||
@LoadBalanceMethodImpl(chosenMethod = RandomBalance.class) | ||
public interface LoadBalance { | ||
//通过负载均衡策略返回相应地址 | ||
String loadBalance(ZooKeeper zooKeeper, String path) throws InterruptedException, KeeperException; | ||
} |
29 changes: 29 additions & 0 deletions
29
zyt-rpc-common/src/main/java/loadbalance/RandomBalance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package loadbalance; | ||
|
||
import org.apache.zookeeper.KeeperException; | ||
import org.apache.zookeeper.ZooKeeper; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class RandomBalance implements LoadBalance{ | ||
@Override | ||
public String loadBalance(ZooKeeper zooKeeper, String path) throws InterruptedException, KeeperException { | ||
List<String> children = zooKeeper.getChildren(path, null,null); | ||
if (children.isEmpty()) | ||
{ | ||
System.out.println("当前没有服务器提供该服务 请联系工作人员"); | ||
} | ||
int size = children.size(); | ||
Random random = new Random(); | ||
//这是应该处于0——size-1之间 | ||
int randomIndex = random.nextInt(size); | ||
String chooseNode = children.get(randomIndex); | ||
byte[] data = zooKeeper.getData(path + "/" + chooseNode, null, null); | ||
int visitedCount = Integer.valueOf(new String(data)); | ||
++visitedCount; | ||
zooKeeper.setData(path+"/"+ chooseNode, String.valueOf(visitedCount).getBytes(StandardCharsets.UTF_8),-1); | ||
return chooseNode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters