Skip to content

Commit fb2c29a

Browse files
author
yongjie.zhang@ucarinc.com
committed
实现一个简单rpc
1 parent 7cc4ba1 commit fb2c29a

File tree

23 files changed

+628
-0
lines changed

23 files changed

+628
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.asher.learn.jvm;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
8+
*
9+
* @author : 张勇杰
10+
* @date : 2019/7/13 10:28
11+
* @Version : v1.0
12+
* VM Args -XX:PermSize=10M -XX:MaxPermSize=10M
13+
* @description
14+
**/
15+
public class PermGenTest {
16+
public static void main(String[] args) {
17+
List<String> list = new ArrayList<>();
18+
int i = 0;
19+
while (true){
20+
list.add(String.valueOf(i).intern());
21+
i++;
22+
}
23+
}
24+
}

java-spi/java-spi.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.asher.spi.HelloSPIServiceImpl
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.asher.spi;
2+
3+
/**
4+
* @author : 张勇杰
5+
* @date : 2019/8/9 11:00
6+
* @Version : v1.0
7+
* @description
8+
**/
9+
public class HelloSPIServiceImpl implements HelloSpiService{
10+
@Override
11+
public void sayHelloToSPI() {
12+
System.out.println("hello,SPI!!");
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.asher.spi;
2+
3+
public interface HelloSpiService {
4+
void sayHelloToSPI();
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.asher.spi;
2+
3+
import java.util.Iterator;
4+
import java.util.ServiceLoader;
5+
6+
/**
7+
* @author : 张勇杰
8+
* @date : 2019/8/9 11:01
9+
* @Version : v1.0
10+
* @description
11+
**/
12+
public class TestMain {
13+
public static void main(String[] args) {
14+
ServiceLoader<HelloSpiService> sl = ServiceLoader.load(HelloSpiService.class);
15+
Iterator<HelloSpiService> iterator = sl.iterator();
16+
while(iterator.hasNext()){
17+
HelloSpiService hss = iterator.next();
18+
hss.sayHelloToSPI();
19+
}
20+
}
21+
}

java-stream/java-stream.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

java-stream/src/com/asher/stream/test/ConsumerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6+
import java.util.Stack;
67
import java.util.function.Consumer;
78
import java.util.function.Predicate;
89
import java.util.function.UnaryOperator;
@@ -39,5 +40,12 @@ public static void main(String[] args) {
3940
// }
4041
// }).forEach(System.out::println);
4142
Stream.iterate(1, n->n<10,n->n*3).forEach(System.out::println);
43+
Stack<Integer> stack = new Stack<>();
44+
int a = 1; int b = 2;
45+
stack.push(a);
46+
stack.push(b);
47+
a = stack.pop();
48+
b = stack.pop();
49+
System.out.println("a->"+a+"b->"+b);
4250
}
4351
}

java-thread/src/com/zyj/AQSLearn.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.zyj;
2+
3+
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
4+
5+
/**
6+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
7+
*
8+
* @author : 张勇杰
9+
* @date : 2019/7/11 14:04
10+
* @Version : v1.0
11+
* @description
12+
**/
13+
public class AQSLearn {
14+
private static int []kmpArr = {0,0,0,0,1,2,0};
15+
public static void main(String[] args) {
16+
String target = "BBC ABCDAB ABCDABCDABDE";
17+
String seek = "ABCDABD";
18+
target.contains(seek);
19+
kmp(target,seek);
20+
}
21+
22+
static void kmp(String target, String seek){
23+
char []tar = target.toCharArray();
24+
char []se = seek.toCharArray();
25+
if(tar.length <= se.length){
26+
return;
27+
}
28+
29+
int j = 0;
30+
int i = 0;
31+
int t = 0;
32+
int num = 0;
33+
while(j < se.length){
34+
if(tar[i] == se[j]){
35+
if(j == 0){
36+
t = i;
37+
}
38+
j ++;
39+
i ++;
40+
}else{
41+
if(j > 1){
42+
i = t+j-kmpArr[j-1];
43+
// i-=kmpArr[j-1];
44+
}else{
45+
i ++;
46+
}
47+
j = 0;
48+
}
49+
num ++;
50+
}
51+
System.out.println(num);
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.zyj;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Scanner;
6+
7+
/**
8+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
9+
*
10+
* @author : 张勇杰
11+
* @date : 2019/7/12 14:04
12+
* @Version : v1.0
13+
* @description
14+
**/
15+
public class NumberTest {
16+
public static void main(String[] args) {
17+
Scanner scanner = new Scanner(System.in);
18+
String tip = scanner.next();
19+
char[] arr = tip.toCharArray();
20+
Integer result = operate(arr);
21+
System.out.println("出现最多的数字为:"+result);
22+
}
23+
public static Integer operate(char [] arr){
24+
HashMap<String,Integer> map = new HashMap<>();
25+
int max = 1;
26+
for(int i = 0;i < arr.length; i++){
27+
if(isNum(arr[i])){
28+
int j = i;
29+
StringBuilder sb = new StringBuilder();
30+
while(j < arr.length && isNum(arr[j])){
31+
sb.append(arr[j++]);
32+
if(!map.containsKey(sb.toString())){
33+
map.put(sb.toString(),1);
34+
}else{
35+
map.put(sb.toString(),map.get(sb.toString())+1);
36+
if(map.get(sb.toString()) > max){
37+
max = map.get(sb.toString());
38+
}
39+
}
40+
}
41+
}
42+
}
43+
int index = 0;
44+
String [] barry = new String[map.size()];
45+
for(Map.Entry<String, Integer> entry :map.entrySet()){
46+
// System.out.println(entry.getKey()+"->"+entry.getValue());
47+
if(entry.getValue() == max){
48+
barry[index++] = entry.getKey();
49+
}
50+
}
51+
int maxNum = Integer.valueOf(barry[0]);
52+
for (int k = 0; k < index; k ++){
53+
if(barry != null && Integer.valueOf(barry[k]) > maxNum){
54+
maxNum = Integer.valueOf(barry[k]);
55+
}
56+
}
57+
return maxNum;
58+
}
59+
60+
public static boolean isNum(char c){
61+
if(c >= '0' && c <= '9'){
62+
return true;
63+
}
64+
return false;
65+
}
66+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.zyj;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2019/7/12 15:40
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class ZhengZeTest {
12+
public static void main(String[] args) {
13+
ZhengZeTest test = new ZhengZeTest();
14+
test.compute();
15+
16+
}
17+
public int compute(){
18+
int a = 1;
19+
int b = 2;
20+
int c = 3;
21+
int d = 4;
22+
int e = a + b;
23+
return e;
24+
}
25+
26+
}

javasimple-RPC/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.asher</groupId>
8+
<artifactId>java-simpleRPC</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.apache.tomcat.embed</groupId>
14+
<artifactId>tomcat-embed-core</artifactId>
15+
<version>9.0.12</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.apache.commons</groupId>
19+
<artifactId>commons.io</artifactId>
20+
<version>1.3.2</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>io.netty</groupId>
25+
<artifactId>netty-all</artifactId>
26+
<version>4.1.16Final</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>mysql</groupId>
31+
<artifactId>mysql-connector-java</artifactId>
32+
<version>8.0.15</version>
33+
</dependency>
34+
</dependencies>
35+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.asher.consumer.client;
2+
3+
import com.asher.provider.service.HelloService;
4+
5+
import java.io.IOException;
6+
import java.io.ObjectOutputStream;
7+
import java.io.OutputStream;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
import java.net.URLConnection;
11+
12+
/**
13+
* @author : 张勇杰
14+
* @date : 2019/8/9 11:55
15+
* @Version : v1.0
16+
* @description
17+
**/
18+
public class HttpClient {
19+
public static void send(URL url){
20+
ObjectOutputStream oos = null;
21+
try {
22+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
23+
connection.setRequestMethod("POST");
24+
connection.setDoOutput(true);
25+
26+
OutputStream os = connection.getOutputStream();
27+
oos = new ObjectOutputStream(os);
28+
Invoker invoker = new Invoker(HelloService.class.getName(),"sayHello",new Class[]{String.class},new Object[]{"zhangyongjie"});
29+
oos.writeObject(invoker);
30+
oos.flush();
31+
oos.close();
32+
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
}finally {
36+
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)