8000 complete registy for zk · JavaCourse00/JavaCourseCodes@6ebd3b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ebd3b2

Browse files
committed
complete registy for zk
1 parent e84d39f commit 6ebd3b2

File tree

18 files changed

+282
-96
lines changed

18 files changed

+282
-96
lines changed

01jvm/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
2. 编译代码, 执行命令: `javac -g HelloNum.java`
3232
3. 查看反编译的代码。
3333
- 3.1 可以安装并使用idea的jclasslib插件, 选中 [HelloNum.java](./HelloNum.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。
34-
- 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -v HelloNum.class`
34+
- 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -c -v -p -l HelloNum.class`
3535
4. 分析相关的字节码。【此步骤需要各位同学自己进行分析】
3636

3737

01jvm/jvm/HelloClassLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public class HelloClassLoader extends ClassLoader {
66

77
public static void main(String[] args) throws Exception {
88

9-
new HelloClassLoader().findClass("jvm.lib.Hello").newInstance();
9+
new HelloClassLoader().findClass("lib.Hello").newInstance();
1010
}
1111

1212
@Override
1313
protected Class<?> findClass(String name) throws ClassNotFoundException {
14-
String helloBase64 = "yv66vgAAADQAHAoABgAOCQAPABAIABEKABIAEwcAFAcAFQEABjxpbml0PgEAAygpVgEABENvZGUBAA9MaW5lTnVtYmVyVGFibGUBAAg8Y2xpbml0PgEAClNvdXJjZUZpbGUBAApIZWxsby5qYXZhDAAHAAgHABYMABcAGAEAGEhlbGxvIENsYXNzIEluaXRpYWxpemVkIQcAGQwAGgAbAQAJanZtL0hlbGxvAQAQamF2YS9sYW5nL09iamVjdAEAEGphdmEvbGFuZy9TeXN0ZW0BAANvdXQBABVMamF2YS9pby9QcmludFN0cmVhbTsBABNqYXZhL2lvL1ByaW50U3RyZWFtA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GphdmEvaW8vUHJpbnRTdHJlYW07AQATamF2YS9pby9QcmludFN0cmVhbQEAB3ByaW50bG4BABUoTGphdmEvbGFuZy9TdHJpbmc7KVYAIQAGAAcAAAAAAAMAAQAIAAkAAQAKAAAAHQABAAEAAAAFKrcAAbEAAAABAAsAAAAGAAEAAAADAAEADAAJAAEACgAAACUAAgABAAAACbIAAhIDtgAEsQAAAAEACwAAAAoAAgAAAAgACAAJAAgADQAJAAEACgAAACUAAgAAAAAACbIAAhIFtgAEsQAAAAEACwAAAAoAAgAAAAUACAAGAAEADgAAAAIADw==";
1515
byte[] bytes = decode(helloBase64);
1616
return defineClass(name,bytes,0,bytes.length);
1717
}

01jvm/lib/Hello.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
//package lib;
2-
//
3-
//public class Hello {
4-
// static {
5-
// System.out.println("Hello Class Initialized!");
6-
// }
7-
// public void hello() {
8-
// System.out.println("Hello class say hello method.");
9-
// }
10-
//
11-
//}
1+
package lib;
2+
3+
public class Hello {
4+
static {
5+
System.out.println("Hello Class Initialized!");
6+
}
7+
public void hello() {
8+
System.out.println("Hello class say hello method.");
9+
System.gc(); // JMX MBean server
10+
}
11+
12+
}

01jvm/out/production/01jvm/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
2. 编译代码, 执行命令: `javac -g HelloNum.java`
3232
3. 查看反编译的代码。
3333
- 3.1 可以安装并使用idea的jclasslib插件, 选中 [HelloNum.java](./HelloNum.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。
34-
- 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -v HelloNum.class`
34+
- 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -c -v -p -l HelloNum.class`
3535
4. 分析相关的字节码。【此步骤需要各位同学自己进行分析】
3636

3737

06db/shardingsphere/init.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ CREATE TABLE IF NOT EXISTS demo_ds_0.t_order_item_1 (order_item_id BIGINT NOT NU
2929
CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_item_0 (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id));
3030
CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_item_1 (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id));
3131

32+
33+
34+
35+
36+
# CREATE TABLE t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id));
37+
# CREATE TABLE t_order_item (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id));

07rpc/rpc01/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.0.9.RELEASE</version>
8+
<version>2.7.3</version>
99
<!-- <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
1010
</parent>
1111
<groupId>io.kimmking</groupId>

07rpc/rpc01/rpcfx-core/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
</exclusion>
7171
</exclusions>
7272
</dependency>
73-
</dependencies>
73+
<dependency>
74+
<groupId>org.apache.curator</groupId>
75+
<artifactId>curator-recipes</artifactId>
76+
<version>5.1.0</version>
77+
</dependency>
78+
</dependencies>
7479

7580
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.kimmking.rpcfx.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* Description for this class.
7+
*
8+
* @Author : kimmking(kimmking@apache.org)
9+
* @create 2024/1/1 20:00
10+
*/
11+
@Documented
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target(ElementType.TYPE)
14+
@Inherited
15+
public @interface RpcfxReference {
16+
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.kimmking.rpcfx.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* Description for this class.
7+
*
8+
* @Author : kimmking(kimmking@apache.org)
9+
* @create 2024/1/1 20:00
10+
*/
11+
12+
@Documented
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Target(ElementType.TYPE)
15+
@Inherited
16+
public @interface RpcfxService {
17+
18+
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@
88
import okhttp3.OkHttpClient;
99
import okhttp3.Request;
1010
import okhttp3.RequestBody;
11+
import org.apache.curator.RetryPolicy;
12+
import org.apache.curator.framework.CuratorFramework;
13+
import org.apache.curator.framework.CuratorFrameworkFactory;
14+
import org.apache.curator.framework.recipes.cache.TreeCache;
15+
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
16+
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
17+
import org.apache.curator.retry.ExponentialBackoffRetry;
18+
import org.apache.zookeeper.CreateMode;
1119

1220
import java.io.IOException;
1321
import java.lang.reflect.InvocationHandler;
1422
import java.lang.reflect.Method;
1523
import java.lang.reflect.Proxy;
24+
import java.net.InetAddress;
1625
import java.util.ArrayList;
1726
import java.util.List;
1827

@@ -26,37 +35,89 @@ public static <T, filters> T createFromRegistry(final Class<T> serviceClass, fin
2635

2736
// 加filte之一
2837

29-
// curator Provider list from zk
38+
String service = serviceClass.getCanonicalName();//"io.kimking.rpcfx.demo.api.UserService";
39+
System.out.println("====> "+service);
3040
List<String> invokers = new ArrayList<>();
41+
42+
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
43+
CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("rpcfx").retryPolicy(retryPolicy).build();
44+
client.start();
45+
46+
try {
47+
// ServiceProviderDesc userServiceSesc = ServiceProviderDesc.builder()
48+
// .host(InetAddress.getLocalHost().getHostAddress())
49+
// .port(8082).serviceClass(service).build();
50+
// String userServiceSescJson = JSON.toJSONString(userServiceSesc);
51+
52+
53+
if ( null == client.checkExists().forPath("/" + service)) {
54+
return null;
55+
}
56+
57+
fetchInvokers(client, service, invokers);
58+
59+
final TreeCache treeCache = TreeCache.newBuilder(client, "/" + service).setCacheData(true).setMaxDepth(2).build();
60+
treeCache.getListenable().addListener(new TreeCacheListener() {
61+
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
62+
System.out.println("treeCacheEvent: "+treeCacheEvent);
63+
fetchInvokers(client, service, invokers);
64+
}
65+
});
66+
treeCache.start();
67+
68+
} catch (Exception ex) {
69+
ex.printStackTrace();
70+
}
71+
72+
//
73+
//
74+
// // register service
75+
// // xxx "io.kimmking.rpcfx.demo.api.UserService"
76+
//
77+
78+
79+
// curator Provider list from zk
80+
3181
// 1. 简单:从zk拿到服务提供的列表
3282
// 2. 挑战:监听zk的临时节点,根据事件更新这个list(注意,需要做个全局map保持每个服务的提供者List)
3383

34-
List<String> urls = router.route(invokers);
84+
return (T) create(serviceClass, invokers, router, loadBalance, filter);
3585

36-
String url = loadBalance.select(urls); // router, loadbalance
86+
}
3787

38-
return (T) create(serviceClass, url, filter);
3988

40-
}
4189

42-
public static <T> T create(final Class<T> serviceClass, final String url, Filter... filters) {
90+
private static void fetchInvokers(CuratorFramework client, String service, List<String> invokers) throws Exception {
91+
List<String> services = client.getChildren().forPath("/" + service);
92+
invokers.clear();
93+
for (String svc : services) {
94+
System.out.println(svc);
95+
String url = svc.replace("_", ":");
96+
invokers.add("http://" + url);
97+
}
98+
}
4399

100+
private static <T> Object create(Class<T> serviceClass, List<String> invokers, Router router, LoadBalancer loadBalance, Filter... filters) {
44101
// 0. 替换动态代理 -> 字节码生成
45-
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url, filters));
46-
102+
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass},
103+
new RpcfxInvocationHandler(serviceClass, invokers, router,loadBalance, filters));
47104
}
48105

49106
public static class RpcfxInvocationHandler implements InvocationHandler {
50107

51108
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
52109

53110
private final Class<?> serviceClass;
54-
private final String url;
111+
private final List<String> invokers;
112+
private final Router router;
113+
private final LoadBalancer loadBalance;
55114
private final Filter[] filters;
56115

57-
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url, Filter... filters) {
116+
public <T> RpcfxInvocationHandler(Class<T> serviceClass, List<String> invokers, Router router, LoadBalancer loadBalance, Filter... filters) {
58117
this.serviceClass = serviceClass;
59-
this.url = url;
118+
this.invokers = invokers;
119+
this.router = router;
120+
this.loadBalance = loadBalance;
60121
this.filters = filters;
61122
}
62123

67128
@Override
68129
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
69130

131+
List<String> urls = router.route(invokers);
132+
System.out.println("router.route => ");
133+
urls.forEach(System.out::println);
134+
String url = loadBalance.select(urls); // router, loadbalance
135+
System.out.println("loadBalance.select => ");
136+
System.out.println("final => " + url);
137+
138+
if (url == null) {
139+
throw new RuntimeException("No available providers from registry center.");
140+
}
141+
70142
// 加filter地方之二
71143
// mock == true, new Student("hubao");
72144

@@ -94,13 +166,15 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
94166
return JSON.parse(response.getResult().toString());
95167
}
96168

169+
OkHttpClient client = new OkHttpClient();
170+
97171
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
98172
String reqJson = JSON.toJSONString(req);
99173
System.out.println("req json: "+reqJson);
100174

101175
// 1.可以复用client
102176
// 2.尝试使用httpclient或者netty client
103-
OkHttpClient client = new OkHttpClient();
177+
104178
final Request request = new Request.Builder()
105179
.url(url)
106180
.post(RequestBody.create(JSONTYPE, reqJson))

0 commit comments

Comments
 (0)
0