Skip to content

Commit f561cb5

Browse files
author
chenweijie
committed
添加代理设计模式 zookeeper相关的工具类
1 parent eab2471 commit f561cb5

39 files changed

+1750
-9
lines changed

pom.xml

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<properties>
1111
<mongodb.driver.version>3.2.2</mongodb.driver.version>
1212
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
13+
<spring.version>4.3.11.RELEASE</spring.version>
1314
</properties>
1415

1516
<dependencies>
@@ -35,7 +36,37 @@
3536
<dependency>
3637
<groupId>org.springframework</groupId>
3738
<artifactId>spring-context</artifactId>
38-
<version>4.3.5.RELEASE</version>
39+
<version>${spring.version}</version>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework</groupId>
44+
<artifactId>spring-core</artifactId>
45+
<version>${spring.version}</version>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-aop</artifactId>
51+
<version>${spring.version}</version>
52+
</dependency>
53+
54+
<!--下面这两个aspectj的依赖是为了引入AspectJ的注解-->
55+
<dependency>
56+
<groupId>org.aspectj</groupId>
57+
<artifactId>aspectjrt</artifactId>
58+
<version>1.6.12</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.aspectj</groupId>
62+
<artifactId>aspectjweaver</artifactId>
63+
<version>1.6.12</version>
64+
</dependency>
65+
<!--Spring AOP底层会使用CGLib来做动态代理-->
66+
<dependency>
67+
<groupId>cglib</groupId>
68+
<artifactId>cglib</artifactId>
69+
<version>2.2</version>
3970
</dependency>
4071

4172
<dependency>
@@ -80,6 +111,19 @@
80111
<artifactId>javax.servlet-api</artifactId>
81112
<version>3.1.0</version>
82113
</dependency>
114+
<dependency>
115+
<groupId>com.101tec</groupId>
116+
<artifactId>zkclient</artifactId>
117+
<version>0.10</version>
118+
</dependency>
119+
120+
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
121+
<dependency>
122+
<groupId>org.apache.zookeeper</groupId>
123+
<artifactId>zookeeper</artifactId>
124+
<version>3.4.6</version>
125+
</dependency>
126+
83127

84128

85129
</dependencies>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.chen.algorithm.consistentHash;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.util.Collection;
6+
import java.util.SortedMap;
7+
import java.util.TreeMap;
8+
9+
/**
10+
* 一致性Hash算法
11+
*
12+
* @param <T> 节点类型
13+
*/
14+
public class ConsistentHash<T> {
15+
/**
16+
* Hash计算对象,用于自定义hash算法
17+
*/
18+
HashFunc hashFunc;
19+
/**
20+
* 复制的节点个数
21+
*/
22+
private final int numberOfReplicas;
23+
/**
24+
* 一致性Hash环
25+
*/
26+
private final SortedMap<Long, T> circle = new TreeMap<>();
27+
28+
/**
29+
* 构造,使用Java默认的Hash算法
30+
* @param numberOfReplicas 复制的节点个数,增加每个节点的复制节点有利于负载均衡
31+
* @param nodes 节点对象
32+
*/
33+
public ConsistentHash(int numberOfReplicas, Collection<T> nodes) {
34+
this.numberOfReplicas = numberOfReplicas;
35+
this.hashFunc = new HashFunc() {
36+
37+
@Override
38+
public Long hash(Object key) {
39+
// return fnv1HashingAlg(key.toString());
40+
return md5HashingAlg(key.toString());
41+
}
42+
};
43+
//初始化节点
44+
for (T node : nodes) {
45+
add(node);
46+
}
47+
}
48+
49+
/**
50+
* 构造
51+
* @param hashFunc hash算法对象
52+
* @param numberOfReplicas 复制的节点个数,增加每个节点的复制节点有利于负载均衡
53+
* @param nodes 节点对象
54+
*/
55+
public ConsistentHash(HashFunc hashFunc, int numberOfReplicas, Collection<T> nodes) {
56+
this.numberOfReplicas = numberOfReplicas;
57+
this.hashFunc = hashFunc;
58+
//初始化节点
59+
for (T node : nodes) {
60+
add(node);
61+
}
62+
}
63+
64+
/**
65+
* 增加节点<br>
66+
* 每增加一个节点,就会在闭环上增加给定复制节点数<br>
67+
* 例如复制节点数是2,则每调用此方法一次,增加两个虚拟节点,这两个节点指向同一Node
68+
* 由于hash算法会调用node的toString方法,故按照toString去重
69+
*
70+
* @param node 节点对象
71+
*/
72+
public void add(T node) {
73+
for (int i = 0; i < numberOfReplicas; i++) {
74+
circle.put(hashFunc.hash(node.toString() + i), node);
75+
}
76+
}
77+
78+
/**
79+
* 移除节点的同时移除相应的虚拟节点
80+
*
81+
* @param node 节点对象
82+
*/
83+
public void remove(T node) {
84+
for (int i = 0; i < numberOfReplicas; i++) {
85+
circle.remove(hashFunc.hash(node.toString() + i));
86+
}
87+
}
88+
89+
/**
90+
* 获得一个最近的顺时针节点
91+
*
92+
* @param key 为给定键取Hash,取得顺时针方向上最近的一个虚拟节点对应的实际节点
93+
* @return 节点对象
94+
*/
95+
public T get(Object key) {
96+
if (circle.isEmpty()) {
97+
return null;
98+
}
99+
long hash = hashFunc.hash(key);
100+
if (!circle.containsKey(hash)) {
101+
SortedMap<Long, T> tailMap = circle.tailMap(hash); //返回此映射的部分视图,其键大于等于 hash
102+
hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey();
103+
}
104+
//正好命中
105+
return circle.get(hash);
106+
}
107+
108+
/**
109+
* 使用MD5算法
110+
* @param key
111+
* @return
112+
*/
113+
private static long md5HashingAlg(String key) {
114+
MessageDigest md5 = null;
115+
try {
116+
md5 = MessageDigest.getInstance("MD5");
117+
md5.reset();
118+
md5.update(key.getBytes());
119+
byte[] bKey = md5.digest();
120+
long res = ((long) (bKey[3] & 0xFF) << 24) | ((long) (bKey[2] & 0xFF) << 16) | ((long) (bKey[1] & 0xFF) << 8)| (long) (bKey[0] & 0xFF);
121+
return res;
122+
} catch (NoSuchAlgorithmException e) {
123+
e.printStackTrace();
124+
}
125+
return 0L;
126+
}
127+
128+
/**
129+
* 使用FNV1hash算法
130+
* @param key
131+
* @return
132+
*/
133+
private static long fnv1HashingAlg(String key) {
134+
final int p = 16777619;
135+
int hash = (int) 2166136261L;
136+
for (int i = 0; i < key.length(); i++)
137+
hash = (hash ^ key.charAt(i)) * p;
138+
hash += hash << 13;
139+
hash ^= hash >> 7;
140+
hash += hash << 3;
141+
hash ^= hash >> 17;
142+
hash += hash << 5;
143+
return hash;
144+
}
145+
146+
/**
147+
* Hash算法对象,用于自定义hash算法
148+
*/
149+
public interface HashFunc {
150+
public Long hash(Object key);
151+
}
152+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.chen.api.util.classloader;
2+
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.Method;
5+
6+
/**
7+
* @author : chen weijie
8+
* @Date: 2019-06-02 02:26
9+
*/
10+
public class GetClass {
11+
12+
13+
public static void printClassMethodMessage(Object obj) {
14+
//要获取类的信息》》首先我们要获取类的类类型
15+
Class c = obj.getClass();
16+
//我们知道Object类是一切类的父类,所以我们传递的是哪个子类的对象,c就是该子类的类类型。
17+
//接下来我们要获取类的名称
18+
System.out.println("类的名称是:" + c.getName());
19+
/*
20+
*我们知道,万事万物都是对象,方法也是对象,是谁的对象呢?
21+
* 在java里面,方法是Method类的对象
22+
*一个成员方法就是一个Method的对象,那么Method就封装了对这个成员
23+
*方法的操作
24+
*/
25+
//如果我们要获得所有的方法,可以用getMethods()方法,这个方法获取的是所有的Public的函数,包括父类继承而来的。如果我们要获取所有该类自己声明的方法,就可以用getDeclaredMethods()方法,这个方法是不问访问权限的。
26+
Method[] ms = c.getMethods();//c.getDeclaredMethods()
27+
//接下来我们拿到这些方法之后干什么?我们就可以获取这些方法的信息,比如方法的名字。
28+
//首先我们要循环遍历这些方法
29+
for (Method m : ms) {
30+
//然后可以得到方法的返回值类型的类类型
31+
Class returnType = m.getReturnType();
32+
//得到方法的返回值类型的名字
33+
System.out.print(returnType.getName() + " ");
34+
//得到方法的名称
35+
System.out.print(m.getName() + "(");
36+
//获取参数类型--->得到的是参数列表的类型的类类型
37+
Class[] paramTypes = m.getParameterTypes();
38+
for (Class class1 : paramTypes) {
39+
System.out.print(class1.getName() + ",");
40+
}
41+
System.out.println(")");
42+
}
43+
}
44+
45+
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException {
46+
47+
Class<?> clazz = HelloWorld.class;
48+
49+
50+
HelloWorld helloWorld = (HelloWorld) clazz.newInstance();
51+
52+
53+
54+
System.out.println(clazz.getName());
55+
System.out.println(clazz.getSimpleName());
56+
System.out.println(clazz.getClassLoader());
57+
System.out.println(clazz.getMethod("wait", long.class));
58+
Field[] fields = clazz.getDeclaredFields();
59+
for (Field field : fields) {
60+
61+
Class<?> classType = field.getType();
62+
String name = field.getName();
63+
System.out.println(classType);
64+
System.out.println(name);
65+
System.out.println(field);
66+
}
67+
68+
69+
// printClassMethodMessage(new HelloWorld());
70+
}
71+
72+
}

src/main/java/com/chen/api/util/classloader/HelloWorld.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@
99
public class HelloWorld {
1010

1111

12+
private String name;
13+
14+
private Integer age;
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public Integer getAge() {
25+
return age;
26+
}
27+
28+
public void setAge(Integer age) {
29+
this.age = age;
30+
}
31+
1232
/**
1333
* 三、类加载的一般过程
1434
* 原理:双亲委托模式
@@ -24,9 +44,13 @@ public class HelloWorld {
2444
* 3、Bootstrap Loader(启动类加载器)是最顶级的类加载器了,其父加载器为null.
2545
*/
2646

27-
public static void main(String[] args) {
47+
public static void main(String[] args) throws ClassNotFoundException {
2848

2949
HelloWorld hello = new HelloWorld();
50+
Class<?> clazz = hello.getClass();
51+
Class<?> tClass = HelloWorld.class;
52+
Class<?> tClass2 = Class.forName("com.chen.api.util.classloader.HelloWorld");
53+
3054
Class<?> c = hello.getClass();
3155
ClassLoader classLoader = c.getClassLoader();
3256
System.out.println(classLoader);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.chen.api.util.jvm.gc;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-05-30 00:05
6+
*/
7+
public class GCTest {
8+
9+
10+
private Object instance = null;
11+
private static final int _10M = 10 * 1 << 20;
12+
// 一个对象占10M,方便在GC日志中看出是否被回收
13+
private byte[] bigSize = new byte[_10M];
14+
15+
public static void main(String[] args) {
16+
GCTest objA = new GCTest();
17+
GCTest objB = new GCTest();
18+
19+
objA.instance = objB;
20+
objB.instance = objA;
21+
22+
objA = null;
23+
objB = null;
24+
25+
System.gc();
26+
}
27+
28+
29+
}

0 commit comments

Comments
 (0)