diff --git a/.gitignore b/.gitignore index 5d2dec13..27a9502e 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,9 @@ hs_err_pid* *.iml *.idea/ +out/ classes/ target/ build/ + +.DS_Store diff --git a/01jvm/AnalysisForList.java b/01jvm/AnalysisForList.java new file mode 100644 index 00000000..86eb5b24 --- /dev/null +++ b/01jvm/AnalysisForList.java @@ -0,0 +1,24 @@ +public class AnalysisForList { + + private int[] array = new int[] {1,2,3}; + + public void testFor() { + for (int i : array) { + System.out.println(i); + } + } + + public void testForIndex() { + for (int i=0;i " + url.toExternalForm()); + } + + // 扩展类加载器 + printClassloader("扩展类加载器",JvmClassLoaderPrintPath.class.getClassLoader().getParent()); + + // 应用类加载器 + printClassloader("应用类加载器",JvmClassLoaderPrintPath.class.getClassLoader()); + + } + + private static void printClassloader(String name, ClassLoader classLoader) { + System.out.println(); + if (null != classLoader) { + System.out.println(name + " Classloader -> " + classLoader.toString()); + printURLForClassloader(classLoader); + } else { + System.out.println(name + " Classloader -> null"); + } + } + + private static void printURLForClassloader(ClassLoader classLoader) { + Object ucp = insightField(classLoader,"ucp"); + Object path = insightField(ucp,"path"); + List paths = (List) path; + for (Object p : paths) { + System.out.println(" ===> " + p.toString()); + } + } + + private static Object insightField(Object obj, String fName) { + Field f = null; + try { + if (obj instanceof URLClassLoader) { + f = URLClassLoader.class.getDeclaredField(fName); + } else { + f = obj.getClass().getDeclaredField(fName); + } + f.setAccessible(true); + return f.get(obj); + } + catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } + + +} diff --git a/01jvm/README.md b/01jvm/README.md index d74bbed1..51c885e3 100644 --- a/01jvm/README.md +++ b/01jvm/README.md @@ -6,9 +6,9 @@ ## 作业内容 -> Week01 作业题目(周四): +> Week01 作业题目: -1.(选做)自己写一个简单的 Hello.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。 +1.(必做)自己写一个简单的 HelloNum.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。 2.(必做)自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法,此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。 @@ -18,24 +18,132 @@ 注意:如果没有线上系统,可以自己 run 一个 web/java 项目。 -> Week01 作业题目(周六): +5.(选做)本机使用 G1 GC 启动一个程序,仿照课上案例分析一下 JVM 情况。 -1.(选做)本机使用 G1 GC 启动一个程序,仿照课上案例分析一下 JVM 情况。 ## 操作步骤 -### 作业2 +### 作业1(必做) + +1. 编写代码, 根据自己的意愿随意编写, 可参考: [HelloNum.java](./HelloNum.java) +2. 编译代码, 执行命令: `javac -g HelloNum.java` +3. 查看反编译的代码。 + - 3.1 可以安装并使用idea的jclasslib插件, 选中 [HelloNum.java](./HelloNum.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。 + - 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -c -v -p -l HelloNum.class` +4. 分析相关的字节码。【此步骤需要各位同学自己进行分析】 + + +### 作业2(必做) 1. 打开 Spring 官网: https://spring.io/ 2. 找到 Projects --> Spring Initializr: https://start.spring.io/ 3. 填写项目信息, 生成 maven 项目; 下载并解压。 4. Idea或者Eclipse从已有的Source导入Maven项目。 -5. 增加课程资源 Hello.xlass 文件到 src/main/resources 目录。 -6. 编写代码,实现 findClass 方法,解码方法 +5. 从课件资料中找到资源 Hello.xlass 文件并复制到 src/main/resources 目录。 +6. 编写代码,实现 findClass 方法,以及对应的解码方法 7. 编写main方法,调用 loadClass 方法; 8. 创建实例,以及调用方法 9. 执行. -具体的参见: [https://github.com/renfufei/JAVA-000/blob/main/Week_01/homework01/src/main/java/com/renfufei/homework01/XlassLoader.java](XlassLoader.java) +具体代码可参考: [XlassLoader.java](./XlassLoader.java) + + +### 作业3(必做) + +对应的图片需要各位同学自己绘制,可以部分参考PPT课件。 + +提示: + +- Xms 设置堆内存的初始值 +- Xmx 设置堆内存的最大值 +- Xmn 设置堆内存中的年轻代的最大值 +- Meta 区不属于堆内存, 归属为非堆 +- DirectMemory 直接内存, 属于 JVM 内存中开辟出来的本地内存空间。 +- Xss设置的是单个线程栈的最大空间; + +JVM进程空间中的内存一般来说包括以下这些部分: + +- 堆内存(Xms ~ Xmx) = 年轻代(~Xmn) + 老年代 +- 非堆 = Meta + CodeCache + ... +- Native内存 = 直接内存 + Native + ... +- 栈内存 = n * Xss + +另外,注意区分规范与实现的区别, 需要根据具体实现以及版本, 才能确定。 一般来说,我们的目的是为了排查故障和诊断问题,大致弄清楚这些参数和空间的关系即可。 具体设置时还需要留一些冗余量。 + + +### 4.(选做) + +这个是具体案例分析, 请各位同学自己分析。 + +比如我们一个生产系统应用的启动参数为: + +``` +JAVA_OPTS=-Xmx200g -Xms200g -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -XX:ZCollectionInterval=30 -XX:ZAllocationSpikeTolerance=5 -XX:ReservedCodeCacheSize=2g -XX:InitialCodeCacheSize=2g -XX:ConcGCThreads=8 -XX:ParallelGCThreads=16 +``` + +另一个系统的启动参数为: + +``` +JAVA_OPTS=-Xmx4g -Xms4g -XX:+UseG1GC -XX:MaxGCPauseMillis=50 +``` + +具体如何设置, 需要考虑的因素包括: + +- 系统容量: 业务规模, 并发, 成本预算; 需要兼顾性能与成本; +- 延迟要求: 最坏情况下能接受多少时间的延迟尖刺。 +- 吞吐量: 根据业务特征来确定, 比如, 网关, 大数据底层平台, 批处理作业系统, 在线实时应用, 他们最重要的需求不一样。 +- 系统架构: 比如拆分为小内存更多节点, 还是大内存少量节点。 +- 其他... + + +### 5.(选做) + +例如使用以下命令: + +``` +# 编译 +javac -g GCLogAnalysis.java +# JDK8 启动程序 +java -Xmx2g -Xms2g -XX:+UseG1GC -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:gc.log GCLogAnalysis +``` + +尝试使用课程中介绍的各种工具JDK命令行和图形工具来进行分析。 + +其中 [GCLogAnalysis.java](./GCLogAnalysis.java) 文件也可以从课件资料zip中找到. + +## 几个命令用法 +### 1、十六进制方式查看文件 +`hexdump -C Hello.class` +输出:`00000000 ca fe ba be 00 00 00 34 00 1c 0a 00 06 00 0e 09` + +可以看到magic number: `cafe babe`, +以及`00 00 00 34`,十六进制34=十进制3*16+4=52,这是jdk8,如果是jdk11则是55,十六进制37. + +### 2、Base64方式编码文件 +`base64 Hello.class` +### 3、显示JVM默认参数 +``` +java -XX:+PrintFlagsFinal -version + +java -XX:+PrintFlagsFinal -version | grep -F " Use" | grep -F "GC " + +java -XX:+PrintFlagsFinal -version | grep MaxNewSize + +``` + +### 4、切换不同jdk +``` +jenv shell 1.8 +jenv shell 11 +``` +显示所有jdk +``` +jenv versions +``` + +## 更多资料 + +更多中英文的技术文章和参考资料: + diff --git a/01jvm/TestAddUrl.java b/01jvm/TestAddUrl.java new file mode 100644 index 00000000..b4c3dc2e --- /dev/null +++ b/01jvm/TestAddUrl.java @@ -0,0 +1,22 @@ + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +public class TestAddUrl { + + public static void main(String[] args) throws Exception { + URLClassLoader classLoader = (URLClassLoader) TestAddUrl.class.getClassLoader(); + String dir = "./lib"; + Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); + method.setAccessible(true); + method.invoke(classLoader, new File(dir).getAbsoluteFile().toURL()); + + Class klass = Class.forName("HelloKimmking",true, classLoader); + Object obj = klass.newInstance(); + Method hello = klass.getDeclaredMethod("hello"); + hello.invoke(obj); + } + +} diff --git a/01jvm/XlassLoader.java b/01jvm/XlassLoader.java new file mode 100644 index 00000000..719b6dc5 --- /dev/null +++ b/01jvm/XlassLoader.java @@ -0,0 +1,75 @@ +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; + +/* + 第一周作业: + 2.(必做)自定义一个 Classloader,加载一个 lib.Hello.xlass 文件,执行 hello 方法,此文件内容是一个 lib.Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。 + */ +public class XlassLoader extends ClassLoader { + + public static void main(String[] args) throws Exception { + // 相关参数 + final String packageName = "lib"; + final String className = "Hello"; + final String methodName = "hello"; + // 创建类加载器 + ClassLoader classLoader = new XlassLoader(); + // 加载相应的类 + Class clazz = classLoader.loadClass(packageName + "." + className); + // 看看里面有些什么方法 + for (Method m : clazz.getDeclaredMethods()) { + System.out.println(clazz.getSimpleName() + "." + m.getName()); + } + // 创建对象 + Object instance = clazz.getDeclaredConstructor().newInstance(); + // 调用实例方法 + Method method = clazz.getMethod(methodName); + method.invoke(instance); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + // 如果支持包名, 则需要进行路径转换 + String resourcePath = name.replace(".", "/"); + // 文件后缀 + final String suffix = ".xlass"; + // 获取输入流 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourcePath + suffix); + try { + // 读取数据 + int length = inputStream.available(); + byte[] byteArray = new byte[length]; + inputStream.read(byteArray); + // 转换 + byte[] classBytes = decode(byteArray); + // 通知底层定义这个类 + return defineClass(name, classBytes, 0, classBytes.length); + } catch (IOException e) { + throw new ClassNotFoundException(name, e); + } finally { + close(inputStream); + } + } + + // 解码 + private static byte[] decode(byte[] byteArray) { + byte[] targetArray = new byte[byteArray.length]; + for (int i = 0; i < byteArray.length; i++) { + targetArray[i] = (byte) (255 - byteArray[i]); + } + return targetArray; + } + + // 关闭 + private static void close(Closeable res) { + if (null != res) { + try { + res.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/01jvm/jvm/HelloClassLoader.java b/01jvm/jvm/HelloClassLoader.java new file mode 100644 index 00000000..51d906f1 --- /dev/null +++ b/01jvm/jvm/HelloClassLoader.java @@ -0,0 +1,22 @@ +package jvm; + +import java.util.Base64; + +public class HelloClassLoader extends ClassLoader { + + public static void main(String[] args) throws Exception { + + new HelloClassLoader().findClass("lib.Hello").newInstance(); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + String helloBase64 = "yv66vgAAADQAHwoABwAQCQARABIIABMKABQAFQgAFgcAFwcAGAEABjxpbml0PgEAAygpVgEABENvZGUBAA9MaW5lTnVtYmVyVGFibGUBAAVoZWxsbwEACDxjbGluaXQ+AQAKU291cmNlRmlsZQEACkhlbGxvLmphdmEMAAgACQcAGQwAGgAbAQAdSGVsbG8gY2xhc3Mgc2F5IGhlbGxvIG1ldGhvZC4HABwMAB0AHgEAGEhlbGxvIENsYXNzIEluaXRpYWxpemVkIQEACWxpYi9IZWxsbwEAEGphdmEvbGFuZy9PYmplY3QBABBqYXZhL2xhbmcvU3lzdGVtAQADb3V0AQAVTGphdmEvaW8vUHJpbnRTdHJlYW07AQATamF2YS9pby9QcmludFN0cmVhbQEAB3ByaW50bG4BABUoTGphdmEvbGFuZy9TdHJpbmc7KVYAIQAGAAcAAAAAAAMAAQAIAAkAAQAKAAAAHQABAAEAAAAFKrcAAbEAAAABAAsAAAAGAAEAAAADAAEADAAJAAEACgAAACUAAgABAAAACbIAAhIDtgAEsQAAAAEACwAAAAoAAgAAAAgACAAJAAgADQAJAAEACgAAACUAAgAAAAAACbIAAhIFtgAEsQAAAAEACwAAAAoAAgAAAAUACAAGAAEADgAAAAIADw==+AQAKU291cmNlRmlsZQEACkhlbGxvLmphdmEMAAgACQcAGQwAGgAbAQAdSGVsbG8gY2xhc3Mgc2F5IGhlbGxvIG1ldGhvZC4HABwMAB0AHgEAGEhlbGxvIENsYXNzIEluaXRpYWxpemVkIQEACWxpYi9IZWxsbwEAEGphdmEvbGFuZy9PYmplY3QBABBqYXZhL2xhbmcvU3lzdGVtAQADb3V0AQAVTGphdmEvaW8vUHJpbnRTdHJlYW07AQATamF2YS9pby9QcmludFN0cmVhbQEAB3ByaW50bG4BABUoTGphdmEvbGFuZy9TdHJpbmc7KVYAIQAGAAcAAAAAAAMAAQAIAAkAAQAKAAAAHQABAAEAAAAFKrcAAbEAAAABAAsAAAAGAAEAAAADAAEADAAJAAEACgAAACUAAgABAAAACbIAAhIDtgAEsQAAAAEACwAAAAoAAgAAAAgACAAJAAgADQAJAAEACgAAACUAAgAAAAAACbIAAhIFtgAEsQAAAAEACwAAAAoAAgAAAAUACAAGAAEADgAAAAIADw=="; + byte[] bytes = decode(helloBase64); + return defineClass(name,bytes,0,bytes.length); + } + + public byte[] decode(String base64) { + return Base64.getDecoder().decode(base64); + } +} diff --git a/01jvm/jvm/TestMem.java b/01jvm/jvm/TestMem.java new file mode 100644 index 00000000..cb915121 --- /dev/null +++ b/01jvm/jvm/TestMem.java @@ -0,0 +1,11 @@ +package jvm; + +public class TestMem { + public static void main(String[] args) { + int[] arr1 = new int[256]; + int[][] arr2 = new int[128][2]; + int[][][] arr3 = new int[64][2][2]; + + System.out.println(); + } +} diff --git a/01jvm/lib/Hello.java b/01jvm/lib/Hello.java new file mode 100644 index 00000000..5d951d97 --- /dev/null +++ b/01jvm/lib/Hello.java @@ -0,0 +1,12 @@ +package lib; + +public class Hello { + static { + System.out.println("Hello Class Initialized!"); + } + public void hello() { + System.out.println("Hello class say hello method."); + System.gc(); // JMX MBean server + } + +} \ No newline at end of file diff --git a/01jvm/lib/Hello.xlass b/01jvm/lib/Hello.xlass new file mode 100644 index 00000000..fa659481 --- /dev/null +++ b/01jvm/lib/Hello.xlass @@ -0,0 +1 @@ +5EAÖ֩𳖑Üѕⷚߜߌߗߒ線߼߶зГаГЬ곕ЖЯ앞ЖЯ׳ГЬ֩HNMINMIN \ No newline at end of file diff --git a/01jvm/lib/HelloKimmking.java b/01jvm/lib/HelloKimmking.java new file mode 100644 index 00000000..1e970fd7 --- /dev/null +++ b/01jvm/lib/HelloKimmking.java @@ -0,0 +1,6 @@ + +public class HelloKimmking { + public void hello() { + System.out.println("hello,kimmking."); + } +} diff --git a/01jvm/out/production/01jvm/README.md b/01jvm/out/production/01jvm/README.md new file mode 100644 index 00000000..51c885e3 --- /dev/null +++ b/01jvm/out/production/01jvm/README.md @@ -0,0 +1,149 @@ +# 第1周作业 + + +参见 我的教室 -> 本周作业 + +## 作业内容 + + +> Week01 作业题目: + +1.(必做)自己写一个简单的 HelloNum.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。 + +2.(必做)自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法,此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。 + +3.(必做)画一张图,展示 Xmx、Xms、Xmn、Meta、DirectMemory、Xss 这些内存参数的关系。 + +4.(选做)检查一下自己维护的业务系统的 JVM 参数配置,用 jstat 和 jstack、jmap 查看一下详情,并且自己独立分析一下大概情况,思考有没有不合理的地方,如何改进。 + +注意:如果没有线上系统,可以自己 run 一个 web/java 项目。 + +5.(选做)本机使用 G1 GC 启动一个程序,仿照课上案例分析一下 JVM 情况。 + + + +## 操作步骤 + + +### 作业1(必做) + +1. 编写代码, 根据自己的意愿随意编写, 可参考: [HelloNum.java](./HelloNum.java) +2. 编译代码, 执行命令: `javac -g HelloNum.java` +3. 查看反编译的代码。 + - 3.1 可以安装并使用idea的jclasslib插件, 选中 [HelloNum.java](./HelloNum.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。 + - 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -c -v -p -l HelloNum.class` +4. 分析相关的字节码。【此步骤需要各位同学自己进行分析】 + + +### 作业2(必做) + +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息, 生成 maven 项目; 下载并解压。 +4. Idea或者Eclipse从已有的Source导入Maven项目。 +5. 从课件资料中找到资源 Hello.xlass 文件并复制到 src/main/resources 目录。 +6. 编写代码,实现 findClass 方法,以及对应的解码方法 +7. 编写main方法,调用 loadClass 方法; +8. 创建实例,以及调用方法 +9. 执行. + +具体代码可参考: [XlassLoader.java](./XlassLoader.java) + + +### 作业3(必做) + +对应的图片需要各位同学自己绘制,可以部分参考PPT课件。 + +提示: + +- Xms 设置堆内存的初始值 +- Xmx 设置堆内存的最大值 +- Xmn 设置堆内存中的年轻代的最大值 +- Meta 区不属于堆内存, 归属为非堆 +- DirectMemory 直接内存, 属于 JVM 内存中开辟出来的本地内存空间。 +- Xss设置的是单个线程栈的最大空间; + +JVM进程空间中的内存一般来说包括以下这些部分: + +- 堆内存(Xms ~ Xmx) = 年轻代(~Xmn) + 老年代 +- 非堆 = Meta + CodeCache + ... +- Native内存 = 直接内存 + Native + ... +- 栈内存 = n * Xss + +另外,注意区分规范与实现的区别, 需要根据具体实现以及版本, 才能确定。 一般来说,我们的目的是为了排查故障和诊断问题,大致弄清楚这些参数和空间的关系即可。 具体设置时还需要留一些冗余量。 + + +### 4.(选做) + +这个是具体案例分析, 请各位同学自己分析。 + +比如我们一个生产系统应用的启动参数为: + +``` +JAVA_OPTS=-Xmx200g -Xms200g -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -XX:ZCollectionInterval=30 -XX:ZAllocationSpikeTolerance=5 -XX:ReservedCodeCacheSize=2g -XX:InitialCodeCacheSize=2g -XX:ConcGCThreads=8 -XX:ParallelGCThreads=16 +``` + +另一个系统的启动参数为: + +``` +JAVA_OPTS=-Xmx4g -Xms4g -XX:+UseG1GC -XX:MaxGCPauseMillis=50 +``` + +具体如何设置, 需要考虑的因素包括: + +- 系统容量: 业务规模, 并发, 成本预算; 需要兼顾性能与成本; +- 延迟要求: 最坏情况下能接受多少时间的延迟尖刺。 +- 吞吐量: 根据业务特征来确定, 比如, 网关, 大数据底层平台, 批处理作业系统, 在线实时应用, 他们最重要的需求不一样。 +- 系统架构: 比如拆分为小内存更多节点, 还是大内存少量节点。 +- 其他... + + +### 5.(选做) + +例如使用以下命令: + +``` +# 编译 +javac -g GCLogAnalysis.java +# JDK8 启动程序 +java -Xmx2g -Xms2g -XX:+UseG1GC -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:gc.log GCLogAnalysis +``` + +尝试使用课程中介绍的各种工具JDK命令行和图形工具来进行分析。 + +其中 [GCLogAnalysis.java](./GCLogAnalysis.java) 文件也可以从课件资料zip中找到. + +## 几个命令用法 +### 1、十六进制方式查看文件 +`hexdump -C Hello.class` +输出:`00000000 ca fe ba be 00 00 00 34 00 1c 0a 00 06 00 0e 09` + +可以看到magic number: `cafe babe`, +以及`00 00 00 34`,十六进制34=十进制3*16+4=52,这是jdk8,如果是jdk11则是55,十六进制37. + +### 2、Base64方式编码文件 +`base64 Hello.class` +### 3、显示JVM默认参数 +``` +java -XX:+PrintFlagsFinal -version + +java -XX:+PrintFlagsFinal -version | grep -F " Use" | grep -F "GC " + +java -XX:+PrintFlagsFinal -version | grep MaxNewSize + +``` + +### 4、切换不同jdk +``` +jenv shell 1.8 +jenv shell 11 +``` +显示所有jdk +``` +jenv versions +``` + +## 更多资料 + +更多中英文的技术文章和参考资料: + diff --git "a/01jvm/out/production/01jvm/\347\216\257\345\242\203\345\207\206\345\244\207.txt" "b/01jvm/out/production/01jvm/\347\216\257\345\242\203\345\207\206\345\244\207.txt" new file mode 100644 index 00000000..a1458b85 --- /dev/null +++ "b/01jvm/out/production/01jvm/\347\216\257\345\242\203\345\207\206\345\244\207.txt" @@ -0,0 +1,38 @@ + + +## Windows + +1.管理员身份打开powershell + +2.运行 +Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) + +3.执行choco install superbenchmarker + +4.输入 sb + +执行 sb -u http://localhost:8088/api/hello -c 20 -N 60 + +## Mac + +1.执行brew install wrk +如果显式brew update很慢,可以ctrl+C打断更新 + +2.输入 wrk + +执行 wrk -t8 -c40 -d60s http://localhost:8088/api/hello + +## 压测程序 + +1.可以从github获取 +git clone https://github.com/kimmking/atlantis +cd atlantis\gateway-server +mvn clean package +然后在target目录可以找到gateway-server-0.0.1-SNAPSHOT.jar + +2.也可以从此处下载已经编译好的: +链接:https://pan.baidu.com/s/1NbpYX4M3YKLYM1JJeIzgSQ +提取码:sp85 + +java -jar -Xmx512m -Xms512 gateway-server-0.0.1-SNAPSHOT.jar + diff --git a/01jvm/subinterface/Mapper.java b/01jvm/subinterface/Mapper.java new file mode 100644 index 00000000..b6578fb5 --- /dev/null +++ b/01jvm/subinterface/Mapper.java @@ -0,0 +1,7 @@ +package subinterface; + +public interface Mapper { + + void insert(T t); + +} diff --git a/01jvm/subinterface/User.java b/01jvm/subinterface/User.java new file mode 100644 index 00000000..2a6a541f --- /dev/null +++ b/01jvm/subinterface/User.java @@ -0,0 +1,17 @@ +package subinterface; + +public class User { + private int id; + + public User(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/01jvm/subinterface/UserDAO.java b/01jvm/subinterface/UserDAO.java new file mode 100644 index 00000000..76dd2fb6 --- /dev/null +++ b/01jvm/subinterface/UserDAO.java @@ -0,0 +1,9 @@ +package subinterface; + +public class UserDAO implements UserMapper { + + @Override + public void insert(User user) { + System.out.println("Insert user id: " + user.getId()); + } +} diff --git a/01jvm/subinterface/UserMain.java b/01jvm/subinterface/UserMain.java new file mode 100644 index 00000000..4ebd24cd --- /dev/null +++ b/01jvm/subinterface/UserMain.java @@ -0,0 +1,10 @@ +package subinterface; + +public class UserMain { + + public static void main(String[] args) { + UserDAO dao = new UserDAO(); + dao.insert(new User(123)); + } + +} diff --git a/01jvm/subinterface/UserMapper.java b/01jvm/subinterface/UserMapper.java new file mode 100644 index 00000000..573c0abc --- /dev/null +++ b/01jvm/subinterface/UserMapper.java @@ -0,0 +1,7 @@ +package subinterface; + +public interface UserMapper extends Mapper { + + @Override + void insert(User user); +} diff --git a/02nio/GCLogAnalysis.java b/02nio/GCLogAnalysis.java new file mode 100644 index 00000000..d2570b27 --- /dev/null +++ b/02nio/GCLogAnalysis.java @@ -0,0 +1,63 @@ +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.LongAdder; +/* +演示GC日志生成与解读 +*/ +public class GCLogAnalysis { + // 随机数; 记得这里可以设置随机数种子; + private static Random random = new Random(); + public static void main(String[] args) { + // 当前毫秒时间戳 + long startMillis = System.currentTimeMillis(); + // 持续运行毫秒数; 可根据需要进行修改 + long timeoutMillis = TimeUnit.SECONDS.toMillis(1); + // 结束时间戳 + long endMillis = startMillis + timeoutMillis; + LongAdder counter = new LongAdder(); + System.out.println("正在执行..."); + // 缓存一部分对象; 进入老年代 + int cacheSize = 2000; + Object[] cachedGarbage = new Object[cacheSize]; + // 在此时间范围内,持续循环 + while (System.currentTimeMillis() < endMillis) { + // 生成垃圾对象 + Object garbage = generateGarbage(100*1024); + counter.increment(); + int randomIndex = random.nextInt(2 * cacheSize); + if (randomIndex < cacheSize) { + cachedGarbage[randomIndex] = garbage; + } + } + System.out.println("执行结束!共生成对象次数:" + counter.longValue()); + } + + // 生成对象 + private static Object generateGarbage(int max) { + int randomSize = random.nextInt(max); + int type = randomSize % 4; + Object result = null; + switch (type) { + case 0: + result = new int[randomSize]; + break; + case 1: + result = new byte[randomSize]; + break; + case 2: + result = new double[randomSize]; + break; + default: + StringBuilder builder = new StringBuilder(); + String randomString = "randomString-Anything"; + while (builder.length() < randomSize) { + builder.append(randomString); + builder.append(max); + builder.append(randomSize); + } + result = builder.toString(); + break; + } + return result; + } +} diff --git a/02nio/README.md b/02nio/README.md index 0681f025..79e9f518 100644 --- a/02nio/README.md +++ b/02nio/README.md @@ -3,24 +3,21 @@ ## 作业内容 -> Week02 作业题目(周四): +> Week02 作业题目: -1.使用 GCLogAnalysis.java 自己演练一遍串行 / 并行 /CMS/G1 的案例。 -2.使用压测工具(wrk 或 sb),演练 gateway-server-0.0.1-SNAPSHOT.jar 示例。 -3.(选做) 如果自己本地有可以运行的项目,可以按照 2 的方式进行演练。 -4.(必做) 根据上述自己对于 1 和 2 的演示,写一段对于不同 GC 的总结,提交到 Github。 +1. (选做)使用 [GCLogAnalysis.java](./GCLogAnalysis.java) 自己演练一遍 串行/并行/CMS/G1 的案例。 +2. (选做)使用压测工具(wrk 或 sb),演练 gateway-server-0.0.1-SNAPSHOT.jar 示例。 +3. (选做)如果自己本地有可以运行的项目,可以按照 2 的方式进行演练。 +4. (必做)根据上述自己对于 1 和 2 的演示,写一段对于不同 GC 和堆内存的总结,提交到 GitHub。 +5. (选做)运行课上的例子,以及 Netty 的例子,分析相关现象。 +6. (必做)写一段代码,使用 HttpClient 或 OkHttp 访问 http://localhost:8801 ,代码提交到 GitHub -> Week02 作业题目(周六): - -1.(选做)运行课上的例子,以及 Netty 的例子,分析相关现象。 - -2.(必做)写一段代码,使用 HttpClient 或 OkHttp 访问 http://localhost:8801 ,代码提交到 Github。 ## 操作步骤 -### 第二周-周六-作业2 +### 第二周-作业6.(必做) 1. 打开 Spring 官网: https://spring.io/ 2. 找到 Projects --> Spring Initializr: https://start.spring.io/ diff --git a/02nio/nio01/dependency-reduced-pom.xml b/02nio/nio01/dependency-reduced-pom.xml new file mode 100644 index 00000000..1646aa28 --- /dev/null +++ b/02nio/nio01/dependency-reduced-pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + java0.nio01 + nio01 + 1.0 + + + + maven-compiler-plugin + + ${maven.compile.source} + ${maven.compile.target} + + + + maven-shade-plugin + 3.2.0 + + + + shade + + + + + + ${app.main.class} + ${maven.compile.source} + ${maven.compile.target} + + + + + + + + + + + Main + 8 + 8 + + diff --git a/02nio/nio01/pom.xml b/02nio/nio01/pom.xml index 0fc8ec7e..4fb902dd 100644 --- a/02nio/nio01/pom.xml +++ b/02nio/nio01/pom.xml @@ -7,18 +7,69 @@ java0.nio01 nio01 1.0 + + + Main + 8 + 8 + + org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + ${maven.compile.source} + ${maven.compile.target} + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.0 + + + + shade + + + + + + ${app.main.class} + ${maven.compile.source} + ${maven.compile.target} + + + + + + + + + + + io.netty + netty-all + 4.1.51.Final + + + com.squareup.okhttp3 + okhttp + 3.12.0 + + + org.apache.httpcomponents + httpclient + 4.5.5 + + + + \ No newline at end of file diff --git a/02nio/nio01/src/main/java/Main.java b/02nio/nio01/src/main/java/Main.java new file mode 100644 index 00000000..b07dca92 --- /dev/null +++ b/02nio/nio01/src/main/java/Main.java @@ -0,0 +1,34 @@ +import java0.nio01.HttpServer01; +import java0.nio01.HttpServer02; +import java0.nio01.HttpServer03; +import java0.nio01.netty.NettyHttpServer; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Main { + + public static void main(String[] args) { + Map map = new HashMap<>(); + map.put("1", HttpServer01.class); + map.put("2", HttpServer02.class); + map.put("3", HttpServer03.class); + map.put("8", NettyHttpServer.class); + + String id = (null == args || args.length == 0) ? "1" : args[0]; + Class clazz = map.get(id); + if( null == clazz ) { + System.out.println("No class for id: " + id); + } + + try { + Method method = clazz.getDeclaredMethod("main", new Class[]{String[].class}); + method.invoke(null, new Object[]{new String[]{}}); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpClientDemo.java b/02nio/nio01/src/main/java/java0/nio01/HttpClientDemo.java new file mode 100644 index 00000000..43f5c9ed --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/HttpClientDemo.java @@ -0,0 +1,51 @@ +package java0.nio01; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +public class HttpClientDemo { + + public static void main(String[] args) throws IOException { + + byte[] bytes = getBody1( "http://localhost:8801"); + System.out.println(new String(bytes)); + + } + + private static byte[] getBody1(String url){ + HttpGet httpGet = new HttpGet(url); + httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); + CloseableHttpClient httpClient = null; + CloseableHttpResponse response = null; + try { + httpClient = HttpClients.createDefault(); + response = httpClient.execute(httpGet); + HttpEntity entity = response.getEntity(); + // System.out.println(EntityUtils.toString(entity)); + return EntityUtils.toByteArray(entity); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + // 释放资源 + if (response != null) { + response.close(); + } + if (httpClient != null) { + httpClient.close(); + } + httpGet.releaseConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } +} diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java b/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java index c9cc060f..bed0f8a4 100644 --- a/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java +++ b/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java @@ -5,6 +5,7 @@ import java.net.ServerSocket; import java.net.Socket; +// 单线程的socket程序 public class HttpServer01 { public static void main(String[] args) throws IOException{ ServerSocket serverSocket = new ServerSocket(8801); @@ -20,17 +21,17 @@ public static void main(String[] args) throws IOException{ private static void service(Socket socket) { try { - Thread.sleep(20); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println("HTTP/1.1 200 OK"); printWriter.println("Content-Type:text/html;charset=utf-8"); - String body = "hello,nio"; + String body = "hello,nio1"; printWriter.println("Content-Length:" + body.getBytes().length); printWriter.println(); printWriter.write(body); + printWriter.flush(); printWriter.close(); socket.close(); - } catch (IOException | InterruptedException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java b/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java index 9b51cf29..298814ac 100644 --- a/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java +++ b/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java @@ -5,6 +5,7 @@ import java.net.ServerSocket; import java.net.Socket; +// 每个请求一个线程 public class HttpServer02 { public static void main(String[] args) throws IOException{ ServerSocket serverSocket = new ServerSocket(8802); @@ -22,17 +23,16 @@ public static void main(String[] args) throws IOException{ private static void service(Socket socket) { try { - Thread.sleep(20); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println("HTTP/1.1 200 OK"); printWriter.println("Content-Type:text/html;charset=utf-8"); - String body = "hello,nio"; + String body = "hello,nio2"; printWriter.println("Content-Length:" + body.getBytes().length); printWriter.println(); printWriter.write(body); printWriter.close(); socket.close(); - } catch (IOException | InterruptedException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java b/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java index fad136a8..bd9b4acb 100644 --- a/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java +++ b/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java @@ -7,9 +7,11 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +// 创建了一个固定大小的线程池处理请求 public class HttpServer03 { public static void main(String[] args) throws IOException{ - ExecutorService executorService = Executors.newFixedThreadPool(40); + ExecutorService executorService = Executors.newFixedThreadPool( + Runtime.getRuntime().availableProcessors() * 4); final ServerSocket serverSocket = new ServerSocket(8803); while (true) { try { @@ -23,17 +25,16 @@ public static void main(String[] args) throws IOException{ private static void service(Socket socket) { try { - Thread.sleep(20); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println("HTTP/1.1 200 OK"); printWriter.println("Content-Type:text/html;charset=utf-8"); - String body = "hello,nio"; + String body = "hello,nio3"; printWriter.println("Content-Length:" + body.getBytes().length); printWriter.println(); printWriter.write(body); printWriter.close(); socket.close(); - } catch (IOException | InterruptedException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/02nio/nio01/src/main/java/java0/nio01/OKHttpClientDemo.java b/02nio/nio01/src/main/java/java0/nio01/OKHttpClientDemo.java new file mode 100644 index 00000000..98678754 --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/OKHttpClientDemo.java @@ -0,0 +1,35 @@ +package java0.nio01; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import java.io.IOException; + +public class OKHttpClientDemo { + + private static OkHttpClient client = new OkHttpClient(); + public static void main(String[] args) throws IOException { + + getBody1(client, "http://localhost:8801"); + client = null; + } + + private static void getBody1(OkHttpClient client, String url){ + + Request request = new Request.Builder() + .get() + .url(url) + .build(); + //String body = "test"; + try { + Response response = client.newCall(request).execute(); + String responseData = response.body().string(); + System.out.println(responseData); + } catch (IOException e) { + e.printStackTrace(); + }finally { + client = null; + } + } +} diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java new file mode 100644 index 00000000..81f31d2e --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java @@ -0,0 +1,81 @@ +package java0.nio01.netty; + +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.util.ReferenceCountUtil; + +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; +import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +public class HttpHandler extends ChannelInboundHandlerAdapter { + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) { + ctx.flush(); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + try { + //logger.info("channelRead流量接口请求开始,时间为{}", startTime); + FullHttpRequest fullRequest = (FullHttpRequest) msg; + String uri = fullRequest.uri(); + //logger.info("接收到的请求url为{}", uri); + if (uri.contains("/test")) { + handlerTest(fullRequest, ctx, "hello,kimmking"); + } else { + handlerTest(fullRequest, ctx, "hello,others"); + } + + } catch(Exception e) { + e.printStackTrace(); + } finally { + ReferenceCountUtil.release(msg); + } + } + + private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx, String body) { + FullHttpResponse response = null; + try { + String value = body; // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据 + +// httpGet ... http://localhost:8801 +// 返回的响应,"hello,nio"; +// value = reponse.... + + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8"))); + response.headers().set("Content-Type", "application/json"); + response.headers().setInt("Content-Length", response.content().readableBytes()); + + } catch (Exception e) { + System.out.println("处理出错:"+e.getMessage()); + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); + } finally { + if (fullRequest != null) { + if (!HttpUtil.isKeepAlive(fullRequest)) { + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } else { + response.headers().set(CONNECTION, KEEP_ALIVE); + ctx.write(response); + } + ctx.flush(); + } + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + +} diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/HttpInitializer.java b/02nio/nio01/src/main/java/java0/nio01/netty/HttpInitializer.java new file mode 100644 index 00000000..ff4ee0be --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/netty/HttpInitializer.java @@ -0,0 +1,19 @@ +package java0.nio01.netty; + +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; + +public class HttpInitializer extends ChannelInitializer { + + @Override + public void initChannel(SocketChannel ch) { + ChannelPipeline p = ch.pipeline(); + p.addLast(new HttpServerCodec()); + //p.addLast(new HttpServerExpectContinueHandler()); + p.addLast(new HttpObjectAggregator(1024 * 1024)); + p.addLast(new HttpHandler()); + } +} diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/NettyHttpServer.java b/02nio/nio01/src/main/java/java0/nio01/netty/NettyHttpServer.java new file mode 100644 index 00000000..487d7283 --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/netty/NettyHttpServer.java @@ -0,0 +1,48 @@ +package java0.nio01.netty; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.Channel; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.epoll.EpollChannelOption; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; + +public class NettyHttpServer { + public static void main(String[] args) throws InterruptedException { + + int port = 8808; + + EventLoopGroup bossGroup = new NioEventLoopGroup(2); + EventLoopGroup workerGroup = new NioEventLoopGroup(16); + + try { + ServerBootstrap b = new ServerBootstrap(); + b.option(ChannelOption.SO_BACKLOG, 128) + .childOption(ChannelOption.TCP_NODELAY, true) + .childOption(ChannelOption.SO_KEEPALIVE, true) + .childOption(ChannelOption.SO_REUSEADDR, true) + .childOption(ChannelOption.SO_RCVBUF, 32 * 1024) + .childOption(ChannelOption.SO_SNDBUF, 32 * 1024) + .childOption(EpollChannelOption.SO_REUSEPORT, true) + .childOption(ChannelOption.SO_KEEPALIVE, true) + .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) + .handler(new LoggingHandler(LogLevel.INFO)) + .childHandler(new HttpInitializer()); + + Channel ch = b.bind(port).sync().channel(); + System.out.println("开启netty http服务器,监听地址和端口为 http://127.0.0.1:" + port + '/'); + ch.closeFuture().sync(); + } finally { + bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); + } + + + } +} diff --git a/02nio/nio02/pom.xml b/02nio/nio02/pom.xml index 6cbbeffd..c10848f8 100644 --- a/02nio/nio02/pom.xml +++ b/02nio/nio02/pom.xml @@ -29,7 +29,7 @@ io.netty netty-all - 4.1.45.Final + 4.1.104.Final @@ -52,6 +52,11 @@ httpasyncclient 4.1.4 + + + org.projectlombok + lombok + + + io.kimmking.javacourse + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + 1.18.22 + compile + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/04fx/demo/src/main/java/A.java b/04fx/demo/src/main/java/A.java new file mode 100644 index 00000000..117e905c --- /dev/null +++ b/04fx/demo/src/main/java/A.java @@ -0,0 +1,8 @@ +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/12/16 10:55 + */ +public class A { +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfig.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfig.java new file mode 100644 index 00000000..c506234e --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfig.java @@ -0,0 +1,18 @@ +package io.kimmking.javacourse.demo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:05 + */ +@Data +public class ConsConfig { + + private String demoName; + private String demoDesc; + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigAutoConfiguration.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigAutoConfiguration.java new file mode 100644 index 00000000..4461b3a8 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigAutoConfiguration.java @@ -0,0 +1,71 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.beans.factory.config.ConstructorArgumentValues; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotationMetadata; + + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/12/22 16:43 + */ + +@Configuration +//@EnableConfigurationProperties(ConsConfigs.class) +@Import(ConsConfigAutoConfiguration.ConsRegistrar.class) +public class ConsConfigAutoConfiguration { + public static class ConsRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { + + Environment environment; +// ApplicationContext applicationContext; +// @Override +// public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { +// this.applicationContext = applicationContext; +// } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + //ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry); + + RootBeanDefinition def = new RootBeanDefinition(); + def.setBeanClass(ConsConfig.class); + registry.registerBeanDefinition("cons", def); + + RootBeanDefinition definition = new RootBeanDefinition(); + definition.setBeanClass(ConsConfigs.class); + definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); + definition.setDependencyCheck(AbstractBeanDefinition.DEPENDENCY_CHECK_NONE); + //ConstructorArgumentValues argumentValues = new ConstructorArgumentValues(); + + //ConsConfig config= applicationContext.getBean(ConsConfig.class); +// String name = ConsConfig.class.getCanonicalName(); +// boolean exist = registry.containsBeanDefinition(name); +// if(exist) { +// argumentValues.addGenericArgumentValue(registry.getBeanDefinition(name)); +// } else { +// ConsConfig cf = new ConsConfig(); +// cf.setDemoName("defaultName1"); +// cf.setDemoDesc("defaultDesc1"); +// argumentValues.addGenericArgumentValue(cf); +// } + //definition.setConstructorArgumentValues(argumentValues); + registry.registerBeanDefinition("conss", definition); + } + } +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigs.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigs.java new file mode 100644 index 00000000..8ef243e6 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigs.java @@ -0,0 +1,20 @@ +package io.kimmking.javacourse.demo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/12/22 16:47 + */ +@Data +@AllArgsConstructor +public class ConsConfigs { + + private List config; + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoApplication.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoApplication.java new file mode 100644 index 00000000..401643fc --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoApplication.java @@ -0,0 +1,72 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.ApplicationContext; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; + +import javax.annotation.Resource; + +@SpringBootApplication +public class DemoApplication implements EnvironmentAware, ApplicationRunner { + + Environment environment; + + @Resource(name="a1") + DemoConfig a1; + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + @Autowired + ApplicationContext context; + + @Bean + //@ConditionalOnMissingBean + public ConsConfig defaultConsConfig() { + ConsConfig cf = new ConsConfig(); + cf.setDemoName("defaultName"); + cf.setDemoDesc("defaultDesc"); + return cf; + } + + @Override + public void run(ApplicationArguments args) throws Exception { + System.out.println(environment.getProperty("a")); + System.out.println(a1.getDemoName()); + + DemoConfig a4 = (DemoConfig) context.getBean("a4"); + System.out.println(a4.getDemoName()); + + System.out.println(context.getBean(ConsConfigs.class)); + + } + + @EnableDemoConfigBindings(prefix = "demo.config", type = DemoConfig.class) + @PropertySource("application.properties") + public static class TestDemoConfig { + + } + + @Bean("a4") + @ConditionalOnClass(name="A") + DemoConfig createA4() { + DemoConfig config = new DemoConfig(); + config.setDemoName("a4"); + config.setDemoDesc("this is a4"); + return config; + } +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfig.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfig.java new file mode 100644 index 00000000..fa484b74 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfig.java @@ -0,0 +1,17 @@ +package io.kimmking.javacourse.demo; + +import lombok.Data; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:05 + */ +@Data +public class DemoConfig { + + private String demoName; + private String demoDesc; + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfigBindingsRegistrar.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfigBindingsRegistrar.java new file mode 100644 index 00000000..2d201027 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfigBindingsRegistrar.java @@ -0,0 +1,127 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.factory.support.*; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +import java.util.*; + +import static io.kimmking.javacourse.demo.PropertySourcesUtils.getSubProperties; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; + +/** + * This class is cloned and modified from https://github.com/apache/dubbo/blob/58d1259cb9d89528594e43db5d8667179005dcfc/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrar.java. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:09 + */ +public class DemoConfigBindingsRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { + + private ConfigurableEnvironment environment; + + @Override + public void setEnvironment(Environment _environment) { + this.environment = (ConfigurableEnvironment) _environment; + } + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) { + registerBeanDefinitions(importingClassMetadata, registry); + } + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + + AnnotationAttributes attributes = AnnotationAttributes.fromMap( + importingClassMetadata.getAnnotationAttributes(EnableDemoConfigBindings.class.getName())); + String prefix = environment.resolvePlaceholders(attributes.getString("prefix")); + Class configClass = attributes.getClass("type"); + boolean multiple = attributes.getBoolean("multiple"); + registerDubboConfigBeans(prefix, configClass, multiple, registry); + } + + private void registerDubboConfigBeans(String prefix, + Class configClass, + boolean multiple, + BeanDefinitionRegistry registry) { + Map properties = getSubProperties(environment.getPropertySources(), prefix); + if (CollectionUtils.isEmpty(properties)) { + System.out.println("There is no property for binding to demo config class [" + configClass.getName() + + "] within prefix [" + prefix + "]"); + return; + } + Set beanNames = multiple ? resolveMultipleBeanNames(properties) : + Collections.singleton(resolveSingleBeanName(properties, configClass, registry)); + Map> groupProperties = getGroupProperties(properties, beanNames); + for (String beanName : beanNames) { + + System.out.println(" ==> registerDemoConfigBean:" + beanName); + + registerDemoConfigBean(beanName, configClass, registry, groupProperties.get(beanName)); + //registerDubboConfigBindingBeanPostProcessor(prefix, beanName, multiple, registry); + } + //registerDubboConfigBeanCustomizers(registry); + } + + private Map> getGroupProperties(Map properties, Set beanNames) { + Map> map = new HashMap<>(); + for (String propertyName : properties.keySet()) { + int index = propertyName.indexOf("."); + if (index > 0) { + String beanName = propertyName.substring(0, index); + String beanPropertyName = propertyName.substring(index + 1); + if (beanNames.contains(beanName)) { + Map group = map.get(beanName); + if (group == null) { + group = new HashMap<>(); + map.put(beanName, group); + } + group.put(beanPropertyName, properties.get(propertyName)); + } + } + } + return map; + } + + private void registerDemoConfigBean(String beanName, Class configClass, + BeanDefinitionRegistry registry, Map properties) { + BeanDefinitionBuilder builder = rootBeanDefinition(configClass); + AbstractBeanDefinition beanDefinition = builder.getBeanDefinition(); + // Convert Map to MutablePropertyValues + MutablePropertyValues propertyValues = new MutablePropertyValues(properties); + beanDefinition.setPropertyValues(propertyValues); + registry.registerBeanDefinition(beanName, beanDefinition); + System.out.println("The demo config bean definition [name : " + beanName + ", class : " + configClass.getName() + + "] has been registered."); + } + + private Set resolveMultipleBeanNames(Map properties) { + Set beanNames = new LinkedHashSet(); + for (String propertyName : properties.keySet()) { + int index = propertyName.indexOf("."); + if (index > 0) { + String beanName = propertyName.substring(0, index); + beanNames.add(beanName); + } + } + return beanNames; + } + + private String resolveSingleBeanName(Map properties, Class configClass, + BeanDefinitionRegistry registry) { + String beanName = (String) properties.get("demoName"); + if (!StringUtils.hasText(beanName)) { + BeanDefinitionBuilder builder = rootBeanDefinition(configClass); + beanName = BeanDefinitionReaderUtils.generateBeanName(builder.getRawBeanDefinition(), registry); + } + return beanName; + } + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/EnableDemoConfigBindings.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/EnableDemoConfigBindings.java new file mode 100644 index 00000000..3a002944 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/EnableDemoConfigBindings.java @@ -0,0 +1,36 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.context.annotation.Import; + +import java.lang.annotation.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:10 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(DemoConfigBindingsRegistrar.class) +public @interface EnableDemoConfigBindings { + /** + * The name prefix of the properties that are valid to bind. + * + * @return the name prefix of the properties to bind + */ + String prefix(); + + /** + * @return The binding type. + */ + Class type(); + + /** + * It indicates whether {@link #prefix()} binding to multiple Spring Beans. + * + * @return the default value is true + */ + boolean multiple() default true; +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/PropertySourcesUtils.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/PropertySourcesUtils.java new file mode 100644 index 00000000..f436313e --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/PropertySourcesUtils.java @@ -0,0 +1,91 @@ +package io.kimmking.javacourse.demo; + + +import org.springframework.core.env.*; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +/** + * {@link PropertySources} Utilities + *

+ * The source code is cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java + * + * @since 2.6.6 + */ +public abstract class PropertySourcesUtils { + + /** + * Get Sub {@link Properties} + * + * @param propertySources {@link PropertySource} Iterable + * @param prefix the prefix of property name + * @return Map + * @see Properties + */ + public static Map getSubProperties(Iterable> propertySources, String prefix) { + + // Non-Extension AbstractEnvironment + AbstractEnvironment environment = new AbstractEnvironment() { + }; + + MutablePropertySources mutablePropertySources = environment.getPropertySources(); + + for (PropertySource source : propertySources) { + mutablePropertySources.addLast(source); + } + + return getSubProperties(environment, prefix); + + } + + /** + * Get Sub {@link Properties} + * + * @param environment {@link ConfigurableEnvironment} + * @param prefix the prefix of property name + * @return Map + * @see Properties + */ + public static Map getSubProperties(ConfigurableEnvironment environment, String prefix) { + + Map subProperties = new LinkedHashMap(); + + MutablePropertySources propertySources = environment.getPropertySources(); + + String normalizedPrefix = normalizePrefix(prefix); + + for (PropertySource source : propertySources) { + if (source instanceof EnumerablePropertySource) { + for (String name : ((EnumerablePropertySource) source).getPropertyNames()) { + if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) { + String subName = name.substring(normalizedPrefix.length()); + if (!subProperties.containsKey(subName)) { // take first one + Object value = source.getProperty(name); + if (value instanceof String) { + // Resolve placeholder + value = environment.resolvePlaceholders((String) value); + } + subProperties.put(subName, value); + } + } + } + } + } + + return Collections.unmodifiableMap(subProperties); + + } + + /** + * Normalize the prefix + * + * @param prefix the prefix + * @return the prefix + */ + public static String normalizePrefix(String prefix) { + return prefix.endsWith(".") ? prefix : prefix + "."; + } +} diff --git a/04fx/demo/src/main/resources/application.properties b/04fx/demo/src/main/resources/application.properties new file mode 100644 index 00000000..f7a98d9a --- /dev/null +++ b/04fx/demo/src/main/resources/application.properties @@ -0,0 +1,9 @@ + +demo.config.a1.demoName = d1 +demo.config.a1.demoDesc = demo1 + +demo.config.a2.demoName = d2 +demo.config.a2.demoDesc = demo2 + +demo.config.a3.demoName = d3 +demo.config.a3.demoDesc = demo3 \ No newline at end of file diff --git a/04fx/demo/src/test/java/io/kimmking/javacourse/demo/DemoApplicationTests.java b/04fx/demo/src/test/java/io/kimmking/javacourse/demo/DemoApplicationTests.java new file mode 100644 index 00000000..5101ce0e --- /dev/null +++ b/04fx/demo/src/test/java/io/kimmking/javacourse/demo/DemoApplicationTests.java @@ -0,0 +1,51 @@ +package io.kimmking.javacourse.demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.PropertySource; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class DemoApplicationTests { + @Test + void testDemoConfig() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(DemoApplication.TestDemoConfig.class); + context.refresh(); + + + + System.out.println(Arrays.toString(context.getBeanNamesForType(DemoConfig.class))); + assertEquals(3, context.getBeansOfType(DemoConfig.class).size()); + + DemoConfig demoConfig = context.getBean("a1", DemoConfig.class); + System.out.println("a1=" + demoConfig.toString()); + assertEquals("d1", demoConfig.getDemoName()); + assertEquals("demo1", demoConfig.getDemoDesc()); + + demoConfig = context.getBean("a2", DemoConfig.class); + System.out.println("a2=" + demoConfig.toString()); + assertEquals("d2", demoConfig.getDemoName()); + assertEquals("demo2", demoConfig.getDemoDesc()); + + demoConfig = context.getBean("a3", DemoConfig.class); + System.out.println("a3=" + demoConfig.toString()); + assertEquals("d3", demoConfig.getDemoName()); + assertEquals("demo3", demoConfig.getDemoDesc()); + +// context.refresh(); +// System.out.println(Arrays.toString(context.getBeanNamesForType(DemoConfig.class))); + + } + +// @EnableDemoConfigBindings(prefix = "demo.config", type = DemoConfig.class) +// @PropertySource("application.properties") +// private static class TestConfig { +// +// } + +} diff --git a/04fx/dtx01/pom.xml b/04fx/dtx01/pom.xml new file mode 100644 index 00000000..9b8dcebc --- /dev/null +++ b/04fx/dtx01/pom.xml @@ -0,0 +1,117 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + + + io.kimmking + dtx01 + 0.0.1-SNAPSHOT + dtx01 + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + mysql + mysql-connector-java + 5.1.47 + + + + org.apache.dubbo + dubbo + 2.7.8 + + + + org.projectlombok + lombok + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/Dtx01Application.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/Dtx01Application.java new file mode 100644 index 00000000..8ae6a888 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/Dtx01Application.java @@ -0,0 +1,17 @@ +package io.kimmking.dtx01; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication(scanBasePackages = "io.kimmking.dtx01") +@MapperScan("io.kimmking.dtx01.mapper") +@EnableCaching +public class Dtx01Application { + + public static void main(String[] args) { + SpringApplication.run(Dtx01Application.class, args); + } + +} diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/controller/UserController.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/controller/UserController.java new file mode 100644 index 00000000..de4e9ac0 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/controller/UserController.java @@ -0,0 +1,31 @@ +package io.kimmking.dtx01.controller; + +import io.kimmking.dtx01.entity.User; +import io.kimmking.dtx01.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@EnableAutoConfiguration +public class UserController { + + @Autowired + UserService userService; + + @RequestMapping("/user/find") + User find(Long id) { + return userService.find(id); + //return new User(1,"KK", 28); + } + + @RequestMapping("/user/list") + List list() { + return userService.list(); +// return Arrays.asList(new User(1,"KK", 28), +// new User(2,"CC", 18)); + } +} \ No newline at end of file diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/entity/User.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/entity/User.java new file mode 100644 index 00000000..ae7f3a19 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/entity/User.java @@ -0,0 +1,16 @@ +package io.kimmking.dtx01.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User implements Serializable { + private Long id; + private String name; + private Integer age; +} \ No newline at end of file diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/mapper/UserMapper.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/mapper/UserMapper.java new file mode 100644 index 00000000..27705655 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/mapper/UserMapper.java @@ -0,0 +1,19 @@ +package io.kimmking.dtx01.mapper; + +import io.kimmking.dtx01.entity.User; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface UserMapper { + + @Select("select * from user where id = #{id};") + User find(@Param("id") Long id); + + @Select("select * from user;") + List list(); + +} diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserService.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserService.java new file mode 100644 index 00000000..f1df3180 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserService.java @@ -0,0 +1,13 @@ +package io.kimmking.dtx01.service; + +import io.kimmking.dtx01.entity.User; + +import java.util.List; + +public interface UserService { + + User find(Long id); + + List list(); + +} diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserServiceImpl.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserServiceImpl.java new file mode 100644 index 00000000..3d953bbf --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserServiceImpl.java @@ -0,0 +1,25 @@ +package io.kimmking.dtx01.service; + +import io.kimmking.dtx01.entity.User; +import io.kimmking.dtx01.mapper.UserMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + UserMapper userMapper; //DAO // Repository + + public User find(Long id) { + System.out.println(" ==> find " + id); + return userMapper.find(id); + } + + public List list(){ + return userMapper.list(); + } + +} diff --git a/04fx/dtx01/src/main/resources/application.yml b/04fx/dtx01/src/main/resources/application.yml new file mode 100644 index 00000000..c55e64c5 --- /dev/null +++ b/04fx/dtx01/src/main/resources/application.yml @@ -0,0 +1,31 @@ +server: + port: 8080 + +spring: + datasource: + username: root + password: + url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false + driver-class-name: com.mysql.jdbc.Driver +# cache: +# type: redis +# redis: +# host: localhost +# lettuce: +# pool: +# max-active: 16 +# max-wait: 10ms + +# type: ehcache +# ehcache: +# config: ehcache.xml + +#mybatis: +# mapper-locations: classpath:mapper/*Mapper.xml +# type-aliases-package: io.kimmking.dtx01.entity + +logging: + level: + io: + kimmking: + cache : info diff --git a/04fx/dtx01/src/main/resources/mapper/UserMapper.xml b/04fx/dtx01/src/main/resources/mapper/UserMapper.xml new file mode 100644 index 00000000..55bd7280 --- /dev/null +++ b/04fx/dtx01/src/main/resources/mapper/UserMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/dtx01/src/test/java/io/kimmking/dtx01/Dtx01ApplicationTests.java b/04fx/dtx01/src/test/java/io/kimmking/dtx01/Dtx01ApplicationTests.java new file mode 100644 index 00000000..d04ffc9d --- /dev/null +++ b/04fx/dtx01/src/test/java/io/kimmking/dtx01/Dtx01ApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.dtx01; + +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Dtx01ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/04fx/java8/pom.xml b/04fx/java8/pom.xml index d26fcb56..695421a5 100644 --- a/04fx/java8/pom.xml +++ b/04fx/java8/pom.xml @@ -27,6 +27,11 @@ + + org.openjdk.jol + jol-core + 0.9 + com.google.guava guava diff --git a/04fx/java8/src/main/java/io/kimmking/java8/A.java b/04fx/java8/src/main/java/io/kimmking/java8/A.java index 569631ef..e3027fce 100644 --- a/04fx/java8/src/main/java/io/kimmking/java8/A.java +++ b/04fx/java8/src/main/java/io/kimmking/java8/A.java @@ -1,10 +1,24 @@ package io.kimmking.java8; -import lombok.Data; +import lombok.*; +import lombok.extern.slf4j.Slf4j; -@Data +@ToString +@NoArgsConstructor +@AllArgsConstructor +@Slf4j +@Builder +@Getter +@Setter public class A { - + private int age; - + + private String name; + +// public void test(){ +// log.info("this message is logged by lombok"); +// System.out.println(this.toString()); +// } + } diff --git a/04fx/java8/src/main/java/io/kimmking/java8/GenericDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/GenericDemo.java index d6851736..7de88d61 100644 --- a/04fx/java8/src/main/java/io/kimmking/java8/GenericDemo.java +++ b/04fx/java8/src/main/java/io/kimmking/java8/GenericDemo.java @@ -1,9 +1,10 @@ package io.kimmking.java8; +import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -public class GenericDemo { +public class GenericDemo implements Serializable { public static void main(String[] args) { Demo demo = new Demo(); Class clazz = demo.getClass(); @@ -20,7 +21,7 @@ public static void main(String[] args) { System.out.println(c); } - public static class Person { + public static class Person { } diff --git a/04fx/java8/src/main/java/io/kimmking/java8/GuavaDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/GuavaDemo.java index a2540278..e505fec7 100644 --- a/04fx/java8/src/main/java/io/kimmking/java8/GuavaDemo.java +++ b/04fx/java8/src/main/java/io/kimmking/java8/GuavaDemo.java @@ -48,7 +48,7 @@ private static void testEventBus() { // Callback/Listener // Student student2 = new Student(2, "KK02"); - System.out.println("I want " + student2 + " run now."); + System.out.println(Thread.currentThread().getName()+" I want " + student2 + " run now."); bus.post(new AEvent(student2)); } @@ -81,6 +81,7 @@ private static List testList() { List list = Lists.newArrayList(4,2,3,5,1,2,2,7,6); List> list1 = Lists.partition(list,3); + print(list1); return list; } @@ -89,10 +90,11 @@ private static List testString() { // 字符串处理 // List lists = Lists.newArrayList("a","b","g","8","9"); + String result = Joiner.on(",").join(lists); System.out.println(result); - String test = "34344,34,34,哈哈"; + String test = "34344,,,34,34,哈哈"; lists = Splitter.on(",").splitToList(test); System.out.println(lists); return lists; @@ -111,7 +113,7 @@ public static class AEvent{ @Subscribe public void handle(AEvent ae){ - System.out.println(ae.student + " is running."); + System.out.println(Thread.currentThread().getName()+" "+ae.student + " is running."); } diff --git a/04fx/java8/src/main/java/io/kimmking/java8/LambdaDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/LambdaDemo.java index f1dffef0..94996f3b 100644 --- a/04fx/java8/src/main/java/io/kimmking/java8/LambdaDemo.java +++ b/04fx/java8/src/main/java/io/kimmking/java8/LambdaDemo.java @@ -17,17 +17,25 @@ public Integer operation(int a, int b) { }; MathOperation op1 = (a, b) -> 1; + + + MathOperation op2 = new MathOperation() { + @Override + public Integer operation(int a, int b) { + return a+b; + } + }; // 类型声明 MathOperation addition = (int a, int b) -> a + b; // 不用类型声明 - MathOperation subtraction = (a, b) -> a - b + 1.0; + MathOperation subtraction = (int a, int b) -> a - b ; // 大括号中的返回语句 MathOperation multiplication = (int a, int b) -> { - int c = 1000; - return a * b + c; + //int c = 1000; + return a * b;// + c; }; // 没有大括号及返回语句 @@ -41,22 +49,26 @@ public Integer operation(int a, int b) { //System.out.println("10 ^ 5 = " + demo.operate(10, 5, (a, b) -> new Double(Math.pow(a,b)).intValue())); System.out.println("10 ^ 5 = " + demo.operate(10, 5, (a, b) -> Math.pow(a,b))); - + + Runnable task = () -> System.out.println(1111); + // 不用括号 GreetingService greetService1 = message -> System.out.println("Hello " + message); // 用括号 - GreetingService greetService2 = (message) -> - System.out.println("Hello " + message); - + GreetingService greetService2 = (message) -> { + System.out.println(message); + }; + GreetingService greetService3 = System.out::println; - + Arrays.asList(1,2,3).forEach( x -> System.out.println(x+3)); Arrays.asList(1,2,3).forEach( LambdaDemo::println ); greetService1.sayMessage("kimmking"); greetService2.sayMessage("Java"); + greetService3.sayMessage("CuiCuilaoshi"); } private static void println(int x) { diff --git a/04fx/java8/src/main/java/io/kimmking/java8/LombokDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/LombokDemo.java index 1fe98cc2..a1333f4f 100644 --- a/04fx/java8/src/main/java/io/kimmking/java8/LombokDemo.java +++ b/04fx/java8/src/main/java/io/kimmking/java8/LombokDemo.java @@ -1,14 +1,24 @@ package io.kimmking.java8; import lombok.extern.java.Log; +import org.slf4j.LoggerFactory; import java.io.IOException; @Log public class LombokDemo { - + public static void main(String[] args) throws IOException { - + + // Spring IoC + // ServiceLoader.load SPI + // Listener/Callback + // EventBus + + A a = new A(1, "KK"); + System.out.println(a.toString()); + A b = A.builder().age(1).name("KKK").build(); + new LombokDemo().demo(); Student student1 = new Student(); @@ -17,6 +27,7 @@ public static void main(String[] args) throws IOException { System.out.println(student1.toString()); Student student2 = new Student(2, "KK02"); + //student2.init(); System.out.println(student2.toString()); } diff --git a/04fx/java8/src/main/java/io/kimmking/java8/ResourceLoader.java b/04fx/java8/src/main/java/io/kimmking/java8/ResourceLoader.java new file mode 100644 index 00000000..6922b2de --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/ResourceLoader.java @@ -0,0 +1,56 @@ +package io.kimmking.java8; + +import lombok.SneakyThrows; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class ResourceLoader { + + static String file = "conf/a.properties"; + + public static void main(String[] args) { + + // testClassLoaderRootPath(); // classloader方式不能加根路径,否则文件路径会直接被替换掉最终成了 /conf/a.properties + testClassLoader(); // 从当前的classpath,不管是 文件夹,还是jar里去找 资源 + testClassRootPath(); // 判断第一个字符是根路径,去掉根,转成 testClassLoader 调用 + // testClass(); // class 方式必须加根路径,不加根会根据类路径io.kimmking.java8.ResourceLoader 去拼成 io/kimmking/java8/conf/a.properties + + + } + + private static void testClassLoader() { + System.out.println("====> testClassLoader"); + loadStream(ResourceLoader.class.getClassLoader().getResourceAsStream(file)); + } + + private static void testClassLoaderRootPath() { + System.out.println("====> testClassLoader"); + loadStream(ResourceLoader.class.getClassLoader().getResourceAsStream("/"+file)); + } + + private static void testClass() { + System.out.println("====> testClass"); + loadStream(ResourceLoader.class.getResourceAsStream(file)); + } + + private static void testClassRootPath() { + System.out.println("====> testClassRootPath"); + loadStream(ResourceLoader.class.getResourceAsStream("/"+file)); + } + + @SneakyThrows + private static void loadStream(InputStream in) { + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + StringBuffer buffer = new StringBuffer(); + String line = null; + while ((line = reader.readLine()) != null) { + buffer.append(line); + } + System.out.println(buffer.toString()); + reader.close(); + in.close(); + } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo.java index 4ec48773..b2771ff3 100644 --- a/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo.java +++ b/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo.java @@ -3,11 +3,7 @@ import com.alibaba.fastjson.JSON; import java.io.IOException; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; public class StreamDemo { @@ -19,14 +15,21 @@ public static void main(String[] args) throws IOException { // Optional Optional first = list.stream().findFirst(); - + System.out.println(first.map(i -> i * 100).orElse(100)); - + + //1,2,3 + // 0, 1, 2, 3 int sum = list.stream().filter( i -> i<4).distinct().reduce(0,(a,b)->a+b); System.out.println("sum="+sum); + + //1,2,3 + // 1, 1, 2, 3 + int multi = list.stream().filter( i -> i<4).distinct().reduce(1,(a,b)->a*b); + System.out.println("multi="+multi); - //Map map = list.stream().collect(Collectors.toMap(a->a,a->(a+1))); - Map map = list.parallelStream().collect(Collectors.toMap(a->a,a->(a+1),(a,b)->a, LinkedHashMap::new)); + //Map map1 = list.stream().collect(Collectors.toMap(a->a,a->(a+1))); + Map map = list.stream().parallel().collect(Collectors.toMap(a->a,a->(a+1),(a,b)->a, LinkedHashMap::new)); print(map); diff --git a/04fx/java8/src/main/java/io/kimmking/java8/TestMem.java b/04fx/java8/src/main/java/io/kimmking/java8/TestMem.java new file mode 100644 index 00000000..1ffcf8f7 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/TestMem.java @@ -0,0 +1,27 @@ +package io.kimmking.java8; + +import org.openjdk.jol.info.ClassLayout; +import org.openjdk.jol.info.GraphLayout; + +public class TestMem { + public static void main(String[] args) { + int[] arr1 = new int[256]; + int[][] arr2 = new int[128][2]; + int[][][] arr3 = new int[64][2][2]; + + print("1-size : " + GraphLayout.parseInstance(arr1).totalSize()); + print(ClassLayout.parseInstance(arr1).toPrintable()); + print("2-size : " + GraphLayout.parseInstance(arr2).totalSize()); + print(ClassLayout.parseInstance(arr2).toPrintable()); + print(GraphLayout.parseInstance(arr2).toPrintable()); + print("3-size : " + GraphLayout.parseInstance(arr3).totalSize()); + print(ClassLayout.parseInstance(arr3).toPrintable()); + print(GraphLayout.parseInstance(arr3).toPrintable()); + System.out.println(); + } + + static void print(String message) { + System.out.println(message); + System.out.println("-------------------------"); + } +} \ No newline at end of file diff --git a/04fx/java8/src/main/resources/conf/a.properties b/04fx/java8/src/main/resources/conf/a.properties new file mode 100644 index 00000000..86fbde04 --- /dev/null +++ b/04fx/java8/src/main/resources/conf/a.properties @@ -0,0 +1,2 @@ +a=1 +b=2 \ No newline at end of file diff --git a/04fx/spring01/pom.xml b/04fx/spring01/pom.xml index 52e59f6d..3075aaf2 100644 --- a/04fx/spring01/pom.xml +++ b/04fx/spring01/pom.xml @@ -10,7 +10,7 @@ - 4.3.29.RELEASE + 4.3.30.RELEASE @@ -19,8 +19,8 @@ org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + 11 + 11 @@ -152,11 +152,6 @@ test - - org.springframework - spring-jms - 4.3.29.RELEASE - org.apache.activemq activemq-client diff --git a/04fx/spring01/src/main/java/io/kimmking/DemoMethodIncepter.java b/04fx/spring01/src/main/java/io/kimmking/DemoMethodIncepter.java new file mode 100644 index 00000000..d9a00e1f --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/DemoMethodIncepter.java @@ -0,0 +1,17 @@ +package io.kimmking; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +public class DemoMethodIncepter implements MethodInterceptor { + + public Object invoke(MethodInvocation invocation) throws Throwable { + + long s = System.currentTimeMillis(); + System.out.println(" *****====> " + s + " " + invocation.getMethod().getName()); + Object result = invocation.proceed(); + System.out.println(" *****====> " + (System.currentTimeMillis() - s) + " ms"); + return result; + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/AnnoDemo.java b/04fx/spring01/src/main/java/io/kimmking/anno/AnnoDemo.java new file mode 100644 index 00000000..630f9e19 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/AnnoDemo.java @@ -0,0 +1,34 @@ +package io.kimmking.anno; + +import java.lang.annotation.Annotation; +import java.util.Arrays; + +public class AnnoDemo { + + public static void main(String[] args) { + IA2 ia2 = new IA2() { + @Override + public void a2() { + System.out.println("a2."); + } + + @Override + public void a1() { + System.out.println("a1."); + } + }; + + Annotation[] annotations = ia2.getClass().getInterfaces()[0].getAnnotations(); + Arrays.stream(annotations).forEach(x -> System.out.println(x.annotationType().getCanonicalName())); + + Annotation[] annos2 = ia2.getClass().getInterfaces()[0].getInterfaces()[0].getAnnotations(); + Arrays.stream(annos2).forEach(x -> System.out.println(x.annotationType().getCanonicalName())); + Arrays.stream(annos2).forEach( + x -> { + IAnno2 anno2 = (IAnno2) x; + System.out.println(anno2.value()); + }); + + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IA1.java b/04fx/spring01/src/main/java/io/kimmking/anno/IA1.java new file mode 100644 index 00000000..8d7f50dc --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IA1.java @@ -0,0 +1,9 @@ +package io.kimmking.anno; + + +@IAnno2("anno2") +public interface IA1 { + + void a1(); + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IA2.java b/04fx/spring01/src/main/java/io/kimmking/anno/IA2.java new file mode 100644 index 00000000..88e8db00 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IA2.java @@ -0,0 +1,9 @@ +package io.kimmking.anno; + + +@IAnnotation +public interface IA2 extends IA1 { + + void a2(); + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IAnno2.java b/04fx/spring01/src/main/java/io/kimmking/anno/IAnno2.java new file mode 100644 index 00000000..da04c7d5 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IAnno2.java @@ -0,0 +1,14 @@ +package io.kimmking.anno; + +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 IAnno2 { + + String value(); + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IAnnotation.java b/04fx/spring01/src/main/java/io/kimmking/anno/IAnnotation.java new file mode 100644 index 00000000..becda7a8 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IAnnotation.java @@ -0,0 +1,12 @@ +package io.kimmking.anno; + + +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 IAnnotation { +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/GuavaDemo.java b/04fx/spring01/src/main/java/io/kimmking/spring01/GuavaDemo.java index 98ef1c51..697b680c 100644 --- a/04fx/spring01/src/main/java/io/kimmking/spring01/GuavaDemo.java +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/GuavaDemo.java @@ -79,7 +79,7 @@ public static void main(String[] args) throws IOException { // SPI+service loader // Callback/Listener // - Student student2 = new Student(2, "KK02"); + Student student2 = Student.create(); System.out.println("I want " + student2 + " run now."); bus.post(new AEvent(student2)); diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/LombokDemo.java b/04fx/spring01/src/main/java/io/kimmking/spring01/LombokDemo.java index 470db3b7..c28b48e2 100644 --- a/04fx/spring01/src/main/java/io/kimmking/spring01/LombokDemo.java +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/LombokDemo.java @@ -16,7 +16,7 @@ public static void main(String[] args) throws IOException { student1.setName("KK01"); System.out.println(student1.toString()); - Student student2 = new Student(2, "KK02"); + Student student2 = Student.create(); System.out.println(student2.toString()); } diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/Student.java b/04fx/spring01/src/main/java/io/kimmking/spring01/Student.java index db8e0061..a2357bf6 100644 --- a/04fx/spring01/src/main/java/io/kimmking/spring01/Student.java +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/Student.java @@ -5,6 +5,10 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanNameAware; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import java.io.Serializable; @@ -13,17 +17,29 @@ @AllArgsConstructor @NoArgsConstructor @ToString +public class Student implements Serializable, BeanNameAware, ApplicationContextAware { + -public class Student implements Serializable { - private int id; private String name; - + + private String beanName; + private ApplicationContext applicationContext; + public void init(){ System.out.println("hello..........."); } - public Student create(){ - return new Student(101,"KK101"); + public static Student create(){ + return new Student(102,"KK102",null, null); } + + public void print() { + System.out.println(this.beanName); + System.out.println(" context.getBeanDefinitionNames() ===>> " + + String.join(",", applicationContext.getBeanDefinitionNames())); + + } + + } diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/Aop1.java b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop1.java index 35447d56..fa919d39 100644 --- a/04fx/spring01/src/main/java/io/kimmking/spring02/Aop1.java +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop1.java @@ -6,21 +6,21 @@ public class Aop1 { //前置通知 public void startTransaction(){ - System.out.println(" ====>begin ding... "); + System.out.println(" ====>begin ding... "); //2 } //后置通知 public void commitTransaction(){ - System.out.println(" ====>finish ding... "); + System.out.println(" ====>finish ding... "); //4 } //环绕通知 public void around(ProceedingJoinPoint joinPoint) throws Throwable{ - System.out.println(" ====>around begin ding"); + System.out.println(" ====>around begin ding"); //1 //调用process()方法才会真正的执行实际被代理的方法 joinPoint.proceed(); - System.out.println(" ====>around finish ding"); + System.out.println(" ====>around finish ding"); //3 } } diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/Aop2.java b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop2.java index b8eae255..4edbe006 100644 --- a/04fx/spring01/src/main/java/io/kimmking/spring02/Aop2.java +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop2.java @@ -18,19 +18,19 @@ public void point(){ @Before(value="point()") public void before(){ - System.out.println("========>begin klass dong..."); + System.out.println("========>begin klass dong... //2"); } @AfterReturning(value = "point()") public void after(){ - System.out.println("========>after klass dong..."); + System.out.println("========>after klass dong... //4"); } @Around("point()") public void around(ProceedingJoinPoint joinPoint) throws Throwable{ - System.out.println("========>around begin klass dong"); + System.out.println("========>around begin klass dong //1"); joinPoint.proceed(); - System.out.println("========>around after klass dong"); + System.out.println("========>around after klass dong //3"); } diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanDefinitionRegistryPostProcessor.java b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanDefinitionRegistryPostProcessor.java new file mode 100644 index 00000000..d6433bd6 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanDefinitionRegistryPostProcessor.java @@ -0,0 +1,29 @@ +package io.kimmking.spring02; + +import io.kimmking.spring01.Student; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.stereotype.Component; + +@Component +public class HelloBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { + System.out.println(" ==> postProcessBeanDefinitionRegistry: "+registry.getBeanDefinitionCount()); + System.out.println(" ==> postProcessBeanDefinitionRegistry: "+String.join(",",registry.getBeanDefinitionNames())); + RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Student.class); + rootBeanDefinition.getPropertyValues().add("id", 101); + rootBeanDefinition.getPropertyValues().add("name", "KK101"); + registry.registerBeanDefinition("s101", rootBeanDefinition); + } + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + System.out.println(" ==> postProcessBeanFactory: "+beanFactory.getBeanDefinitionCount()); + System.out.println(" ==> postProcessBeanFactory: "+String.join(",",beanFactory.getBeanDefinitionNames())); + beanFactory.registerSingleton("s102", Student.create()); + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanPostProcessor.java b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanPostProcessor.java new file mode 100644 index 00000000..451c829b --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanPostProcessor.java @@ -0,0 +1,27 @@ +package io.kimmking.spring02; + +import io.kimmking.spring01.Student; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.stereotype.Component; + +@Component +public class HelloBeanPostProcessor implements BeanPostProcessor { + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + System.out.println(" ====> postProcessBeforeInitialization " + beanName +":"+ bean); + // 可以加点额外处理 + // 例如 + if (bean instanceof Student) { + Student student = (Student) bean; + student.setName(student.getName() + "-" + System.currentTimeMillis()); + } + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + System.out.println(" ====> postProcessAfterInitialization " + beanName +":"+ bean); + return bean; + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/School.java b/04fx/spring01/src/main/java/io/kimmking/spring02/School.java index 7c700e64..c80ba638 100644 --- a/04fx/spring01/src/main/java/io/kimmking/spring02/School.java +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/School.java @@ -4,7 +4,9 @@ import io.kimmking.spring01.Student; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import javax.annotation.PostConstruct; import javax.annotation.Resource; @Data @@ -23,5 +25,6 @@ public void ding(){ System.out.println("Class1 have " + this.class1.getStudents().size() + " students and one is " + this.student100); } + } diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java index 8241be04..ae3b7206 100644 --- a/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java @@ -2,41 +2,104 @@ import io.kimmking.aop.ISchool; import io.kimmking.spring01.Student; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import java.lang.reflect.Method; + public class SpringDemo01 { public static void main(String[] args) { + + long s = System.currentTimeMillis(); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // Student student123 = context.getBean(Student.class); Student student123 = (Student) context.getBean("student123"); System.out.println(student123.toString()); + + student123.print(); Student student100 = (Student) context.getBean("student100"); System.out.println(student100.toString()); - + + student100.print(); + + Klass class1 = context.getBean(Klass.class); System.out.println(class1); System.out.println("Klass对象AOP代理后的实际类型:"+class1.getClass()); System.out.println("Klass对象AOP代理后的实际类型是否是Klass子类:"+(class1 instanceof Klass)); - + + s = System.currentTimeMillis(); + class1.dong(); + System.out.println(" *****====> class1.dong() " + (System.currentTimeMillis() - s) + " ms"); + ISchool school = context.getBean(ISchool.class); System.out.println(school); System.out.println("ISchool接口的对象AOP代理后的实际类型:"+school.getClass()); - + + + + + s = System.currentTimeMillis(); + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(Demo.class); + enhancer.setCallback(new MI()); + enhancer.setUseCache(true); + Demo demo = (Demo) enhancer.create(); + for (int i = 0; i < 1; i++) { + demo.a1(1); + demo.a2("hello"); + } + System.out.println(" *****====> enhancer proxy " + (System.currentTimeMillis() - s) + " ms"); + + + + ISchool school1 = context.getBean(ISchool.class); System.out.println(school1); System.out.println("ISchool接口的对象AOP代理后的实际类型:"+school1.getClass()); - + school1.ding(); - + class1.dong(); System.out.println(" context.getBeanDefinitionNames() ===>> "+ String.join(",", context.getBeanDefinitionNames())); - + Student s101 = (Student) context.getBean("s101"); + if (s101 != null) { + System.out.println(s101); + } + Student s102 = (Student) context.getBean("s102"); + if (s102 != null) { + System.out.println(s102); + } + } + + static class MI implements MethodInterceptor { + @Override + public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + long s = System.currentTimeMillis(); + System.out.println(" *****====> " + s + " " +"Before:"+method.getName()); + Object result= methodProxy.invokeSuper(obj, objects); + System.out.println(" *****====> " + (System.currentTimeMillis() - s) + " ms After:"+method.getName()); + return result; + } } + + static class Demo { + public void a1(int i) { + System.out.println(" *****====> Demo a1, " + i); + } + + public void a2(String str) { + System.out.println(" *****====> Demo a2," + str); + } + } + } diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java new file mode 100644 index 00000000..bcbc7e30 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java @@ -0,0 +1,49 @@ +package io.kimmking.spring02; + +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; + +import java.lang.reflect.Method; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/22 18:01 + */ +public class SpringDemo11 { + + public static void main(String[] args) { + long s = System.currentTimeMillis(); + Enhancer enhancer = new Enhancer(); + enhancer.setInterfaces(new Class[]{IAction.class}); + enhancer.setCallback(new MI()); + enhancer.setUseCache(true); + IAction demo = (IAction) enhancer.create(); + for (int i = 0; i < 5; i++) { + long ss = System.currentTimeMillis(); + System.out.println(demo.action()); + System.out.println( i + " *****====> invoke proxy " + (System.currentTimeMillis() - ss) + " ms"); + } + System.out.println(" *****====> enhancer proxy " + (System.currentTimeMillis() - s) + " ms"); + + } + + public interface IAction { + Object action(); + } + + + static class MI implements MethodInterceptor { + @Override + public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + long s = System.currentTimeMillis(); + System.out.println(" *****==MI==> " + s + " " +"Before:"+method.getName()); + Object result = "S-" + s;//methodProxy.invokeSuper(obj, objects); + System.out.println(" *****==MI==> " + (System.currentTimeMillis() - s) + " ms After:"+method.getName()); + return result; + } + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring03/Spring03Main.java b/04fx/spring01/src/main/java/io/kimmking/spring03/Spring03Main.java new file mode 100644 index 00000000..5ad222e7 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring03/Spring03Main.java @@ -0,0 +1,14 @@ +package io.kimmking.spring03; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class Spring03Main { + + public static void main(String[] args) { + ApplicationContext context = new AnnotationConfigApplicationContext(Spring03Main.class.getPackage().getName()); + TestService1 testService1 = context.getBean(TestService1.class); + testService1.test1(); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring03/TestService1.java b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService1.java new file mode 100644 index 00000000..1a215396 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService1.java @@ -0,0 +1,24 @@ +package io.kimmking.spring03; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.stereotype.Service; + +@Service +@Data +@EnableAsync +public class TestService1 { // TODO rename this class to TestService6 and it works well. + + @Autowired + private TestService2 service2; + + @Async + public void test1() { + System.out.println("test1"); + System.out.println(this.getClass().getCanonicalName()); + System.out.println(Thread.currentThread().getName()); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring03/TestService2.java b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService2.java new file mode 100644 index 00000000..37349631 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService2.java @@ -0,0 +1,18 @@ +package io.kimmking.spring03; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +@Data +public class TestService2 { + + @Autowired + private TestService1 service1; + + public void test2() { + System.out.println("test2"); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/ABCPlugin.java b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCPlugin.java new file mode 100644 index 00000000..0971612d --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCPlugin.java @@ -0,0 +1,10 @@ +package io.kimmking.spring04; + +import org.springframework.core.Ordered; + +public interface ABCPlugin extends Ordered { + + void startup() throws Exception; + void shutdown() throws Exception; + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartup.java b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartup.java new file mode 100644 index 00000000..7c451e01 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartup.java @@ -0,0 +1,26 @@ +package io.kimmking.spring04; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PreDestroy; +import java.util.List; + +@Component +public class ABCStartup { + + @Autowired + private List services; + + public void call () { + services.forEach(System.out::println); + } + + @PreDestroy + public void destroy() { + + System.out.println(Thread.currentThread().getName()+"-destroy:" + this.getClass().getSimpleName()); + + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartupListener.java b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartupListener.java new file mode 100644 index 00000000..9cd14157 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartupListener.java @@ -0,0 +1,46 @@ +package io.kimmking.spring04; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.ContextStartedEvent; +import org.springframework.context.event.ContextStoppedEvent; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class ABCStartupListener implements ApplicationListener { + + @Autowired + private List services; + + @Override + public void onApplicationEvent(ApplicationEvent event) { + + System.out.println(event); + + // 如果用spring boot可以怎么改进 + if (event instanceof ContextStartedEvent || event instanceof ContextRefreshedEvent) { + services.forEach(x -> { + try { + x.startup(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + + if (event instanceof ContextClosedEvent | event instanceof ContextStoppedEvent) { + services.forEach(x -> { + try { + x.shutdown(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin1.java b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin1.java new file mode 100644 index 00000000..a159eced --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin1.java @@ -0,0 +1,36 @@ +package io.kimmking.spring04; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.core.Ordered; +import org.springframework.stereotype.Component; + +@Component() +public class MockABCPlugin1 implements ABCPlugin, DisposableBean { + + @Override + public String toString() { + System.out.println(Thread.currentThread().getName()+"-"+this.getClass().getSimpleName()+" toString."); + return this.getClass().getSimpleName(); + } + + @Override + public int getOrder() { + return 1; + } + + @Override + public void startup() throws Exception { + // mock for netty server start + System.out.println(Thread.currentThread().getName()+"-"+this.toString()+" started."); + } + + @Override + public void shutdown() throws Exception { // 线程池之类可以改进为优雅停机 + System.out.println(Thread.currentThread().getName()+"-"+this.toString()+" stopped."); + } + + @Override + public void destroy() throws Exception { + System.out.println(Thread.currentThread().getName()+"-DisposableBean:" + this.toString()); + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin2.java b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin2.java new file mode 100644 index 00000000..5c9e6af6 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin2.java @@ -0,0 +1,28 @@ +package io.kimmking.spring04; + +import org.springframework.core.Ordered; +import org.springframework.stereotype.Component; + +@Component() +public class MockABCPlugin2 implements ABCPlugin { + + @Override + public String toString() { + return this.getClass().getSimpleName(); + } + + @Override + public int getOrder() { + return 2; + } + + @Override + public void startup() throws Exception { + System.out.println(this.toString()+" started."); + } + + @Override + public void shutdown() throws Exception { + System.out.println(this.toString()+" stopped."); + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/Spring04Main.java b/04fx/spring01/src/main/java/io/kimmking/spring04/Spring04Main.java new file mode 100644 index 00000000..8c7a2f0d --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/Spring04Main.java @@ -0,0 +1,21 @@ +package io.kimmking.spring04; + +import org.springframework.context.ApplicationEvent; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class Spring04Main { + + public static void main(String[] args) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spring04Main.class.getPackage().getName()); + ABCStartup abc = context.getBean(ABCStartup.class); + abc.call(); + + context.publishEvent(new ApplicationEvent("myEvent") {}); + + context.close(); + // context.destroy(); + // context.stop(); // 这3个有什么区别? + + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/springjms/JmsSender.java b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsSender.java index e9b65a01..fb0fbeec 100644 --- a/04fx/spring01/src/main/java/io/kimmking/springjms/JmsSender.java +++ b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsSender.java @@ -8,12 +8,14 @@ public class JmsSender { public static void main( String[] args ) { - Student student2 = new Student(200, "KK0200"); + Student student2 = Student.create(); ApplicationContext context = new ClassPathXmlApplicationContext("classpath:springjms-sender.xml"); SendService sendService = (SendService)context.getBean("sendService"); - + + student2.setName("KK103"); + sendService.send(student2); System.out.println("send successfully, please visit http://localhost:8161/admin to see it"); diff --git a/04fx/spring01/src/main/java/io/kimmking/springjms/SendService.java b/04fx/spring01/src/main/java/io/kimmking/springjms/SendService.java index 1c09e4c5..15bafce1 100644 --- a/04fx/spring01/src/main/java/io/kimmking/springjms/SendService.java +++ b/04fx/spring01/src/main/java/io/kimmking/springjms/SendService.java @@ -1,5 +1,6 @@ package io.kimmking.springjms; +import com.alibaba.fastjson.JSON; import io.kimmking.spring01.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; @@ -19,7 +20,7 @@ public void send(final Student user) { jmsTemplate.send("test.queue", new MessageCreator() { public Message createMessage(Session session) throws JMSException { - return session.createObjectMessage(user); + return session.createObjectMessage(JSON.toJSONString(user)); } }); } diff --git a/04fx/spring01/src/main/resources/applicationContext.xml b/04fx/spring01/src/main/resources/applicationContext.xml index 681cf409..83bc6eac 100644 --- a/04fx/spring01/src/main/resources/applicationContext.xml +++ b/04fx/spring01/src/main/resources/applicationContext.xml @@ -38,6 +38,8 @@ + + @@ -51,10 +53,14 @@ + + - - + + + + diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/Springboot01Application.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/Springboot01Application.java index 331167c1..0cfd0cb6 100644 --- a/04fx/springboot01/src/main/java/io/kimmking/springboot01/Springboot01Application.java +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/Springboot01Application.java @@ -7,8 +7,8 @@ import org.springframework.jms.annotation.EnableJms; @SpringBootApplication -@EnableJms //启动消息队列 -@EnableMongoRepositories +//@EnableJms //启动消息队列 +//@EnableMongoRepositories public class Springboot01Application { public static void main(String[] args) { diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/MongoController.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/MongoController.java index 0e78ec51..3cc93c0a 100644 --- a/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/MongoController.java +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/MongoController.java @@ -15,10 +15,10 @@ @Controller @EnableAutoConfiguration public class MongoController { - + @Autowired MongoTemplate mongoTemplate; - + @RequestMapping("/mongo/list") @ResponseBody List mongo() { @@ -27,7 +27,7 @@ List mongo() { //String name = mongotemplate.findOne(query, User.class).getName(); return mongoTemplate.findAll(User.class); } - + @RequestMapping("/mongo/test") @ResponseBody String test() { @@ -41,6 +41,6 @@ String test() { mongoTemplate.insert(user); return "test ok"; } - - + + } \ No newline at end of file diff --git a/04fx/springboot01/src/main/resources/application.yml b/04fx/springboot01/src/main/resources/application.yml index fa5370a3..048ab343 100644 --- a/04fx/springboot01/src/main/resources/application.yml +++ b/04fx/springboot01/src/main/resources/application.yml @@ -18,7 +18,7 @@ spring: enabled: true max-connections: 10 #连接池最大连接数 idle-timeout: 30000 #空闲的连接过期时间,默认为30秒 - + data: diff --git a/06db/shardingsphere/config-replica-query.yaml b/06db/shardingsphere/config-replica-query.yaml new file mode 100644 index 00000000..d060ab5e --- /dev/null +++ b/06db/shardingsphere/config-replica-query.yaml @@ -0,0 +1,30 @@ + +schemaName: replica_query_db + +dataSourceCommon: + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 10 + minPoolSize: 1 + maintenanceIntervalMilliseconds: 30000 + +dataSources: + primary_ds: + url: jdbc:mysql://127.0.0.1:3306/demo_master?serverTimezone=UTC&useSSL=false + replica_ds_0: + url: jdbc:mysql://127.0.0.1:3306/demo_slave_0?serverTimezone=UTC&useSSL=false + replica_ds_1: + url: jdbc:mysql://127.0.0.1:3306/demo_slave_1?serverTimezone=UTC&useSSL=false + +rules: +- !REPLICA_QUERY + dataSources: + pr_ds: + name: pr_ds + primaryDataSourceName: primary_ds + replicaDataSourceNames: + - replica_ds_0 + - replica_ds_1 diff --git a/06db/shardingsphere/config-sharding.yaml b/06db/shardingsphere/config-sharding.yaml new file mode 100644 index 00000000..ffbc1546 --- /dev/null +++ b/06db/shardingsphere/config-sharding.yaml @@ -0,0 +1,68 @@ + +schemaName: sharding_db + +dataSourceCommon: + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 5 + minPoolSize: 1 + maintenanceIntervalMilliseconds: 30000 + +dataSources: + ds_0: + url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + ds_1: + url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + +rules: +- !SHARDING + tables: + t_order: + actualDataNodes: ds_${0..1}.t_order_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: t_order_inline + keyGenerateStrategy: + column: order_id + keyGeneratorName: snowflake + t_order_item: + actualDataNodes: ds_${0..1}.t_order_item_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: t_order_item_inline + keyGenerateStrategy: + column: order_item_id + keyGeneratorName: snowflake + bindingTables: + - t_order,t_order_item + defaultDatabaseStrategy: + standard: + shardingColumn: user_id + shardingAlgorithmName: database_inline + defaultTableStrategy: + none: + + shardingAlgorithms: + database_inline: + type: INLINE + props: + algorithm-expression: ds_${user_id % 2} + t_order_inline: + type: INLINE + props: + algorithm-expression: t_order_${order_id % 2} + t_order_item_inline: + type: INLINE + props: + algorithm-expression: t_order_item_${order_id % 2} + + keyGenerators: + snowflake: + type: SNOWFLAKE + props: + worker-id: 123 diff --git a/06db/shardingsphere/init.sql b/06db/shardingsphere/init.sql new file mode 100644 index 00000000..9b930268 --- /dev/null +++ b/06db/shardingsphere/init.sql @@ -0,0 +1,37 @@ + +## 读写分离 + +create schema demo_master; +create schema demo_slave_0; +create schema demo_slave_1; + +create table demo_master.users(id bigint, name varchar(8), comment varchar(16)); +create table demo_slave_0.users(id bigint, name varchar(8), comment varchar(16)); +create table demo_slave_1.users(id bigint, name varchar(8), comment varchar(16)); + +insert into demo_master.users values(1,'KK01','master'),(2,'KK02','master'),(3,'KK03','master'); +insert into demo_slave_0.users values(1,'KK01','slave0'),(2,'KK02','slave0'),(3,'KK03','slave0'); +insert into demo_slave_1.users values(1,'KK01','slave1'),(2,'KK02','slave1'),(3,'KK03','slave1'); + + +## 分库分表 + +create schema demo_ds_0; +create schema demo_ds_1; + +CREATE TABLE IF NOT EXISTS demo_ds_0.t_order_0 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +CREATE TABLE IF NOT EXISTS demo_ds_0.t_order_1 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_0 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_1 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); + +CREATE TABLE IF NOT EXISTS demo_ds_0.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)); +CREATE TABLE IF NOT EXISTS demo_ds_0.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)); +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)); +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)); + + + + + +# CREATE TABLE t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +# 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)); diff --git a/06db/shardingsphere/server.yaml b/06db/shardingsphere/server.yaml new file mode 100644 index 00000000..87df2ef0 --- /dev/null +++ b/06db/shardingsphere/server.yaml @@ -0,0 +1,35 @@ + +# governance: +# name: governance_ds +# registryCenter: +# type: ZooKeeper +# serverLists: localhost:2181 +# props: +# retryIntervalMilliseconds: 500 +# timeToLiveSeconds: 60 +# maxRetries: 3 +# operationTimeoutMilliseconds: 500 +# overwrite: true + +authentication: + users: + root: + password: root +# sharding: +# password: sharding +# authorizedSchemas: sharding_db + +props: + max-connections-size-per-query: 1 + acceptor-size: 16 # The default value is available processors count * 2. + executor-size: 16 # Infinite by default. + proxy-frontend-flush-threshold: 128 # The default value is 128. + # LOCAL: Proxy will run with LOCAL transaction. + # XA: Proxy will run with XA transaction. + # BASE: Proxy will run with B.A.S.E transaction. + proxy-transaction-type: LOCAL + proxy-opentracing-enabled: false + proxy-hint-enabled: false + query-with-cipher-column: false + sql-show: true + check-table-metadata-enabled: false diff --git a/07rpc/README.md b/07rpc/README.md new file mode 100644 index 00000000..746e228e --- /dev/null +++ b/07rpc/README.md @@ -0,0 +1,77 @@ +# 第7周作业 + + +## 作业内容 + +> Week07 作业题目: + +###Week07 作业题目 + +1. (选做)用今天课上学习的知识,分析自己系统的 SQL 和表结构 +2. (必做)按自己设计的表结构,插入 100 万订单模拟数据,测试不同方式的插入效率 +3. (选做)按自己设计的表结构,插入 1000 万订单模拟数据,测试不同方式的插入效率 +4. (选做)使用不同的索引或组合,测试不同方式查询效率 +5. (选做)调整测试数据,使得数据尽量均匀,模拟 1 年时间内的交易,计算一年的销售报表:销售总额,订单数,客单价,每月销售量,前十的商品等等(可以自己设计更多指标) +6. (选做)尝试自己做一个 ID 生成器(可以模拟 Seq 或 Snowflake) +7. (选做)尝试实现或改造一个非精确分页的程序 +8. (选做)配置一遍异步复制,半同步复制、组复制 +9. (必做)读写分离 - 动态切换数据源版本 1.0 +10. (必做)读写分离 - 数据库框架版本 2.0 +11. (选做)读写分离 - 数据库中间件版本 3.0 +12. (选做)配置 MHA,模拟 master 宕机 +13. (选做)配置 MGR,模拟 master 宕机 +14. (选做)配置 Orchestrator,模拟 master 宕机,演练 UI 调整拓扑结构 + +### 作业提交规范: + +1. 作业不要打包 ; +2. 同学们写在 md 文件里,而不要发 Word, Excel , PDF 等 ; +3. 代码类作业需提交完整 Java 代码,不能是片段; +4. 作业按课时分目录,仅上传作业相关,笔记分开记录; +5. 画图类作业提交可直接打开的图片或 md,手画的图手机拍照上传后太大,难以查看,推荐画图(推荐 PPT、Keynote); +6. 提交记录最好要标明明确的含义(比如第几题作业)。 + + + +## 操作步骤 + + +### 第七周-作业1. (选做) + +> 本题目需要同学们自己完成. + +1. 找一个业务系统。 +2. 分析SQL表结构 +3. 范式 +4. 考虑: 开发效率、查询效率、拓展性、索引、有哪些优化空间。 +5. 其他 + + +### 第七周-作业2. (必做) + +0. 作业题目: 按自己设计的表结构,插入 100 万订单模拟数据,测试不同方式的插入效率 +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息: + * Project: Maven Project + * Language: Java + * Spring Boot版本: 2.5.4 + * Group: 自己的包名, 以做标识; + * Artifact: mysql-demo + * JDK版本: 8 + * Dependencies: 添加依赖, 比如 MyBatis, Spring Web, MySQL, JDBC + * 生成 maven 项目; 下载并解压。 参考: [mysql-demo项目Share信息](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.4&packaging=jar&jvmVersion=1.8&groupId=com.cncounter&artifactId=mysql-demo&name=mysql-demo&description=MySQL%20Demo&packageName=com.cncounter.mysql-demo&dependencies=mybatis,web,mysql,data-jdbc) + +4. Idea或者Eclipse从已有的Source导入Maven项目。 +5. 搜索依赖, 推荐 mvnrepository: https://mvnrepository.com/ +6. 搜索 fastjson , 然后在 pom.xml 之中增加对应的依赖。 +7. 准备MySQL环境, 创建订单表 + - 7.1 +8. 生成项目; + - 8.1 创建Controller +9. 执行与测试. + +参考: [mysql-demo/README.md](./mysql-demo/README.md) + + +https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ diff --git a/07rpc/mysql-demo/.gitignore b/07rpc/mysql-demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/07rpc/mysql-demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/07rpc/mysql-demo/README.md b/07rpc/mysql-demo/README.md new file mode 100644 index 00000000..fad579e2 --- /dev/null +++ b/07rpc/mysql-demo/README.md @@ -0,0 +1,208 @@ +## 说明 + +第七周-作业2. (必做): 按自己设计的表结构,插入 100 万订单模拟数据,测试不同方式的插入效率 + +## 操作步骤 + + +### 第七周-作业2. (必做) + + +#### 1. 创建基本项目 + +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息: + * Project: Maven Project + * Language: Java + * Spring Boot版本: 2.5.4 + * Group: 自己的包名, 以做标识; + * Artifact: mysql-demo + * JDK版本: 8 + * Dependencies: 添加依赖, 比如 MyBatis, Spring Web, MySQL, JDBC + * 生成 maven 项目; 下载并解压。 参考: [mysql-demo项目Share信息](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.4&packaging=jar&jvmVersion=1.8&groupId=com.cncounter&artifactId=mysql-demo&name=mysql-demo&description=MySQL%20Demo&packageName=com.cncounter.mysql-demo&dependencies=mybatis,web,mysql,data-jdbc) + +4. Idea或者Eclipse从已有的Source导入Maven项目。 + + +#### 2. 准备mysql环境 + +mysql服务器, 可以使用这些方式: + +- 物理机安装 +- 虚拟机安装 +- 购买云服务 +- 使用已有MySQL +- 使用Docker +- 使用docker-compose: 参考配置文件: [docker-compose.yml](./docker-compose.yml) + +这里使用 docker-compose 方式。 + +根据配置信息, 我们需要创建好以下2个目录: + +- `./docker_data/docker-entrypoint-initdb.d` ; MySQL镜像启动时会自动执行下面的SQL. +- `./docker_data/var/lib/mysql` ; 注意这个目录下不能有任何文件, 否则启动报错。 + +安装好 docker 之后, 使用 `docker-compose up -d` 命令来启动。 + +这个命令会自动查找当前目录下的 [docker-compose.yml](./docker-compose.yml) 配置文件。 + +如果启动失败, 可以在当前目录下, 使用 `docker-compose logs -f` 命令查看日志信息。 + +可以使用命令来查看当前物理机监听的端口号; + + +```shell +# mac +lsof -iTCP -sTCP:LISTEN -n -P + +# linux +netstat -ntlp + +``` + +启动成功后, 应该能看到我们配置的 `13306` 端口信息. + +然后使用MySQL客户端连接即可, 我们在配置文件里面指定了ROOT用户的密码为 `123456`; + + +#### 3. 项目基础配置 + +如果直接启动下载下来的 MysqlDemoApplication, 报错信息大致如下: + +```text +*************************** +APPLICATION FAILED TO START +*************************** + +Description: + +Failed to configure a DataSource: + 'url' attribute is not specified + and no embedded datasource could be configured. + +Reason: Failed to determine a suitable driver class +``` + +可以看到这里提示 缺少了DataSource的url等配置信息 + +我们增加一个配置文件: [src/main/resources/application.yml](./src/main/resources/application.yml) + +文件内容参考: + +```yaml +# Spring相关配置 +spring: + datasource: + type: com.zaxxer.hikari.HikariDataSource + hikari: + minimum-idle: 1 + maximum-pool-size: 5 + auto-commit: true + idle-timeout: 30000 + pool-name: MySQLHikariCP1 + max-lifetime: 1800000 + connection-timeout: 30000 + connection-test-query: SELECT 1 + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:13306/mysql_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false + username: root + password: 123456 + +``` + +然后再增加 logback的日志级别: + +```yaml +# Logback日志级别配置 +logging.level.root: info +logging.level.com.cncounter: debug +logging.level.org.springframework.test.web.servlet.result: debug + +``` + +以及 Tomcat 配置: + +```yaml +# Tomcat 配置 +server: + port: 8080 + tomcat: + uri-encoding: UTF-8 + max-threads: 1000 + min-spare-threads: 10 + servlet: + context-path: / + encoding: + enabled: true + force: true + charset: UTF-8 +``` + + +然后重新启动 MysqlDemoApplication. + +启动成功之后, 可以查看本机的端口号, 也可以访问: [http://localhost:8080/](http://localhost:8080/) + + +提示: 可以用 jvisualvm 来观察启动好的Java进程。 + + +#### 4. 数据库脚本 + +可以使用前面课程的作业中提交的数据库表。 + +也可以参考: [docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql](./docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql) + + +#### 5. MyBatis生成器与配置 + +官方文档: [MyBatis Generator Doc](https://mybatis.org/generator/configreference/xmlconfig.html) + +配置文件: [src/main/resources/MybatisGeneratorConfig.xml](./src/main/resources/MybatisGeneratorConfig.xml) + +在 [pom.xml](./pom.xml) 文件中增加 `mybatis-generator-maven-plugin` 插件. + +执行生成(也可以使用 [generateMapper.sh](./generateMapper.sh) 脚本文件): + +```shell +mvn mybatis-generator:generate -X -e +``` + +可以看到生成了以下文件: + +- [`src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml`](./src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml) +- [`src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java`](./src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java) +- [`src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java`](./src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java) + +#### MyBatis 扫描配置 + +要使用MyBatis, 首先要配置数据源. 当然, 在前面的步骤中我们已经配置好了. + +文件内容参考: [src/main/resources/application.yml](./src/main/resources/application.yml) + +然后, 我们在配置文件中增加 MyBatis 相关的配置 + + + +#### WebMVC + + + + + + +#### 请求链路分析 + + + + + + +#### MySQL服务器参数调整 + + + + + +#### 应用程序优化 \ No newline at end of file diff --git a/07rpc/mysql-demo/docker-compose.yml b/07rpc/mysql-demo/docker-compose.yml new file mode 100644 index 00000000..90c90acf --- /dev/null +++ b/07rpc/mysql-demo/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.1' + +services: + + mysql: + image: mysql:5.7 + command: --default-authentication-plugin=mysql_native_password + ports: + - 13306:3306 + environment: + MYSQL_ROOT_PASSWORD: 123456 + volumes: + - ./docker_data/docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d + - ./docker_data/var/lib/mysql/:/var/lib/mysql diff --git a/07rpc/mysql-demo/docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql b/07rpc/mysql-demo/docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql new file mode 100644 index 00000000..fff3f7ed --- /dev/null +++ b/07rpc/mysql-demo/docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql @@ -0,0 +1,16 @@ +CREATE DATABASE IF NOT EXISTS `mysql_demo`; + +USE `mysql_demo`; + +-- 业务订单表 +CREATE TABLE `t_biz_order` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键;订单id', + `user_id` bigint(20) NOT NULL COMMENT '用户userId', + `state` int(8) NOT NULL DEFAULT '0' COMMENT '订单状态;参考OrderStateEnum', + `create_time` bigint(20) NOT NULL COMMENT '下单时间', + `update_time` bigint(20) NOT NULL COMMENT '更新时间', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单信息表'; + +-- 订单-商品信息表; diff --git a/07rpc/mysql-demo/generateMapper.sh b/07rpc/mysql-demo/generateMapper.sh new file mode 100755 index 00000000..7f5ae537 --- /dev/null +++ b/07rpc/mysql-demo/generateMapper.sh @@ -0,0 +1,6 @@ + +echo mvn clean -U -DskipTests +mvn clean -U -DskipTests + +echo mvn mybatis-generator:generate -X -e +mvn mybatis-generator:generate -X -e diff --git a/07rpc/mysql-demo/pom.xml b/07rpc/mysql-demo/pom.xml new file mode 100644 index 00000000..bf7ac49e --- /dev/null +++ b/07rpc/mysql-demo/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.4 + + + com.cncounter + mysql-demo + 0.0.1-SNAPSHOT + mysql-demo + MySQL Demo + + 1.8 + + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.0 + + + + + com.alibaba + fastjson + 1.2.78 + + + + mysql + mysql-connector-java + 8.0.26 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.6 + + ${basedir}/src/main/resources/MybatisGeneratorConfig.xml + true + true + + + + + mysql + mysql-connector-java + 8.0.26 + + + + + + + diff --git a/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/MysqlDemoApplication.java b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/MysqlDemoApplication.java new file mode 100644 index 00000000..e839acba --- /dev/null +++ b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/MysqlDemoApplication.java @@ -0,0 +1,13 @@ +package com.cncounter.mysqldemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MysqlDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(MysqlDemoApplication.class, args); + } + +} diff --git a/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java new file mode 100644 index 00000000..623c5c16 --- /dev/null +++ b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java @@ -0,0 +1,53 @@ +package com.cncounter.mysqldemo.dao.mapper; + +import com.cncounter.mysqldemo.model.TBizOrder; + +public interface TBizOrderMapper { + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int deleteByPrimaryKey(Long id); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int insert(TBizOrder record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int insertSelective(TBizOrder record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + TBizOrder selectByPrimaryKey(Long id); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int updateByPrimaryKeySelective(TBizOrder record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int updateByPrimaryKey(TBizOrder record); +} \ No newline at end of file diff --git a/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java new file mode 100644 index 00000000..0592cd94 --- /dev/null +++ b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java @@ -0,0 +1,187 @@ +package com.cncounter.mysqldemo.model; + +/** + * Database Table Remarks: + * 订单信息表 + * + * This class was generated by MyBatis Generator. + * This class corresponds to the database table t_biz_order + * + * @mbg.generated do_not_delete_during_merge + */ +public class TBizOrder { + /** + * Database Column Remarks: + * 自增主键;订单id + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.id + * + * @mbg.generated + */ + private Long id; + + /** + * Database Column Remarks: + * 用户userId + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.user_id + * + * @mbg.generated + */ + private Long userId; + + /** + * Database Column Remarks: + * 订单状态;参考OrderStateEnum + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.state + * + * @mbg.generated + */ + private Integer state; + + /** + * Database Column Remarks: + * 下单时间 + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.create_time + * + * @mbg.generated + */ + private Long createTime; + + /** + * Database Column Remarks: + * 更新时间 + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.update_time + * + * @mbg.generated + */ + private Long updateTime; + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.id + * + * @return the value of t_biz_order.id + * + * @mbg.generated + */ + public Long getId() { + return id; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.id + * + * @param id the value for t_biz_order.id + * + * @mbg.generated + */ + public void setId(Long id) { + this.id = id; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.user_id + * + * @return the value of t_biz_order.user_id + * + * @mbg.generated + */ + public Long getUserId() { + return userId; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.user_id + * + * @param userId the value for t_biz_order.user_id + * + * @mbg.generated + */ + public void setUserId(Long userId) { + this.userId = userId; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.state + * + * @return the value of t_biz_order.state + * + * @mbg.generated + */ + public Integer getState() { + return state; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.state + * + * @param state the value for t_biz_order.state + * + * @mbg.generated + */ + public void setState(Integer state) { + this.state = state; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.create_time + * + * @return the value of t_biz_order.create_time + * + * @mbg.generated + */ + public Long getCreateTime() { + return createTime; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.create_time + * + * @param createTime the value for t_biz_order.create_time + * + * @mbg.generated + */ + public void setCreateTime(Long createTime) { + this.createTime = createTime; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.update_time + * + * @return the value of t_biz_order.update_time + * + * @mbg.generated + */ + public Long getUpdateTime() { + return updateTime; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.update_time + * + * @param updateTime the value for t_biz_order.update_time + * + * @mbg.generated + */ + public void setUpdateTime(Long updateTime) { + this.updateTime = updateTime; + } +} \ No newline at end of file diff --git a/07rpc/mysql-demo/src/main/resources/MybatisGeneratorConfig.xml b/07rpc/mysql-demo/src/main/resources/MybatisGeneratorConfig.xml new file mode 100644 index 00000000..54506445 --- /dev/null +++ b/07rpc/mysql-demo/src/main/resources/MybatisGeneratorConfig.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
\ No newline at end of file diff --git a/07rpc/mysql-demo/src/main/resources/application.yml b/07rpc/mysql-demo/src/main/resources/application.yml new file mode 100644 index 00000000..967bfa95 --- /dev/null +++ b/07rpc/mysql-demo/src/main/resources/application.yml @@ -0,0 +1,50 @@ +# Tomcat 配置 +server: + port: 8080 + tomcat: + uri-encoding: UTF-8 + max-threads: 1000 + min-spare-threads: 10 + servlet: + context-path: / + encoding: + enabled: true + force: true + charset: UTF-8 + +# MyBatis 配置 +mybatis: + config-locations: classpath:mybatis-configuration.xml + mapper-locations: classpath:com/cncounter/mysqldemo/dao/mapper/*.xml + type-handlers-package: com.cncounter.mysqldemo.dao.mybatis.handlers.auto + configuration: + cache-enabled: false + lazy-loading-enabled: false + use-generated-keys: true + auto-mapping-behavior: full + default-executor-type: REUSE + default-statement-timeout: 20000 + +# Spring相关配置 +spring: + datasource: + type: com.zaxxer.hikari.HikariDataSource + hikari: + minimum-idle: 1 + maximum-pool-size: 5 + auto-commit: true + idle-timeout: 30000 + pool-name: MySQLHikariCP1 + max-lifetime: 1800000 + connection-timeout: 30000 + connection-test-query: SELECT 1 + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:13306/mysql_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false + username: root + password: 123456 + +# Logback日志级别配置 +logging.level.root: info +logging.level.com.cncounter: debug +logging.level.org.springframework.test.web.servlet.result: debug + diff --git a/07rpc/mysql-demo/src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml b/07rpc/mysql-demo/src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml new file mode 100644 index 00000000..030c2b54 --- /dev/null +++ b/07rpc/mysql-demo/src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + id, user_id, state, create_time, update_time + + + + + delete from t_biz_order + where id = #{id,jdbcType=BIGINT} + + + + + SELECT LAST_INSERT_ID() + + insert into t_biz_order (user_id, state, create_time, + update_time) + values (#{userId,jdbcType=BIGINT}, #{state,jdbcType=TINYINT}, #{createTime,jdbcType=BIGINT}, + #{updateTime,jdbcType=BIGINT}) + + + + + SELECT LAST_INSERT_ID() + + insert into t_biz_order + + + user_id, + + + state, + + + create_time, + + + update_time, + + + + + #{userId,jdbcType=BIGINT}, + + + #{state,jdbcType=TINYINT}, + + + #{createTime,jdbcType=BIGINT}, + + + #{updateTime,jdbcType=BIGINT}, + + + + + + update t_biz_order + + + user_id = #{userId,jdbcType=BIGINT}, + + + state = #{state,jdbcType=TINYINT}, + + + create_time = #{createTime,jdbcType=BIGINT}, + + + update_time = #{updateTime,jdbcType=BIGINT}, + + + where id = #{id,jdbcType=BIGINT} + + + + update t_biz_order + set user_id = #{userId,jdbcType=BIGINT}, + state = #{state,jdbcType=TINYINT}, + create_time = #{createTime,jdbcType=BIGINT}, + update_time = #{updateTime,jdbcType=BIGINT} + where id = #{id,jdbcType=BIGINT} + + \ No newline at end of file diff --git a/07rpc/mysql-demo/src/test/java/com/cncounter/mysqldemo/MysqlDemoApplicationTests.java b/07rpc/mysql-demo/src/test/java/com/cncounter/mysqldemo/MysqlDemoApplicationTests.java new file mode 100644 index 00000000..0403040d --- /dev/null +++ b/07rpc/mysql-demo/src/test/java/com/cncounter/mysqldemo/MysqlDemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.cncounter.mysqldemo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class MysqlDemoApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/07rpc/rpc01/.DS_Store b/07rpc/rpc01/.DS_Store deleted file mode 100644 index 9398e803..00000000 Binary files a/07rpc/rpc01/.DS_Store and /dev/null differ diff --git a/07rpc/rpc01/client-rest.http b/07rpc/rpc01/client-rest.http new file mode 100644 index 00000000..cfe742ec --- /dev/null +++ b/07rpc/rpc01/client-rest.http @@ -0,0 +1 @@ +http://127.0.0.1:8091/api/hello \ No newline at end of file diff --git a/07rpc/rpc01/pom.xml b/07rpc/rpc01/pom.xml index ccd4068e..8c924dcf 100644 --- a/07rpc/rpc01/pom.xml +++ b/07rpc/rpc01/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.9.RELEASE + 2.7.3 io.kimmking @@ -16,9 +16,10 @@ RPC demo project for Spring Boot - rpcfx-api - rpcfx-server - rpcfx-client + rpcfx-core + rpcfx-demo-api + rpcfx-demo-consumer + rpcfx-demo-provider diff --git a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java b/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java deleted file mode 100644 index 3a4de089..00000000 --- a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java +++ /dev/null @@ -1,34 +0,0 @@ -package io.kimmking.rpcfx.api; - -public class RpcfxRequest { - - private String serviceClass; - - private String method; - - private Object[] params; - - public String getServiceClass() { - return serviceClass; - } - - public void setServiceClass(String serviceClass) { - this.serviceClass = serviceClass; - } - - public String getMethod() { - return method; - } - - public void setMethod(String method) { - this.method = method; - } - - public Object[] getParams() { - return params; - } - - public void setParams(Object[] params) { - this.params = params; - } -} diff --git a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java b/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java deleted file mode 100644 index ec574b4f..00000000 --- a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java +++ /dev/null @@ -1,32 +0,0 @@ -package io.kimmking.rpcfx.api; - -public class RpcfxResponse { - - private Object result; - private boolean status; - private Exception exception; - - public Object getResult() { - return result; - } - - public void setResult(Object result) { - this.result = result; - } - - public boolean isStatus() { - return status; - } - - public void setStatus(boolean status) { - this.status = status; - } - - public Exception getException() { - return exception; - } - - public void setException(Exception exception) { - this.exception = exception; - } -} diff --git a/07rpc/rpc01/rpcfx-client/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java b/07rpc/rpc01/rpcfx-client/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java deleted file mode 100644 index 3beb76da..00000000 --- a/07rpc/rpc01/rpcfx-client/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java +++ /dev/null @@ -1,58 +0,0 @@ -package io.kimmking.rpcfx.client; - - -import com.alibaba.fastjson.JSON; -import io.kimmking.rpcfx.api.RpcfxRequest; -import io.kimmking.rpcfx.api.RpcfxResponse; -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; - -import java.io.IOException; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - -public class Rpcfx { - public static T create(final Class serviceClass, final String url) { - - return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url)); - - } - - public static class RpcfxInvocationHandler implements InvocationHandler { - - public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8"); - - - private final Class serviceClass; - private final String url; - public RpcfxInvocationHandler(Class serviceClass, String url) { - this.serviceClass = serviceClass; - this.url = url; - } - - @Override - public Object invoke(Object proxy, Method method, Object[] params) throws Throwable { - RpcfxRequest request = new RpcfxRequest(); - request.setServiceClass(this.serviceClass.getName()); - request.setMethod(method.getName()); - request.setParams(params); - - RpcfxResponse response = post(request, url); - return JSON.parse(response.getResult().toString()); - } - - private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { - String reqJson = JSON.toJSONString(req); - OkHttpClient client = new OkHttpClient(); - final Request request = new Request.Builder() - .url(url) - .post(RequestBody.create(JSONTYPE, reqJson)) - .build(); - String respJson = client.newCall(request).execute().body().string(); - return JSON.parseObject(respJson, RpcfxResponse.class); - } - } -} diff --git a/07rpc/rpc01/rpcfx-client/src/main/java/io/kimmking/rpcfx/client/RpcfxClientApplication.java b/07rpc/rpc01/rpcfx-client/src/main/java/io/kimmking/rpcfx/client/RpcfxClientApplication.java deleted file mode 100644 index 987ae56a..00000000 --- a/07rpc/rpc01/rpcfx-client/src/main/java/io/kimmking/rpcfx/client/RpcfxClientApplication.java +++ /dev/null @@ -1,24 +0,0 @@ -package io.kimmking.rpcfx.client; - -import com.alibaba.fastjson.parser.ParserConfig; -import io.kimmking.rpcfx.api.User; -import io.kimmking.rpcfx.api.UserService; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class RpcfxClientApplication { - - static { - ParserConfig.getGlobalInstance().addAccept("io.kimmking"); - } - - public static void main(String[] args) { - - UserService service = Rpcfx.create(UserService.class, "http://localhost:8080/"); - User user = service.findById(1); - System.out.println("find user id=1 from server: " + user.getName()); - -// SpringApplication.run(RpcfxClientApplication.class, args); - } - -} diff --git a/07rpc/rpc01/rpcfx-client/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-client/src/main/resources/application.yml deleted file mode 100644 index 927305da..00000000 --- a/07rpc/rpc01/rpcfx-client/src/main/resources/application.yml +++ /dev/null @@ -1,2 +0,0 @@ -#server: -# port: 8080 diff --git a/07rpc/rpc01/rpcfx-client/src/test/java/io/kimmking/rpcfx/client/Springboot01ApplicationTests.java b/07rpc/rpc01/rpcfx-client/src/test/java/io/kimmking/rpcfx/client/Springboot01ApplicationTests.java deleted file mode 100644 index 9567c9c0..00000000 --- a/07rpc/rpc01/rpcfx-client/src/test/java/io/kimmking/rpcfx/client/Springboot01ApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -//package io.kimmking.springboot01; -// -//import org.junit.Test; -//import org.springframework.boot.test.context.SpringBootTest; -// -//@SpringBootTest -//class Springboot01ApplicationTests { -// -// @Test -// void contextLoads() { -// } -// -//} diff --git a/07rpc/rpc01/rpcfx-client/pom.xml b/07rpc/rpc01/rpcfx-core/pom.xml similarity index 74% rename from 07rpc/rpc01/rpcfx-client/pom.xml rename to 07rpc/rpc01/rpcfx-core/pom.xml index b0d40524..5a1eeac3 100644 --- a/07rpc/rpc01/rpcfx-client/pom.xml +++ b/07rpc/rpc01/rpcfx-core/pom.xml @@ -9,9 +9,9 @@ io.kimmking - rpcfx-client + rpcfx-core 0.0.1-SNAPSHOT - rpcfx-client + rpcfx-core 1.8 @@ -19,31 +19,44 @@ - io.kimmking - rpcfx-api - ${project.version} + com.alibaba + fastjson + 1.2.83 - org.springframework.boot - spring-boot-starter + org.projectlombok + lombok + 1.18.16 - org.springframework.boot - spring-boot-starter-web + com.squareup.okhttp3 + okhttp + 3.12.2 + - com.alibaba - fastjson - 1.2.70 + org.apache.curator + curator-client + 5.1.0 - com.squareup.okhttp3 - okhttp - 3.12.2 + org.apache.curator + curator-recipes + 5.1.0 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web @@ -57,15 +70,7 @@ - - - - - org.springframework.boot - spring-boot-maven-plugin - - - +
diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxReference.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxReference.java new file mode 100644 index 00000000..570b8ab8 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxReference.java @@ -0,0 +1,17 @@ +package io.kimmking.rpcfx.annotation; + +import java.lang.annotation.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/1 20:00 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Inherited +public @interface RpcfxReference { + +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxService.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxService.java new file mode 100644 index 00000000..9db3c7e3 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxService.java @@ -0,0 +1,18 @@ +package io.kimmking.rpcfx.annotation; + +import java.lang.annotation.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/1 20:00 + */ + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface RpcfxService { + +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java new file mode 100644 index 00000000..29060ace --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java @@ -0,0 +1,11 @@ +package io.kimmking.rpcfx.api; + +public interface Filter { + + RpcfxResponse prefilter(RpcfxRequest request); + + RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response); + + // Filter next(); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java new file mode 100644 index 00000000..5ac4fab2 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java @@ -0,0 +1,11 @@ +package io.kimmking.rpcfx.api; + +import io.kimmking.rpcfx.meta.InstanceMeta; + +import java.util.List; + +public interface LoadBalancer { + + InstanceMeta select(List instances); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java new file mode 100644 index 00000000..a4ed3225 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java @@ -0,0 +1,10 @@ +package io.kimmking.rpcfx.api; + +import io.kimmking.rpcfx.meta.InstanceMeta; + +import java.util.List; + +public interface Router { + + List route(List instances); +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java new file mode 100644 index 00000000..79a65bde --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java @@ -0,0 +1,41 @@ +package io.kimmking.rpcfx.api; + +import io.kimmking.rpcfx.meta.ProviderMeta; +import lombok.Getter; +import lombok.Setter; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +import java.util.HashMap; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 20:34 + */ +public class RpcContext { + + @Getter + private final MultiValueMap providerHolder = new LinkedMultiValueMap<>(); + + @Getter + private final Map consumerHolder = new HashMap<>(); + + @Getter + private final Map parameters = new HashMap<>(); + + @Getter + @Setter + private Router router; + + @Getter + @Setter + private LoadBalancer loadBalancer; + + @Getter + @Setter + private Filter[] filters; + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java new file mode 100644 index 00000000..1e9edfb4 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java @@ -0,0 +1,10 @@ +package io.kimmking.rpcfx.api; + +import lombok.Data; + +@Data +public class RpcfxRequest { + private String serviceClass; + private String methodSign; + private Object[] params; +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java new file mode 100644 index 00000000..b37747ad --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java @@ -0,0 +1,10 @@ +package io.kimmking.rpcfx.api; + +import lombok.Data; + +@Data +public class RpcfxResponse { + private Object result; + private boolean status; + private Exception exception; +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/ServiceProviderDesc.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/ServiceProviderDesc.java new file mode 100644 index 00000000..f66ec625 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/ServiceProviderDesc.java @@ -0,0 +1,16 @@ +package io.kimmking.rpcfx.api; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class ServiceProviderDesc { + + private String host; + private Integer port; + private String serviceClass; + + // group + // version +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java new file mode 100644 index 00000000..a69fe083 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java @@ -0,0 +1,110 @@ +package io.kimmking.rpcfx.consumer; + +import io.kimmking.rpcfx.annotation.RpcfxReference; +import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.registry.RegistryConfiguration; +import io.kimmking.rpcfx.stub.StubSkeletonHelper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeansException; +import org.springframework.beans.PropertyValues; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.context.annotation.Import; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 23:26 + */ +@Slf4j +@Component +@Import({RegistryConfiguration.class}) +public class ConsumerBootstrap implements Closeable, InstantiationAwareBeanPostProcessor { + + private RpcContext context = new RpcContext(); + + private String scanPackage = "io.kimmking"; + + @Value("${app.id:app1}") + public String app; + @Value("${app.namespace:public}") + public String ns; + @Value("${app.env:dev}") + public String env; + @Value("${app.mock:false}") + public boolean mock; + @Value("${app.cache:false}") + public boolean cache; + @Value("${app.retry:1}") + public int retry; + + @Autowired + RegistryCenter rc; + + @PostConstruct + public void init() { + this.context.getParameters().put("app.id", app); + this.context.getParameters().put("app.namespace", ns); + this.context.getParameters().put("app.env", env); + this.context.getParameters().put("app.mock", String.valueOf(mock)); + this.context.getParameters().put("app.cache", String.valueOf(cache)); + this.context.getParameters().put("app.retry", String.valueOf(retry)); + } + + @Override + public void close() throws IOException { + + } + + @Override + public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException { + if (bean.getClass().getPackage().getName().startsWith(scanPackage)) { + Field[] declaredFields = resolveAllField(bean.getClass()); // 解决父类里的注解扫描不到的问题 + + List consumers = Arrays.stream(declaredFields) + .filter(field -> field.isAnnotationPresent(RpcfxReference.class)) + .collect(Collectors.toList()); + + consumers.stream().forEach(field -> { + Object consumer = createConsumer(field.getType()); + try { + field.setAccessible(true); + field.set(bean, consumer); + } catch (IllegalAccessException e) { + log.error(e.getMessage(), e); + } + }); + } + return null; + } + + private Field[] resolveAllField(Class aClass) { + List res = new ArrayList<>(20); + while ( !Object.class.equals(aClass) ) { + Field[] fields = aClass.getDeclaredFields(); + res.addAll(Arrays.asList(fields)); + aClass = aClass.getSuperclass(); + } + return res.toArray(new Field[0]); + } + + private T createConsumer(Class clazz) { + ServiceMeta sm = ServiceMeta.builder().name(clazz.getCanonicalName()) + .app(app).namespace(ns).env(env).build(); + return (T) StubSkeletonHelper.createConsumer(sm, context, rc); + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java new file mode 100644 index 00000000..66d9ea89 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java @@ -0,0 +1,70 @@ +package io.kimmking.rpcfx.consumer; + + +import com.alibaba.fastjson.parser.ParserConfig; +import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; + +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; + +public final class RpcfxConsumerInvoker { + + static { + ParserConfig.getGlobalInstance().addAccept("io.kimmking"); + } + + RpcContext ctx; + + RegistryCenter rc; + + public RpcfxConsumerInvoker(RpcContext ctx, RegistryCenter rc) { + this.ctx = ctx; + this.rc = rc; //"localhost:2181" + } + + public void start() { + this.rc.start(); + } + + public void stop() { + this.rc.stop(); + } + + public T createFromRegistry(final ServiceMeta sm, RpcContext ctx) { + + String service = sm.getName();//"io.kimking.rpcfx.demo.api.UserService"; + System.out.println("====> "+service); + List invokers = new ArrayList<>(); + Class serviceClass = null; + try { + + serviceClass = Class.forName(service); + + List insts = rc.fetchInstances(sm); + if(insts != null && insts.size()>0) invokers.addAll(insts); + rc.subscribe(sm, e -> { + invokers.clear(); + invokers.addAll((List)e.getData()); + }); + + } catch (Exception ex) { + ex.printStackTrace(); + throw new RuntimeException(ex); + } + + return (T) create(serviceClass, invokers, ctx); + + } + + private T create(Class serviceClass, List invokers, RpcContext ctx) { + RpcfxInvocationHandler invocationHandler + = new RpcfxInvocationHandler(serviceClass, invokers, ctx); + return (T) Proxy.newProxyInstance(RpcfxConsumerInvoker.class.getClassLoader(), + new Class[]{serviceClass}, invocationHandler); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java new file mode 100644 index 00000000..0325e0c7 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java @@ -0,0 +1,140 @@ +package io.kimmking.rpcfx.consumer; + +import com.alibaba.fastjson.JSON; +import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.stub.StubSkeletonHelper; +import io.kimmking.rpcfx.utils.MethodUtils; +import okhttp3.*; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.net.SocketTimeoutException; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class RpcfxInvocationHandler implements InvocationHandler { + + public final Object target = new Object(); + + public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8"); + + private final Class serviceClass; + private final List invokers; + + private final RpcContext context; + + public RpcfxInvocationHandler(Class serviceClass, List invokers, RpcContext ctx) { + this.serviceClass = serviceClass; + this.invokers = invokers; + this.context = ctx; + } + + // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx + // int byte char float double long bool + // [], data class + + @Override + public Object invoke(Object proxy, Method method, Object[] params) throws Throwable { + + long start = System.currentTimeMillis(); + + if (!StubSkeletonHelper.checkRpcMethod(method)){ + return method.invoke(target, params); + } + + + int retry = 2; + while (retry-- > 0) { + System.out.println("retry:" + retry); + try { + + // check mock, 挡板功能 TODO 3 + + List insts = context.getRouter().route(invokers); +// System.out.println("router.route => "); +// urls.forEach(System.out::println); + InstanceMeta instance = context.getLoadBalancer().select(insts); // router, loadbalance +// System.out.println("loadBalance.select => "); +// System.out.println("final => " + url); + + if (instance == null) { + throw new RuntimeException("No available providers from registry center."); + } + + + RpcfxRequest request = new RpcfxRequest(); + request.setServiceClass(this.serviceClass.getName()); + request.setMethodSign(MethodUtils.methodSign(method)); + request.setParams(params); + + Filter[] filters = context.getFilters(); + + if (null != filters) { + for (Filter filter : filters) { + RpcfxResponse response = filter.prefilter(request); + if (response != null) { + return JSON.parse(response.getResult().toString()); + } + } + } + + // 没有控制超时,可能会很久 TODO 2 + RpcfxResponse response = post(request, instance); + + if (null != filters) { + for (Filter filter : filters) { + RpcfxResponse postResponse = filter.postfilter(request, response); + if (postResponse!=null) { + response = postResponse; + } + } + } + + System.out.println("Invoke spend " + (System.currentTimeMillis()-start) + " ms"); + + // 加filter地方之三 + // Student.setTeacher("cuijing"); + + // 这里判断response.status,处理异常 + // 考虑封装一个全局的RpcfxException + + return JSON.parse(response.getResult().toString()); + + } catch (RuntimeException ex) { + ex.printStackTrace(); + if(! (ex.getCause() instanceof SocketTimeoutException)) { + break; + } + } + } + return null; + + } + + OkHttpClient client = new OkHttpClient.Builder() + .connectionPool(new ConnectionPool(128, 60, TimeUnit.SECONDS)) +// .dispatcher(dispatcher) + .readTimeout(1, TimeUnit.SECONDS) + .writeTimeout(1, TimeUnit.SECONDS) + .connectTimeout(1, TimeUnit.SECONDS) + .build(); + + private RpcfxResponse post(RpcfxRequest req, InstanceMeta instance) throws Exception { + String reqJson = JSON.toJSONString(req); + System.out.println("req json: "+reqJson); + + final Request request = new Request.Builder() + .url(instance.toString()) + .post(RequestBody.create(JSONTYPE, reqJson)) + .build(); + String respJson; + try { + respJson = client.newCall(request).execute().body().string(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + System.out.println("resp json: "+respJson); + return JSON.parseObject(respJson, RpcfxResponse.class); + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java new file mode 100644 index 00000000..e1388143 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java @@ -0,0 +1,48 @@ +package io.kimmking.rpcfx.meta; + +import com.google.common.base.Strings; +import lombok.*; + +import java.net.URI; +import java.util.Map; +import java.util.Objects; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 19:46 + */ + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(of = {"scheme", "host", "port", "context"}) +public class InstanceMeta { + + private String scheme; + private String host; + private Integer port; + private String context; + private boolean status; + private Map metadata; + + public static InstanceMeta from(String instance) { + URI uri = URI.create(instance); + String path = uri.getPath(); + path = Strings.isNullOrEmpty(path) ? "" : path.substring(1); + return InstanceMeta.builder() + .scheme(uri.getScheme()) + .host(uri.getHost()) + .port(uri.getPort()) + .context(path) + .build(); + } + + @Override + public String toString() { + return scheme + "://" + host + ":" + port + "/" + context; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ProviderMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ProviderMeta.java new file mode 100644 index 00000000..f0b9e0ae --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ProviderMeta.java @@ -0,0 +1,19 @@ +package io.kimmking.rpcfx.meta; + +import lombok.Data; + +import java.lang.reflect.Method; + +/** + * @author lirui + */ +@Data +public class ProviderMeta { + + private Object serviceImpl; + + private Method method; + + private String methodSign; + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java new file mode 100644 index 00000000..239042b8 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java @@ -0,0 +1,24 @@ +package io.kimmking.rpcfx.meta; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/4/13 21:43 + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(of = {"url"}) +public class ServerMeta { + private String url; + private boolean leader; + private boolean status; + private long version; +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java new file mode 100644 index 00000000..c443a272 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java @@ -0,0 +1,25 @@ +package io.kimmking.rpcfx.meta; + +import lombok.Builder; +import lombok.Data; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 19:46 + */ +@Data +@Builder +public class ServiceMeta { + + private String app; + private String namespace; + private String env; + private String name; + + @Override + public String toString() { + return String.format("%s_%s_%s_%s", app, namespace, env, name); + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java new file mode 100644 index 00000000..ce714bb0 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java @@ -0,0 +1,154 @@ +package io.kimmking.rpcfx.provider; + +import io.kimmking.rpcfx.annotation.RpcfxService; +import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.registry.RegistryConfiguration; +import io.kimmking.rpcfx.stub.StubSkeletonHelper; +import lombok.Getter; +import lombok.SneakyThrows; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationRunner; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.Order; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import java.net.InetAddress; +import java.util.Arrays; +import java.util.Objects; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 20:27 + */ + +@Component +@Import({RegistryConfiguration.class}) +public class ProviderBootstrap { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + Environment environment; + + @Value("${app.id:app1}") + public String app; + @Value("${app.namespace:public}") + public String ns; + @Value("${app.env:dev}") + public String env; + + private final RpcContext context = new RpcContext(); + + @Getter + private final RpcfxProviderInvoker invoker = new RpcfxProviderInvoker(context);; + + private static String SCHEME = "http"; + private static String ip; + private static int port; + + @Autowired + RegistryCenter registry;// = new KKRegistryCenter(); + + @SneakyThrows + @PostConstruct + public void start(){ + System.out.println("build all services from annotation..."); + buildProvider(); + + System.out.println("get IP and PORT..."); + ip = InetAddress.getLocalHost().getHostAddress(); + port = Integer.parseInt(Objects.requireNonNull(environment.getProperty("server.port"))); + } + + private void buildProvider() { + String[] beansName = applicationContext.getBeanDefinitionNames(); + for (int i = 0; i < beansName.length; i++) { + String beanName = beansName[i]; + Object bean = applicationContext.getBean(beanName); + RpcfxService provider = AnnotationUtils.findAnnotation(bean.getClass(), RpcfxService.class); + if (provider == null) { + continue; + } + Class[] classes = bean.getClass().getInterfaces(); + if (classes == null || classes.length == 0) { + continue; + } + Arrays.stream(classes).forEach(c -> this.createProvider(c, bean)); + } + } + + private void createProvider(Class clazz, Object bean) { + StubSkeletonHelper.createProvider(clazz, bean, context); // 初始化了holder + } + + @Order(Integer.MIN_VALUE) + @Bean + public ApplicationRunner run() throws Exception { + return x -> registerServices(); + } + + private void registerServices() { + + registry.start(); + + System.out.println("registry all services from RegistryCenter..."); + context.getProviderHolder().forEach( (x, y) -> + { + System.out.println(" register " + x); + ServiceMeta sm = ServiceMeta.builder().name(x) + .app(app).namespace(ns).env(env).build(); + + InstanceMeta im = InstanceMeta.builder() + .scheme(SCHEME).host(ip).port(port).context("").build(); + try { + registry.registerService(sm, im); + registry.heartbeat(sm, im); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + ); + } + + @PreDestroy + public void stop() { + unregisterServices(); + } + + private void unregisterServices() { + System.out.println("unregistry all services from RegistryCenter..."); + context.getProviderHolder().forEach( (x, y) -> + { + System.out.println(" unregister " + x); + ServiceMeta sm = ServiceMeta.builder().name(x) + .app(app).namespace(ns).env(env).build(); + InstanceMeta im = InstanceMeta.builder() + .scheme(SCHEME).host(ip).port(port).context("").build(); + try { + registry.unregisterService(sm, im); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + ); + + registry.stop(); + + } + + + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java new file mode 100644 index 00000000..6842dfe6 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java @@ -0,0 +1,61 @@ +package io.kimmking.rpcfx.provider; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; +import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.api.RpcfxRequest; +import io.kimmking.rpcfx.api.RpcfxResponse; +import io.kimmking.rpcfx.meta.ProviderMeta; +import org.springframework.util.CollectionUtils; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Optional; + +public class RpcfxProviderInvoker { + + RpcContext context; + + public RpcfxProviderInvoker(RpcContext context) { + this.context = context; + } + + public RpcfxResponse invoke(RpcfxRequest request) { + RpcfxResponse response = new RpcfxResponse(); + String serviceClass = request.getServiceClass(); + + ProviderMeta meta = findProvider(serviceClass, request.getMethodSign()); + + try { + Method method = meta.getMethod(); + // 没有控制超时,所以可能会很久 TODO 1 + Object result = method.invoke(meta.getServiceImpl(), request.getParams()); // dubbo, fastjson, + // 两次json序列化能否合并成一个 + response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName)); + response.setStatus(true); + return response; + } catch ( IllegalAccessException | InvocationTargetException e) { + + // 3.Xstream + + // 2.封装一个统一的RpcfxException + // 客户端也需要判断异常 + e.printStackTrace(); + response.setException(e); + response.setStatus(false); + return response; + } + } + + protected ProviderMeta findProvider(String interfaceName, String methodSign) { + List providerMetas = context.getProviderHolder().get(interfaceName); + if (!CollectionUtils.isEmpty(providerMetas)) { + Optional providerMeta = providerMetas.stream() + .filter(provider -> methodSign.equals(provider.getMethodSign())).findFirst(); + return providerMeta.orElse(null); + } + return null; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java new file mode 100644 index 00000000..971cbcda --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java @@ -0,0 +1,13 @@ +package io.kimmking.rpcfx.registry; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 20:19 + */ +public interface ChangedListener { + + void fireEvent(Event e); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java new file mode 100644 index 00000000..19a92cd4 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java @@ -0,0 +1,29 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.meta.InstanceMeta; +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 20:20 + */ +public interface Event { + + T getData(); + + static Event> withData(List list) { + return new ChangedEvent(list); + } + + @Data + @AllArgsConstructor + class ChangedEvent implements Event> { + List data; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java new file mode 100644 index 00000000..871c1a7e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java @@ -0,0 +1,35 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.api.ServiceProviderDesc; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; + +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 15:23 + */ +public interface RegistryCenter { + + void start(); + + void stop(); + + void registerService(ServiceMeta service, InstanceMeta instance) throws Exception; + + void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception; + + List fetchInstances(ServiceMeta service) throws Exception; + + void subscribe(ServiceMeta service, ChangedListener> listener); + + void heartbeat(ServiceMeta service, InstanceMeta instance); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java new file mode 100644 index 00000000..8391c4aa --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java @@ -0,0 +1,24 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.registry.kkregistry.KKRegistryCenter; +import io.kimmking.rpcfx.registry.zookeeper.ZookeeperRegistryCenter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/9 01:05 + */ + +@Configuration +public class RegistryConfiguration { + + @Bean + RegistryCenter createRC() { + return new KKRegistryCenter(); + //return new ZookeeperRegistryCenter(); //KKRegistryCenter(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java new file mode 100644 index 00000000..bbf0e17e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java @@ -0,0 +1,42 @@ +package io.kimmking.rpcfx.registry.kkregistry; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/1 06:18 + */ +public class KKHeathChecker { + + final int interval = 5_000; + + final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"); + + public void check(Callback callback) { + executor.scheduleWithFixedDelay(() -> { + System.out.println("start to check kk health ...[" + DTF.format(LocalDateTime.now()) + "]"); + try { + callback.call(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }, interval, interval, TimeUnit.MILLISECONDS); + } + + public void stop() { + this.executor.shutdown(); + } + + public interface Callback { + void call() throws Exception; + } + + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java new file mode 100644 index 00000000..001a3854 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java @@ -0,0 +1,209 @@ +package io.kimmking.rpcfx.registry.kkregistry; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServerMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.ChangedListener; +import io.kimmking.rpcfx.registry.Event; +import io.kimmking.rpcfx.registry.RegistryCenter; +import lombok.SneakyThrows; +import okhttp3.ConnectionPool; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +import static io.kimmking.rpcfx.consumer.RpcfxInvocationHandler.JSONTYPE; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 15:25 + */ +public class KKRegistryCenter implements RegistryCenter { + + public String RC_Server = "http://localhost:8485"; + private ServerMeta leader; + private List servers; + private Map TV = new HashMap<>(); + + OkHttpClient client; + @SneakyThrows + @Override + public void start() { + client = new OkHttpClient.Builder() + .connectionPool(new ConnectionPool(128, 60, TimeUnit.SECONDS)) +// .dispatcher(dispatcher) + .readTimeout(65, TimeUnit.SECONDS) + .writeTimeout(65, TimeUnit.SECONDS) + .connectTimeout(3, TimeUnit.SECONDS) + .build(); + + String url = RC_Server + "/cluster"; + boolean init = false; + while(!init) { + System.out.println("===============>> cluster info from :" + url); + List new_servers = null; + ServerMeta new_leader = null; + try { + String respJson = get(url); + new_servers = JSON.parseObject(respJson, new TypeReference>() { + }); + new_leader = new_servers.stream().filter(ServerMeta::isStatus) + .filter(ServerMeta::isLeader).findFirst().orElse(null); + } catch (Exception exception) { + exception.printStackTrace(); + } + + if(new_leader == null) { + System.out.println("===============>> no leader, 500ms later and retry."); + Thread.sleep(500); + Random random = new Random(); + if(new_servers !=null && new_servers.size() > 1) { + url = new_servers.get(random.nextInt(new_servers.size())).getUrl() + "/cluster"; + } else if((new_servers ==null || new_servers.isEmpty()) && !servers.isEmpty()) { + url = servers.get(random.nextInt(servers.size())).getUrl() + "/cluster"; + } + } else { + this.servers = new_servers; + this.leader = new_leader; + init = true; + System.out.println("===============>> init ok, new_leader = " + new_leader); + System.out.println("===============>> init ok, new_servers = " + new_servers); + } + } + } + + @Override + public void stop() { + this.checker.stop(); + } + + @Override + public void registerService(ServiceMeta service, InstanceMeta instance) throws Exception { + instance.setStatus(true); + String reqJson = JSON.toJSONString(instance); + String url = leader.getUrl() + "/reg?service=" + service; + post(url, reqJson); +// String reqJson = "{\n" + +// " \"scheme\": \"http\",\n" + +// " \"ip\": \"" + instance.getIp() + "\",\n" + +// " \"port\": \"" + instance.getPort() + "\",\n" + +// " \"context\": \"\",\n" + +// " \"status\": \"online\",\n" + +// " \"metadata\": {\n" + +// " \"env\": \"dev\",\n" + +// " \"tag\": \"RED\"\n" + +// " }\n" + +// "}"; +// final Request request = new Request.Builder() +// .url("http://localhost:8484/reg?service=" + service) +// .post(RequestBody.create(JSONTYPE, reqJson)) +// .build(); +// String respJson = client.newCall(request).execute().body().string(); +// System.out.println(respJson); + } + + private String post(String url, String reqJson) throws IOException { + System.out.println(" ====> request: " + url); + final Request request = new Request.Builder() + .url(url) + .post(RequestBody.create(JSONTYPE, reqJson)) + .build(); + String respJson = client.newCall(request).execute().body().string(); + System.out.println(" ====> response: " + respJson); + return respJson; + } + + private String get(String url) throws IOException { + System.out.println(" ====> request: " + url); + final Request request = new Request.Builder() + .url(url) + .get() + .build(); + String respJson = client.newCall(request).execute().body().string(); + System.out.println(" ====> response: " + respJson); + return respJson; + } + + @Override + public void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception { + String reqJson = "{\n" + + " \"scheme\": \"http\",\n" + + " \"host\": \"" + instance.getHost() + "\",\n" + + " \"port\": \"" + instance.getPort() + "\",\n" + + " \"context\": \"\"\n" + + "}"; + String url = leader.getUrl() + "/unreg?service=" + service; + post(url, reqJson); + } + + public List fetchInstances(ServiceMeta service) throws Exception { + String url = RC_Server + "/findAll?service=" + service; + String respJson = get(url); + List instances = JSON.parseObject(respJson, new TypeReference>() { + }); + return instances; + } + + KKHeathChecker checker = new KKHeathChecker(); + + // for Consumer + public void subscribe(ServiceMeta service, final ChangedListener> listener) { + checker.check( () -> { + if(hb(service)) { + List instances = fetchInstances(service); + Event> e = Event.withData(instances); + listener.fireEvent(e); + } + }); + + // 定时器轮询 + // 保存上一次的TV + // 如果有差异就fire + } + + private boolean hb(ServiceMeta service) throws Exception { + String svc = service.toString(); + String url = RC_Server + "/version?service=" + svc; + String respJson = get(url); + Long v = Long.valueOf(respJson); + Long o = TV.getOrDefault(svc, -1L); + if ( v > o) { + TV.put(svc, v); + return o > -1L; + } + return false; + } + + + // for Provider + public void heartbeat(ServiceMeta service, InstanceMeta instance) { + checker.check( () -> { + heart(service, instance); + }); + } + + Long heart(ServiceMeta service, InstanceMeta instance) throws Exception { + String reqJson = "{\n" + + " \"scheme\": \"http\",\n" + + " \"host\": \"" + instance.getHost() + "\",\n" + + " \"port\": \"" + instance.getPort() + "\",\n" + + " \"context\": \"\",\n" + + " \"status\": true\n" + + "}"; + String url = leader.getUrl() + "/renew?service=" + service; + String respJson = post(url, reqJson); + return Long.valueOf(respJson); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java new file mode 100644 index 00000000..37f7b6ea --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java @@ -0,0 +1,101 @@ +package io.kimmking.rpcfx.registry.zookeeper; + +import io.kimmking.rpcfx.api.ServiceProviderDesc; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.ChangedListener; +import io.kimmking.rpcfx.registry.Event; +import io.kimmking.rpcfx.registry.RegistryCenter; +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; + +import java.util.ArrayList; +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 20:16 + */ +public class ZookeeperRegistryCenter implements RegistryCenter { + +// private final List listeners = new ArrayList<>(); +// public void addListener(ChangedListener listener) { +// this.listeners.add(listener); +// } + + CuratorFramework client = null; + public void start() { + RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); + client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("rpcfx").retryPolicy(retryPolicy).build(); + client.start(); + } + + public void stop(){ + client.close(); + } + + public void registerService(ServiceMeta service, InstanceMeta instance) throws Exception { + ServiceProviderDesc userServiceSesc = ServiceProviderDesc.builder() + .host(instance.getHost()) + .port(instance.getPort()).serviceClass(service.getName()).build(); + // String userServiceSescJson = JSON.toJSONString(userServiceSesc); + + try { + if ( null == client.checkExists().forPath("/" + service)) { + client.create().withMode(CreateMode.PERSISTENT).forPath("/" + service, "service".getBytes()); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + client.create().withMode(CreateMode.EPHEMERAL). + forPath( "/" + service + "/" + userServiceSesc.getHost() + "_" + userServiceSesc.getPort(), "provider".getBytes()); + } + + public void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception { + + if (null == client.checkExists().forPath("/" + service)) { + return; + } + System.out.println("delete " + "/" + service + "/" + instance.getHost() + "_" + instance.getPort()); + client.delete().quietly(). + forPath( "/" + service + "/" + instance.getHost() + "_" + instance.getPort()); + } + + public List fetchInstances(ServiceMeta service) throws Exception { + List services = client.getChildren().forPath("/" + service); + List instances = new ArrayList<>(); + for (String svc : services) { + System.out.println(svc); + String url = svc.replace("_", ":"); + instances.add(InstanceMeta.from("http://" + url)); + } + return instances; + } + + public void subscribe(ServiceMeta service, ChangedListener listener) { + final TreeCache treeCache = TreeCache.newBuilder(client, "/" + service).setCacheData(true).setMaxDepth(2).build(); + treeCache.getListenable().addListener((curatorFramework, treeCacheEvent) -> { + System.out.println("treeCacheEvent: "+treeCacheEvent); + List instances = fetchInstances(service); + listener.fireEvent(Event.withData(instances)); + }); + try { + treeCache.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void heartbeat(ServiceMeta service, InstanceMeta instance) { + // do nothing + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java new file mode 100644 index 00000000..9fe5269c --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java @@ -0,0 +1,30 @@ +package io.kimmking.rpcfx.stub; + +import io.kimmking.rpcfx.utils.MockUtils; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/11 02:57 + */ +public class MockHandler implements InvocationHandler { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Class type = method.getReturnType(); + System.out.println("invoke by mock handler..."); + return MockUtils.mock(type, null); + } + + public static T createMock(Class serviceClass) { + //final ServiceMeta sm, Router router, LoadBalancer loadBalance, Filter filter) { + return (T) Proxy.newProxyInstance(MockHandler.class.getClassLoader(), + new Class[]{serviceClass}, new MockHandler()); + + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java new file mode 100644 index 00000000..e77215a8 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java @@ -0,0 +1,186 @@ +package io.kimmking.rpcfx.stub; + +import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.consumer.RpcfxConsumerInvoker; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ProviderMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.utils.MethodUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.MultiValueMap; + +import java.lang.reflect.Method; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @author lirui + */ +public class StubSkeletonHelper { + + public static void createProvider(Class clazz, Object serviceImpl, RpcContext rpcContext) { + String clazzName = clazz.getName(); + Class callClass = serviceImpl.getClass(); + + Method[] methodList = callClass.getMethods(); + for (Method method : methodList) { + if (!checkRpcMethod(method)) { + continue; + } + ProviderMeta providerMeta = buildProviderMeta(method, serviceImpl); + + MultiValueMap providerHolder = rpcContext.getProviderHolder(); + providerHolder.add(clazzName, providerMeta); + } + } + + private static ProviderMeta buildProviderMeta(Method method, Object serviceImpl) { + String methodSign = MethodUtils.methodSign(method); + ProviderMeta providerMeta = new ProviderMeta(); + providerMeta.setMethod(method); + providerMeta.setServiceImpl(serviceImpl); + providerMeta.setMethodSign(methodSign); + return providerMeta; + } + + public static boolean checkRpcMethod(final Method method) { + //本地方法不代理 + if ("toString".equals(method.getName()) || + "hashCode".equals(method.getName()) || + "notifyAll".equals(method.getName()) || + "equals".equals(method.getName()) || + "wait".equals(method.getName()) || + "getClass".equals(method.getName()) || + "notify".equals(method.getName())) { + return false; + } + return true; + } + + public static T createConsumer(ServiceMeta sm, RpcContext ctx, RegistryCenter rc) { + String clazzName = sm.getName(); + Class serviceClass = null; + try { + serviceClass = Class.forName(clazzName); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + T proxyHandler = (T) ctx.getConsumerHolder().get(clazzName); + if (proxyHandler == null) { // TODO configuration + + ctx.setRouter(new TagRouter()); + ctx.setLoadBalancer(new RoundRibbonLoadBalancer()); + ctx.setFilters(createFilters(ctx)); + + T mockHandler = createMockHandler(ctx, serviceClass); + if(mockHandler != null) { + return mockHandler; + } + + RpcfxConsumerInvoker consumerInvoker = new RpcfxConsumerInvoker(ctx, rc); + consumerInvoker.start(); + proxyHandler = consumerInvoker.createFromRegistry(sm, ctx); + ctx.getConsumerHolder().put(clazzName, proxyHandler); + } + return proxyHandler; + } + + private static Filter[] createFilters(RpcContext ctx) { + String cache = ctx.getParameters().getOrDefault("app.cache", "false"); + Filter[] filters = null; + if("true".equalsIgnoreCase(cache)) { + filters = new Filter[]{new CuicuiFilter(), new CacheFilter()}; + } else { + filters = new Filter[]{new CuicuiFilter()}; + } + return filters; + } + + private static T createMockHandler(RpcContext ctx, Class serviceClass) { + String mock = ctx.getParameters().getOrDefault("app.mock", "false"); + if("true".equalsIgnoreCase(mock)) { + return (T) MockHandler.createMock(serviceClass); + } + return null; + } + + + private static class TagRouter implements Router { + @Override + public List route(List instances) { + return instances; + } + } + + private static class RoundRibbonLoadBalancer implements LoadBalancer { + private final AtomicInteger count = new AtomicInteger(0); + @Override + public InstanceMeta select(List instances) { + if(instances.isEmpty()) return null; + return instances.get((count.getAndIncrement() & Integer.MAX_VALUE) % instances.size()); + } + } + + private static class RandomLoadBalancer implements LoadBalancer { + private final Random random = new Random(); + @Override + public InstanceMeta select(List instances) { + if(instances.isEmpty()) return null; + return instances.get(random.nextInt(instances.size())); + } + } + + @Slf4j + private static class CuicuiFilter implements Filter { + @Override + public RpcfxResponse prefilter(RpcfxRequest request) { + //log.info("filter {} -> {}", this.getClass().getName(), request.toString()); + //System.out.printf("filter %s -> %s%n", this.getClass().getName(), request.toString()); + return null; + } + + @Override + public RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response) { + return response; + } + + } + + private static class CacheFilter implements Filter { + + static Map CACHE = new HashMap<>(); + + @Override + public RpcfxResponse prefilter(RpcfxRequest request) { + RpcfxResponse response = CACHE.get(genKey(request)); + if(response != null) { + System.out.println("CacheFilter.prefilter hit! => request: \n" + request + "\n =>response: \n" + response); + } + return response; + //log.info("filter {} -> {}", this.getClass().getName(), request.toString()); + //System.out.printf("filter %s -> %s%n", this.getClass().getName(), request.toString()); + } + + @Override + public RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response) { + String key = genKey(request); + if(!CACHE.containsKey(key)) { + CACHE.put(key, response); + } + return response; + } + + } + + public static String genKey(RpcfxRequest request) { + StringBuilder sb = new StringBuilder(); + sb.append(request.getServiceClass()); + sb.append("@"); + sb.append(request.getMethodSign()); + //sb.append(""); + Arrays.stream(request.getParams()).forEach(x -> sb.append("_"+x.toString())); + return sb.toString(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java new file mode 100644 index 00000000..f17a102f --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java @@ -0,0 +1,30 @@ +package io.kimmking.rpcfx.utils; + +import org.springframework.util.DigestUtils; + +import java.lang.reflect.Method; +import java.util.Arrays; + +public class MethodUtils { + + public static String methodSign(Method method) { + if (method != null) { + StringBuilder builder = new StringBuilder(); + String name = method.getName(); + builder.append(name); + builder.append("@"); + int count = method.getParameterCount(); + builder.append(count); + builder.append("_"); + if (count > 0) { + Class[] classes = method.getParameterTypes(); + Arrays.stream(classes).forEach(c -> builder.append(c.getName() + ",")); + } + return builder.toString(); +// String string = builder.toString(); +// return DigestUtils.md5DigestAsHex(string.getBytes()); + } + return ""; + } + +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java new file mode 100644 index 00000000..8d22536a --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java @@ -0,0 +1,143 @@ +package io.kimmking.rpcfx.utils; + +import lombok.Data; +import org.springframework.util.ClassUtils; + +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/11 03:15 + */ +public class MockUtils { + + public static Object mock(Class clazz, Type[] generics) { + boolean primitiveOrWrapper = ClassUtils.isPrimitiveOrWrapper(clazz); + if(primitiveOrWrapper) return mockPrimitive(clazz); + if(String.class.equals(clazz)) return mockString(); + if (Number.class.isAssignableFrom(clazz)) { + return 10; + } + if(clazz.isArray()) { + return mockArray(clazz.getComponentType()); + } + if(List.class.isAssignableFrom(clazz)) { + return mockList(clazz, generics[0]); + } + if(Map.class.isAssignableFrom(clazz)) { + return mockMap(clazz, generics[1]); + } + return mockPojo(clazz); + } + + private static Object mockMap(Class clazz, Type generic) { + HashMap map = new HashMap<>(); + map.put("a", mock((Class)generic, null)); + map.put("b", mock((Class)generic, null)); + return map; + } + + private static Object mockList(Class clazz, Type generic) { + List list = new ArrayList<>(); + list.add(mock((Class)generic, null)); + list.add(mock((Class)generic, null)); + return list; + } + + private static Object mockArray(Class clazz) { + Object array = Array.newInstance(clazz, 2); + Array.set(array, 0, mock(clazz, null)); + Array.set(array, 1, mock(clazz,null)); + return array; + } + + private static Object mockPojo(Class clazz) { + try { + Object object = clazz.getDeclaredConstructor().newInstance(); + Field[] fields = clazz.getDeclaredFields(); + for (Field f : fields) { + f.setAccessible(true); + Type genericType = f.getGenericType(); +// System.out.println(f.getGenericType()); +// System.out.println(f.getType()); + if (genericType instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericType; + Type[] typeArguments = parameterizedType.getActualTypeArguments(); + System.out.println("genericType="+Arrays.toString(typeArguments)); + f.set(object, mock(f.getType(), typeArguments)); + } else { + f.set(object, mock(f.getType(),null)); + } + } + return object; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + private static Object mockString() { + return "this_is_a_mock_string"; + } + + private static Object mockPrimitive(Class clazz) { + + if (Boolean.class.equals(clazz)) { + return true; + } + + return 1; + } + + public static void main(String[] args) { + + System.out.println(mock(ListPojo.class,null)); + + +// System.out.println(mock(Byte.class)); +// System.out.println(mock(Character.class)); +// System.out.println(mock(Boolean.class)); +// System.out.println(mock(Integer.class)); +// System.out.println(mock(Float.class)); +// System.out.println(mock(Short.class)); +// System.out.println(mock(Long.class)); +// System.out.println(mock(Double.class)); +// System.out.println(mock(BigInteger.class)); +// System.out.println(mock(BigDecimal.class)); +// System.out.println(mock(String.class)); + +// System.out.println(mock(Pojo.class)); + +// Arrays.stream(((Pojo[]) mock(new Pojo[]{}.getClass()))).forEach(System.out::println); + + } + + + @Data + public static class Pojo { + private int id; + private String name; + private float amount; + private InnerPojo inner; + } + + @Data + public static class InnerPojo { + private int value; + private String key; + } + + @Data + public static class ListPojo { + private List list; + private Integer inner; + private Map map; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java new file mode 100644 index 00000000..6ada40ed --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java @@ -0,0 +1,219 @@ +package io.kimmking.rpcfx.utils; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/13 23:44 + */ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by caojun on 2018/2/20. + * + * 基本概念: + * weight: 配置文件中指定的该后端的权重,这个值是固定不变的。 + * effective_weight: 后端的有效权重,初始值为weight。 + * 在释放后端时,如果发现和后端的通信过程中发生了错误,就减小effective_weight。 + * 此后有新的请求过来时,在选取后端的过程中,再逐步增加effective_weight,最终又恢复到weight。 + * 之所以增加这个字段,是为了当后端发生错误时,降低其权重。 + * current_weight: + * 后端目前的权重,一开始为0,之后会动态调整。那么是怎么个动态调整呢? + * 每次选取后端时,会遍历集群中所有后端,对于每个后端,让它的current_weight增加它的effective_weight, + * 同时累加所有后端的effective_weight,保存为total。 + * 如果该后端的current_weight是最大的,就选定这个后端,然后把它的current_weight减去total。 + * 如果该后端没有被选定,那么current_weight不用减小。 + * + * 算法逻辑: + * 1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行: + *     peer->current_weight += peer->effecitve_weight。 + *     同时累加所有peer的effective_weight,保存为total。 + * 2. 从集群中选出current_weight最大的peer,作为本次选定的后端。 + * 3. 对于本次选定的后端,执行:peer->current_weight -= total。 + * + */ +public class RoundRobinByWeightLoadBalance { + + //约定的invoker和权重的键值对 + final private List nodes; + + public RoundRobinByWeightLoadBalance(Map invokersWeight){ + if (invokersWeight != null && !invokersWeight.isEmpty()) { + nodes = new ArrayList<>(invokersWeight.size()); + invokersWeight.forEach((invoker, weight)->nodes.add(new Node(invoker, weight))); + }else + nodes = null; + } + + /** + * 算法逻辑: + * 1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行: + *     peer->current_weight += peer->effecitve_weight。 + *     同时累加所有peer的effective_weight,保存为total。 + * 2. 从集群中选出current_weight最大的peer,作为本次选定的后端。 + * 3. 对于本次选定的后端,执行:peer->current_weight -= total。 + * + * @Return ivoker + */ + public Invoker select(){ + if (! checkNodes()) + return null; + else if (nodes.size() == 1) { + if (nodes.get(0).invoker.isAvalable()) + return nodes.get(0).invoker; + else + return null; + } + Integer total = 0; + Node nodeOfMaxWeight = null; + for (Node node : nodes) { + total += node.effectiveWeight; + node.currentWeight += node.effectiveWeight; + + if (nodeOfMaxWeight == null) { + nodeOfMaxWeight = node; + }else{ + nodeOfMaxWeight = nodeOfMaxWeight.compareTo(node) > 0 ? nodeOfMaxWeight : node; + } + } + + nodeOfMaxWeight.currentWeight -= total; + return nodeOfMaxWeight.invoker; + } + + public void onInvokeSuccess(Invoker invoker){ + if (checkNodes()){ + nodes.stream() + .filter((Node node)->invoker.id().equals(node.invoker.id())) + .findFirst() + .get() + .onInvokeSuccess(); + } + } + + public void onInvokeFail(Invoker invoker){ + if (checkNodes()){ + nodes.stream() + .filter((Node node)->invoker.id().equals(node.invoker.id())) + .findFirst() + .get() + .onInvokeFail(); + } + } + + private boolean checkNodes(){ + return (nodes != null && nodes.size() > 0); + } + + public void printCurrenctWeightBeforeSelect(){ + if (checkNodes()) { + final StringBuffer out = new StringBuffer("{"); + nodes.forEach(node->out.append(node.invoker.id()) + .append("=") + .append(node.currentWeight+node.effectiveWeight) + .append(",")); + out.append("}"); + System.out.print(out); + } + } + + public void printCurrenctWeight(){ + if (checkNodes()) { + final StringBuffer out = new StringBuffer("{"); + nodes.forEach(node->out.append(node.invoker.id()) + .append("=") + .append(node.currentWeight) + .append(",")); + out.append("}"); + System.out.print(out); + } + } + + public interface Invoker{ + Boolean isAvalable(); + String id(); + } + + private static class Node implements Comparable{ + final Invoker invoker; + final Integer weight; + Integer effectiveWeight; + Integer currentWeight; + + Node(Invoker invoker, Integer weight){ + this.invoker = invoker; + this.weight = weight; + this.effectiveWeight = weight; + this.currentWeight = 0; + } + + @Override + public int compareTo(Node o) { + return currentWeight > o.currentWeight ? 1 : (currentWeight.equals(o.currentWeight) ? 0 : -1); + } + + public void onInvokeSuccess(){ + if (effectiveWeight < this.weight) + effectiveWeight++; + } + + public void onInvokeFail(){ + effectiveWeight--; + } + } + + public static void main(String[] args){ + Map invokersWeight = new HashMap<>(3); + Integer aWeight = 4; + Integer bWeight = 2; + Integer cWeight = 1; + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "a"; + } + }, aWeight); + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "b"; + } + }, bWeight); + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "c"; + } + }, cWeight); + + Integer times = 7; + RoundRobinByWeightLoadBalance roundRobin = new RoundRobinByWeightLoadBalance(invokersWeight); + for(int i=1; i<=times; i++){ + System.out.print(new StringBuffer(i+"").append(" ")); + roundRobin.printCurrenctWeightBeforeSelect(); + Invoker invoker = roundRobin.select(); + System.out.print(new StringBuffer(" ").append(invoker.id()).append(" ")); + roundRobin.printCurrenctWeight(); + System.out.println(); + } + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java b/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java new file mode 100644 index 00000000..7375135e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java @@ -0,0 +1,47 @@ +import java.util.HashMap; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/3/14 17:52 + */ +public class FourSumCount { + public static void main(String[] args) { + int[] numsA = {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2}; + int[] numsB = {-2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1,-2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1}; + int[] numsC = {-1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2}; + int[] numsD = {0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2}; + + long start = System.nanoTime(); + int count = fourSumCount(numsA, numsB, numsC, numsD); + System.out.println(" take " + (System.nanoTime()-start)/1000000.0 + " ms"); + System.out.println(count); + } + + public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Map map = new HashMap<>(); + int temp; + int res = 0; + for (int i : nums1) { + for (int j : nums2) { + temp = i + j; + if (map.containsKey(temp)) { + map.put(temp, map.get(temp) + 1); + } else { + map.put(temp, 1); + } + } + } + for (int i : nums3) { + for (int j : nums4) { + temp = i + j; + if (map.containsKey(0 - temp)) { + res += map.get(0 - temp); + } + } + } + return res; + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-api/pom.xml b/07rpc/rpc01/rpcfx-demo-api/pom.xml similarity index 89% rename from 07rpc/rpc01/rpcfx-api/pom.xml rename to 07rpc/rpc01/rpcfx-demo-api/pom.xml index 72fe2704..2a378409 100644 --- a/07rpc/rpc01/rpcfx-api/pom.xml +++ b/07rpc/rpc01/rpcfx-demo-api/pom.xml @@ -9,9 +9,9 @@ io.kimmking - rpcfx-api + rpcfx-demo-api 0.0.1-SNAPSHOT - rpcfx-api + rpcfx-demo-api 1.8 diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/Order.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/Order.java new file mode 100644 index 00000000..a2fd6c91 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/Order.java @@ -0,0 +1,40 @@ +package io.kimmking.rpcfx.demo.api; + +public class Order { + + private int id; + + private String name; + + private float amount; + + public Order(int id, String name, float amount) { + this.id = id; + this.name = name; + this.amount = amount; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public float getAmount() { + return amount; + } + + public void setAmount(float amount) { + this.amount = amount; + } +} diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/OrderService.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/OrderService.java new file mode 100644 index 00000000..1884fedb --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/OrderService.java @@ -0,0 +1,7 @@ +package io.kimmking.rpcfx.demo.api; + +public interface OrderService { + + Order findOrderById(int id); + +} diff --git a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/User.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/User.java similarity index 92% rename from 07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/User.java rename to 07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/User.java index d6ce1bbb..dec23e7c 100644 --- a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/User.java +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/User.java @@ -1,4 +1,4 @@ -package io.kimmking.rpcfx.api; +package io.kimmking.rpcfx.demo.api; public class User { diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java new file mode 100644 index 00000000..c7678f10 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java @@ -0,0 +1,11 @@ +package io.kimmking.rpcfx.demo.api; + +public interface UserService { + + User findById(int id); + + User find(int timeout); + + //User findById(int id, String name); + +} diff --git a/07rpc/rpc01/rpcfx-demo-consumer/pom.xml b/07rpc/rpc01/rpcfx-demo-consumer/pom.xml new file mode 100644 index 00000000..8f994f07 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/pom.xml @@ -0,0 +1,63 @@ + + + + rpcfx + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + rpcfx-demo-consumer + + + 8 + 8 + + + + + io.kimmking + rpcfx-core + ${project.version} + + + io.kimmking + rpcfx-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/HelloController.java b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/HelloController.java new file mode 100644 index 00000000..14f75607 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/HelloController.java @@ -0,0 +1,26 @@ +package io.kimmking.rpcfx.demo.consumer; + +import io.kimmking.rpcfx.annotation.RpcfxReference; +import io.kimmking.rpcfx.demo.api.User; +import io.kimmking.rpcfx.demo.api.UserService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/14 00:35 + */ + +@RestController +public class HelloController { + @RpcfxReference + UserService userService2;// = Rpcfx.createFromRegistry(UserService.class, "localhost:2181", new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter()); + + @GetMapping("/api/hello") + public User invoke() { + return userService2.findById(100); + } + +} diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java new file mode 100644 index 00000000..88e23382 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java @@ -0,0 +1,57 @@ +package io.kimmking.rpcfx.demo.consumer; + +import com.alibaba.fastjson.JSON; +import io.kimmking.rpcfx.annotation.RpcfxReference; +import io.kimmking.rpcfx.demo.api.OrderService; +import io.kimmking.rpcfx.demo.api.UserService; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan({"io.kimmking.rpcfx.consumer","io.kimmking.rpcfx.demo.consumer"}) +public class RpcfxClientApplication { + + public static void main(String[] args) { + + // UserService service = new xxx(); + // service.findById + +// UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/"); +// User user = userService.findById(1); +// System.out.println("find user id=1 from server: " + user.getName()); +// +// OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/"); +// Order order = orderService.findOrderById(1992129); +// System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount())); + +// UserService userService2 = Rpcfx.createFromRegistry(UserService.class, "localhost:2181", new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter()); +// User user = userService2.findById(1); +// System.out.println(user.getName()); + + SpringApplication.run(RpcfxClientApplication.class, args); + } + + @RpcfxReference + UserService userService; + + @RpcfxReference + OrderService orderService; + + @Bean + public ApplicationRunner runUserService() { + System.out.println(JSON.toJSONString(userService.hashCode())); + return x -> System.out.println(JSON.toJSONString(userService.find(500))); + } + +// @Bean +// public ApplicationRunner runOrderService() { +// return x -> System.out.println(JSON.toJSONString(orderService.findOrderById(11))); +// } + +} + + + diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml new file mode 100644 index 00000000..8b216a03 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml @@ -0,0 +1,22 @@ +server: + port: 8080 + shutdown: graceful + +spring: + lifecycle: + timeout-per-shutdown-phase: 20s + main: + allow-circular-references: true + task: + execution: + pool: + core-size: 32 + max-size: 128 + +app: + id: app2 + namespace: ns1 + env: sit + mock: false + cache: false + retry: 2 diff --git a/07rpc/rpc01/rpcfx-demo-provider/pom.xml b/07rpc/rpc01/rpcfx-demo-provider/pom.xml new file mode 100644 index 00000000..c0f9b5c9 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/pom.xml @@ -0,0 +1,63 @@ + + + + rpcfx + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + rpcfx-demo-provider + + + 8 + 8 + + + + + io.kimmking + rpcfx-core + ${project.version} + + + io.kimmking + rpcfx-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java new file mode 100644 index 00000000..96a4768a --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java @@ -0,0 +1,22 @@ +package io.kimmking.rpcfx.demo.provider; + +import io.kimmking.rpcfx.annotation.RpcfxService; +import io.kimmking.rpcfx.demo.api.Order; +import io.kimmking.rpcfx.demo.api.OrderService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@RpcfxService +@Component +public class OrderServiceImpl implements OrderService { + + @Autowired + Environment environment; + + @Override + public Order findOrderById(int id) { + return new Order(id, "KK-" + + environment.getProperty("server.port") + "_Order" + System.currentTimeMillis(), 9.9f); + } +} diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java new file mode 100644 index 00000000..ca236d16 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java @@ -0,0 +1,51 @@ +package io.kimmking.rpcfx.demo.provider; + +import com.alibaba.fastjson.JSON; +import io.kimmking.rpcfx.api.RpcfxRequest; +import io.kimmking.rpcfx.api.RpcfxResponse; +import io.kimmking.rpcfx.provider.ProviderBootstrap; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + + +@SpringBootApplication +@RestController +@ComponentScan({"io.kimmking.rpcfx.provider", "io.kimmking.rpcfx.demo.provider"}) +public class RpcfxServerApplication implements CommandLineRunner { + + @Autowired + ProviderBootstrap bootstrap; + + public static void main(String[] args) { + SpringApplication.run(RpcfxServerApplication.class, args); + } + + + @PostMapping("/") + public RpcfxResponse invoke(@RequestBody RpcfxRequest request) { + return bootstrap.getInvoker().invoke(request); + } + + @GetMapping("/api/hello") + public RpcfxResponse invoke() { + RpcfxRequest request = new RpcfxRequest(); + request.setServiceClass("io.kimmking.rpcfx.demo.api.UserService"); + request.setParams(new Object[]{1}); + request.setMethodSign("findById@1_int,"); + return bootstrap.getInvoker().invoke(request); + } + + @Override + public void run(String... args) { + RpcfxResponse response = invoke(); + System.out.println(JSON.toJSONString(response)); + } + +} diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java new file mode 100644 index 00000000..425fe315 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java @@ -0,0 +1,32 @@ +package io.kimmking.rpcfx.demo.provider; + +import io.kimmking.rpcfx.annotation.RpcfxService; +import io.kimmking.rpcfx.demo.api.User; +import io.kimmking.rpcfx.demo.api.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component("io.kimmking.rpcfx.demo.api.UserService") +@RpcfxService +public class UserServiceImpl implements UserService { + + @Autowired + Environment environment; + + @Override + public User findById(int id) { + return new User(id, "KK-" + + environment.getProperty("server.port") + "_" + System.currentTimeMillis()); + } + + public User find(int timeout) { + try { + String p = environment.getProperty("server.port"); + if("8081".equals(p)) Thread.sleep(timeout); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return findById(100); + } +} diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml new file mode 100644 index 00000000..9abe8100 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml @@ -0,0 +1,14 @@ +server: + port: 8083 + shutdown: graceful + +spring: + lifecycle: + timeout-per-shutdown-phase: 20s + main: + allow-circular-references: true + +app: + id: app2 + namespace: ns1 + env: sit \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-server/src/main/java/io/kimmking/rpcfx/server/RpcfxServerApplication.java b/07rpc/rpc01/rpcfx-server/src/main/java/io/kimmking/rpcfx/server/RpcfxServerApplication.java deleted file mode 100644 index c491653e..00000000 --- a/07rpc/rpc01/rpcfx-server/src/main/java/io/kimmking/rpcfx/server/RpcfxServerApplication.java +++ /dev/null @@ -1,51 +0,0 @@ -package io.kimmking.rpcfx.server; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; -import io.kimmking.rpcfx.api.RpcfxRequest; -import io.kimmking.rpcfx.api.RpcfxResponse; -import io.kimmking.rpcfx.api.User; -import io.kimmking.rpcfx.api.UserService; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.context.annotation.Bean; -import org.springframework.util.ReflectionUtils; -import org.springframework.web.bind.annotation.*; - -@SpringBootApplication -@RestController -public class RpcfxServerApplication implements ApplicationContextAware { - - private ApplicationContext applicationContext; - - @Override - public void setApplicationContext(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } - - public static void main(String[] args) { - SpringApplication.run(RpcfxServerApplication.class, args); - } - - @PostMapping("/") - public RpcfxResponse call(@RequestBody RpcfxRequest request) { - - String serviceClass = request.getServiceClass(); - // 作业1:改成泛型和反射 - UserService service = (UserService) this.applicationContext.getBean(serviceClass); - int id = Integer.parseInt(request.getParams()[0].toString()); - Object result = service.findById(id); - RpcfxResponse response = new RpcfxResponse(); - response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName)); - response.setStatus(true); - return response; - } - - @Bean(name = "io.kimmking.rpcfx.api.UserService") - public UserService create(){ - return new UserServiceImpl(); - } - -} diff --git a/07rpc/rpc01/rpcfx-server/src/main/java/io/kimmking/rpcfx/server/UserServiceImpl.java b/07rpc/rpc01/rpcfx-server/src/main/java/io/kimmking/rpcfx/server/UserServiceImpl.java deleted file mode 100644 index 7da6440f..00000000 --- a/07rpc/rpc01/rpcfx-server/src/main/java/io/kimmking/rpcfx/server/UserServiceImpl.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.kimmking.rpcfx.server; - -import io.kimmking.rpcfx.api.User; -import io.kimmking.rpcfx.api.UserService; - -public class UserServiceImpl implements UserService { - - @Override - public User findById(int id) { - return new User(id, "KK" + System.currentTimeMillis()); - } -} diff --git a/07rpc/rpc01/rpcfx-server/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-server/src/main/resources/application.yml deleted file mode 100644 index 5164ac23..00000000 --- a/07rpc/rpc01/rpcfx-server/src/main/resources/application.yml +++ /dev/null @@ -1,6 +0,0 @@ -server: - port: 8080 - - profiles: - active: true - diff --git a/07rpc/rpc02/dubbo-demo-api/pom.xml b/07rpc/rpc02/dubbo-demo-api/pom.xml new file mode 100644 index 00000000..7735be34 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + io.kimmking + dubbo-demo + 0.0.1-SNAPSHOT + + + io.kimmking + dubbo-demo-api + 0.0.1-SNAPSHOT + dubbo-demo-api + + + 1.8 + + + diff --git a/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/Order.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/Order.java new file mode 100644 index 00000000..9f9b1b27 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/Order.java @@ -0,0 +1,40 @@ +package io.kimmking.dubbo.demo.api; + +public class Order implements java.io.Serializable { + + private int id; + + private String name; + + private float amount; + + public Order(int id, String name, float amount) { + this.id = id; + this.name = name; + this.amount = amount; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public float getAmount() { + return amount; + } + + public void setAmount(float amount) { + this.amount = amount; + } +} diff --git a/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/OrderService.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/OrderService.java new file mode 100644 index 00000000..1ff086d7 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/OrderService.java @@ -0,0 +1,7 @@ +package io.kimmking.dubbo.demo.api; + +public interface OrderService { + + Order findOrderById(int id); + +} diff --git a/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/User.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/User.java new file mode 100644 index 00000000..5223bb69 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/User.java @@ -0,0 +1,30 @@ +package io.kimmking.dubbo.demo.api; + +public class User implements java.io.Serializable { + + public User(){} + + public User(int id, String name) { + this.id = id; + this.name = name; + } + + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/UserService.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/UserService.java similarity index 63% rename from 07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/UserService.java rename to 07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/UserService.java index c1e209ca..a4d26ca4 100644 --- a/07rpc/rpc01/rpcfx-api/src/main/java/io/kimmking/rpcfx/api/UserService.java +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/UserService.java @@ -1,4 +1,4 @@ -package io.kimmking.rpcfx.api; +package io.kimmking.dubbo.demo.api; public interface UserService { diff --git a/07rpc/rpc02/dubbo-demo-consumer/pom.xml b/07rpc/rpc02/dubbo-demo-consumer/pom.xml new file mode 100644 index 00000000..c67eae75 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-consumer/pom.xml @@ -0,0 +1,64 @@ + + + + dubbo-demo + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + dubbo-demo-consumer + + + 8 + 8 + + + + + io.kimmking + dubbo-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + + + + + + org.apache.dubbo + dubbo-spring-boot-starter + 2.7.7 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc02/dubbo-demo-consumer/src/main/java/io/kimmking/dubbo/demo/consumer/DubboClientApplication.java b/07rpc/rpc02/dubbo-demo-consumer/src/main/java/io/kimmking/dubbo/demo/consumer/DubboClientApplication.java new file mode 100644 index 00000000..4818df30 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-consumer/src/main/java/io/kimmking/dubbo/demo/consumer/DubboClientApplication.java @@ -0,0 +1,56 @@ +package io.kimmking.dubbo.demo.consumer; + +import io.kimmking.dubbo.demo.api.Order; +import io.kimmking.dubbo.demo.api.OrderService; +import io.kimmking.dubbo.demo.api.User; +import io.kimmking.dubbo.demo.api.UserService; +import org.apache.dubbo.config.annotation.DubboReference; +import org.apache.dubbo.rpc.service.EchoService; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class DubboClientApplication { + + @DubboReference(version = "1.0.0", timeout = 1000) //, url = "dubbo://127.0.0.1:12345") + private UserService userService; + + @DubboReference(version = "1.0.0", timeout = 1000) //, url = "dubbo://127.0.0.1:12345") + private OrderService orderService; + + public static void main(String[] args) { + + SpringApplication.run(DubboClientApplication.class).close(); + + // UserService service = new xxx(); + // service.findById + +// UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/"); +// User user = userService.findById(1); +// System.out.println("find user id=1 from server: " + user.getName()); +// +// OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/"); +// Order order = orderService.findOrderById(1992129); +// System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount())); + + } + + @Bean + public ApplicationRunner runner() { + return args -> { + + EchoService echoService = (EchoService) userService; + System.out.println(echoService.$echo("hello,user.")); + + for (int i = 0; i < 10000; i++) { + User user = userService.findById(1); + System.out.println("find user id=1 from server: " + user.getName()); + Order order = orderService.findOrderById(1992129); + System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount())); + } + }; + } + +} diff --git a/07rpc/rpc02/dubbo-demo-consumer/src/main/resources/application.yml b/07rpc/rpc02/dubbo-demo-consumer/src/main/resources/application.yml new file mode 100644 index 00000000..d900c6dd --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-consumer/src/main/resources/application.yml @@ -0,0 +1,20 @@ + +spring: + application: + name: dubbo-demo-consumer + main: + allow-bean-definition-overriding: true + web-application-type: none +dubbo: + scan: + base-packages: io.kimmking.dubbo.demo.consumer + registry: + address: zookeeper://localhost:2181 + metadata-report: + address: zookeeper://localhost:2181 +logging: + level: + root: debug + org: debug + sun: info + com: info \ No newline at end of file diff --git a/07rpc/rpc02/dubbo-demo-provider/pom.xml b/07rpc/rpc02/dubbo-demo-provider/pom.xml new file mode 100644 index 00000000..e36f904b --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/pom.xml @@ -0,0 +1,64 @@ + + + + dubbo-demo + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + dubbo-demo-provider + + + 8 + 8 + + + + + io.kimmking + dubbo-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.apache.dubbo + dubbo-spring-boot-starter + 2.7.7 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/DubboServerApplication.java b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/DubboServerApplication.java new file mode 100644 index 00000000..0b23758d --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/DubboServerApplication.java @@ -0,0 +1,13 @@ +package io.kimmking.dubbo.demo.provider; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DubboServerApplication { + + public static void main(String[] args) { + SpringApplication.run(DubboServerApplication.class, args); + } + +} diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/OrderServiceImpl.java b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/OrderServiceImpl.java new file mode 100644 index 00000000..9f27b588 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/OrderServiceImpl.java @@ -0,0 +1,15 @@ +package io.kimmking.dubbo.demo.provider; + +import io.kimmking.dubbo.demo.api.Order; +import io.kimmking.dubbo.demo.api.OrderService; +import org.apache.dubbo.config.annotation.DubboService; + + +@DubboService(version = "1.0.0", tag = "red", weight = 100) +public class OrderServiceImpl implements OrderService { + + @Override + public Order findOrderById(int id) { + return new Order(id, "Order" + System.currentTimeMillis(), 9.9f); + } +} diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/UserServiceImpl.java b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/UserServiceImpl.java new file mode 100644 index 00000000..425300ae --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/UserServiceImpl.java @@ -0,0 +1,20 @@ +package io.kimmking.dubbo.demo.provider; + +import io.kimmking.dubbo.demo.api.User; +import io.kimmking.dubbo.demo.api.UserService; +import org.apache.dubbo.config.annotation.DubboService; + +@DubboService(version = "1.0.0") +public class UserServiceImpl implements UserService { + + @Override + public User findById(int id) { +// try { +// System.out.println(" ==>" + id); +// Thread.sleep(1010); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + return new User(id, "KK" + System.currentTimeMillis()); + } +} diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/resources/application.yml b/07rpc/rpc02/dubbo-demo-provider/src/main/resources/application.yml new file mode 100644 index 00000000..cf479c50 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/resources/application.yml @@ -0,0 +1,31 @@ +server: + port: 8088 + +spring: + application: + name: dubbo-demo-provider + + +dubbo: + scan: + base-packages: io.kimmking.dubbo.demo.provider + protocol: + name: dubbo + port: 12345 + payload: 134217728 + registry: + address: zookeeper://localhost:2181 + metadata-report: + address: zookeeper://localhost:2181 + application: + qosEnable: true + qosPort: 22222 + qosAcceptForeignIp: true + qos-enable-compatible: true + qos-host-compatible: localhost + qos-port-compatible: 22222 + qos-accept-foreign-ip-compatible: true + qos-host: localhost +logging: + level: + root: debug \ No newline at end of file diff --git a/07rpc/rpc02/pom.xml b/07rpc/rpc02/pom.xml new file mode 100644 index 00000000..8b97b26b --- /dev/null +++ b/07rpc/rpc02/pom.xml @@ -0,0 +1,98 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.0.RELEASE + + + io.kimmking + dubbo-demo + 0.0.1-SNAPSHOT + dubbo-demo + pom + RPC demo project for Spring Boot + + + dubbo-demo-api + dubbo-demo-consumer + dubbo-demo-provider + + + + 1.8 + 2.3.0.RELEASE + 2.7.7 + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + org.apache.dubbo + dubbo-dependencies-bom + ${dubbo.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.apache.dubbo + dubbo-spring-boot-starter + ${dubbo.version} + + + + + org.apache.dubbo + dubbo-dependencies-zookeeper + ${dubbo.version} + pom + + + org.slf4j + slf4j-log4j12 + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + diff --git a/08cache/README.md b/08cache/README.md new file mode 100644 index 00000000..20e1807e --- /dev/null +++ b/08cache/README.md @@ -0,0 +1,35 @@ +# 第8周作业 + + +## 作业内容 + +> Week08 作业题目: + +###Week08 作业题目 + +1. (选做)分析前面作业设计的表,是否可以做垂直拆分。 +2. (必做)设计对前面的订单表数据进行水平分库分表,拆分 2 个库,每个库 16 张表。并在新结构在演示常见的增删改查操作。代码、sql 和配置文件,上传到 Github。 +3. (选做)模拟 1000 万的订单单表数据,迁移到上面作业 2 的分库分表中。 +4. (选做)重新搭建一套 4 个库各 64 个表的分库分表,将作业 2 中的数据迁移到新分库。 +5. (选做)列举常见的分布式事务,简单分析其使用场景和优缺点。 +6. (必做)基于 hmily TCC 或 ShardingSphere 的 Atomikos XA 实现一个简单的分布式事务应用 demo(二选一),提交到 Github。 +7. (选做)基于 ShardingSphere narayana XA 实现一个简单的分布式事务 demo。 +8. (选做)基于 seata 框架实现 TCC 或 AT 模式的分布式事务 demo。 +9. (选做☆)设计实现一个简单的 XA 分布式事务框架 demo,只需要能管理和调用 2 个 MySQL 的本地事务即可,不需要考虑全局事务的持久化和恢复、高可用等。 +10. (选做☆)设计实现一个 TCC 分布式事务框架的简单 Demo,需要实现事务管理器,不需要实现全局事务的持久化和恢复、高可用等。 +11. (选做☆)设计实现一个 AT 分布式事务框架的简单 Demo,仅需要支持根据主键 id 进行的单个删改操作的 SQL 或插入操作的事务。 + +### 作业提交规范: + +1. 作业不要打包 ; +2. 同学们写在 md 文件里,而不要发 Word, Excel , PDF 等 ; +3. 代码类作业需提交完整 Java 代码,不能是片段; +4. 作业按课时分目录,仅上传作业相关,笔记分开记录; +5. 画图类作业提交可直接打开的图片或 md,手画的图手机拍照上传后太大,难以查看,推荐画图(推荐 PPT、Keynote); +6. 提交记录最好要标明明确的含义(比如第几题作业)。 + + +## 操作步骤 + + +### 第八周-作业1. (选做) diff --git a/08cache/cache/pom.xml b/08cache/cache/pom.xml new file mode 100644 index 00000000..88c9a37e --- /dev/null +++ b/08cache/cache/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + + + io.kimmking.08cache + cache + 0.0.1-SNAPSHOT + cache + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + mysql + mysql-connector-java + 5.1.47 + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-data-redis + + + io.lettuce + lettuce-core + + + org.apache.commons + commons-pool2 + + + net.sf.ehcache + ehcache + 2.8.3 + + + org.mybatis + mybatis-ehcache + 1.0.0 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/08cache/cache/src/main/java/io/kimmking/cache/CacheApplication.java b/08cache/cache/src/main/java/io/kimmking/cache/CacheApplication.java new file mode 100644 index 00000000..9ed3fa75 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/CacheApplication.java @@ -0,0 +1,77 @@ +package io.kimmking.cache; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; +import java.sql.*; + +@SpringBootApplication(scanBasePackages = "io.kimmking.cache") +@MapperScan("io.kimmking.cache.mapper") +@EnableCaching +public class CacheApplication { + + public static void main(String[] args) { + SpringApplication.run(CacheApplication.class, args); + } + + @Bean + @Autowired + public ApplicationRunner runner(DataSource dataSource) { + return (x) -> { + // testInsert(dataSource); + // 测试 insert into on duplicate key update时如果没有执行更新条件,则回填id为空 + }; + } + + private void testInsert(DataSource dataSource) throws SQLException { + Connection connection = dataSource.getConnection(); + + System.out.println(" =====> test Insert ..."); + PreparedStatement statement = connection.prepareStatement("insert into user(name,age) values(?,20)", Statement.RETURN_GENERATED_KEYS); + //boolean b = statement.execute("insert into user(name,age) values('K8',20)"); + //System.out.println("insert:" +b); + // You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement(). + statement.setString(1, "K8"); + boolean b = statement.execute(); +// System.out.println("insert:" +b); + ResultSet rs = statement.getGeneratedKeys(); + System.out.println("insert getGeneratedKeys => " + (rs.next() ? rs.getInt(1): "NULL")); + rs.close(); + statement.close(); + + + System.out.println(" =====> test Duplicate ..."); + statement = connection.prepareStatement("insert into user(name,age) values(?,20) on duplicate key update age=age+1", Statement.RETURN_GENERATED_KEYS); + statement.setString(1, "K8"); + b = statement.execute(); +// System.out.println("insert:" +b); + rs = statement.getGeneratedKeys(); + System.out.println("insert getGeneratedKeys => " + (rs.next() ? rs.getInt(1): "NULL")); + rs.close(); + //statement.execute("delete from user where name='K8'"); + statement.close(); + + + System.out.println(" =====> test Duplicate No Update ..."); + statement = connection.prepareStatement("insert into user(name,age) values(?,20) on duplicate key update age=age", Statement.RETURN_GENERATED_KEYS); + statement.setString(1, "K8"); + b = statement.execute(); +// System.out.println("insert:" +b); + rs = statement.getGeneratedKeys(); + System.out.println("insert getGeneratedKeys => " + (rs.next() ? rs.getInt(1): "NULL")); + rs.close(); + //statement.execute("delete from user where name='K8'"); + statement.close(); + + + connection.close(); + } + + +} diff --git a/08cache/cache/src/main/java/io/kimmking/cache/CacheConfig.java b/08cache/cache/src/main/java/io/kimmking/cache/CacheConfig.java new file mode 100644 index 00000000..24ad27fe --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/CacheConfig.java @@ -0,0 +1,83 @@ +//package io.kimmking.cache; +// +//import org.springframework.cache.CacheManager; +//import org.springframework.cache.annotation.CachingConfigurerSupport; +//import org.springframework.cache.interceptor.*; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.data.redis.cache.RedisCacheConfiguration; +//import org.springframework.data.redis.cache.RedisCacheManager; +//import org.springframework.data.redis.connection.RedisConnectionFactory; +//import org.springframework.data.redis.core.RedisTemplate; +//import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +//import org.springframework.data.redis.serializer.RedisSerializationContext; +//import org.springframework.data.redis.serializer.StringRedisSerializer; +// +//import javax.annotation.Resource; +// +//import static org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig; +// +//@Configuration +//public class CacheConfig extends CachingConfigurerSupport { +// +// @Resource +// private RedisConnectionFactory factory; +// +// /** +// * 自定义生成redis-key +// * +// * @return +// */ +// @Override +// @Bean +// public KeyGenerator keyGenerator() { +// return (o, method, objects) -> { +// StringBuilder sb = new StringBuilder(); +// sb.append(o.getClass().getName()).append("."); +// sb.append(method.getName()).append("."); +// for (Object obj : objects) { +// sb.append(obj.toString()).append("."); +// } +// //System.out.println("keyGenerator=" + sb.toString()); +// return sb.toString(); +// }; +// } +// +// @Bean +// public RedisTemplate redisTemplate() { +// RedisTemplate redisTemplate = new RedisTemplate<>(); +// redisTemplate.setConnectionFactory(factory); +// +// GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); +// +// redisTemplate.setKeySerializer(genericJackson2JsonRedisSerializer); +// redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); +// +// redisTemplate.setHashKeySerializer(new StringRedisSerializer()); +// redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer); +// return redisTemplate; +// } +// +// @Bean +// @Override +// public CacheResolver cacheResolver() { +// return new SimpleCacheResolver(cacheManager()); +// } +// +// @Bean +// @Override +// public CacheErrorHandler errorHandler() { +// // 用于捕获从Cache中进行CRUD时的异常的回调处理器。 +// return new SimpleCacheErrorHandler(); +// } +// +// @Bean +// @Override +// public CacheManager cacheManager() { +// RedisCacheConfiguration cacheConfiguration = +// defaultCacheConfig() +// .disableCachingNullValues() +// .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); +// return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build(); +// } +//} \ No newline at end of file diff --git a/08cache/cache/src/main/java/io/kimmking/cache/controller/UserController.java b/08cache/cache/src/main/java/io/kimmking/cache/controller/UserController.java new file mode 100644 index 00000000..b19d9553 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/controller/UserController.java @@ -0,0 +1,31 @@ +package io.kimmking.cache.controller; + +import io.kimmking.cache.entity.User; +import io.kimmking.cache.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@EnableAutoConfiguration +public class UserController { + + @Autowired + UserService userService; + + @RequestMapping("/user/find") + User find(Integer id) { + return userService.find(id); + //return new User(1,"KK", 28); + } + + @RequestMapping("/user/list") + List list() { + return userService.list(); +// return Arrays.asList(new User(1,"KK", 28), +// new User(2,"CC", 18)); + } +} \ No newline at end of file diff --git a/08cache/cache/src/main/java/io/kimmking/cache/entity/User.java b/08cache/cache/src/main/java/io/kimmking/cache/entity/User.java new file mode 100644 index 00000000..4f67ce12 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/entity/User.java @@ -0,0 +1,16 @@ +package io.kimmking.cache.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User implements Serializable { + private Integer id; + private String name; + private Integer age; +} \ No newline at end of file diff --git a/08cache/cache/src/main/java/io/kimmking/cache/mapper/UserMapper.java b/08cache/cache/src/main/java/io/kimmking/cache/mapper/UserMapper.java new file mode 100644 index 00000000..0c5d7608 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/mapper/UserMapper.java @@ -0,0 +1,15 @@ +package io.kimmking.cache.mapper; + +import io.kimmking.cache.entity.User; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface UserMapper { + + User find(int id); + + List list(); + +} diff --git a/08cache/cache/src/main/java/io/kimmking/cache/service/UserService.java b/08cache/cache/src/main/java/io/kimmking/cache/service/UserService.java new file mode 100644 index 00000000..22c3987b --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/service/UserService.java @@ -0,0 +1,15 @@ +package io.kimmking.cache.service; + +import io.kimmking.cache.entity.User; +import org.springframework.cache.annotation.CacheConfig; + +import java.util.List; + +@CacheConfig(cacheNames = "users") +public interface UserService { + + User find(int id); + + List list(); + +} diff --git a/08cache/cache/src/main/java/io/kimmking/cache/service/UserServiceImpl.java b/08cache/cache/src/main/java/io/kimmking/cache/service/UserServiceImpl.java new file mode 100644 index 00000000..4a1f5acf --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/service/UserServiceImpl.java @@ -0,0 +1,30 @@ +package io.kimmking.cache.service; + +import io.kimmking.cache.entity.User; +import io.kimmking.cache.mapper.UserMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + UserMapper userMapper; //DAO // Repository + + // 开启spring cache + @Cacheable(key="#id",value="userCache") + public User find(int id) { + System.out.println(" ==> find " + id); + return userMapper.find(id); + } + + // 开启spring cache + @Cacheable //(key="methodName",value="userCache") + public List list(){ + return userMapper.list(); + } + +} diff --git a/08cache/cache/src/main/resources/application.yml b/08cache/cache/src/main/resources/application.yml new file mode 100644 index 00000000..835c04a4 --- /dev/null +++ b/08cache/cache/src/main/resources/application.yml @@ -0,0 +1,45 @@ +server: + port: 8080 + +spring: + datasource: + username: root + password: + url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC + driver-class-name: com.mysql.jdbc.Driver + cache: + type: simple + +# type: redis +# redis: +# timeout: 3000ms +# database: 0 +# cluster: +# nodes: +# - 127.0.0.1:7000 +# - 127.0.0.1:7001 +# - 127.0.0.1:7002 +# - 127.0.0.1:7003 +# - 127.0.0.1:7004 +# - 127.0.0.1:7005 +# max-redirects: 3 # 获取失败 最大重定向次数 +# lettuce: +# pool: +# max-active: 1000 #连接池最大连接数(使用负值表示没有限制) +# max-idle: 10 # 连接池中的最大空闲连接 +# min-idle: 5 # 连接池中的最小空闲连接 +# max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) + +# type: ehcache +# ehcache: +# config: ehcache.xml + +mybatis: + mapper-locations: classpath:mapper/*Mapper.xml + type-aliases-package: io.kimmking.cache.entity + +logging: + level: + io: + kimmking: + cache : info diff --git a/08cache/cache/src/main/resources/ehcache.xml b/08cache/cache/src/main/resources/ehcache.xml new file mode 100644 index 00000000..f1a3efc5 --- /dev/null +++ b/08cache/cache/src/main/resources/ehcache.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/08cache/cache/src/main/resources/mapper/UserMapper.xml b/08cache/cache/src/main/resources/mapper/UserMapper.xml new file mode 100644 index 00000000..0d2e8ae5 --- /dev/null +++ b/08cache/cache/src/main/resources/mapper/UserMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/08cache/cache/test.sql b/08cache/cache/test.sql new file mode 100644 index 00000000..76e9b60b --- /dev/null +++ b/08cache/cache/test.sql @@ -0,0 +1,54 @@ +-- MySQL dump 10.13 Distrib 5.7.32, for osx10.15 (x86_64) +-- +-- Host: localhost Database: test +-- ------------------------------------------------------ +-- Server version 5.7.32 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `user` +-- + +DROP TABLE IF EXISTS `user`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `user` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(32) NOT NULL, + `age` int(11) NOT NULL, + PRIMARY KEY (`id`), + constraint user_name_uindex + unique (name) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `user` +-- + +LOCK TABLES `user` WRITE; +/*!40000 ALTER TABLE `user` DISABLE KEYS */; +INSERT INTO `user` VALUES (1,'K1',21),(2,'K2',22),(3,'K3',23),(4,'K4',24),(5,'K5',25),(6,'K6',26); +/*!40000 ALTER TABLE `user` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2020-12-28 15:46:14 diff --git a/08cache/ha/conf/cluster.txt b/08cache/ha/conf/cluster.txt new file mode 100644 index 00000000..6e6d2b6e --- /dev/null +++ b/08cache/ha/conf/cluster.txt @@ -0,0 +1,61 @@ +redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes + +redis-server --port 7004 --cluster-enabled yes --cluster-config-file nodes-7004.conf --cluster-node-timeout 3000 --appendonly yes --appendfilename appendonly-7004.aof --dbfilename dump-7004.rdb --logfile 7004.log --daemonize yes + + +redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS + + +redis-cli -p $PORT shutdown nosave + +redis-cli -p $PORT cluster nodes | head -30 + +redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9 + + + +redis-cli -p 7000 cluster info + + +测试存取值,客户端连接集群redis-cli需要带上 -c ,redis-cli -c -p 端口号 + + + + +Redis 集群使用命令 CLUSTER SETSLOT 将一个槽中的所有 keys 从一个节点迁移至另一个节点。稍后介绍在其他命令配合下迁移是如何操作的。我们假定要操作的哈希槽的当前所有者为源节点,将要迁移至的节点为目的节点。 + +使用命令 CLUSTER SETSLOT IMPORTING 将目的节点槽置为 importing 状态。 +使用命令 CLUSTER SETSLOT MIGRATING 将源节点槽置为 migrating 状态。 +使用命令 CLUSTER GETKEYSINSLOT 从源节点获取所有 keys,并使用命令 MIGRATE 将它们导入到目的节点。 +在源节点活目的节点执行命令 CLUSTER SETSLOT NODE 。 +注意: + +步骤 1 和步骤 2 的顺序很重要。我们希望在源节点配置了重定向之后,目的节点已经可以接受 ASK 重定向。 +步骤 4 中,技术上讲,在重哈希不涉及的节点上执行 SETSLOT 是非必须的,因为新配置最终会分发到所有节点上,但是,这样操作也有好处,会快速停止节点中对已迁移的哈希槽的错误指向,降低命令重定向的发生。 + +https://www.knowledgedict.com/tutorial/redis-command-cluster-setslot.html + + +k2: +CLUSTER SETSLOT 449 importing c025732be5696d0446aad624dba3c31e13750341 +CLUSTER SETSLOT 449 migrating 2cf0fb991563bdedea2704a3cd43e5c211ea16b9 +CLUSTER COUNTKEYSINSLOT 449 +CLUSTER GETKEYSINSLOT 449 10 +MIGRATE 127.0.0.1 7004 k2 0 100 COPY +CLUSTER SETSLOT 449 node 2cf0fb991563bdedea2704a3cd43e5c211ea16b9 // 这句没有执行完之前,获取key会死循环 + +k6: +CLUSTER SETSLOT 325 node 2cf0fb991563bdedea2704a3cd43e5c211ea16b9 + + + +几个问题: +1、添加新节点后slot为空,需要从其他节点迁移slot,然后迁移key,一个个的处理,处理完后通知集群这个slot在新节点上方可使用,中间访问会重定向死循环。 +2、分片+主从? +3、 + + +解决办法:使用批量操作脚本来做。 +提供 reshard 一键重新分配数据的功能。 + + diff --git a/08cache/ha/conf/redis6379.conf b/08cache/ha/conf/redis6379.conf new file mode 100644 index 00000000..e33949fa --- /dev/null +++ b/08cache/ha/conf/redis6379.conf @@ -0,0 +1,1869 @@ +# Redis configuration file example. +# +# Note that in order to read the configuration file, Redis must be +# started with the file path as first argument: +# +# ./redis-server /path/to/redis.conf + +# Note on units: when memory size is needed, it is possible to specify +# it in the usual form of 1k 5GB 4M and so forth: +# +# 1k => 1000 bytes +# 1kb => 1024 bytes +# 1m => 1000000 bytes +# 1mb => 1024*1024 bytes +# 1g => 1000000000 bytes +# 1gb => 1024*1024*1024 bytes +# +# units are case insensitive so 1GB 1Gb 1gB are all the same. + +################################## INCLUDES ################################### + +# Include one or more other config files here. This is useful if you +# have a standard template that goes to all Redis servers but also need +# to customize a few per-server settings. Include files can include +# other files, so use this wisely. +# +# Note that option "include" won't be rewritten by command "CONFIG REWRITE" +# from admin or Redis Sentinel. Since Redis always uses the last processed +# line as value of a configuration directive, you'd better put includes +# at the beginning of this file to avoid overwriting config change at runtime. +# +# If instead you are interested in using includes to override configuration +# options, it is better to use include as the last line. +# +# include /path/to/local.conf +# include /path/to/other.conf + +################################## MODULES ##################################### + +# Load modules at startup. If the server is not able to load modules +# it will abort. It is possible to use multiple loadmodule directives. +# +# loadmodule /path/to/my_module.so +# loadmodule /path/to/other_module.so + +################################## NETWORK ##################################### + +# By default, if no "bind" configuration directive is specified, Redis listens +# for connections from all available network interfaces on the host machine. +# It is possible to listen to just one or multiple selected interfaces using +# the "bind" configuration directive, followed by one or more IP addresses. +# +# Examples: +# +# bind 192.168.1.100 10.0.0.1 +# bind 127.0.0.1 ::1 +# +# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the +# internet, binding to all the interfaces is dangerous and will expose the +# instance to everybody on the internet. So by default we uncomment the +# following bind directive, that will force Redis to listen only on the +# IPv4 loopback interface address (this means Redis will only be able to +# accept client connections from the same host that it is running on). +# +# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES +# JUST COMMENT OUT THE FOLLOWING LINE. +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bind 127.0.0.1 ::1 + +# Protected mode is a layer of security protection, in order to avoid that +# Redis instances left open on the internet are accessed and exploited. +# +# When protected mode is on and if: +# +# 1) The server is not binding explicitly to a set of addresses using the +# "bind" directive. +# 2) No password is configured. +# +# The server only accepts connections from clients connecting from the +# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain +# sockets. +# +# By default protected mode is enabled. You should disable it only if +# you are sure you want clients from other hosts to connect to Redis +# even if no authentication is configured, nor a specific set of interfaces +# are explicitly listed using the "bind" directive. +protected-mode yes + +# Accept connections on the specified port, default is 6379 (IANA #815344). +# If port 0 is specified Redis will not listen on a TCP socket. +port 6379 + +# TCP listen() backlog. +# +# In high requests-per-second environments you need a high backlog in order +# to avoid slow clients connection issues. Note that the Linux kernel +# will silently truncate it to the value of /proc/sys/net/core/somaxconn so +# make sure to raise both the value of somaxconn and tcp_max_syn_backlog +# in order to get the desired effect. +tcp-backlog 511 + +# Unix socket. +# +# Specify the path for the Unix socket that will be used to listen for +# incoming connections. There is no default, so Redis will not listen +# on a unix socket when not specified. +# +# unixsocket /tmp/redis.sock +# unixsocketperm 700 + +# Close the connection after a client is idle for N seconds (0 to disable) +timeout 0 + +# TCP keepalive. +# +# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence +# of communication. This is useful for two reasons: +# +# 1) Detect dead peers. +# 2) Force network equipment in the middle to consider the connection to be +# alive. +# +# On Linux, the specified value (in seconds) is the period used to send ACKs. +# Note that to close the connection the double of the time is needed. +# On other kernels the period depends on the kernel configuration. +# +# A reasonable value for this option is 300 seconds, which is the new +# Redis default starting with Redis 3.2.1. +tcp-keepalive 300 + +################################# TLS/SSL ##################################### + +# By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration +# directive can be used to define TLS-listening ports. To enable TLS on the +# default port, use: +# +# port 0 +# tls-port 6379 + +# Configure a X.509 certificate and private key to use for authenticating the +# server to connected clients, masters or cluster peers. These files should be +# PEM formatted. +# +# tls-cert-file redis.crt +# tls-key-file redis.key + +# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange: +# +# tls-dh-params-file redis.dh + +# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL +# clients and peers. Redis requires an explicit configuration of at least one +# of these, and will not implicitly use the system wide configuration. +# +# tls-ca-cert-file ca.crt +# tls-ca-cert-dir /etc/ssl/certs + +# By default, clients (including replica servers) on a TLS port are required +# to authenticate using valid client side certificates. +# +# If "no" is specified, client certificates are not required and not accepted. +# If "optional" is specified, client certificates are accepted and must be +# valid if provided, but are not required. +# +# tls-auth-clients no +# tls-auth-clients optional + +# By default, a Redis replica does not attempt to establish a TLS connection +# with its master. +# +# Use the following directive to enable TLS on replication links. +# +# tls-replication yes + +# By default, the Redis Cluster bus uses a plain TCP connection. To enable +# TLS for the bus protocol, use the following directive: +# +# tls-cluster yes + +# Explicitly specify TLS versions to support. Allowed values are case insensitive +# and include "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" (OpenSSL >= 1.1.1) or +# any combination. To enable only TLSv1.2 and TLSv1.3, use: +# +# tls-protocols "TLSv1.2 TLSv1.3" + +# Configure allowed ciphers. See the ciphers(1ssl) manpage for more information +# about the syntax of this string. +# +# Note: this configuration applies only to <= TLSv1.2. +# +# tls-ciphers DEFAULT:!MEDIUM + +# Configure allowed TLSv1.3 ciphersuites. See the ciphers(1ssl) manpage for more +# information about the syntax of this string, and specifically for TLSv1.3 +# ciphersuites. +# +# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256 + +# When choosing a cipher, use the server's preference instead of the client +# preference. By default, the server follows the client's preference. +# +# tls-prefer-server-ciphers yes + +# By default, TLS session caching is enabled to allow faster and less expensive +# reconnections by clients that support it. Use the following directive to disable +# caching. +# +# tls-session-caching no + +# Change the default number of TLS sessions cached. A zero value sets the cache +# to unlimited size. The default size is 20480. +# +# tls-session-cache-size 5000 + +# Change the default timeout of cached TLS sessions. The default timeout is 300 +# seconds. +# +# tls-session-cache-timeout 60 + +################################# GENERAL ##################################### + +# By default Redis does not run as a daemon. Use 'yes' if you need it. +# Note that Redis will write a pid file in /usr/local/var/run/redis.pid when daemonized. +daemonize no + +# If you run Redis from upstart or systemd, Redis can interact with your +# supervision tree. Options: +# supervised no - no supervision interaction +# supervised upstart - signal upstart by putting Redis into SIGSTOP mode +# requires "expect stop" in your upstart job config +# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET +# supervised auto - detect upstart or systemd method based on +# UPSTART_JOB or NOTIFY_SOCKET environment variables +# Note: these supervision methods only signal "process is ready." +# They do not enable continuous pings back to your supervisor. +supervised no + +# If a pid file is specified, Redis writes it where specified at startup +# and removes it at exit. +# +# When the server runs non daemonized, no pid file is created if none is +# specified in the configuration. When the server is daemonized, the pid file +# is used even if not specified, defaulting to "/usr/local/var/run/redis.pid". +# +# Creating a pid file is best effort: if Redis is not able to create it +# nothing bad happens, the server will start and run normally. +pidfile "/var/run/redis_6379.pid" + +# Specify the server verbosity level. +# This can be one of: +# debug (a lot of information, useful for development/testing) +# verbose (many rarely useful info, but not a mess like the debug level) +# notice (moderately verbose, what you want in production probably) +# warning (only very important / critical messages are logged) +loglevel notice + +# Specify the log file name. Also the empty string can be used to force +# Redis to log on the standard output. Note that if you use standard +# output for logging but daemonize, logs will be sent to /dev/null +logfile "" + +# To enable logging to the system logger, just set 'syslog-enabled' to yes, +# and optionally update the other syslog parameters to suit your needs. +# syslog-enabled no + +# Specify the syslog identity. +# syslog-ident redis + +# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7. +# syslog-facility local0 + +# Set the number of databases. The default database is DB 0, you can select +# a different one on a per-connection basis using SELECT where +# dbid is a number between 0 and 'databases'-1 +databases 16 + +# By default Redis shows an ASCII art logo only when started to log to the +# standard output and if the standard output is a TTY. Basically this means +# that normally a logo is displayed only in interactive sessions. +# +# However it is possible to force the pre-4.0 behavior and always show a +# ASCII art logo in startup logs by setting the following option to yes. +always-show-logo yes + +################################ SNAPSHOTTING ################################ +# +# Save the DB on disk: +# +# save +# +# Will save the DB if both the given number of seconds and the given +# number of write operations against the DB occurred. +# +# In the example below the behavior will be to save: +# after 900 sec (15 min) if at least 1 key changed +# after 300 sec (5 min) if at least 10 keys changed +# after 60 sec if at least 10000 keys changed +# +# Note: you can disable saving completely by commenting out all "save" lines. +# +# It is also possible to remove all the previously configured save +# points by adding a save directive with a single empty string argument +# like in the following example: +# +# save "" + +save 900 1 +save 300 10 +save 60 10000 + +# By default Redis will stop accepting writes if RDB snapshots are enabled +# (at least one save point) and the latest background save failed. +# This will make the user aware (in a hard way) that data is not persisting +# on disk properly, otherwise chances are that no one will notice and some +# disaster will happen. +# +# If the background saving process will start working again Redis will +# automatically allow writes again. +# +# However if you have setup your proper monitoring of the Redis server +# and persistence, you may want to disable this feature so that Redis will +# continue to work as usual even if there are problems with disk, +# permissions, and so forth. +stop-writes-on-bgsave-error yes + +# Compress string objects using LZF when dump .rdb databases? +# By default compression is enabled as it's almost always a win. +# If you want to save some CPU in the saving child set it to 'no' but +# the dataset will likely be bigger if you have compressible values or keys. +rdbcompression yes + +# Since version 5 of RDB a CRC64 checksum is placed at the end of the file. +# This makes the format more resistant to corruption but there is a performance +# hit to pay (around 10%) when saving and loading RDB files, so you can disable it +# for maximum performances. +# +# RDB files created with checksum disabled have a checksum of zero that will +# tell the loading code to skip the check. +rdbchecksum yes + +# The filename where to dump the DB +dbfilename "dump.rdb" + +# Remove RDB files used by replication in instances without persistence +# enabled. By default this option is disabled, however there are environments +# where for regulations or other security concerns, RDB files persisted on +# disk by masters in order to feed replicas, or stored on disk by replicas +# in order to load them for the initial synchronization, should be deleted +# ASAP. Note that this option ONLY WORKS in instances that have both AOF +# and RDB persistence disabled, otherwise is completely ignored. +# +# An alternative (and sometimes better) way to obtain the same effect is +# to use diskless replication on both master and replicas instances. However +# in the case of replicas, diskless is not always an option. +rdb-del-sync-files no + +# The working directory. +# +# The DB will be written inside this directory, with the filename specified +# above using the 'dbfilename' configuration directive. +# +# The Append Only File will also be created inside this directory. +# +# Note that you must specify a directory here, not a file name. +dir "/Users/kimmking/logs/redis0" + +################################# REPLICATION ################################# + +# Master-Replica replication. Use replicaof to make a Redis instance a copy of +# another Redis server. A few things to understand ASAP about Redis replication. +# +# +------------------+ +---------------+ +# | Master | ---> | Replica | +# | (receive writes) | | (exact copy) | +# +------------------+ +---------------+ +# +# 1) Redis replication is asynchronous, but you can configure a master to +# stop accepting writes if it appears to be not connected with at least +# a given number of replicas. +# 2) Redis replicas are able to perform a partial resynchronization with the +# master if the replication link is lost for a relatively small amount of +# time. You may want to configure the replication backlog size (see the next +# sections of this file) with a sensible value depending on your needs. +# 3) Replication is automatic and does not need user intervention. After a +# network partition replicas automatically try to reconnect to masters +# and resynchronize with them. +# +# replicaof + +# If the master is password protected (using the "requirepass" configuration +# directive below) it is possible to tell the replica to authenticate before +# starting the replication synchronization process, otherwise the master will +# refuse the replica request. +# +# masterauth +# +# However this is not enough if you are using Redis ACLs (for Redis version +# 6 or greater), and the default user is not capable of running the PSYNC +# command and/or other commands needed for replication. In this case it's +# better to configure a special user to use with replication, and specify the +# masteruser configuration as such: +# +# masteruser +# +# When masteruser is specified, the replica will authenticate against its +# master using the new AUTH form: AUTH . + +# When a replica loses its connection with the master, or when the replication +# is still in progress, the replica can act in two different ways: +# +# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will +# still reply to client requests, possibly with out of date data, or the +# data set may just be empty if this is the first synchronization. +# +# 2) If replica-serve-stale-data is set to 'no' the replica will reply with +# an error "SYNC with master in progress" to all commands except: +# INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE, +# UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST, +# HOST and LATENCY. +# +replica-serve-stale-data yes + +# You can configure a replica instance to accept writes or not. Writing against +# a replica instance may be useful to store some ephemeral data (because data +# written on a replica will be easily deleted after resync with the master) but +# may also cause problems if clients are writing to it because of a +# misconfiguration. +# +# Since Redis 2.6 by default replicas are read-only. +# +# Note: read only replicas are not designed to be exposed to untrusted clients +# on the internet. It's just a protection layer against misuse of the instance. +# Still a read only replica exports by default all the administrative commands +# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve +# security of read only replicas using 'rename-command' to shadow all the +# administrative / dangerous commands. +replica-read-only yes + +# Replication SYNC strategy: disk or socket. +# +# New replicas and reconnecting replicas that are not able to continue the +# replication process just receiving differences, need to do what is called a +# "full synchronization". An RDB file is transmitted from the master to the +# replicas. +# +# The transmission can happen in two different ways: +# +# 1) Disk-backed: The Redis master creates a new process that writes the RDB +# file on disk. Later the file is transferred by the parent +# process to the replicas incrementally. +# 2) Diskless: The Redis master creates a new process that directly writes the +# RDB file to replica sockets, without touching the disk at all. +# +# With disk-backed replication, while the RDB file is generated, more replicas +# can be queued and served with the RDB file as soon as the current child +# producing the RDB file finishes its work. With diskless replication instead +# once the transfer starts, new replicas arriving will be queued and a new +# transfer will start when the current one terminates. +# +# When diskless replication is used, the master waits a configurable amount of +# time (in seconds) before starting the transfer in the hope that multiple +# replicas will arrive and the transfer can be parallelized. +# +# With slow disks and fast (large bandwidth) networks, diskless replication +# works better. +repl-diskless-sync no + +# When diskless replication is enabled, it is possible to configure the delay +# the server waits in order to spawn the child that transfers the RDB via socket +# to the replicas. +# +# This is important since once the transfer starts, it is not possible to serve +# new replicas arriving, that will be queued for the next RDB transfer, so the +# server waits a delay in order to let more replicas arrive. +# +# The delay is specified in seconds, and by default is 5 seconds. To disable +# it entirely just set it to 0 seconds and the transfer will start ASAP. +repl-diskless-sync-delay 5 + +# ----------------------------------------------------------------------------- +# WARNING: RDB diskless load is experimental. Since in this setup the replica +# does not immediately store an RDB on disk, it may cause data loss during +# failovers. RDB diskless load + Redis modules not handling I/O reads may also +# cause Redis to abort in case of I/O errors during the initial synchronization +# stage with the master. Use only if your do what you are doing. +# ----------------------------------------------------------------------------- +# +# Replica can load the RDB it reads from the replication link directly from the +# socket, or store the RDB to a file and read that file after it was completely +# received from the master. +# +# In many cases the disk is slower than the network, and storing and loading +# the RDB file may increase replication time (and even increase the master's +# Copy on Write memory and salve buffers). +# However, parsing the RDB file directly from the socket may mean that we have +# to flush the contents of the current database before the full rdb was +# received. For this reason we have the following options: +# +# "disabled" - Don't use diskless load (store the rdb file to the disk first) +# "on-empty-db" - Use diskless load only when it is completely safe. +# "swapdb" - Keep a copy of the current db contents in RAM while parsing +# the data directly from the socket. note that this requires +# sufficient memory, if you don't have it, you risk an OOM kill. +repl-diskless-load disabled + +# Replicas send PINGs to server in a predefined interval. It's possible to +# change this interval with the repl_ping_replica_period option. The default +# value is 10 seconds. +# +# repl-ping-replica-period 10 + +# The following option sets the replication timeout for: +# +# 1) Bulk transfer I/O during SYNC, from the point of view of replica. +# 2) Master timeout from the point of view of replicas (data, pings). +# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings). +# +# It is important to make sure that this value is greater than the value +# specified for repl-ping-replica-period otherwise a timeout will be detected +# every time there is low traffic between the master and the replica. The default +# value is 60 seconds. +# +# repl-timeout 60 + +# Disable TCP_NODELAY on the replica socket after SYNC? +# +# If you select "yes" Redis will use a smaller number of TCP packets and +# less bandwidth to send data to replicas. But this can add a delay for +# the data to appear on the replica side, up to 40 milliseconds with +# Linux kernels using a default configuration. +# +# If you select "no" the delay for data to appear on the replica side will +# be reduced but more bandwidth will be used for replication. +# +# By default we optimize for low latency, but in very high traffic conditions +# or when the master and replicas are many hops away, turning this to "yes" may +# be a good idea. +repl-disable-tcp-nodelay no + +# Set the replication backlog size. The backlog is a buffer that accumulates +# replica data when replicas are disconnected for some time, so that when a +# replica wants to reconnect again, often a full resync is not needed, but a +# partial resync is enough, just passing the portion of data the replica +# missed while disconnected. +# +# The bigger the replication backlog, the longer the replica can endure the +# disconnect and later be able to perform a partial resynchronization. +# +# The backlog is only allocated if there is at least one replica connected. +# +# repl-backlog-size 1mb + +# After a master has no connected replicas for some time, the backlog will be +# freed. The following option configures the amount of seconds that need to +# elapse, starting from the time the last replica disconnected, for the backlog +# buffer to be freed. +# +# Note that replicas never free the backlog for timeout, since they may be +# promoted to masters later, and should be able to correctly "partially +# resynchronize" with other replicas: hence they should always accumulate backlog. +# +# A value of 0 means to never release the backlog. +# +# repl-backlog-ttl 3600 + +# The replica priority is an integer number published by Redis in the INFO +# output. It is used by Redis Sentinel in order to select a replica to promote +# into a master if the master is no longer working correctly. +# +# A replica with a low priority number is considered better for promotion, so +# for instance if there are three replicas with priority 10, 100, 25 Sentinel +# will pick the one with priority 10, that is the lowest. +# +# However a special priority of 0 marks the replica as not able to perform the +# role of master, so a replica with priority of 0 will never be selected by +# Redis Sentinel for promotion. +# +# By default the priority is 100. +replica-priority 100 + +# It is possible for a master to stop accepting writes if there are less than +# N replicas connected, having a lag less or equal than M seconds. +# +# The N replicas need to be in "online" state. +# +# The lag in seconds, that must be <= the specified value, is calculated from +# the last ping received from the replica, that is usually sent every second. +# +# This option does not GUARANTEE that N replicas will accept the write, but +# will limit the window of exposure for lost writes in case not enough replicas +# are available, to the specified number of seconds. +# +# For example to require at least 3 replicas with a lag <= 10 seconds use: +# +# min-replicas-to-write 3 +# min-replicas-max-lag 10 +# +# Setting one or the other to 0 disables the feature. +# +# By default min-replicas-to-write is set to 0 (feature disabled) and +# min-replicas-max-lag is set to 10. + +# A Redis master is able to list the address and port of the attached +# replicas in different ways. For example the "INFO replication" section +# offers this information, which is used, among other tools, by +# Redis Sentinel in order to discover replica instances. +# Another place where this info is available is in the output of the +# "ROLE" command of a master. +# +# The listed IP address and port normally reported by a replica is +# obtained in the following way: +# +# IP: The address is auto detected by checking the peer address +# of the socket used by the replica to connect with the master. +# +# Port: The port is communicated by the replica during the replication +# handshake, and is normally the port that the replica is using to +# listen for connections. +# +# However when port forwarding or Network Address Translation (NAT) is +# used, the replica may actually be reachable via different IP and port +# pairs. The following two options can be used by a replica in order to +# report to its master a specific set of IP and port, so that both INFO +# and ROLE will report those values. +# +# There is no need to use both the options if you need to override just +# the port or the IP address. +# +# replica-announce-ip 5.5.5.5 +# replica-announce-port 1234 + +############################### KEYS TRACKING ################################# + +# Redis implements server assisted support for client side caching of values. +# This is implemented using an invalidation table that remembers, using +# 16 millions of slots, what clients may have certain subsets of keys. In turn +# this is used in order to send invalidation messages to clients. Please +# check this page to understand more about the feature: +# +# https://redis.io/topics/client-side-caching +# +# When tracking is enabled for a client, all the read only queries are assumed +# to be cached: this will force Redis to store information in the invalidation +# table. When keys are modified, such information is flushed away, and +# invalidation messages are sent to the clients. However if the workload is +# heavily dominated by reads, Redis could use more and more memory in order +# to track the keys fetched by many clients. +# +# For this reason it is possible to configure a maximum fill value for the +# invalidation table. By default it is set to 1M of keys, and once this limit +# is reached, Redis will start to evict keys in the invalidation table +# even if they were not modified, just to reclaim memory: this will in turn +# force the clients to invalidate the cached values. Basically the table +# maximum size is a trade off between the memory you want to spend server +# side to track information about who cached what, and the ability of clients +# to retain cached objects in memory. +# +# If you set the value to 0, it means there are no limits, and Redis will +# retain as many keys as needed in the invalidation table. +# In the "stats" INFO section, you can find information about the number of +# keys in the invalidation table at every given moment. +# +# Note: when key tracking is used in broadcasting mode, no memory is used +# in the server side so this setting is useless. +# +# tracking-table-max-keys 1000000 + +################################## SECURITY ################################### + +# Warning: since Redis is pretty fast, an outside user can try up to +# 1 million passwords per second against a modern box. This means that you +# should use very strong passwords, otherwise they will be very easy to break. +# Note that because the password is really a shared secret between the client +# and the server, and should not be memorized by any human, the password +# can be easily a long string from /dev/urandom or whatever, so by using a +# long and unguessable password no brute force attack will be possible. + +# Redis ACL users are defined in the following format: +# +# user ... acl rules ... +# +# For example: +# +# user worker +@list +@connection ~jobs:* on >ffa9203c493aa99 +# +# The special username "default" is used for new connections. If this user +# has the "nopass" rule, then new connections will be immediately authenticated +# as the "default" user without the need of any password provided via the +# AUTH command. Otherwise if the "default" user is not flagged with "nopass" +# the connections will start in not authenticated state, and will require +# AUTH (or the HELLO command AUTH option) in order to be authenticated and +# start to work. +# +# The ACL rules that describe what a user can do are the following: +# +# on Enable the user: it is possible to authenticate as this user. +# off Disable the user: it's no longer possible to authenticate +# with this user, however the already authenticated connections +# will still work. +# + Allow the execution of that command +# - Disallow the execution of that command +# +@ Allow the execution of all the commands in such category +# with valid categories are like @admin, @set, @sortedset, ... +# and so forth, see the full list in the server.c file where +# the Redis command table is described and defined. +# The special category @all means all the commands, but currently +# present in the server, and that will be loaded in the future +# via modules. +# +|subcommand Allow a specific subcommand of an otherwise +# disabled command. Note that this form is not +# allowed as negative like -DEBUG|SEGFAULT, but +# only additive starting with "+". +# allcommands Alias for +@all. Note that it implies the ability to execute +# all the future commands loaded via the modules system. +# nocommands Alias for -@all. +# ~ Add a pattern of keys that can be mentioned as part of +# commands. For instance ~* allows all the keys. The pattern +# is a glob-style pattern like the one of KEYS. +# It is possible to specify multiple patterns. +# allkeys Alias for ~* +# resetkeys Flush the list of allowed keys patterns. +# > Add this password to the list of valid password for the user. +# For example >mypass will add "mypass" to the list. +# This directive clears the "nopass" flag (see later). +# < Remove this password from the list of valid passwords. +# nopass All the set passwords of the user are removed, and the user +# is flagged as requiring no password: it means that every +# password will work against this user. If this directive is +# used for the default user, every new connection will be +# immediately authenticated with the default user without +# any explicit AUTH command required. Note that the "resetpass" +# directive will clear this condition. +# resetpass Flush the list of allowed passwords. Moreover removes the +# "nopass" status. After "resetpass" the user has no associated +# passwords and there is no way to authenticate without adding +# some password (or setting it as "nopass" later). +# reset Performs the following actions: resetpass, resetkeys, off, +# -@all. The user returns to the same state it has immediately +# after its creation. +# +# ACL rules can be specified in any order: for instance you can start with +# passwords, then flags, or key patterns. However note that the additive +# and subtractive rules will CHANGE MEANING depending on the ordering. +# For instance see the following example: +# +# user alice on +@all -DEBUG ~* >somepassword +# +# This will allow "alice" to use all the commands with the exception of the +# DEBUG command, since +@all added all the commands to the set of the commands +# alice can use, and later DEBUG was removed. However if we invert the order +# of two ACL rules the result will be different: +# +# user alice on -DEBUG +@all ~* >somepassword +# +# Now DEBUG was removed when alice had yet no commands in the set of allowed +# commands, later all the commands are added, so the user will be able to +# execute everything. +# +# Basically ACL rules are processed left-to-right. +# +# For more information about ACL configuration please refer to +# the Redis web site at https://redis.io/topics/acl + +# ACL LOG +# +# The ACL Log tracks failed commands and authentication events associated +# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked +# by ACLs. The ACL Log is stored in memory. You can reclaim memory with +# ACL LOG RESET. Define the maximum entry length of the ACL Log below. +acllog-max-len 128 + +# Using an external ACL file +# +# Instead of configuring users here in this file, it is possible to use +# a stand-alone file just listing users. The two methods cannot be mixed: +# if you configure users here and at the same time you activate the external +# ACL file, the server will refuse to start. +# +# The format of the external ACL user file is exactly the same as the +# format that is used inside redis.conf to describe users. +# +# aclfile /etc/redis/users.acl + +# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility +# layer on top of the new ACL system. The option effect will be just setting +# the password for the default user. Clients will still authenticate using +# AUTH as usually, or more explicitly with AUTH default +# if they follow the new protocol: both will work. +# +# requirepass foobared + +# Command renaming (DEPRECATED). +# +# ------------------------------------------------------------------------ +# WARNING: avoid using this option if possible. Instead use ACLs to remove +# commands from the default user, and put them only in some admin user you +# create for administrative purposes. +# ------------------------------------------------------------------------ +# +# It is possible to change the name of dangerous commands in a shared +# environment. For instance the CONFIG command may be renamed into something +# hard to guess so that it will still be available for internal-use tools +# but not available for general clients. +# +# Example: +# +# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 +# +# It is also possible to completely kill a command by renaming it into +# an empty string: +# +# rename-command CONFIG "" +# +# Please note that changing the name of commands that are logged into the +# AOF file or transmitted to replicas may cause problems. + +################################### CLIENTS #################################### + +# Set the max number of connected clients at the same time. By default +# this limit is set to 10000 clients, however if the Redis server is not +# able to configure the process file limit to allow for the specified limit +# the max number of allowed clients is set to the current file limit +# minus 32 (as Redis reserves a few file descriptors for internal uses). +# +# Once the limit is reached Redis will close all the new connections sending +# an error 'max number of clients reached'. +# +# IMPORTANT: When Redis Cluster is used, the max number of connections is also +# shared with the cluster bus: every node in the cluster will use two +# connections, one incoming and another outgoing. It is important to size the +# limit accordingly in case of very large clusters. +# +# maxclients 10000 + +############################## MEMORY MANAGEMENT ################################ + +# Set a memory usage limit to the specified amount of bytes. +# When the memory limit is reached Redis will try to remove keys +# according to the eviction policy selected (see maxmemory-policy). +# +# If Redis can't remove keys according to the policy, or if the policy is +# set to 'noeviction', Redis will start to reply with errors to commands +# that would use more memory, like SET, LPUSH, and so on, and will continue +# to reply to read-only commands like GET. +# +# This option is usually useful when using Redis as an LRU or LFU cache, or to +# set a hard memory limit for an instance (using the 'noeviction' policy). +# +# WARNING: If you have replicas attached to an instance with maxmemory on, +# the size of the output buffers needed to feed the replicas are subtracted +# from the used memory count, so that network problems / resyncs will +# not trigger a loop where keys are evicted, and in turn the output +# buffer of replicas is full with DELs of keys evicted triggering the deletion +# of more keys, and so forth until the database is completely emptied. +# +# In short... if you have replicas attached it is suggested that you set a lower +# limit for maxmemory so that there is some free RAM on the system for replica +# output buffers (but this is not needed if the policy is 'noeviction'). +# +# maxmemory + +# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory +# is reached. You can select one from the following behaviors: +# +# volatile-lru -> Evict using approximated LRU, only keys with an expire set. +# allkeys-lru -> Evict any key using approximated LRU. +# volatile-lfu -> Evict using approximated LFU, only keys with an expire set. +# allkeys-lfu -> Evict any key using approximated LFU. +# volatile-random -> Remove a random key having an expire set. +# allkeys-random -> Remove a random key, any key. +# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) +# noeviction -> Don't evict anything, just return an error on write operations. +# +# LRU means Least Recently Used +# LFU means Least Frequently Used +# +# Both LRU, LFU and volatile-ttl are implemented using approximated +# randomized algorithms. +# +# Note: with any of the above policies, Redis will return an error on write +# operations, when there are no suitable keys for eviction. +# +# At the date of writing these commands are: set setnx setex append +# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd +# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby +# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby +# getset mset msetnx exec sort +# +# The default is: +# +# maxmemory-policy noeviction + +# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated +# algorithms (in order to save memory), so you can tune it for speed or +# accuracy. By default Redis will check five keys and pick the one that was +# used least recently, you can change the sample size using the following +# configuration directive. +# +# The default of 5 produces good enough results. 10 Approximates very closely +# true LRU but costs more CPU. 3 is faster but not very accurate. +# +# maxmemory-samples 5 + +# Starting from Redis 5, by default a replica will ignore its maxmemory setting +# (unless it is promoted to master after a failover or manually). It means +# that the eviction of keys will be just handled by the master, sending the +# DEL commands to the replica as keys evict in the master side. +# +# This behavior ensures that masters and replicas stay consistent, and is usually +# what you want, however if your replica is writable, or you want the replica +# to have a different memory setting, and you are sure all the writes performed +# to the replica are idempotent, then you may change this default (but be sure +# to understand what you are doing). +# +# Note that since the replica by default does not evict, it may end using more +# memory than the one set via maxmemory (there are certain buffers that may +# be larger on the replica, or data structures may sometimes take more memory +# and so forth). So make sure you monitor your replicas and make sure they +# have enough memory to never hit a real out-of-memory condition before the +# master hits the configured maxmemory setting. +# +# replica-ignore-maxmemory yes + +# Redis reclaims expired keys in two ways: upon access when those keys are +# found to be expired, and also in background, in what is called the +# "active expire key". The key space is slowly and interactively scanned +# looking for expired keys to reclaim, so that it is possible to free memory +# of keys that are expired and will never be accessed again in a short time. +# +# The default effort of the expire cycle will try to avoid having more than +# ten percent of expired keys still in memory, and will try to avoid consuming +# more than 25% of total memory and to add latency to the system. However +# it is possible to increase the expire "effort" that is normally set to +# "1", to a greater value, up to the value "10". At its maximum value the +# system will use more CPU, longer cycles (and technically may introduce +# more latency), and will tolerate less already expired keys still present +# in the system. It's a tradeoff between memory, CPU and latency. +# +# active-expire-effort 1 + +############################# LAZY FREEING #################################### + +# Redis has two primitives to delete keys. One is called DEL and is a blocking +# deletion of the object. It means that the server stops processing new commands +# in order to reclaim all the memory associated with an object in a synchronous +# way. If the key deleted is associated with a small object, the time needed +# in order to execute the DEL command is very small and comparable to most other +# O(1) or O(log_N) commands in Redis. However if the key is associated with an +# aggregated value containing millions of elements, the server can block for +# a long time (even seconds) in order to complete the operation. +# +# For the above reasons Redis also offers non blocking deletion primitives +# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and +# FLUSHDB commands, in order to reclaim memory in background. Those commands +# are executed in constant time. Another thread will incrementally free the +# object in the background as fast as possible. +# +# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled. +# It's up to the design of the application to understand when it is a good +# idea to use one or the other. However the Redis server sometimes has to +# delete keys or flush the whole database as a side effect of other operations. +# Specifically Redis deletes objects independently of a user call in the +# following scenarios: +# +# 1) On eviction, because of the maxmemory and maxmemory policy configurations, +# in order to make room for new data, without going over the specified +# memory limit. +# 2) Because of expire: when a key with an associated time to live (see the +# EXPIRE command) must be deleted from memory. +# 3) Because of a side effect of a command that stores data on a key that may +# already exist. For example the RENAME command may delete the old key +# content when it is replaced with another one. Similarly SUNIONSTORE +# or SORT with STORE option may delete existing keys. The SET command +# itself removes any old content of the specified key in order to replace +# it with the specified string. +# 4) During replication, when a replica performs a full resynchronization with +# its master, the content of the whole database is removed in order to +# load the RDB file just transferred. +# +# In all the above cases the default is to delete objects in a blocking way, +# like if DEL was called. However you can configure each case specifically +# in order to instead release memory in a non-blocking way like if UNLINK +# was called, using the following configuration directives. + +lazyfree-lazy-eviction no +lazyfree-lazy-expire no +lazyfree-lazy-server-del no +replica-lazy-flush no + +# It is also possible, for the case when to replace the user code DEL calls +# with UNLINK calls is not easy, to modify the default behavior of the DEL +# command to act exactly like UNLINK, using the following configuration +# directive: + +lazyfree-lazy-user-del no + +################################ THREADED I/O ################################# + +# Redis is mostly single threaded, however there are certain threaded +# operations such as UNLINK, slow I/O accesses and other things that are +# performed on side threads. +# +# Now it is also possible to handle Redis clients socket reads and writes +# in different I/O threads. Since especially writing is so slow, normally +# Redis users use pipelining in order to speed up the Redis performances per +# core, and spawn multiple instances in order to scale more. Using I/O +# threads it is possible to easily speedup two times Redis without resorting +# to pipelining nor sharding of the instance. +# +# By default threading is disabled, we suggest enabling it only in machines +# that have at least 4 or more cores, leaving at least one spare core. +# Using more than 8 threads is unlikely to help much. We also recommend using +# threaded I/O only if you actually have performance problems, with Redis +# instances being able to use a quite big percentage of CPU time, otherwise +# there is no point in using this feature. +# +# So for instance if you have a four cores boxes, try to use 2 or 3 I/O +# threads, if you have a 8 cores, try to use 6 threads. In order to +# enable I/O threads use the following configuration directive: +# +# io-threads 4 +# +# Setting io-threads to 1 will just use the main thread as usual. +# When I/O threads are enabled, we only use threads for writes, that is +# to thread the write(2) syscall and transfer the client buffers to the +# socket. However it is also possible to enable threading of reads and +# protocol parsing using the following configuration directive, by setting +# it to yes: +# +# io-threads-do-reads no +# +# Usually threading reads doesn't help much. +# +# NOTE 1: This configuration directive cannot be changed at runtime via +# CONFIG SET. Aso this feature currently does not work when SSL is +# enabled. +# +# NOTE 2: If you want to test the Redis speedup using redis-benchmark, make +# sure you also run the benchmark itself in threaded mode, using the +# --threads option to match the number of Redis threads, otherwise you'll not +# be able to notice the improvements. + +############################ KERNEL OOM CONTROL ############################## + +# On Linux, it is possible to hint the kernel OOM killer on what processes +# should be killed first when out of memory. +# +# Enabling this feature makes Redis actively control the oom_score_adj value +# for all its processes, depending on their role. The default scores will +# attempt to have background child processes killed before all others, and +# replicas killed before masters. + +oom-score-adj no + +# When oom-score-adj is used, this directive controls the specific values used +# for master, replica and background child processes. Values range -1000 to +# 1000 (higher means more likely to be killed). +# +# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities) +# can freely increase their value, but not decrease it below its initial +# settings. +# +# Values are used relative to the initial value of oom_score_adj when the server +# starts. Because typically the initial value is 0, they will often match the +# absolute values. + +oom-score-adj-values 0 200 800 + +############################## APPEND ONLY MODE ############################### + +# By default Redis asynchronously dumps the dataset on disk. This mode is +# good enough in many applications, but an issue with the Redis process or +# a power outage may result into a few minutes of writes lost (depending on +# the configured save points). +# +# The Append Only File is an alternative persistence mode that provides +# much better durability. For instance using the default data fsync policy +# (see later in the config file) Redis can lose just one second of writes in a +# dramatic event like a server power outage, or a single write if something +# wrong with the Redis process itself happens, but the operating system is +# still running correctly. +# +# AOF and RDB persistence can be enabled at the same time without problems. +# If the AOF is enabled on startup Redis will load the AOF, that is the file +# with the better durability guarantees. +# +# Please check http://redis.io/topics/persistence for more information. + +appendonly no + +# The name of the append only file (default: "appendonly.aof") + +appendfilename "appendonly.aof" + +# The fsync() call tells the Operating System to actually write data on disk +# instead of waiting for more data in the output buffer. Some OS will really flush +# data on disk, some other OS will just try to do it ASAP. +# +# Redis supports three different modes: +# +# no: don't fsync, just let the OS flush the data when it wants. Faster. +# always: fsync after every write to the append only log. Slow, Safest. +# everysec: fsync only one time every second. Compromise. +# +# The default is "everysec", as that's usually the right compromise between +# speed and data safety. It's up to you to understand if you can relax this to +# "no" that will let the operating system flush the output buffer when +# it wants, for better performances (but if you can live with the idea of +# some data loss consider the default persistence mode that's snapshotting), +# or on the contrary, use "always" that's very slow but a bit safer than +# everysec. +# +# More details please check the following article: +# http://antirez.com/post/redis-persistence-demystified.html +# +# If unsure, use "everysec". + +# appendfsync always +appendfsync everysec +# appendfsync no + +# When the AOF fsync policy is set to always or everysec, and a background +# saving process (a background save or AOF log background rewriting) is +# performing a lot of I/O against the disk, in some Linux configurations +# Redis may block too long on the fsync() call. Note that there is no fix for +# this currently, as even performing fsync in a different thread will block +# our synchronous write(2) call. +# +# In order to mitigate this problem it's possible to use the following option +# that will prevent fsync() from being called in the main process while a +# BGSAVE or BGREWRITEAOF is in progress. +# +# This means that while another child is saving, the durability of Redis is +# the same as "appendfsync none". In practical terms, this means that it is +# possible to lose up to 30 seconds of log in the worst scenario (with the +# default Linux settings). +# +# If you have latency problems turn this to "yes". Otherwise leave it as +# "no" that is the safest pick from the point of view of durability. + +no-appendfsync-on-rewrite no + +# Automatic rewrite of the append only file. +# Redis is able to automatically rewrite the log file implicitly calling +# BGREWRITEAOF when the AOF log size grows by the specified percentage. +# +# This is how it works: Redis remembers the size of the AOF file after the +# latest rewrite (if no rewrite has happened since the restart, the size of +# the AOF at startup is used). +# +# This base size is compared to the current size. If the current size is +# bigger than the specified percentage, the rewrite is triggered. Also +# you need to specify a minimal size for the AOF file to be rewritten, this +# is useful to avoid rewriting the AOF file even if the percentage increase +# is reached but it is still pretty small. +# +# Specify a percentage of zero in order to disable the automatic AOF +# rewrite feature. + +auto-aof-rewrite-percentage 100 +auto-aof-rewrite-min-size 64mb + +# An AOF file may be found to be truncated at the end during the Redis +# startup process, when the AOF data gets loaded back into memory. +# This may happen when the system where Redis is running +# crashes, especially when an ext4 filesystem is mounted without the +# data=ordered option (however this can't happen when Redis itself +# crashes or aborts but the operating system still works correctly). +# +# Redis can either exit with an error when this happens, or load as much +# data as possible (the default now) and start if the AOF file is found +# to be truncated at the end. The following option controls this behavior. +# +# If aof-load-truncated is set to yes, a truncated AOF file is loaded and +# the Redis server starts emitting a log to inform the user of the event. +# Otherwise if the option is set to no, the server aborts with an error +# and refuses to start. When the option is set to no, the user requires +# to fix the AOF file using the "redis-check-aof" utility before to restart +# the server. +# +# Note that if the AOF file will be found to be corrupted in the middle +# the server will still exit with an error. This option only applies when +# Redis will try to read more data from the AOF file but not enough bytes +# will be found. +aof-load-truncated yes + +# When rewriting the AOF file, Redis is able to use an RDB preamble in the +# AOF file for faster rewrites and recoveries. When this option is turned +# on the rewritten AOF file is composed of two different stanzas: +# +# [RDB file][AOF tail] +# +# When loading, Redis recognizes that the AOF file starts with the "REDIS" +# string and loads the prefixed RDB file, then continues loading the AOF +# tail. +aof-use-rdb-preamble yes + +################################ LUA SCRIPTING ############################### + +# Max execution time of a Lua script in milliseconds. +# +# If the maximum execution time is reached Redis will log that a script is +# still in execution after the maximum allowed time and will start to +# reply to queries with an error. +# +# When a long running script exceeds the maximum execution time only the +# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be +# used to stop a script that did not yet call any write commands. The second +# is the only way to shut down the server in the case a write command was +# already issued by the script but the user doesn't want to wait for the natural +# termination of the script. +# +# Set it to 0 or a negative value for unlimited execution without warnings. +lua-time-limit 5000 + +################################ REDIS CLUSTER ############################### + +# Normal Redis instances can't be part of a Redis Cluster; only nodes that are +# started as cluster nodes can. In order to start a Redis instance as a +# cluster node enable the cluster support uncommenting the following: +# +# cluster-enabled yes + +# Every cluster node has a cluster configuration file. This file is not +# intended to be edited by hand. It is created and updated by Redis nodes. +# Every Redis Cluster node requires a different cluster configuration file. +# Make sure that instances running in the same system do not have +# overlapping cluster configuration file names. +# +# cluster-config-file nodes-6379.conf + +# Cluster node timeout is the amount of milliseconds a node must be unreachable +# for it to be considered in failure state. +# Most other internal time limits are a multiple of the node timeout. +# +# cluster-node-timeout 15000 + +# A replica of a failing master will avoid to start a failover if its data +# looks too old. +# +# There is no simple way for a replica to actually have an exact measure of +# its "data age", so the following two checks are performed: +# +# 1) If there are multiple replicas able to failover, they exchange messages +# in order to try to give an advantage to the replica with the best +# replication offset (more data from the master processed). +# Replicas will try to get their rank by offset, and apply to the start +# of the failover a delay proportional to their rank. +# +# 2) Every single replica computes the time of the last interaction with +# its master. This can be the last ping or command received (if the master +# is still in the "connected" state), or the time that elapsed since the +# disconnection with the master (if the replication link is currently down). +# If the last interaction is too old, the replica will not try to failover +# at all. +# +# The point "2" can be tuned by user. Specifically a replica will not perform +# the failover if, since the last interaction with the master, the time +# elapsed is greater than: +# +# (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period +# +# So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor +# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the +# replica will not try to failover if it was not able to talk with the master +# for longer than 310 seconds. +# +# A large cluster-replica-validity-factor may allow replicas with too old data to failover +# a master, while a too small value may prevent the cluster from being able to +# elect a replica at all. +# +# For maximum availability, it is possible to set the cluster-replica-validity-factor +# to a value of 0, which means, that replicas will always try to failover the +# master regardless of the last time they interacted with the master. +# (However they'll always try to apply a delay proportional to their +# offset rank). +# +# Zero is the only value able to guarantee that when all the partitions heal +# the cluster will always be able to continue. +# +# cluster-replica-validity-factor 10 + +# Cluster replicas are able to migrate to orphaned masters, that are masters +# that are left without working replicas. This improves the cluster ability +# to resist to failures as otherwise an orphaned master can't be failed over +# in case of failure if it has no working replicas. +# +# Replicas migrate to orphaned masters only if there are still at least a +# given number of other working replicas for their old master. This number +# is the "migration barrier". A migration barrier of 1 means that a replica +# will migrate only if there is at least 1 other working replica for its master +# and so forth. It usually reflects the number of replicas you want for every +# master in your cluster. +# +# Default is 1 (replicas migrate only if their masters remain with at least +# one replica). To disable migration just set it to a very large value. +# A value of 0 can be set but is useful only for debugging and dangerous +# in production. +# +# cluster-migration-barrier 1 + +# By default Redis Cluster nodes stop accepting queries if they detect there +# is at least a hash slot uncovered (no available node is serving it). +# This way if the cluster is partially down (for example a range of hash slots +# are no longer covered) all the cluster becomes, eventually, unavailable. +# It automatically returns available as soon as all the slots are covered again. +# +# However sometimes you want the subset of the cluster which is working, +# to continue to accept queries for the part of the key space that is still +# covered. In order to do so, just set the cluster-require-full-coverage +# option to no. +# +# cluster-require-full-coverage yes + +# This option, when set to yes, prevents replicas from trying to failover its +# master during master failures. However the master can still perform a +# manual failover, if forced to do so. +# +# This is useful in different scenarios, especially in the case of multiple +# data center operations, where we want one side to never be promoted if not +# in the case of a total DC failure. +# +# cluster-replica-no-failover no + +# This option, when set to yes, allows nodes to serve read traffic while the +# the cluster is in a down state, as long as it believes it owns the slots. +# +# This is useful for two cases. The first case is for when an application +# doesn't require consistency of data during node failures or network partitions. +# One example of this is a cache, where as long as the node has the data it +# should be able to serve it. +# +# The second use case is for configurations that don't meet the recommended +# three shards but want to enable cluster mode and scale later. A +# master outage in a 1 or 2 shard configuration causes a read/write outage to the +# entire cluster without this option set, with it set there is only a write outage. +# Without a quorum of masters, slot ownership will not change automatically. +# +# cluster-allow-reads-when-down no + +# In order to setup your cluster make sure to read the documentation +# available at http://redis.io web site. + +########################## CLUSTER DOCKER/NAT support ######################## + +# In certain deployments, Redis Cluster nodes address discovery fails, because +# addresses are NAT-ted or because ports are forwarded (the typical case is +# Docker and other containers). +# +# In order to make Redis Cluster working in such environments, a static +# configuration where each node knows its public address is needed. The +# following two options are used for this scope, and are: +# +# * cluster-announce-ip +# * cluster-announce-port +# * cluster-announce-bus-port +# +# Each instructs the node about its address, client port, and cluster message +# bus port. The information is then published in the header of the bus packets +# so that other nodes will be able to correctly map the address of the node +# publishing the information. +# +# If the above options are not used, the normal Redis Cluster auto-detection +# will be used instead. +# +# Note that when remapped, the bus port may not be at the fixed offset of +# clients port + 10000, so you can specify any port and bus-port depending +# on how they get remapped. If the bus-port is not set, a fixed offset of +# 10000 will be used as usual. +# +# Example: +# +# cluster-announce-ip 10.1.1.5 +# cluster-announce-port 6379 +# cluster-announce-bus-port 6380 + +################################## SLOW LOG ################################### + +# The Redis Slow Log is a system to log queries that exceeded a specified +# execution time. The execution time does not include the I/O operations +# like talking with the client, sending the reply and so forth, +# but just the time needed to actually execute the command (this is the only +# stage of command execution where the thread is blocked and can not serve +# other requests in the meantime). +# +# You can configure the slow log with two parameters: one tells Redis +# what is the execution time, in microseconds, to exceed in order for the +# command to get logged, and the other parameter is the length of the +# slow log. When a new command is logged the oldest one is removed from the +# queue of logged commands. + +# The following time is expressed in microseconds, so 1000000 is equivalent +# to one second. Note that a negative number disables the slow log, while +# a value of zero forces the logging of every command. +slowlog-log-slower-than 10000 + +# There is no limit to this length. Just be aware that it will consume memory. +# You can reclaim memory used by the slow log with SLOWLOG RESET. +slowlog-max-len 128 + +################################ LATENCY MONITOR ############################## + +# The Redis latency monitoring subsystem samples different operations +# at runtime in order to collect data related to possible sources of +# latency of a Redis instance. +# +# Via the LATENCY command this information is available to the user that can +# print graphs and obtain reports. +# +# The system only logs operations that were performed in a time equal or +# greater than the amount of milliseconds specified via the +# latency-monitor-threshold configuration directive. When its value is set +# to zero, the latency monitor is turned off. +# +# By default latency monitoring is disabled since it is mostly not needed +# if you don't have latency issues, and collecting data has a performance +# impact, that while very small, can be measured under big load. Latency +# monitoring can easily be enabled at runtime using the command +# "CONFIG SET latency-monitor-threshold " if needed. +latency-monitor-threshold 0 + +############################# EVENT NOTIFICATION ############################## + +# Redis can notify Pub/Sub clients about events happening in the key space. +# This feature is documented at http://redis.io/topics/notifications +# +# For instance if keyspace events notification is enabled, and a client +# performs a DEL operation on key "foo" stored in the Database 0, two +# messages will be published via Pub/Sub: +# +# PUBLISH __keyspace@0__:foo del +# PUBLISH __keyevent@0__:del foo +# +# It is possible to select the events that Redis will notify among a set +# of classes. Every class is identified by a single character: +# +# K Keyspace events, published with __keyspace@__ prefix. +# E Keyevent events, published with __keyevent@__ prefix. +# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ... +# $ String commands +# l List commands +# s Set commands +# h Hash commands +# z Sorted set commands +# x Expired events (events generated every time a key expires) +# e Evicted events (events generated when a key is evicted for maxmemory) +# t Stream commands +# m Key-miss events (Note: It is not included in the 'A' class) +# A Alias for g$lshzxet, so that the "AKE" string means all the events +# (Except key-miss events which are excluded from 'A' due to their +# unique nature). +# +# The "notify-keyspace-events" takes as argument a string that is composed +# of zero or multiple characters. The empty string means that notifications +# are disabled. +# +# Example: to enable list and generic events, from the point of view of the +# event name, use: +# +# notify-keyspace-events Elg +# +# Example 2: to get the stream of the expired keys subscribing to channel +# name __keyevent@0__:expired use: +# +# notify-keyspace-events Ex +# +# By default all notifications are disabled because most users don't need +# this feature and the feature has some overhead. Note that if you don't +# specify at least one of K or E, no events will be delivered. +notify-keyspace-events "" + +############################### GOPHER SERVER ################################# + +# Redis contains an implementation of the Gopher protocol, as specified in +# the RFC 1436 (https://www.ietf.org/rfc/rfc1436.txt). +# +# The Gopher protocol was very popular in the late '90s. It is an alternative +# to the web, and the implementation both server and client side is so simple +# that the Redis server has just 100 lines of code in order to implement this +# support. +# +# What do you do with Gopher nowadays? Well Gopher never *really* died, and +# lately there is a movement in order for the Gopher more hierarchical content +# composed of just plain text documents to be resurrected. Some want a simpler +# internet, others believe that the mainstream internet became too much +# controlled, and it's cool to create an alternative space for people that +# want a bit of fresh air. +# +# Anyway for the 10nth birthday of the Redis, we gave it the Gopher protocol +# as a gift. +# +# --- HOW IT WORKS? --- +# +# The Redis Gopher support uses the inline protocol of Redis, and specifically +# two kind of inline requests that were anyway illegal: an empty request +# or any request that starts with "/" (there are no Redis commands starting +# with such a slash). Normal RESP2/RESP3 requests are completely out of the +# path of the Gopher protocol implementation and are served as usual as well. +# +# If you open a connection to Redis when Gopher is enabled and send it +# a string like "/foo", if there is a key named "/foo" it is served via the +# Gopher protocol. +# +# In order to create a real Gopher "hole" (the name of a Gopher site in Gopher +# talking), you likely need a script like the following: +# +# https://github.com/antirez/gopher2redis +# +# --- SECURITY WARNING --- +# +# If you plan to put Redis on the internet in a publicly accessible address +# to server Gopher pages MAKE SURE TO SET A PASSWORD to the instance. +# Once a password is set: +# +# 1. The Gopher server (when enabled, not by default) will still serve +# content via Gopher. +# 2. However other commands cannot be called before the client will +# authenticate. +# +# So use the 'requirepass' option to protect your instance. +# +# Note that Gopher is not currently supported when 'io-threads-do-reads' +# is enabled. +# +# To enable Gopher support, uncomment the following line and set the option +# from no (the default) to yes. +# +# gopher-enabled no + +############################### ADVANCED CONFIG ############################### + +# Hashes are encoded using a memory efficient data structure when they have a +# small number of entries, and the biggest entry does not exceed a given +# threshold. These thresholds can be configured using the following directives. +hash-max-ziplist-entries 512 +hash-max-ziplist-value 64 + +# Lists are also encoded in a special way to save a lot of space. +# The number of entries allowed per internal list node can be specified +# as a fixed maximum size or a maximum number of elements. +# For a fixed maximum size, use -5 through -1, meaning: +# -5: max size: 64 Kb <-- not recommended for normal workloads +# -4: max size: 32 Kb <-- not recommended +# -3: max size: 16 Kb <-- probably not recommended +# -2: max size: 8 Kb <-- good +# -1: max size: 4 Kb <-- good +# Positive numbers mean store up to _exactly_ that number of elements +# per list node. +# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size), +# but if your use case is unique, adjust the settings as necessary. +list-max-ziplist-size -2 + +# Lists may also be compressed. +# Compress depth is the number of quicklist ziplist nodes from *each* side of +# the list to *exclude* from compression. The head and tail of the list +# are always uncompressed for fast push/pop operations. Settings are: +# 0: disable all list compression +# 1: depth 1 means "don't start compressing until after 1 node into the list, +# going from either the head or tail" +# So: [head]->node->node->...->node->[tail] +# [head], [tail] will always be uncompressed; inner nodes will compress. +# 2: [head]->[next]->node->node->...->node->[prev]->[tail] +# 2 here means: don't compress head or head->next or tail->prev or tail, +# but compress all nodes between them. +# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail] +# etc. +list-compress-depth 0 + +# Sets have a special encoding in just one case: when a set is composed +# of just strings that happen to be integers in radix 10 in the range +# of 64 bit signed integers. +# The following configuration setting sets the limit in the size of the +# set in order to use this special memory saving encoding. +set-max-intset-entries 512 + +# Similarly to hashes and lists, sorted sets are also specially encoded in +# order to save a lot of space. This encoding is only used when the length and +# elements of a sorted set are below the following limits: +zset-max-ziplist-entries 128 +zset-max-ziplist-value 64 + +# HyperLogLog sparse representation bytes limit. The limit includes the +# 16 bytes header. When an HyperLogLog using the sparse representation crosses +# this limit, it is converted into the dense representation. +# +# A value greater than 16000 is totally useless, since at that point the +# dense representation is more memory efficient. +# +# The suggested value is ~ 3000 in order to have the benefits of +# the space efficient encoding without slowing down too much PFADD, +# which is O(N) with the sparse encoding. The value can be raised to +# ~ 10000 when CPU is not a concern, but space is, and the data set is +# composed of many HyperLogLogs with cardinality in the 0 - 15000 range. +hll-sparse-max-bytes 3000 + +# Streams macro node max size / items. The stream data structure is a radix +# tree of big nodes that encode multiple items inside. Using this configuration +# it is possible to configure how big a single node can be in bytes, and the +# maximum number of items it may contain before switching to a new node when +# appending new stream entries. If any of the following settings are set to +# zero, the limit is ignored, so for instance it is possible to set just a +# max entires limit by setting max-bytes to 0 and max-entries to the desired +# value. +stream-node-max-bytes 4kb +stream-node-max-entries 100 + +# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in +# order to help rehashing the main Redis hash table (the one mapping top-level +# keys to values). The hash table implementation Redis uses (see dict.c) +# performs a lazy rehashing: the more operation you run into a hash table +# that is rehashing, the more rehashing "steps" are performed, so if the +# server is idle the rehashing is never complete and some more memory is used +# by the hash table. +# +# The default is to use this millisecond 10 times every second in order to +# actively rehash the main dictionaries, freeing memory when possible. +# +# If unsure: +# use "activerehashing no" if you have hard latency requirements and it is +# not a good thing in your environment that Redis can reply from time to time +# to queries with 2 milliseconds delay. +# +# use "activerehashing yes" if you don't have such hard requirements but +# want to free memory asap when possible. +activerehashing yes + +# The client output buffer limits can be used to force disconnection of clients +# that are not reading data from the server fast enough for some reason (a +# common reason is that a Pub/Sub client can't consume messages as fast as the +# publisher can produce them). +# +# The limit can be set differently for the three different classes of clients: +# +# normal -> normal clients including MONITOR clients +# replica -> replica clients +# pubsub -> clients subscribed to at least one pubsub channel or pattern +# +# The syntax of every client-output-buffer-limit directive is the following: +# +# client-output-buffer-limit +# +# A client is immediately disconnected once the hard limit is reached, or if +# the soft limit is reached and remains reached for the specified number of +# seconds (continuously). +# So for instance if the hard limit is 32 megabytes and the soft limit is +# 16 megabytes / 10 seconds, the client will get disconnected immediately +# if the size of the output buffers reach 32 megabytes, but will also get +# disconnected if the client reaches 16 megabytes and continuously overcomes +# the limit for 10 seconds. +# +# By default normal clients are not limited because they don't receive data +# without asking (in a push way), but just after a request, so only +# asynchronous clients may create a scenario where data is requested faster +# than it can read. +# +# Instead there is a default limit for pubsub and replica clients, since +# subscribers and replicas receive data in a push fashion. +# +# Both the hard or the soft limit can be disabled by setting them to zero. +client-output-buffer-limit normal 0 0 0 +client-output-buffer-limit replica 256mb 64mb 60 +client-output-buffer-limit pubsub 32mb 8mb 60 + +# Client query buffers accumulate new commands. They are limited to a fixed +# amount by default in order to avoid that a protocol desynchronization (for +# instance due to a bug in the client) will lead to unbound memory usage in +# the query buffer. However you can configure it here if you have very special +# needs, such us huge multi/exec requests or alike. +# +# client-query-buffer-limit 1gb + +# In the Redis protocol, bulk requests, that are, elements representing single +# strings, are normally limited to 512 mb. However you can change this limit +# here, but must be 1mb or greater +# +# proto-max-bulk-len 512mb + +# Redis calls an internal function to perform many background tasks, like +# closing connections of clients in timeout, purging expired keys that are +# never requested, and so forth. +# +# Not all tasks are performed with the same frequency, but Redis checks for +# tasks to perform according to the specified "hz" value. +# +# By default "hz" is set to 10. Raising the value will use more CPU when +# Redis is idle, but at the same time will make Redis more responsive when +# there are many keys expiring at the same time, and timeouts may be +# handled with more precision. +# +# The range is between 1 and 500, however a value over 100 is usually not +# a good idea. Most users should use the default of 10 and raise this up to +# 100 only in environments where very low latency is required. +hz 10 + +# Normally it is useful to have an HZ value which is proportional to the +# number of clients connected. This is useful in order, for instance, to +# avoid too many clients are processed for each background task invocation +# in order to avoid latency spikes. +# +# Since the default HZ value by default is conservatively set to 10, Redis +# offers, and enables by default, the ability to use an adaptive HZ value +# which will temporarily raise when there are many connected clients. +# +# When dynamic HZ is enabled, the actual configured HZ will be used +# as a baseline, but multiples of the configured HZ value will be actually +# used as needed once more clients are connected. In this way an idle +# instance will use very little CPU time while a busy instance will be +# more responsive. +dynamic-hz yes + +# When a child rewrites the AOF file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +aof-rewrite-incremental-fsync yes + +# When redis saves RDB file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +rdb-save-incremental-fsync yes + +# Redis LFU eviction (see maxmemory setting) can be tuned. However it is a good +# idea to start with the default settings and only change them after investigating +# how to improve the performances and how the keys LFU change over time, which +# is possible to inspect via the OBJECT FREQ command. +# +# There are two tunable parameters in the Redis LFU implementation: the +# counter logarithm factor and the counter decay time. It is important to +# understand what the two parameters mean before changing them. +# +# The LFU counter is just 8 bits per key, it's maximum value is 255, so Redis +# uses a probabilistic increment with logarithmic behavior. Given the value +# of the old counter, when a key is accessed, the counter is incremented in +# this way: +# +# 1. A random number R between 0 and 1 is extracted. +# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1). +# 3. The counter is incremented only if R < P. +# +# The default lfu-log-factor is 10. This is a table of how the frequency +# counter changes with a different number of accesses with different +# logarithmic factors: +# +# +--------+------------+------------+------------+------------+------------+ +# | factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits | +# +--------+------------+------------+------------+------------+------------+ +# | 0 | 104 | 255 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 1 | 18 | 49 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 10 | 10 | 18 | 142 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 100 | 8 | 11 | 49 | 143 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# +# NOTE: The above table was obtained by running the following commands: +# +# redis-benchmark -n 1000000 incr foo +# redis-cli object freq foo +# +# NOTE 2: The counter initial value is 5 in order to give new objects a chance +# to accumulate hits. +# +# The counter decay time is the time, in minutes, that must elapse in order +# for the key counter to be divided by two (or decremented if it has a value +# less <= 10). +# +# The default value for the lfu-decay-time is 1. A special value of 0 means to +# decay the counter every time it happens to be scanned. +# +# lfu-log-factor 10 +# lfu-decay-time 1 + +########################### ACTIVE DEFRAGMENTATION ####################### +# +# What is active defragmentation? +# ------------------------------- +# +# Active (online) defragmentation allows a Redis server to compact the +# spaces left between small allocations and deallocations of data in memory, +# thus allowing to reclaim back memory. +# +# Fragmentation is a natural process that happens with every allocator (but +# less so with Jemalloc, fortunately) and certain workloads. Normally a server +# restart is needed in order to lower the fragmentation, or at least to flush +# away all the data and create it again. However thanks to this feature +# implemented by Oran Agra for Redis 4.0 this process can happen at runtime +# in a "hot" way, while the server is running. +# +# Basically when the fragmentation is over a certain level (see the +# configuration options below) Redis will start to create new copies of the +# values in contiguous memory regions by exploiting certain specific Jemalloc +# features (in order to understand if an allocation is causing fragmentation +# and to allocate it in a better place), and at the same time, will release the +# old copies of the data. This process, repeated incrementally for all the keys +# will cause the fragmentation to drop back to normal values. +# +# Important things to understand: +# +# 1. This feature is disabled by default, and only works if you compiled Redis +# to use the copy of Jemalloc we ship with the source code of Redis. +# This is the default with Linux builds. +# +# 2. You never need to enable this feature if you don't have fragmentation +# issues. +# +# 3. Once you experience fragmentation, you can enable this feature when +# needed with the command "CONFIG SET activedefrag yes". +# +# The configuration parameters are able to fine tune the behavior of the +# defragmentation process. If you are not sure about what they mean it is +# a good idea to leave the defaults untouched. + +# Enabled active defragmentation +# activedefrag no + +# Minimum amount of fragmentation waste to start active defrag +# active-defrag-ignore-bytes 100mb + +# Minimum percentage of fragmentation to start active defrag +# active-defrag-threshold-lower 10 + +# Maximum percentage of fragmentation at which we use maximum effort +# active-defrag-threshold-upper 100 + +# Minimal effort for defrag in CPU percentage, to be used when the lower +# threshold is reached +# active-defrag-cycle-min 1 + +# Maximal effort for defrag in CPU percentage, to be used when the upper +# threshold is reached +# active-defrag-cycle-max 25 + +# Maximum number of set/hash/zset/list fields that will be processed from +# the main dictionary scan +# active-defrag-max-scan-fields 1000 + +# Jemalloc background thread for purging will be enabled by default +jemalloc-bg-thread yes + +# It is possible to pin different threads and processes of Redis to specific +# CPUs in your system, in order to maximize the performances of the server. +# This is useful both in order to pin different Redis threads in different +# CPUs, but also in order to make sure that multiple Redis instances running +# in the same host will be pinned to different CPUs. +# +# Normally you can do this using the "taskset" command, however it is also +# possible to this via Redis configuration directly, both in Linux and FreeBSD. +# +# You can pin the server/IO threads, bio threads, aof rewrite child process, and +# the bgsave child process. The syntax to specify the cpu list is the same as +# the taskset command: +# +# Set redis server/io threads to cpu affinity 0,2,4,6: +# server_cpulist 0-7:2 +# +# Set bio threads to cpu affinity 1,3: +# bio_cpulist 1,3 +# +# Set aof rewrite child process to cpu affinity 8,9,10,11: +# aof_rewrite_cpulist 8-11 +# +# Set bgsave child process to cpu affinity 1,10,11 +# bgsave_cpulist 1,10-11 +# Generated by CONFIG REWRITE +user default on nopass sanitize-payload ~* &* +@all + +replicaof 127.0.0.1 6380 diff --git a/08cache/ha/conf/redis6380.conf b/08cache/ha/conf/redis6380.conf new file mode 100644 index 00000000..20311df0 --- /dev/null +++ b/08cache/ha/conf/redis6380.conf @@ -0,0 +1,1868 @@ +# Redis configuration file example. +# +# Note that in order to read the configuration file, Redis must be +# started with the file path as first argument: +# +# ./redis-server /path/to/redis.conf + +# Note on units: when memory size is needed, it is possible to specify +# it in the usual form of 1k 5GB 4M and so forth: +# +# 1k => 1000 bytes +# 1kb => 1024 bytes +# 1m => 1000000 bytes +# 1mb => 1024*1024 bytes +# 1g => 1000000000 bytes +# 1gb => 1024*1024*1024 bytes +# +# units are case insensitive so 1GB 1Gb 1gB are all the same. + +################################## INCLUDES ################################### + +# Include one or more other config files here. This is useful if you +# have a standard template that goes to all Redis servers but also need +# to customize a few per-server settings. Include files can include +# other files, so use this wisely. +# +# Note that option "include" won't be rewritten by command "CONFIG REWRITE" +# from admin or Redis Sentinel. Since Redis always uses the last processed +# line as value of a configuration directive, you'd better put includes +# at the beginning of this file to avoid overwriting config change at runtime. +# +# If instead you are interested in using includes to override configuration +# options, it is better to use include as the last line. +# +# include /path/to/local.conf +# include /path/to/other.conf + +################################## MODULES ##################################### + +# Load modules at startup. If the server is not able to load modules +# it will abort. It is possible to use multiple loadmodule directives. +# +# loadmodule /path/to/my_module.so +# loadmodule /path/to/other_module.so + +################################## NETWORK ##################################### + +# By default, if no "bind" configuration directive is specified, Redis listens +# for connections from all available network interfaces on the host machine. +# It is possible to listen to just one or multiple selected interfaces using +# the "bind" configuration directive, followed by one or more IP addresses. +# +# Examples: +# +# bind 192.168.1.100 10.0.0.1 +# bind 127.0.0.1 ::1 +# +# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the +# internet, binding to all the interfaces is dangerous and will expose the +# instance to everybody on the internet. So by default we uncomment the +# following bind directive, that will force Redis to listen only on the +# IPv4 loopback interface address (this means Redis will only be able to +# accept client connections from the same host that it is running on). +# +# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES +# JUST COMMENT OUT THE FOLLOWING LINE. +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bind 127.0.0.1 ::1 + +# Protected mode is a layer of security protection, in order to avoid that +# Redis instances left open on the internet are accessed and exploited. +# +# When protected mode is on and if: +# +# 1) The server is not binding explicitly to a set of addresses using the +# "bind" directive. +# 2) No password is configured. +# +# The server only accepts connections from clients connecting from the +# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain +# sockets. +# +# By default protected mode is enabled. You should disable it only if +# you are sure you want clients from other hosts to connect to Redis +# even if no authentication is configured, nor a specific set of interfaces +# are explicitly listed using the "bind" directive. +protected-mode yes + +# Accept connections on the specified port, default is 6379 (IANA #815344). +# If port 0 is specified Redis will not listen on a TCP socket. +port 6380 + +# TCP listen() backlog. +# +# In high requests-per-second environments you need a high backlog in order +# to avoid slow clients connection issues. Note that the Linux kernel +# will silently truncate it to the value of /proc/sys/net/core/somaxconn so +# make sure to raise both the value of somaxconn and tcp_max_syn_backlog +# in order to get the desired effect. +tcp-backlog 511 + +# Unix socket. +# +# Specify the path for the Unix socket that will be used to listen for +# incoming connections. There is no default, so Redis will not listen +# on a unix socket when not specified. +# +# unixsocket /tmp/redis.sock +# unixsocketperm 700 + +# Close the connection after a client is idle for N seconds (0 to disable) +timeout 0 + +# TCP keepalive. +# +# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence +# of communication. This is useful for two reasons: +# +# 1) Detect dead peers. +# 2) Force network equipment in the middle to consider the connection to be +# alive. +# +# On Linux, the specified value (in seconds) is the period used to send ACKs. +# Note that to close the connection the double of the time is needed. +# On other kernels the period depends on the kernel configuration. +# +# A reasonable value for this option is 300 seconds, which is the new +# Redis default starting with Redis 3.2.1. +tcp-keepalive 300 + +################################# TLS/SSL ##################################### + +# By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration +# directive can be used to define TLS-listening ports. To enable TLS on the +# default port, use: +# +# port 0 +# tls-port 6379 + +# Configure a X.509 certificate and private key to use for authenticating the +# server to connected clients, masters or cluster peers. These files should be +# PEM formatted. +# +# tls-cert-file redis.crt +# tls-key-file redis.key + +# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange: +# +# tls-dh-params-file redis.dh + +# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL +# clients and peers. Redis requires an explicit configuration of at least one +# of these, and will not implicitly use the system wide configuration. +# +# tls-ca-cert-file ca.crt +# tls-ca-cert-dir /etc/ssl/certs + +# By default, clients (including replica servers) on a TLS port are required +# to authenticate using valid client side certificates. +# +# If "no" is specified, client certificates are not required and not accepted. +# If "optional" is specified, client certificates are accepted and must be +# valid if provided, but are not required. +# +# tls-auth-clients no +# tls-auth-clients optional + +# By default, a Redis replica does not attempt to establish a TLS connection +# with its master. +# +# Use the following directive to enable TLS on replication links. +# +# tls-replication yes + +# By default, the Redis Cluster bus uses a plain TCP connection. To enable +# TLS for the bus protocol, use the following directive: +# +# tls-cluster yes + +# Explicitly specify TLS versions to support. Allowed values are case insensitive +# and include "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" (OpenSSL >= 1.1.1) or +# any combination. To enable only TLSv1.2 and TLSv1.3, use: +# +# tls-protocols "TLSv1.2 TLSv1.3" + +# Configure allowed ciphers. See the ciphers(1ssl) manpage for more information +# about the syntax of this string. +# +# Note: this configuration applies only to <= TLSv1.2. +# +# tls-ciphers DEFAULT:!MEDIUM + +# Configure allowed TLSv1.3 ciphersuites. See the ciphers(1ssl) manpage for more +# information about the syntax of this string, and specifically for TLSv1.3 +# ciphersuites. +# +# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256 + +# When choosing a cipher, use the server's preference instead of the client +# preference. By default, the server follows the client's preference. +# +# tls-prefer-server-ciphers yes + +# By default, TLS session caching is enabled to allow faster and less expensive +# reconnections by clients that support it. Use the following directive to disable +# caching. +# +# tls-session-caching no + +# Change the default number of TLS sessions cached. A zero value sets the cache +# to unlimited size. The default size is 20480. +# +# tls-session-cache-size 5000 + +# Change the default timeout of cached TLS sessions. The default timeout is 300 +# seconds. +# +# tls-session-cache-timeout 60 + +################################# GENERAL ##################################### + +# By default Redis does not run as a daemon. Use 'yes' if you need it. +# Note that Redis will write a pid file in /usr/local/var/run/redis.pid when daemonized. +daemonize no + +# If you run Redis from upstart or systemd, Redis can interact with your +# supervision tree. Options: +# supervised no - no supervision interaction +# supervised upstart - signal upstart by putting Redis into SIGSTOP mode +# requires "expect stop" in your upstart job config +# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET +# supervised auto - detect upstart or systemd method based on +# UPSTART_JOB or NOTIFY_SOCKET environment variables +# Note: these supervision methods only signal "process is ready." +# They do not enable continuous pings back to your supervisor. +supervised no + +# If a pid file is specified, Redis writes it where specified at startup +# and removes it at exit. +# +# When the server runs non daemonized, no pid file is created if none is +# specified in the configuration. When the server is daemonized, the pid file +# is used even if not specified, defaulting to "/usr/local/var/run/redis.pid". +# +# Creating a pid file is best effort: if Redis is not able to create it +# nothing bad happens, the server will start and run normally. +pidfile "/var/run/redis_6380.pid" + +# Specify the server verbosity level. +# This can be one of: +# debug (a lot of information, useful for development/testing) +# verbose (many rarely useful info, but not a mess like the debug level) +# notice (moderately verbose, what you want in production probably) +# warning (only very important / critical messages are logged) +loglevel notice + +# Specify the log file name. Also the empty string can be used to force +# Redis to log on the standard output. Note that if you use standard +# output for logging but daemonize, logs will be sent to /dev/null +logfile "" + +# To enable logging to the system logger, just set 'syslog-enabled' to yes, +# and optionally update the other syslog parameters to suit your needs. +# syslog-enabled no + +# Specify the syslog identity. +# syslog-ident redis + +# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7. +# syslog-facility local0 + +# Set the number of databases. The default database is DB 0, you can select +# a different one on a per-connection basis using SELECT where +# dbid is a number between 0 and 'databases'-1 +databases 16 + +# By default Redis shows an ASCII art logo only when started to log to the +# standard output and if the standard output is a TTY. Basically this means +# that normally a logo is displayed only in interactive sessions. +# +# However it is possible to force the pre-4.0 behavior and always show a +# ASCII art logo in startup logs by setting the following option to yes. +always-show-logo yes + +################################ SNAPSHOTTING ################################ +# +# Save the DB on disk: +# +# save +# +# Will save the DB if both the given number of seconds and the given +# number of write operations against the DB occurred. +# +# In the example below the behavior will be to save: +# after 900 sec (15 min) if at least 1 key changed +# after 300 sec (5 min) if at least 10 keys changed +# after 60 sec if at least 10000 keys changed +# +# Note: you can disable saving completely by commenting out all "save" lines. +# +# It is also possible to remove all the previously configured save +# points by adding a save directive with a single empty string argument +# like in the following example: +# +# save "" + +save 900 1 +save 300 10 +save 60 10000 + +# By default Redis will stop accepting writes if RDB snapshots are enabled +# (at least one save point) and the latest background save failed. +# This will make the user aware (in a hard way) that data is not persisting +# on disk properly, otherwise chances are that no one will notice and some +# disaster will happen. +# +# If the background saving process will start working again Redis will +# automatically allow writes again. +# +# However if you have setup your proper monitoring of the Redis server +# and persistence, you may want to disable this feature so that Redis will +# continue to work as usual even if there are problems with disk, +# permissions, and so forth. +stop-writes-on-bgsave-error yes + +# Compress string objects using LZF when dump .rdb databases? +# By default compression is enabled as it's almost always a win. +# If you want to save some CPU in the saving child set it to 'no' but +# the dataset will likely be bigger if you have compressible values or keys. +rdbcompression yes + +# Since version 5 of RDB a CRC64 checksum is placed at the end of the file. +# This makes the format more resistant to corruption but there is a performance +# hit to pay (around 10%) when saving and loading RDB files, so you can disable it +# for maximum performances. +# +# RDB files created with checksum disabled have a checksum of zero that will +# tell the loading code to skip the check. +rdbchecksum yes + +# The filename where to dump the DB +dbfilename "dump.rdb" + +# Remove RDB files used by replication in instances without persistence +# enabled. By default this option is disabled, however there are environments +# where for regulations or other security concerns, RDB files persisted on +# disk by masters in order to feed replicas, or stored on disk by replicas +# in order to load them for the initial synchronization, should be deleted +# ASAP. Note that this option ONLY WORKS in instances that have both AOF +# and RDB persistence disabled, otherwise is completely ignored. +# +# An alternative (and sometimes better) way to obtain the same effect is +# to use diskless replication on both master and replicas instances. However +# in the case of replicas, diskless is not always an option. +rdb-del-sync-files no + +# The working directory. +# +# The DB will be written inside this directory, with the filename specified +# above using the 'dbfilename' configuration directive. +# +# The Append Only File will also be created inside this directory. +# +# Note that you must specify a directory here, not a file name. +dir "/Users/kimmking/logs/redis1" + +################################# REPLICATION ################################# + +# Master-Replica replication. Use replicaof to make a Redis instance a copy of +# another Redis server. A few things to understand ASAP about Redis replication. +# +# +------------------+ +---------------+ +# | Master | ---> | Replica | +# | (receive writes) | | (exact copy) | +# +------------------+ +---------------+ +# +# 1) Redis replication is asynchronous, but you can configure a master to +# stop accepting writes if it appears to be not connected with at least +# a given number of replicas. +# 2) Redis replicas are able to perform a partial resynchronization with the +# master if the replication link is lost for a relatively small amount of +# time. You may want to configure the replication backlog size (see the next +# sections of this file) with a sensible value depending on your needs. +# 3) Replication is automatic and does not need user intervention. After a +# network partition replicas automatically try to reconnect to masters +# and resynchronize with them. +# +# replicaof + +# If the master is password protected (using the "requirepass" configuration +# directive below) it is possible to tell the replica to authenticate before +# starting the replication synchronization process, otherwise the master will +# refuse the replica request. +# +# masterauth +# +# However this is not enough if you are using Redis ACLs (for Redis version +# 6 or greater), and the default user is not capable of running the PSYNC +# command and/or other commands needed for replication. In this case it's +# better to configure a special user to use with replication, and specify the +# masteruser configuration as such: +# +# masteruser +# +# When masteruser is specified, the replica will authenticate against its +# master using the new AUTH form: AUTH . + +# When a replica loses its connection with the master, or when the replication +# is still in progress, the replica can act in two different ways: +# +# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will +# still reply to client requests, possibly with out of date data, or the +# data set may just be empty if this is the first synchronization. +# +# 2) If replica-serve-stale-data is set to 'no' the replica will reply with +# an error "SYNC with master in progress" to all commands except: +# INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE, +# UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST, +# HOST and LATENCY. +# +replica-serve-stale-data yes + +# You can configure a replica instance to accept writes or not. Writing against +# a replica instance may be useful to store some ephemeral data (because data +# written on a replica will be easily deleted after resync with the master) but +# may also cause problems if clients are writing to it because of a +# misconfiguration. +# +# Since Redis 2.6 by default replicas are read-only. +# +# Note: read only replicas are not designed to be exposed to untrusted clients +# on the internet. It's just a protection layer against misuse of the instance. +# Still a read only replica exports by default all the administrative commands +# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve +# security of read only replicas using 'rename-command' to shadow all the +# administrative / dangerous commands. +replica-read-only yes + +# Replication SYNC strategy: disk or socket. +# +# New replicas and reconnecting replicas that are not able to continue the +# replication process just receiving differences, need to do what is called a +# "full synchronization". An RDB file is transmitted from the master to the +# replicas. +# +# The transmission can happen in two different ways: +# +# 1) Disk-backed: The Redis master creates a new process that writes the RDB +# file on disk. Later the file is transferred by the parent +# process to the replicas incrementally. +# 2) Diskless: The Redis master creates a new process that directly writes the +# RDB file to replica sockets, without touching the disk at all. +# +# With disk-backed replication, while the RDB file is generated, more replicas +# can be queued and served with the RDB file as soon as the current child +# producing the RDB file finishes its work. With diskless replication instead +# once the transfer starts, new replicas arriving will be queued and a new +# transfer will start when the current one terminates. +# +# When diskless replication is used, the master waits a configurable amount of +# time (in seconds) before starting the transfer in the hope that multiple +# replicas will arrive and the transfer can be parallelized. +# +# With slow disks and fast (large bandwidth) networks, diskless replication +# works better. +repl-diskless-sync no + +# When diskless replication is enabled, it is possible to configure the delay +# the server waits in order to spawn the child that transfers the RDB via socket +# to the replicas. +# +# This is important since once the transfer starts, it is not possible to serve +# new replicas arriving, that will be queued for the next RDB transfer, so the +# server waits a delay in order to let more replicas arrive. +# +# The delay is specified in seconds, and by default is 5 seconds. To disable +# it entirely just set it to 0 seconds and the transfer will start ASAP. +repl-diskless-sync-delay 5 + +# ----------------------------------------------------------------------------- +# WARNING: RDB diskless load is experimental. Since in this setup the replica +# does not immediately store an RDB on disk, it may cause data loss during +# failovers. RDB diskless load + Redis modules not handling I/O reads may also +# cause Redis to abort in case of I/O errors during the initial synchronization +# stage with the master. Use only if your do what you are doing. +# ----------------------------------------------------------------------------- +# +# Replica can load the RDB it reads from the replication link directly from the +# socket, or store the RDB to a file and read that file after it was completely +# received from the master. +# +# In many cases the disk is slower than the network, and storing and loading +# the RDB file may increase replication time (and even increase the master's +# Copy on Write memory and salve buffers). +# However, parsing the RDB file directly from the socket may mean that we have +# to flush the contents of the current database before the full rdb was +# received. For this reason we have the following options: +# +# "disabled" - Don't use diskless load (store the rdb file to the disk first) +# "on-empty-db" - Use diskless load only when it is completely safe. +# "swapdb" - Keep a copy of the current db contents in RAM while parsing +# the data directly from the socket. note that this requires +# sufficient memory, if you don't have it, you risk an OOM kill. +repl-diskless-load disabled + +# Replicas send PINGs to server in a predefined interval. It's possible to +# change this interval with the repl_ping_replica_period option. The default +# value is 10 seconds. +# +# repl-ping-replica-period 10 + +# The following option sets the replication timeout for: +# +# 1) Bulk transfer I/O during SYNC, from the point of view of replica. +# 2) Master timeout from the point of view of replicas (data, pings). +# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings). +# +# It is important to make sure that this value is greater than the value +# specified for repl-ping-replica-period otherwise a timeout will be detected +# every time there is low traffic between the master and the replica. The default +# value is 60 seconds. +# +# repl-timeout 60 + +# Disable TCP_NODELAY on the replica socket after SYNC? +# +# If you select "yes" Redis will use a smaller number of TCP packets and +# less bandwidth to send data to replicas. But this can add a delay for +# the data to appear on the replica side, up to 40 milliseconds with +# Linux kernels using a default configuration. +# +# If you select "no" the delay for data to appear on the replica side will +# be reduced but more bandwidth will be used for replication. +# +# By default we optimize for low latency, but in very high traffic conditions +# or when the master and replicas are many hops away, turning this to "yes" may +# be a good idea. +repl-disable-tcp-nodelay no + +# Set the replication backlog size. The backlog is a buffer that accumulates +# replica data when replicas are disconnected for some time, so that when a +# replica wants to reconnect again, often a full resync is not needed, but a +# partial resync is enough, just passing the portion of data the replica +# missed while disconnected. +# +# The bigger the replication backlog, the longer the replica can endure the +# disconnect and later be able to perform a partial resynchronization. +# +# The backlog is only allocated if there is at least one replica connected. +# +# repl-backlog-size 1mb + +# After a master has no connected replicas for some time, the backlog will be +# freed. The following option configures the amount of seconds that need to +# elapse, starting from the time the last replica disconnected, for the backlog +# buffer to be freed. +# +# Note that replicas never free the backlog for timeout, since they may be +# promoted to masters later, and should be able to correctly "partially +# resynchronize" with other replicas: hence they should always accumulate backlog. +# +# A value of 0 means to never release the backlog. +# +# repl-backlog-ttl 3600 + +# The replica priority is an integer number published by Redis in the INFO +# output. It is used by Redis Sentinel in order to select a replica to promote +# into a master if the master is no longer working correctly. +# +# A replica with a low priority number is considered better for promotion, so +# for instance if there are three replicas with priority 10, 100, 25 Sentinel +# will pick the one with priority 10, that is the lowest. +# +# However a special priority of 0 marks the replica as not able to perform the +# role of master, so a replica with priority of 0 will never be selected by +# Redis Sentinel for promotion. +# +# By default the priority is 100. +replica-priority 100 + +# It is possible for a master to stop accepting writes if there are less than +# N replicas connected, having a lag less or equal than M seconds. +# +# The N replicas need to be in "online" state. +# +# The lag in seconds, that must be <= the specified value, is calculated from +# the last ping received from the replica, that is usually sent every second. +# +# This option does not GUARANTEE that N replicas will accept the write, but +# will limit the window of exposure for lost writes in case not enough replicas +# are available, to the specified number of seconds. +# +# For example to require at least 3 replicas with a lag <= 10 seconds use: +# +# min-replicas-to-write 3 +# min-replicas-max-lag 10 +# +# Setting one or the other to 0 disables the feature. +# +# By default min-replicas-to-write is set to 0 (feature disabled) and +# min-replicas-max-lag is set to 10. + +# A Redis master is able to list the address and port of the attached +# replicas in different ways. For example the "INFO replication" section +# offers this information, which is used, among other tools, by +# Redis Sentinel in order to discover replica instances. +# Another place where this info is available is in the output of the +# "ROLE" command of a master. +# +# The listed IP address and port normally reported by a replica is +# obtained in the following way: +# +# IP: The address is auto detected by checking the peer address +# of the socket used by the replica to connect with the master. +# +# Port: The port is communicated by the replica during the replication +# handshake, and is normally the port that the replica is using to +# listen for connections. +# +# However when port forwarding or Network Address Translation (NAT) is +# used, the replica may actually be reachable via different IP and port +# pairs. The following two options can be used by a replica in order to +# report to its master a specific set of IP and port, so that both INFO +# and ROLE will report those values. +# +# There is no need to use both the options if you need to override just +# the port or the IP address. +# +# replica-announce-ip 5.5.5.5 +# replica-announce-port 1234 + +############################### KEYS TRACKING ################################# + +# Redis implements server assisted support for client side caching of values. +# This is implemented using an invalidation table that remembers, using +# 16 millions of slots, what clients may have certain subsets of keys. In turn +# this is used in order to send invalidation messages to clients. Please +# check this page to understand more about the feature: +# +# https://redis.io/topics/client-side-caching +# +# When tracking is enabled for a client, all the read only queries are assumed +# to be cached: this will force Redis to store information in the invalidation +# table. When keys are modified, such information is flushed away, and +# invalidation messages are sent to the clients. However if the workload is +# heavily dominated by reads, Redis could use more and more memory in order +# to track the keys fetched by many clients. +# +# For this reason it is possible to configure a maximum fill value for the +# invalidation table. By default it is set to 1M of keys, and once this limit +# is reached, Redis will start to evict keys in the invalidation table +# even if they were not modified, just to reclaim memory: this will in turn +# force the clients to invalidate the cached values. Basically the table +# maximum size is a trade off between the memory you want to spend server +# side to track information about who cached what, and the ability of clients +# to retain cached objects in memory. +# +# If you set the value to 0, it means there are no limits, and Redis will +# retain as many keys as needed in the invalidation table. +# In the "stats" INFO section, you can find information about the number of +# keys in the invalidation table at every given moment. +# +# Note: when key tracking is used in broadcasting mode, no memory is used +# in the server side so this setting is useless. +# +# tracking-table-max-keys 1000000 + +################################## SECURITY ################################### + +# Warning: since Redis is pretty fast, an outside user can try up to +# 1 million passwords per second against a modern box. This means that you +# should use very strong passwords, otherwise they will be very easy to break. +# Note that because the password is really a shared secret between the client +# and the server, and should not be memorized by any human, the password +# can be easily a long string from /dev/urandom or whatever, so by using a +# long and unguessable password no brute force attack will be possible. + +# Redis ACL users are defined in the following format: +# +# user ... acl rules ... +# +# For example: +# +# user worker +@list +@connection ~jobs:* on >ffa9203c493aa99 +# +# The special username "default" is used for new connections. If this user +# has the "nopass" rule, then new connections will be immediately authenticated +# as the "default" user without the need of any password provided via the +# AUTH command. Otherwise if the "default" user is not flagged with "nopass" +# the connections will start in not authenticated state, and will require +# AUTH (or the HELLO command AUTH option) in order to be authenticated and +# start to work. +# +# The ACL rules that describe what a user can do are the following: +# +# on Enable the user: it is possible to authenticate as this user. +# off Disable the user: it's no longer possible to authenticate +# with this user, however the already authenticated connections +# will still work. +# + Allow the execution of that command +# - Disallow the execution of that command +# +@ Allow the execution of all the commands in such category +# with valid categories are like @admin, @set, @sortedset, ... +# and so forth, see the full list in the server.c file where +# the Redis command table is described and defined. +# The special category @all means all the commands, but currently +# present in the server, and that will be loaded in the future +# via modules. +# +|subcommand Allow a specific subcommand of an otherwise +# disabled command. Note that this form is not +# allowed as negative like -DEBUG|SEGFAULT, but +# only additive starting with "+". +# allcommands Alias for +@all. Note that it implies the ability to execute +# all the future commands loaded via the modules system. +# nocommands Alias for -@all. +# ~ Add a pattern of keys that can be mentioned as part of +# commands. For instance ~* allows all the keys. The pattern +# is a glob-style pattern like the one of KEYS. +# It is possible to specify multiple patterns. +# allkeys Alias for ~* +# resetkeys Flush the list of allowed keys patterns. +# > Add this password to the list of valid password for the user. +# For example >mypass will add "mypass" to the list. +# This directive clears the "nopass" flag (see later). +# < Remove this password from the list of valid passwords. +# nopass All the set passwords of the user are removed, and the user +# is flagged as requiring no password: it means that every +# password will work against this user. If this directive is +# used for the default user, every new connection will be +# immediately authenticated with the default user without +# any explicit AUTH command required. Note that the "resetpass" +# directive will clear this condition. +# resetpass Flush the list of allowed passwords. Moreover removes the +# "nopass" status. After "resetpass" the user has no associated +# passwords and there is no way to authenticate without adding +# some password (or setting it as "nopass" later). +# reset Performs the following actions: resetpass, resetkeys, off, +# -@all. The user returns to the same state it has immediately +# after its creation. +# +# ACL rules can be specified in any order: for instance you can start with +# passwords, then flags, or key patterns. However note that the additive +# and subtractive rules will CHANGE MEANING depending on the ordering. +# For instance see the following example: +# +# user alice on +@all -DEBUG ~* >somepassword +# +# This will allow "alice" to use all the commands with the exception of the +# DEBUG command, since +@all added all the commands to the set of the commands +# alice can use, and later DEBUG was removed. However if we invert the order +# of two ACL rules the result will be different: +# +# user alice on -DEBUG +@all ~* >somepassword +# +# Now DEBUG was removed when alice had yet no commands in the set of allowed +# commands, later all the commands are added, so the user will be able to +# execute everything. +# +# Basically ACL rules are processed left-to-right. +# +# For more information about ACL configuration please refer to +# the Redis web site at https://redis.io/topics/acl + +# ACL LOG +# +# The ACL Log tracks failed commands and authentication events associated +# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked +# by ACLs. The ACL Log is stored in memory. You can reclaim memory with +# ACL LOG RESET. Define the maximum entry length of the ACL Log below. +acllog-max-len 128 + +# Using an external ACL file +# +# Instead of configuring users here in this file, it is possible to use +# a stand-alone file just listing users. The two methods cannot be mixed: +# if you configure users here and at the same time you activate the external +# ACL file, the server will refuse to start. +# +# The format of the external ACL user file is exactly the same as the +# format that is used inside redis.conf to describe users. +# +# aclfile /etc/redis/users.acl + +# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility +# layer on top of the new ACL system. The option effect will be just setting +# the password for the default user. Clients will still authenticate using +# AUTH as usually, or more explicitly with AUTH default +# if they follow the new protocol: both will work. +# +# requirepass foobared + +# Command renaming (DEPRECATED). +# +# ------------------------------------------------------------------------ +# WARNING: avoid using this option if possible. Instead use ACLs to remove +# commands from the default user, and put them only in some admin user you +# create for administrative purposes. +# ------------------------------------------------------------------------ +# +# It is possible to change the name of dangerous commands in a shared +# environment. For instance the CONFIG command may be renamed into something +# hard to guess so that it will still be available for internal-use tools +# but not available for general clients. +# +# Example: +# +# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 +# +# It is also possible to completely kill a command by renaming it into +# an empty string: +# +# rename-command CONFIG "" +# +# Please note that changing the name of commands that are logged into the +# AOF file or transmitted to replicas may cause problems. + +################################### CLIENTS #################################### + +# Set the max number of connected clients at the same time. By default +# this limit is set to 10000 clients, however if the Redis server is not +# able to configure the process file limit to allow for the specified limit +# the max number of allowed clients is set to the current file limit +# minus 32 (as Redis reserves a few file descriptors for internal uses). +# +# Once the limit is reached Redis will close all the new connections sending +# an error 'max number of clients reached'. +# +# IMPORTANT: When Redis Cluster is used, the max number of connections is also +# shared with the cluster bus: every node in the cluster will use two +# connections, one incoming and another outgoing. It is important to size the +# limit accordingly in case of very large clusters. +# +# maxclients 10000 + +############################## MEMORY MANAGEMENT ################################ + +# Set a memory usage limit to the specified amount of bytes. +# When the memory limit is reached Redis will try to remove keys +# according to the eviction policy selected (see maxmemory-policy). +# +# If Redis can't remove keys according to the policy, or if the policy is +# set to 'noeviction', Redis will start to reply with errors to commands +# that would use more memory, like SET, LPUSH, and so on, and will continue +# to reply to read-only commands like GET. +# +# This option is usually useful when using Redis as an LRU or LFU cache, or to +# set a hard memory limit for an instance (using the 'noeviction' policy). +# +# WARNING: If you have replicas attached to an instance with maxmemory on, +# the size of the output buffers needed to feed the replicas are subtracted +# from the used memory count, so that network problems / resyncs will +# not trigger a loop where keys are evicted, and in turn the output +# buffer of replicas is full with DELs of keys evicted triggering the deletion +# of more keys, and so forth until the database is completely emptied. +# +# In short... if you have replicas attached it is suggested that you set a lower +# limit for maxmemory so that there is some free RAM on the system for replica +# output buffers (but this is not needed if the policy is 'noeviction'). +# +# maxmemory + +# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory +# is reached. You can select one from the following behaviors: +# +# volatile-lru -> Evict using approximated LRU, only keys with an expire set. +# allkeys-lru -> Evict any key using approximated LRU. +# volatile-lfu -> Evict using approximated LFU, only keys with an expire set. +# allkeys-lfu -> Evict any key using approximated LFU. +# volatile-random -> Remove a random key having an expire set. +# allkeys-random -> Remove a random key, any key. +# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) +# noeviction -> Don't evict anything, just return an error on write operations. +# +# LRU means Least Recently Used +# LFU means Least Frequently Used +# +# Both LRU, LFU and volatile-ttl are implemented using approximated +# randomized algorithms. +# +# Note: with any of the above policies, Redis will return an error on write +# operations, when there are no suitable keys for eviction. +# +# At the date of writing these commands are: set setnx setex append +# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd +# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby +# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby +# getset mset msetnx exec sort +# +# The default is: +# +# maxmemory-policy noeviction + +# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated +# algorithms (in order to save memory), so you can tune it for speed or +# accuracy. By default Redis will check five keys and pick the one that was +# used least recently, you can change the sample size using the following +# configuration directive. +# +# The default of 5 produces good enough results. 10 Approximates very closely +# true LRU but costs more CPU. 3 is faster but not very accurate. +# +# maxmemory-samples 5 + +# Starting from Redis 5, by default a replica will ignore its maxmemory setting +# (unless it is promoted to master after a failover or manually). It means +# that the eviction of keys will be just handled by the master, sending the +# DEL commands to the replica as keys evict in the master side. +# +# This behavior ensures that masters and replicas stay consistent, and is usually +# what you want, however if your replica is writable, or you want the replica +# to have a different memory setting, and you are sure all the writes performed +# to the replica are idempotent, then you may change this default (but be sure +# to understand what you are doing). +# +# Note that since the replica by default does not evict, it may end using more +# memory than the one set via maxmemory (there are certain buffers that may +# be larger on the replica, or data structures may sometimes take more memory +# and so forth). So make sure you monitor your replicas and make sure they +# have enough memory to never hit a real out-of-memory condition before the +# master hits the configured maxmemory setting. +# +# replica-ignore-maxmemory yes + +# Redis reclaims expired keys in two ways: upon access when those keys are +# found to be expired, and also in background, in what is called the +# "active expire key". The key space is slowly and interactively scanned +# looking for expired keys to reclaim, so that it is possible to free memory +# of keys that are expired and will never be accessed again in a short time. +# +# The default effort of the expire cycle will try to avoid having more than +# ten percent of expired keys still in memory, and will try to avoid consuming +# more than 25% of total memory and to add latency to the system. However +# it is possible to increase the expire "effort" that is normally set to +# "1", to a greater value, up to the value "10". At its maximum value the +# system will use more CPU, longer cycles (and technically may introduce +# more latency), and will tolerate less already expired keys still present +# in the system. It's a tradeoff between memory, CPU and latency. +# +# active-expire-effort 1 + +############################# LAZY FREEING #################################### + +# Redis has two primitives to delete keys. One is called DEL and is a blocking +# deletion of the object. It means that the server stops processing new commands +# in order to reclaim all the memory associated with an object in a synchronous +# way. If the key deleted is associated with a small object, the time needed +# in order to execute the DEL command is very small and comparable to most other +# O(1) or O(log_N) commands in Redis. However if the key is associated with an +# aggregated value containing millions of elements, the server can block for +# a long time (even seconds) in order to complete the operation. +# +# For the above reasons Redis also offers non blocking deletion primitives +# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and +# FLUSHDB commands, in order to reclaim memory in background. Those commands +# are executed in constant time. Another thread will incrementally free the +# object in the background as fast as possible. +# +# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled. +# It's up to the design of the application to understand when it is a good +# idea to use one or the other. However the Redis server sometimes has to +# delete keys or flush the whole database as a side effect of other operations. +# Specifically Redis deletes objects independently of a user call in the +# following scenarios: +# +# 1) On eviction, because of the maxmemory and maxmemory policy configurations, +# in order to make room for new data, without going over the specified +# memory limit. +# 2) Because of expire: when a key with an associated time to live (see the +# EXPIRE command) must be deleted from memory. +# 3) Because of a side effect of a command that stores data on a key that may +# already exist. For example the RENAME command may delete the old key +# content when it is replaced with another one. Similarly SUNIONSTORE +# or SORT with STORE option may delete existing keys. The SET command +# itself removes any old content of the specified key in order to replace +# it with the specified string. +# 4) During replication, when a replica performs a full resynchronization with +# its master, the content of the whole database is removed in order to +# load the RDB file just transferred. +# +# In all the above cases the default is to delete objects in a blocking way, +# like if DEL was called. However you can configure each case specifically +# in order to instead release memory in a non-blocking way like if UNLINK +# was called, using the following configuration directives. + +lazyfree-lazy-eviction no +lazyfree-lazy-expire no +lazyfree-lazy-server-del no +replica-lazy-flush no + +# It is also possible, for the case when to replace the user code DEL calls +# with UNLINK calls is not easy, to modify the default behavior of the DEL +# command to act exactly like UNLINK, using the following configuration +# directive: + +lazyfree-lazy-user-del no + +################################ THREADED I/O ################################# + +# Redis is mostly single threaded, however there are certain threaded +# operations such as UNLINK, slow I/O accesses and other things that are +# performed on side threads. +# +# Now it is also possible to handle Redis clients socket reads and writes +# in different I/O threads. Since especially writing is so slow, normally +# Redis users use pipelining in order to speed up the Redis performances per +# core, and spawn multiple instances in order to scale more. Using I/O +# threads it is possible to easily speedup two times Redis without resorting +# to pipelining nor sharding of the instance. +# +# By default threading is disabled, we suggest enabling it only in machines +# that have at least 4 or more cores, leaving at least one spare core. +# Using more than 8 threads is unlikely to help much. We also recommend using +# threaded I/O only if you actually have performance problems, with Redis +# instances being able to use a quite big percentage of CPU time, otherwise +# there is no point in using this feature. +# +# So for instance if you have a four cores boxes, try to use 2 or 3 I/O +# threads, if you have a 8 cores, try to use 6 threads. In order to +# enable I/O threads use the following configuration directive: +# +# io-threads 4 +# +# Setting io-threads to 1 will just use the main thread as usual. +# When I/O threads are enabled, we only use threads for writes, that is +# to thread the write(2) syscall and transfer the client buffers to the +# socket. However it is also possible to enable threading of reads and +# protocol parsing using the following configuration directive, by setting +# it to yes: +# +# io-threads-do-reads no +# +# Usually threading reads doesn't help much. +# +# NOTE 1: This configuration directive cannot be changed at runtime via +# CONFIG SET. Aso this feature currently does not work when SSL is +# enabled. +# +# NOTE 2: If you want to test the Redis speedup using redis-benchmark, make +# sure you also run the benchmark itself in threaded mode, using the +# --threads option to match the number of Redis threads, otherwise you'll not +# be able to notice the improvements. + +############################ KERNEL OOM CONTROL ############################## + +# On Linux, it is possible to hint the kernel OOM killer on what processes +# should be killed first when out of memory. +# +# Enabling this feature makes Redis actively control the oom_score_adj value +# for all its processes, depending on their role. The default scores will +# attempt to have background child processes killed before all others, and +# replicas killed before masters. + +oom-score-adj no + +# When oom-score-adj is used, this directive controls the specific values used +# for master, replica and background child processes. Values range -1000 to +# 1000 (higher means more likely to be killed). +# +# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities) +# can freely increase their value, but not decrease it below its initial +# settings. +# +# Values are used relative to the initial value of oom_score_adj when the server +# starts. Because typically the initial value is 0, they will often match the +# absolute values. + +oom-score-adj-values 0 200 800 + +############################## APPEND ONLY MODE ############################### + +# By default Redis asynchronously dumps the dataset on disk. This mode is +# good enough in many applications, but an issue with the Redis process or +# a power outage may result into a few minutes of writes lost (depending on +# the configured save points). +# +# The Append Only File is an alternative persistence mode that provides +# much better durability. For instance using the default data fsync policy +# (see later in the config file) Redis can lose just one second of writes in a +# dramatic event like a server power outage, or a single write if something +# wrong with the Redis process itself happens, but the operating system is +# still running correctly. +# +# AOF and RDB persistence can be enabled at the same time without problems. +# If the AOF is enabled on startup Redis will load the AOF, that is the file +# with the better durability guarantees. +# +# Please check http://redis.io/topics/persistence for more information. + +appendonly no + +# The name of the append only file (default: "appendonly.aof") + +appendfilename "appendonly.aof" + +# The fsync() call tells the Operating System to actually write data on disk +# instead of waiting for more data in the output buffer. Some OS will really flush +# data on disk, some other OS will just try to do it ASAP. +# +# Redis supports three different modes: +# +# no: don't fsync, just let the OS flush the data when it wants. Faster. +# always: fsync after every write to the append only log. Slow, Safest. +# everysec: fsync only one time every second. Compromise. +# +# The default is "everysec", as that's usually the right compromise between +# speed and data safety. It's up to you to understand if you can relax this to +# "no" that will let the operating system flush the output buffer when +# it wants, for better performances (but if you can live with the idea of +# some data loss consider the default persistence mode that's snapshotting), +# or on the contrary, use "always" that's very slow but a bit safer than +# everysec. +# +# More details please check the following article: +# http://antirez.com/post/redis-persistence-demystified.html +# +# If unsure, use "everysec". + +# appendfsync always +appendfsync everysec +# appendfsync no + +# When the AOF fsync policy is set to always or everysec, and a background +# saving process (a background save or AOF log background rewriting) is +# performing a lot of I/O against the disk, in some Linux configurations +# Redis may block too long on the fsync() call. Note that there is no fix for +# this currently, as even performing fsync in a different thread will block +# our synchronous write(2) call. +# +# In order to mitigate this problem it's possible to use the following option +# that will prevent fsync() from being called in the main process while a +# BGSAVE or BGREWRITEAOF is in progress. +# +# This means that while another child is saving, the durability of Redis is +# the same as "appendfsync none". In practical terms, this means that it is +# possible to lose up to 30 seconds of log in the worst scenario (with the +# default Linux settings). +# +# If you have latency problems turn this to "yes". Otherwise leave it as +# "no" that is the safest pick from the point of view of durability. + +no-appendfsync-on-rewrite no + +# Automatic rewrite of the append only file. +# Redis is able to automatically rewrite the log file implicitly calling +# BGREWRITEAOF when the AOF log size grows by the specified percentage. +# +# This is how it works: Redis remembers the size of the AOF file after the +# latest rewrite (if no rewrite has happened since the restart, the size of +# the AOF at startup is used). +# +# This base size is compared to the current size. If the current size is +# bigger than the specified percentage, the rewrite is triggered. Also +# you need to specify a minimal size for the AOF file to be rewritten, this +# is useful to avoid rewriting the AOF file even if the percentage increase +# is reached but it is still pretty small. +# +# Specify a percentage of zero in order to disable the automatic AOF +# rewrite feature. + +auto-aof-rewrite-percentage 100 +auto-aof-rewrite-min-size 64mb + +# An AOF file may be found to be truncated at the end during the Redis +# startup process, when the AOF data gets loaded back into memory. +# This may happen when the system where Redis is running +# crashes, especially when an ext4 filesystem is mounted without the +# data=ordered option (however this can't happen when Redis itself +# crashes or aborts but the operating system still works correctly). +# +# Redis can either exit with an error when this happens, or load as much +# data as possible (the default now) and start if the AOF file is found +# to be truncated at the end. The following option controls this behavior. +# +# If aof-load-truncated is set to yes, a truncated AOF file is loaded and +# the Redis server starts emitting a log to inform the user of the event. +# Otherwise if the option is set to no, the server aborts with an error +# and refuses to start. When the option is set to no, the user requires +# to fix the AOF file using the "redis-check-aof" utility before to restart +# the server. +# +# Note that if the AOF file will be found to be corrupted in the middle +# the server will still exit with an error. This option only applies when +# Redis will try to read more data from the AOF file but not enough bytes +# will be found. +aof-load-truncated yes + +# When rewriting the AOF file, Redis is able to use an RDB preamble in the +# AOF file for faster rewrites and recoveries. When this option is turned +# on the rewritten AOF file is composed of two different stanzas: +# +# [RDB file][AOF tail] +# +# When loading, Redis recognizes that the AOF file starts with the "REDIS" +# string and loads the prefixed RDB file, then continues loading the AOF +# tail. +aof-use-rdb-preamble yes + +################################ LUA SCRIPTING ############################### + +# Max execution time of a Lua script in milliseconds. +# +# If the maximum execution time is reached Redis will log that a script is +# still in execution after the maximum allowed time and will start to +# reply to queries with an error. +# +# When a long running script exceeds the maximum execution time only the +# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be +# used to stop a script that did not yet call any write commands. The second +# is the only way to shut down the server in the case a write command was +# already issued by the script but the user doesn't want to wait for the natural +# termination of the script. +# +# Set it to 0 or a negative value for unlimited execution without warnings. +lua-time-limit 5000 + +################################ REDIS CLUSTER ############################### + +# Normal Redis instances can't be part of a Redis Cluster; only nodes that are +# started as cluster nodes can. In order to start a Redis instance as a +# cluster node enable the cluster support uncommenting the following: +# +# cluster-enabled yes + +# Every cluster node has a cluster configuration file. This file is not +# intended to be edited by hand. It is created and updated by Redis nodes. +# Every Redis Cluster node requires a different cluster configuration file. +# Make sure that instances running in the same system do not have +# overlapping cluster configuration file names. +# +# cluster-config-file nodes-6379.conf + +# Cluster node timeout is the amount of milliseconds a node must be unreachable +# for it to be considered in failure state. +# Most other internal time limits are a multiple of the node timeout. +# +# cluster-node-timeout 15000 + +# A replica of a failing master will avoid to start a failover if its data +# looks too old. +# +# There is no simple way for a replica to actually have an exact measure of +# its "data age", so the following two checks are performed: +# +# 1) If there are multiple replicas able to failover, they exchange messages +# in order to try to give an advantage to the replica with the best +# replication offset (more data from the master processed). +# Replicas will try to get their rank by offset, and apply to the start +# of the failover a delay proportional to their rank. +# +# 2) Every single replica computes the time of the last interaction with +# its master. This can be the last ping or command received (if the master +# is still in the "connected" state), or the time that elapsed since the +# disconnection with the master (if the replication link is currently down). +# If the last interaction is too old, the replica will not try to failover +# at all. +# +# The point "2" can be tuned by user. Specifically a replica will not perform +# the failover if, since the last interaction with the master, the time +# elapsed is greater than: +# +# (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period +# +# So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor +# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the +# replica will not try to failover if it was not able to talk with the master +# for longer than 310 seconds. +# +# A large cluster-replica-validity-factor may allow replicas with too old data to failover +# a master, while a too small value may prevent the cluster from being able to +# elect a replica at all. +# +# For maximum availability, it is possible to set the cluster-replica-validity-factor +# to a value of 0, which means, that replicas will always try to failover the +# master regardless of the last time they interacted with the master. +# (However they'll always try to apply a delay proportional to their +# offset rank). +# +# Zero is the only value able to guarantee that when all the partitions heal +# the cluster will always be able to continue. +# +# cluster-replica-validity-factor 10 + +# Cluster replicas are able to migrate to orphaned masters, that are masters +# that are left without working replicas. This improves the cluster ability +# to resist to failures as otherwise an orphaned master can't be failed over +# in case of failure if it has no working replicas. +# +# Replicas migrate to orphaned masters only if there are still at least a +# given number of other working replicas for their old master. This number +# is the "migration barrier". A migration barrier of 1 means that a replica +# will migrate only if there is at least 1 other working replica for its master +# and so forth. It usually reflects the number of replicas you want for every +# master in your cluster. +# +# Default is 1 (replicas migrate only if their masters remain with at least +# one replica). To disable migration just set it to a very large value. +# A value of 0 can be set but is useful only for debugging and dangerous +# in production. +# +# cluster-migration-barrier 1 + +# By default Redis Cluster nodes stop accepting queries if they detect there +# is at least a hash slot uncovered (no available node is serving it). +# This way if the cluster is partially down (for example a range of hash slots +# are no longer covered) all the cluster becomes, eventually, unavailable. +# It automatically returns available as soon as all the slots are covered again. +# +# However sometimes you want the subset of the cluster which is working, +# to continue to accept queries for the part of the key space that is still +# covered. In order to do so, just set the cluster-require-full-coverage +# option to no. +# +# cluster-require-full-coverage yes + +# This option, when set to yes, prevents replicas from trying to failover its +# master during master failures. However the master can still perform a +# manual failover, if forced to do so. +# +# This is useful in different scenarios, especially in the case of multiple +# data center operations, where we want one side to never be promoted if not +# in the case of a total DC failure. +# +# cluster-replica-no-failover no + +# This option, when set to yes, allows nodes to serve read traffic while the +# the cluster is in a down state, as long as it believes it owns the slots. +# +# This is useful for two cases. The first case is for when an application +# doesn't require consistency of data during node failures or network partitions. +# One example of this is a cache, where as long as the node has the data it +# should be able to serve it. +# +# The second use case is for configurations that don't meet the recommended +# three shards but want to enable cluster mode and scale later. A +# master outage in a 1 or 2 shard configuration causes a read/write outage to the +# entire cluster without this option set, with it set there is only a write outage. +# Without a quorum of masters, slot ownership will not change automatically. +# +# cluster-allow-reads-when-down no + +# In order to setup your cluster make sure to read the documentation +# available at http://redis.io web site. + +########################## CLUSTER DOCKER/NAT support ######################## + +# In certain deployments, Redis Cluster nodes address discovery fails, because +# addresses are NAT-ted or because ports are forwarded (the typical case is +# Docker and other containers). +# +# In order to make Redis Cluster working in such environments, a static +# configuration where each node knows its public address is needed. The +# following two options are used for this scope, and are: +# +# * cluster-announce-ip +# * cluster-announce-port +# * cluster-announce-bus-port +# +# Each instructs the node about its address, client port, and cluster message +# bus port. The information is then published in the header of the bus packets +# so that other nodes will be able to correctly map the address of the node +# publishing the information. +# +# If the above options are not used, the normal Redis Cluster auto-detection +# will be used instead. +# +# Note that when remapped, the bus port may not be at the fixed offset of +# clients port + 10000, so you can specify any port and bus-port depending +# on how they get remapped. If the bus-port is not set, a fixed offset of +# 10000 will be used as usual. +# +# Example: +# +# cluster-announce-ip 10.1.1.5 +# cluster-announce-port 6379 +# cluster-announce-bus-port 6380 + +################################## SLOW LOG ################################### + +# The Redis Slow Log is a system to log queries that exceeded a specified +# execution time. The execution time does not include the I/O operations +# like talking with the client, sending the reply and so forth, +# but just the time needed to actually execute the command (this is the only +# stage of command execution where the thread is blocked and can not serve +# other requests in the meantime). +# +# You can configure the slow log with two parameters: one tells Redis +# what is the execution time, in microseconds, to exceed in order for the +# command to get logged, and the other parameter is the length of the +# slow log. When a new command is logged the oldest one is removed from the +# queue of logged commands. + +# The following time is expressed in microseconds, so 1000000 is equivalent +# to one second. Note that a negative number disables the slow log, while +# a value of zero forces the logging of every command. +slowlog-log-slower-than 10000 + +# There is no limit to this length. Just be aware that it will consume memory. +# You can reclaim memory used by the slow log with SLOWLOG RESET. +slowlog-max-len 128 + +################################ LATENCY MONITOR ############################## + +# The Redis latency monitoring subsystem samples different operations +# at runtime in order to collect data related to possible sources of +# latency of a Redis instance. +# +# Via the LATENCY command this information is available to the user that can +# print graphs and obtain reports. +# +# The system only logs operations that were performed in a time equal or +# greater than the amount of milliseconds specified via the +# latency-monitor-threshold configuration directive. When its value is set +# to zero, the latency monitor is turned off. +# +# By default latency monitoring is disabled since it is mostly not needed +# if you don't have latency issues, and collecting data has a performance +# impact, that while very small, can be measured under big load. Latency +# monitoring can easily be enabled at runtime using the command +# "CONFIG SET latency-monitor-threshold " if needed. +latency-monitor-threshold 0 + +############################# EVENT NOTIFICATION ############################## + +# Redis can notify Pub/Sub clients about events happening in the key space. +# This feature is documented at http://redis.io/topics/notifications +# +# For instance if keyspace events notification is enabled, and a client +# performs a DEL operation on key "foo" stored in the Database 0, two +# messages will be published via Pub/Sub: +# +# PUBLISH __keyspace@0__:foo del +# PUBLISH __keyevent@0__:del foo +# +# It is possible to select the events that Redis will notify among a set +# of classes. Every class is identified by a single character: +# +# K Keyspace events, published with __keyspace@__ prefix. +# E Keyevent events, published with __keyevent@__ prefix. +# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ... +# $ String commands +# l List commands +# s Set commands +# h Hash commands +# z Sorted set commands +# x Expired events (events generated every time a key expires) +# e Evicted events (events generated when a key is evicted for maxmemory) +# t Stream commands +# m Key-miss events (Note: It is not included in the 'A' class) +# A Alias for g$lshzxet, so that the "AKE" string means all the events +# (Except key-miss events which are excluded from 'A' due to their +# unique nature). +# +# The "notify-keyspace-events" takes as argument a string that is composed +# of zero or multiple characters. The empty string means that notifications +# are disabled. +# +# Example: to enable list and generic events, from the point of view of the +# event name, use: +# +# notify-keyspace-events Elg +# +# Example 2: to get the stream of the expired keys subscribing to channel +# name __keyevent@0__:expired use: +# +# notify-keyspace-events Ex +# +# By default all notifications are disabled because most users don't need +# this feature and the feature has some overhead. Note that if you don't +# specify at least one of K or E, no events will be delivered. +notify-keyspace-events "" + +############################### GOPHER SERVER ################################# + +# Redis contains an implementation of the Gopher protocol, as specified in +# the RFC 1436 (https://www.ietf.org/rfc/rfc1436.txt). +# +# The Gopher protocol was very popular in the late '90s. It is an alternative +# to the web, and the implementation both server and client side is so simple +# that the Redis server has just 100 lines of code in order to implement this +# support. +# +# What do you do with Gopher nowadays? Well Gopher never *really* died, and +# lately there is a movement in order for the Gopher more hierarchical content +# composed of just plain text documents to be resurrected. Some want a simpler +# internet, others believe that the mainstream internet became too much +# controlled, and it's cool to create an alternative space for people that +# want a bit of fresh air. +# +# Anyway for the 10nth birthday of the Redis, we gave it the Gopher protocol +# as a gift. +# +# --- HOW IT WORKS? --- +# +# The Redis Gopher support uses the inline protocol of Redis, and specifically +# two kind of inline requests that were anyway illegal: an empty request +# or any request that starts with "/" (there are no Redis commands starting +# with such a slash). Normal RESP2/RESP3 requests are completely out of the +# path of the Gopher protocol implementation and are served as usual as well. +# +# If you open a connection to Redis when Gopher is enabled and send it +# a string like "/foo", if there is a key named "/foo" it is served via the +# Gopher protocol. +# +# In order to create a real Gopher "hole" (the name of a Gopher site in Gopher +# talking), you likely need a script like the following: +# +# https://github.com/antirez/gopher2redis +# +# --- SECURITY WARNING --- +# +# If you plan to put Redis on the internet in a publicly accessible address +# to server Gopher pages MAKE SURE TO SET A PASSWORD to the instance. +# Once a password is set: +# +# 1. The Gopher server (when enabled, not by default) will still serve +# content via Gopher. +# 2. However other commands cannot be called before the client will +# authenticate. +# +# So use the 'requirepass' option to protect your instance. +# +# Note that Gopher is not currently supported when 'io-threads-do-reads' +# is enabled. +# +# To enable Gopher support, uncomment the following line and set the option +# from no (the default) to yes. +# +# gopher-enabled no + +############################### ADVANCED CONFIG ############################### + +# Hashes are encoded using a memory efficient data structure when they have a +# small number of entries, and the biggest entry does not exceed a given +# threshold. These thresholds can be configured using the following directives. +hash-max-ziplist-entries 512 +hash-max-ziplist-value 64 + +# Lists are also encoded in a special way to save a lot of space. +# The number of entries allowed per internal list node can be specified +# as a fixed maximum size or a maximum number of elements. +# For a fixed maximum size, use -5 through -1, meaning: +# -5: max size: 64 Kb <-- not recommended for normal workloads +# -4: max size: 32 Kb <-- not recommended +# -3: max size: 16 Kb <-- probably not recommended +# -2: max size: 8 Kb <-- good +# -1: max size: 4 Kb <-- good +# Positive numbers mean store up to _exactly_ that number of elements +# per list node. +# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size), +# but if your use case is unique, adjust the settings as necessary. +list-max-ziplist-size -2 + +# Lists may also be compressed. +# Compress depth is the number of quicklist ziplist nodes from *each* side of +# the list to *exclude* from compression. The head and tail of the list +# are always uncompressed for fast push/pop operations. Settings are: +# 0: disable all list compression +# 1: depth 1 means "don't start compressing until after 1 node into the list, +# going from either the head or tail" +# So: [head]->node->node->...->node->[tail] +# [head], [tail] will always be uncompressed; inner nodes will compress. +# 2: [head]->[next]->node->node->...->node->[prev]->[tail] +# 2 here means: don't compress head or head->next or tail->prev or tail, +# but compress all nodes between them. +# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail] +# etc. +list-compress-depth 0 + +# Sets have a special encoding in just one case: when a set is composed +# of just strings that happen to be integers in radix 10 in the range +# of 64 bit signed integers. +# The following configuration setting sets the limit in the size of the +# set in order to use this special memory saving encoding. +set-max-intset-entries 512 + +# Similarly to hashes and lists, sorted sets are also specially encoded in +# order to save a lot of space. This encoding is only used when the length and +# elements of a sorted set are below the following limits: +zset-max-ziplist-entries 128 +zset-max-ziplist-value 64 + +# HyperLogLog sparse representation bytes limit. The limit includes the +# 16 bytes header. When an HyperLogLog using the sparse representation crosses +# this limit, it is converted into the dense representation. +# +# A value greater than 16000 is totally useless, since at that point the +# dense representation is more memory efficient. +# +# The suggested value is ~ 3000 in order to have the benefits of +# the space efficient encoding without slowing down too much PFADD, +# which is O(N) with the sparse encoding. The value can be raised to +# ~ 10000 when CPU is not a concern, but space is, and the data set is +# composed of many HyperLogLogs with cardinality in the 0 - 15000 range. +hll-sparse-max-bytes 3000 + +# Streams macro node max size / items. The stream data structure is a radix +# tree of big nodes that encode multiple items inside. Using this configuration +# it is possible to configure how big a single node can be in bytes, and the +# maximum number of items it may contain before switching to a new node when +# appending new stream entries. If any of the following settings are set to +# zero, the limit is ignored, so for instance it is possible to set just a +# max entires limit by setting max-bytes to 0 and max-entries to the desired +# value. +stream-node-max-bytes 4kb +stream-node-max-entries 100 + +# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in +# order to help rehashing the main Redis hash table (the one mapping top-level +# keys to values). The hash table implementation Redis uses (see dict.c) +# performs a lazy rehashing: the more operation you run into a hash table +# that is rehashing, the more rehashing "steps" are performed, so if the +# server is idle the rehashing is never complete and some more memory is used +# by the hash table. +# +# The default is to use this millisecond 10 times every second in order to +# actively rehash the main dictionaries, freeing memory when possible. +# +# If unsure: +# use "activerehashing no" if you have hard latency requirements and it is +# not a good thing in your environment that Redis can reply from time to time +# to queries with 2 milliseconds delay. +# +# use "activerehashing yes" if you don't have such hard requirements but +# want to free memory asap when possible. +activerehashing yes + +# The client output buffer limits can be used to force disconnection of clients +# that are not reading data from the server fast enough for some reason (a +# common reason is that a Pub/Sub client can't consume messages as fast as the +# publisher can produce them). +# +# The limit can be set differently for the three different classes of clients: +# +# normal -> normal clients including MONITOR clients +# replica -> replica clients +# pubsub -> clients subscribed to at least one pubsub channel or pattern +# +# The syntax of every client-output-buffer-limit directive is the following: +# +# client-output-buffer-limit +# +# A client is immediately disconnected once the hard limit is reached, or if +# the soft limit is reached and remains reached for the specified number of +# seconds (continuously). +# So for instance if the hard limit is 32 megabytes and the soft limit is +# 16 megabytes / 10 seconds, the client will get disconnected immediately +# if the size of the output buffers reach 32 megabytes, but will also get +# disconnected if the client reaches 16 megabytes and continuously overcomes +# the limit for 10 seconds. +# +# By default normal clients are not limited because they don't receive data +# without asking (in a push way), but just after a request, so only +# asynchronous clients may create a scenario where data is requested faster +# than it can read. +# +# Instead there is a default limit for pubsub and replica clients, since +# subscribers and replicas receive data in a push fashion. +# +# Both the hard or the soft limit can be disabled by setting them to zero. +client-output-buffer-limit normal 0 0 0 +client-output-buffer-limit replica 256mb 64mb 60 +client-output-buffer-limit pubsub 32mb 8mb 60 + +# Client query buffers accumulate new commands. They are limited to a fixed +# amount by default in order to avoid that a protocol desynchronization (for +# instance due to a bug in the client) will lead to unbound memory usage in +# the query buffer. However you can configure it here if you have very special +# needs, such us huge multi/exec requests or alike. +# +# client-query-buffer-limit 1gb + +# In the Redis protocol, bulk requests, that are, elements representing single +# strings, are normally limited to 512 mb. However you can change this limit +# here, but must be 1mb or greater +# +# proto-max-bulk-len 512mb + +# Redis calls an internal function to perform many background tasks, like +# closing connections of clients in timeout, purging expired keys that are +# never requested, and so forth. +# +# Not all tasks are performed with the same frequency, but Redis checks for +# tasks to perform according to the specified "hz" value. +# +# By default "hz" is set to 10. Raising the value will use more CPU when +# Redis is idle, but at the same time will make Redis more responsive when +# there are many keys expiring at the same time, and timeouts may be +# handled with more precision. +# +# The range is between 1 and 500, however a value over 100 is usually not +# a good idea. Most users should use the default of 10 and raise this up to +# 100 only in environments where very low latency is required. +hz 10 + +# Normally it is useful to have an HZ value which is proportional to the +# number of clients connected. This is useful in order, for instance, to +# avoid too many clients are processed for each background task invocation +# in order to avoid latency spikes. +# +# Since the default HZ value by default is conservatively set to 10, Redis +# offers, and enables by default, the ability to use an adaptive HZ value +# which will temporarily raise when there are many connected clients. +# +# When dynamic HZ is enabled, the actual configured HZ will be used +# as a baseline, but multiples of the configured HZ value will be actually +# used as needed once more clients are connected. In this way an idle +# instance will use very little CPU time while a busy instance will be +# more responsive. +dynamic-hz yes + +# When a child rewrites the AOF file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +aof-rewrite-incremental-fsync yes + +# When redis saves RDB file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +rdb-save-incremental-fsync yes + +# Redis LFU eviction (see maxmemory setting) can be tuned. However it is a good +# idea to start with the default settings and only change them after investigating +# how to improve the performances and how the keys LFU change over time, which +# is possible to inspect via the OBJECT FREQ command. +# +# There are two tunable parameters in the Redis LFU implementation: the +# counter logarithm factor and the counter decay time. It is important to +# understand what the two parameters mean before changing them. +# +# The LFU counter is just 8 bits per key, it's maximum value is 255, so Redis +# uses a probabilistic increment with logarithmic behavior. Given the value +# of the old counter, when a key is accessed, the counter is incremented in +# this way: +# +# 1. A random number R between 0 and 1 is extracted. +# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1). +# 3. The counter is incremented only if R < P. +# +# The default lfu-log-factor is 10. This is a table of how the frequency +# counter changes with a different number of accesses with different +# logarithmic factors: +# +# +--------+------------+------------+------------+------------+------------+ +# | factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits | +# +--------+------------+------------+------------+------------+------------+ +# | 0 | 104 | 255 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 1 | 18 | 49 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 10 | 10 | 18 | 142 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 100 | 8 | 11 | 49 | 143 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# +# NOTE: The above table was obtained by running the following commands: +# +# redis-benchmark -n 1000000 incr foo +# redis-cli object freq foo +# +# NOTE 2: The counter initial value is 5 in order to give new objects a chance +# to accumulate hits. +# +# The counter decay time is the time, in minutes, that must elapse in order +# for the key counter to be divided by two (or decremented if it has a value +# less <= 10). +# +# The default value for the lfu-decay-time is 1. A special value of 0 means to +# decay the counter every time it happens to be scanned. +# +# lfu-log-factor 10 +# lfu-decay-time 1 + +########################### ACTIVE DEFRAGMENTATION ####################### +# +# What is active defragmentation? +# ------------------------------- +# +# Active (online) defragmentation allows a Redis server to compact the +# spaces left between small allocations and deallocations of data in memory, +# thus allowing to reclaim back memory. +# +# Fragmentation is a natural process that happens with every allocator (but +# less so with Jemalloc, fortunately) and certain workloads. Normally a server +# restart is needed in order to lower the fragmentation, or at least to flush +# away all the data and create it again. However thanks to this feature +# implemented by Oran Agra for Redis 4.0 this process can happen at runtime +# in a "hot" way, while the server is running. +# +# Basically when the fragmentation is over a certain level (see the +# configuration options below) Redis will start to create new copies of the +# values in contiguous memory regions by exploiting certain specific Jemalloc +# features (in order to understand if an allocation is causing fragmentation +# and to allocate it in a better place), and at the same time, will release the +# old copies of the data. This process, repeated incrementally for all the keys +# will cause the fragmentation to drop back to normal values. +# +# Important things to understand: +# +# 1. This feature is disabled by default, and only works if you compiled Redis +# to use the copy of Jemalloc we ship with the source code of Redis. +# This is the default with Linux builds. +# +# 2. You never need to enable this feature if you don't have fragmentation +# issues. +# +# 3. Once you experience fragmentation, you can enable this feature when +# needed with the command "CONFIG SET activedefrag yes". +# +# The configuration parameters are able to fine tune the behavior of the +# defragmentation process. If you are not sure about what they mean it is +# a good idea to leave the defaults untouched. + +# Enabled active defragmentation +# activedefrag no + +# Minimum amount of fragmentation waste to start active defrag +# active-defrag-ignore-bytes 100mb + +# Minimum percentage of fragmentation to start active defrag +# active-defrag-threshold-lower 10 + +# Maximum percentage of fragmentation at which we use maximum effort +# active-defrag-threshold-upper 100 + +# Minimal effort for defrag in CPU percentage, to be used when the lower +# threshold is reached +# active-defrag-cycle-min 1 + +# Maximal effort for defrag in CPU percentage, to be used when the upper +# threshold is reached +# active-defrag-cycle-max 25 + +# Maximum number of set/hash/zset/list fields that will be processed from +# the main dictionary scan +# active-defrag-max-scan-fields 1000 + +# Jemalloc background thread for purging will be enabled by default +jemalloc-bg-thread yes + +# It is possible to pin different threads and processes of Redis to specific +# CPUs in your system, in order to maximize the performances of the server. +# This is useful both in order to pin different Redis threads in different +# CPUs, but also in order to make sure that multiple Redis instances running +# in the same host will be pinned to different CPUs. +# +# Normally you can do this using the "taskset" command, however it is also +# possible to this via Redis configuration directly, both in Linux and FreeBSD. +# +# You can pin the server/IO threads, bio threads, aof rewrite child process, and +# the bgsave child process. The syntax to specify the cpu list is the same as +# the taskset command: +# +# Set redis server/io threads to cpu affinity 0,2,4,6: +# server_cpulist 0-7:2 +# +# Set bio threads to cpu affinity 1,3: +# bio_cpulist 1,3 +# +# Set aof rewrite child process to cpu affinity 8,9,10,11: +# aof_rewrite_cpulist 8-11 +# +# Set bgsave child process to cpu affinity 1,10,11 +# bgsave_cpulist 1,10-11 +# Generated by CONFIG REWRITE +user default on nopass sanitize-payload ~* &* +@all + diff --git a/08cache/ha/conf/sentinel0.conf b/08cache/ha/conf/sentinel0.conf new file mode 100644 index 00000000..eb9f8db1 --- /dev/null +++ b/08cache/ha/conf/sentinel0.conf @@ -0,0 +1,14 @@ +sentinel myid 8d992c54df8f8677b0b345825f61fb733c73d14c +sentinel deny-scripts-reconfig yes +sentinel monitor mymaster 127.0.0.1 6380 1 +sentinel down-after-milliseconds mymaster 2000 +# Generated by CONFIG REWRITE +protected-mode no +port 26379 +user default on nopass sanitize-payload ~* &* +@all +dir "/Users/kimmking/kimmking/JavaCourseCodes/08cache/ha/conf" +sentinel config-epoch mymaster 4 +sentinel leader-epoch mymaster 5 +sentinel known-sentinel mymaster 127.0.0.1 26380 8d992c54df8f8677b0b345825f61fb733c73d14d +sentinel current-epoch 5 +sentinel known-replica mymaster 127.0.0.1 6379 diff --git a/08cache/ha/conf/sentinel1.conf b/08cache/ha/conf/sentinel1.conf new file mode 100644 index 00000000..0c35a846 --- /dev/null +++ b/08cache/ha/conf/sentinel1.conf @@ -0,0 +1,14 @@ +sentinel myid 8d992c54df8f8677b0b345825f61fb733c73d14d +sentinel deny-scripts-reconfig yes +sentinel monitor mymaster 127.0.0.1 6380 1 +port 26380 +sentinel down-after-milliseconds mymaster 2000 +# Generated by CONFIG REWRITE +protected-mode no +user default on nopass sanitize-payload ~* &* +@all +dir "/Users/kimmking/kimmking/JavaCourseCodes/08cache/ha/conf" +sentinel config-epoch mymaster 4 +sentinel leader-epoch mymaster 5 +sentinel known-sentinel mymaster 127.0.0.1 26379 8d992c54df8f8677b0b345825f61fb733c73d14c +sentinel current-epoch 5 +sentinel known-replica mymaster 127.0.0.1 6379 diff --git a/07rpc/rpc01/rpcfx-server/pom.xml b/08cache/redis/pom.xml similarity index 59% rename from 07rpc/rpc01/rpcfx-server/pom.xml rename to 08cache/redis/pom.xml index 099e8107..6f0603dd 100644 --- a/07rpc/rpc01/rpcfx-server/pom.xml +++ b/08cache/redis/pom.xml @@ -3,43 +3,64 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - io.kimmking - rpcfx - 0.0.1-SNAPSHOT - + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + - io.kimmking - rpcfx-server + io.kimmking.08cache + redis 0.0.1-SNAPSHOT - rpcfx-server + redis + Demo project for Spring Boot 1.8 - - io.kimmking - rpcfx-api - ${project.version} - org.springframework.boot spring-boot-starter - org.springframework.boot spring-boot-starter-web + + org.projectlombok + lombok + + + + redis.clients + jedis + + com.alibaba fastjson 1.2.70 + + org.redisson + redisson-spring-boot-starter + 3.14.1 + + + + com.hazelcast + hazelcast + + + + org.springframework.boot + spring-boot-starter-data-redis + + org.springframework.boot spring-boot-starter-test @@ -61,5 +82,4 @@ - diff --git a/08cache/redis/src/main/java/io/kimmking/cache/RedisApplication.java b/08cache/redis/src/main/java/io/kimmking/cache/RedisApplication.java new file mode 100644 index 00000000..f685a7b8 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/RedisApplication.java @@ -0,0 +1,74 @@ +package io.kimmking.cache; + +import com.alibaba.fastjson.JSON; +import io.kimmking.cache.cluster.ClusterJedis; +import io.kimmking.cache.sentinel.SentinelJedis; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisCluster; + +import java.util.List; +import java.util.Map; + +@SpringBootApplication(scanBasePackages = "io.kimmking.cache") +public class RedisApplication { + + public static void main(String[] args) { + + // C1.最简单demo + Jedis jedis = new Jedis("localhost", 6379); + System.out.println(jedis.info()); + jedis.set("uptime", new Long(System.currentTimeMillis()).toString()); + System.out.println(jedis.get("uptime")); + jedis.set("teacher", "Cuijing"); + System.out.println(jedis.get("teacher")); +// +// // C2.基于sentinel和连接池的demo +// Jedis sjedis = SentinelJedis.getJedis(); +// System.out.println(sjedis.info()); +// sjedis.set("uptime2", new Long(System.currentTimeMillis()).toString()); +// System.out.println(sjedis.get("uptime2")); +// SentinelJedis.close(); +// +// // C3. 直接连接sentinel进行操作 +// Jedis jedisSentinel = new Jedis("localhost", 26379); // 连接到sentinel +// List> masters = jedisSentinel.sentinelMasters(); +// System.out.println(JSON.toJSONString(masters)); +// List> slaves = jedisSentinel.sentinelSlaves("mymaster"); +// System.out.println(JSON.toJSONString(slaves)); + + + // 作业: + // 1. 参考C2,实现基于Lettuce和Redission的Sentinel配置 + // 2. 实现springboot/spring data redis的sentinel配置 + // 3. 使用jedis命令,使用java代码手动切换 redis 主从 + // Jedis jedis1 = new Jedis("localhost", 6379); + // jedis1.info... + // jedis1.set xxx... + // Jedis jedis2 = new Jedis("localhost", 6380); + // jedis2.slaveof... + // jedis2.get xxx + // 4. 使用C3的方式,使用java代码手动操作sentinel + + + // C4. Redis Cluster + // 作业: + // 5.使用命令行配置Redis cluster: + // 1) 以cluster方式启动redis-server + // 2) 用meet,添加cluster节点,确认集群节点数目 + // 3) 分配槽位,确认分配成功 + // 4) 测试简单的get/set是否成功 + // 然后运行如下代码 +// JedisCluster cluster = ClusterJedis.getJedisCluster(); +// for (int i = 0; i < 100; i++) { +// cluster.set("cluster:" + i, "data:" + i); +// } +// System.out.println(cluster.get("cluster:92")); +// ClusterJedis.close(); + + //SpringApplication.run(RedisApplication.class, args); + + } + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/cluster/ClusterJedis.java b/08cache/redis/src/main/java/io/kimmking/cache/cluster/ClusterJedis.java new file mode 100644 index 00000000..162284f7 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/cluster/ClusterJedis.java @@ -0,0 +1,48 @@ +package io.kimmking.cache.cluster; + +import lombok.SneakyThrows; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.JedisPoolConfig; + +import java.util.HashSet; +import java.util.Set; + +public final class ClusterJedis { + + private static JedisCluster CLUSTER = createJedisCluster(); + + private static JedisCluster createJedisCluster() { + JedisCluster jedisCluster = null; + // 添加集群的服务节点Set集合 + Set hostAndPortsSet = new HashSet(); + // 添加节点 + hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6379)); + hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6380)); + // hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6381)); + + // Jedis连接池配置 + JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); + // 最大空闲连接数, 默认8个 + jedisPoolConfig.setMaxIdle(12); + // 最大连接数, 默认8个 + jedisPoolConfig.setMaxTotal(16); + //最小空闲连接数, 默认0 + jedisPoolConfig.setMinIdle(4); + // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1 + jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒 + //对拿到的connection进行validateObject校验 + jedisPoolConfig.setTestOnBorrow(true); + jedisCluster = new JedisCluster(hostAndPortsSet, jedisPoolConfig); + return jedisCluster; + } + + public static JedisCluster getJedisCluster() { + return CLUSTER; + } + + @SneakyThrows + public static void close(){ + CLUSTER.close(); + } +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/controller/UserController.java b/08cache/redis/src/main/java/io/kimmking/cache/controller/UserController.java new file mode 100644 index 00000000..3bbbf988 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/controller/UserController.java @@ -0,0 +1,26 @@ +package io.kimmking.cache.controller; + +import io.kimmking.cache.entity.User; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RestController +@EnableAutoConfiguration +public class UserController { + + + @RequestMapping("/user/find") + User find(Integer id) { + return new User(1,"KK", 28); + } + + @RequestMapping("/user/list") + List list() { + return Arrays.asList(new User(1,"KK", 28), + new User(2,"CC", 18)); + } +} \ No newline at end of file diff --git a/08cache/redis/src/main/java/io/kimmking/cache/entity/User.java b/08cache/redis/src/main/java/io/kimmking/cache/entity/User.java new file mode 100644 index 00000000..e3b5dadd --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/entity/User.java @@ -0,0 +1,14 @@ +package io.kimmking.cache.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User { + private Integer id; + private String name; + private Integer age; +} \ No newline at end of file diff --git a/08cache/redis/src/main/java/io/kimmking/cache/hazelcast/HazelcastDemo.java b/08cache/redis/src/main/java/io/kimmking/cache/hazelcast/HazelcastDemo.java new file mode 100644 index 00000000..919bd90b --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/hazelcast/HazelcastDemo.java @@ -0,0 +1,9 @@ +package io.kimmking.cache.hazelcast; + +public class HazelcastDemo { + + public static void main(String[] args) { + + } + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo.java b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo.java new file mode 100644 index 00000000..bd86f2fb --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo.java @@ -0,0 +1,46 @@ +package io.kimmking.cache.redission; + +import lombok.SneakyThrows; +import org.redisson.Redisson; +import org.redisson.RedissonMap; +import org.redisson.api.RLock; +import org.redisson.api.RMap; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; + +public class RedissionDemo { + + @SneakyThrows + public static void main(String[] args) { + Config config = new Config(); + config.useSingleServer().setAddress("redis://127.0.0.1:6379"); + + final RedissonClient client = Redisson.create(config); + RMap rmap = client.getMap("map1"); + RLock lock = client.getLock("lock1"); + + try{ + lock.lock(); + + for (int i = 0; i < 15; i++) { + rmap.put("rkey:"+i, "111rvalue:"+i); + } + + // 如果代码块 W1 在这里会怎么样? + // 代码块 W1 + while(true) { + Thread.sleep(2000); + System.out.println(rmap.get("rkey:10")); + } + + }finally{ + lock.unlock(); + } + + + + } + + // 可参阅:https://www.jianshu.com/p/47fd7f86c848 + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo1.java b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo1.java new file mode 100644 index 00000000..8534b3c8 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo1.java @@ -0,0 +1,34 @@ +package io.kimmking.cache.redission; + +import org.redisson.Redisson; +import org.redisson.api.RLock; +import org.redisson.api.RMap; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; + +public class RedissionDemo1 { + + public static void main(String[] args) { + Config config = new Config(); + config.useSingleServer().setAddress("redis://127.0.0.1:6379"); + + final RedissonClient client = Redisson.create(config); + RLock lock = client.getLock("lock1"); + + try{ + lock.lock(); + + RMap rmap = client.getMap("map1"); + + for (int i = 0; i < 15; i++) { + rmap.put("rkey:"+i, "rvalue:22222-"+i); + } + + System.out.println(rmap.get("rkey:10")); + + }finally{ + lock.unlock(); + } + } + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/sentinel/SentinelJedis.java b/08cache/redis/src/main/java/io/kimmking/cache/sentinel/SentinelJedis.java new file mode 100644 index 00000000..79a6819c --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/sentinel/SentinelJedis.java @@ -0,0 +1,66 @@ +package io.kimmking.cache.sentinel; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.JedisSentinelPool; + +import java.util.HashSet; +import java.util.Set; + +public final class SentinelJedis { + + //可用连接实例的最大数目,默认为8; + //如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽) + private static Integer MAX_TOTAL = 16; + //控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8 + private static Integer MAX_IDLE = 12; + //等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。 + //如果超过等待时间,则直接抛出JedisConnectionException + private static Integer MAX_WAIT_MILLIS = 10000; + //客户端超时时间配置 + private static Integer TIMEOUT = 10000; + //在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作; + //如果为true,则得到的jedis实例均是可用的 + private static Boolean TEST_ON_BORROW = true; + //在空闲时检查有效性, 默认false + private static Boolean TEST_WHILE_IDLE = true; + //是否进行有效性检查 + private static Boolean TEST_ON_RETURN = true; + + private static JedisSentinelPool POOL = createJedisPool(); + + /** + * 创建连接池 + */ + private static JedisSentinelPool createJedisPool() { + JedisPoolConfig config = new JedisPoolConfig(); + /*注意: + 在高版本的jedis jar包,比如本版本2.9.0,JedisPoolConfig没有setMaxActive和setMaxWait属性了 + 这是因为高版本中官方废弃了此方法,用以下两个属性替换。 + maxActive ==> maxTotal + maxWait==> maxWaitMillis + */ + config.setMaxTotal(MAX_TOTAL); + config.setMaxIdle(MAX_IDLE); + config.setMaxWaitMillis(MAX_WAIT_MILLIS); + config.setTestOnBorrow(TEST_ON_BORROW); + config.setTestWhileIdle(TEST_WHILE_IDLE); + config.setTestOnReturn(TEST_ON_RETURN); + String masterName = "mymaster"; + Set sentinels = new HashSet(); + sentinels.add(new HostAndPort("127.0.0.1",26379).toString()); + sentinels.add(new HostAndPort("127.0.0.1",26380).toString()); + JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels, config, TIMEOUT, null); + return pool; + } + + public static Jedis getJedis() { + return POOL.getResource(); + } + + public static void close(){ + POOL.close(); + } + +} diff --git a/08cache/redis/src/main/resources/application.yml b/08cache/redis/src/main/resources/application.yml new file mode 100644 index 00000000..673cb414 --- /dev/null +++ b/08cache/redis/src/main/resources/application.yml @@ -0,0 +1,10 @@ +server: + port: 8080 + +logging: + level: + io: + kimmking: + cache : info + +# 作业,在这里使用spring boot配置各项内容, diff --git a/09mq/.gitignore b/09mq/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/.mvn/wrapper/maven-wrapper.jar b/09mq/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/.mvn/wrapper/maven-wrapper.properties b/09mq/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/activemq-demo/mvnw b/09mq/activemq-demo/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/activemq-demo/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/activemq-demo/mvnw.cmd b/09mq/activemq-demo/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/activemq-demo/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/activemq-demo/pom.xml b/09mq/activemq-demo/pom.xml new file mode 100644 index 00000000..c3352476 --- /dev/null +++ b/09mq/activemq-demo/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.0.RELEASE + + + io.kimmking.javacourse.mq + activemq-demo + 0.0.1-SNAPSHOT + activemq-demo + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.apache.activemq + activemq-all + 5.16.0 + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActiveMQServer.java b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActiveMQServer.java new file mode 100644 index 00000000..b2b6a252 --- /dev/null +++ b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActiveMQServer.java @@ -0,0 +1,9 @@ +package io.kimmking.javacourse.mq.activemq; + +public class ActiveMQServer { + + public static void main(String[] args) { + // 尝试用java代码启动一个ActiveMQ broker server + // 然后用前面的测试demo代码,连接这个嵌入式的server + } +} diff --git a/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActivemqApplication.java b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActivemqApplication.java new file mode 100644 index 00000000..834f015f --- /dev/null +++ b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActivemqApplication.java @@ -0,0 +1,74 @@ +package io.kimmking.javacourse.mq.activemq; + +import org.apache.activemq.ActiveMQConnection; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.command.ActiveMQQueue; +import org.apache.activemq.command.ActiveMQTopic; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import javax.jms.*; +import java.util.concurrent.atomic.AtomicInteger; + + +//@SpringBootApplication +public class ActivemqApplication { + + public static void main(String[] args) { + + // 定义Destination + Destination destination = new ActiveMQTopic("test.topic"); + // Destination destination = new ActiveMQQueue("test.queue"); + + testDestination(destination); + + //SpringApplication.run(ActivemqApplication.class, args); + } + + public static void testDestination(Destination destination) { + try { + // 创建连接和会话 + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); + ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection(); + conn.start(); + Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // 创建消费者 + MessageConsumer consumer = session.createConsumer( destination ); + final AtomicInteger count = new AtomicInteger(0); + MessageListener listener = new MessageListener() { + public void onMessage(Message message) { + try { + // 打印所有的消息内容 + // Thread.sleep(); + System.out.println(count.incrementAndGet() + " => receive from " + destination.toString() + ": " + message); + // message.acknowledge(); // 前面所有未被确认的消息全部都确认。 + + } catch (Exception e) { + e.printStackTrace(); // 不要吞任何这里的异常, + } + } + }; + // 绑定消息监听器 + consumer.setMessageListener(listener); + + //consumer.receive() + + // 创建生产者,生产100个消息 + MessageProducer producer = session.createProducer(destination); + int index = 0; + while (index++ < 100) { + TextMessage message = session.createTextMessage(index + " message."); + producer.send(message); + } + + Thread.sleep(20000); + session.close(); + conn.close(); + + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/09mq/activemq-demo/src/main/resources/application.properties b/09mq/activemq-demo/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/activemq-demo/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/activemq-demo/src/test/java/io/kimmking/javacourse/mq/activemq/ActivemqApplicationTests.java b/09mq/activemq-demo/src/test/java/io/kimmking/javacourse/mq/activemq/ActivemqApplicationTests.java new file mode 100644 index 00000000..b4559d51 --- /dev/null +++ b/09mq/activemq-demo/src/test/java/io/kimmking/javacourse/mq/activemq/ActivemqApplicationTests.java @@ -0,0 +1,13 @@ +//package io.kimmking.javacourse.mq.activemq; +// +//import org.junit.jupiter.api.Test; +//import org.springframework.boot.test.context.SpringBootTest; +// +//@SpringBootTest +//class ActivemqApplicationTests { +// +// @Test +// void contextLoads() { +// } +// +//} diff --git a/09mq/kafka-demo/.gitignore b/09mq/kafka-demo/.gitignore new file mode 100644 index 00000000..868f9075 --- /dev/null +++ b/09mq/kafka-demo/.gitignore @@ -0,0 +1,26 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +.idea +*.iml diff --git a/09mq/kafka-demo/LICENSE b/09mq/kafka-demo/LICENSE new file mode 100644 index 00000000..261eeb9e --- /dev/null +++ b/09mq/kafka-demo/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/09mq/kafka-demo/README.md b/09mq/kafka-demo/README.md new file mode 100644 index 00000000..e0bb2acc --- /dev/null +++ b/09mq/kafka-demo/README.md @@ -0,0 +1,37 @@ +# 消息队列培训-Kafka进阶 + +## 作业:具体场景下的Kafka接口设计实现 + +### 背景 +请各位同学结合定序 & 撮合的业务特点, 练习 Producer + Consuer 的用法: +- 定序要求: 数据有序并且不丢失 +- 撮合要求: 可重放, 顺序消费, 消息仅处理一次 + +### 消息格式 +消息的内容格式为:// 见本仓库的代码 +``` +public class Order{ +private Long id; +private Long ts; +private String symbol; +private Double price; + +// 省略setter getter... +} +``` + +### 作业要求 +1. `Producer`接口应该有序发送消息 +2. `Producer`接口应该不丢失消息 +3. `Consumer`接口应该顺序消费消息 +4. `Consumer`接口应该支持从offset重新接收消息 +5. `Consumer`接口应该支持消息的幂等处理,即根据id去重 +6. 设计挑战:尝试接口对外暴露的使用方式,完全屏蔽了kafka的原生接口和类 +7. 编码挑战:实现程序都正确,而且写了单元测试 + +### 提交方式:过程分(3分) +1. 设计一个 `io.kimmking.javacourse.自己姓名拼音.Producer` 接口,需满足以上要求,并尝试实现 +2. 设计一个 `io.kimmking.javacourse.自己姓名拼音.Consumer` 接口,需满足以上要求,并尝试实现 +3. 提交以上代码,并提交一个Pull Request到本仓库,在钉钉群回复已经完成,1分 + + diff --git a/09mq/kafka-demo/pom.xml b/09mq/kafka-demo/pom.xml new file mode 100644 index 00000000..87efa723 --- /dev/null +++ b/09mq/kafka-demo/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + io.kimmking.javacourse + kafka-demo + 0.0.1-SNAPSHOT + jar + + kafka-demo + Demo project for training-mq-03 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + + + + org.apache.kafka + kafka_2.12 + 2.6.0 + + + + org.apache.kafka + kafka-clients + 2.6.0 + + + + com.alibaba + fastjson + 1.2.58 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaConsumerDemo.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaConsumerDemo.java new file mode 100644 index 00000000..a950facc --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaConsumerDemo.java @@ -0,0 +1,16 @@ +package io.kimmking.javacourse.kafka; + +import io.kimmking.javacourse.kafka.kimmking.ConsumerImpl; + +public class KafkaConsumerDemo { + + public static void main(String[] args) { + testConsumer(); + } + + private static void testConsumer() { + ConsumerImpl consumer = new ConsumerImpl(); + consumer.consumeOrder(); + + } +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaProducerDemo.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaProducerDemo.java new file mode 100644 index 00000000..1467492a --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaProducerDemo.java @@ -0,0 +1,18 @@ +package io.kimmking.javacourse.kafka; + +import io.kimmking.javacourse.kafka.kimmking.ProducerImpl; + +public class KafkaProducerDemo { + + public static void main(String[] args) { + testProducer(); + } + + private static void testProducer() { + ProducerImpl producer = new ProducerImpl(); + for (int i = 0; i < 1000; i++) { + producer.send(new Order(1000L + i,System.currentTimeMillis(),"USD2CNY", 6.5d)); + producer.send(new Order(2000L + i,System.currentTimeMillis(),"USD2CNY", 6.51d)); + } + } +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/Order.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/Order.java new file mode 100644 index 00000000..43af1c02 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/Order.java @@ -0,0 +1,19 @@ +package io.kimmking.javacourse.kafka; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Order { // 此类型为需要使用的消息内容 + + private Long id; + private Long ts; + private String symbol; + private Double price; + +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Consumer.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Consumer.java new file mode 100644 index 00000000..2c081d92 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Consumer.java @@ -0,0 +1,15 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import io.kimmking.javacourse.kafka.Order; + +public interface Consumer { + + void consumeOrder(); + + void close(); + + // add your interface method here + + // and then implement it + +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ConsumerImpl.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ConsumerImpl.java new file mode 100644 index 00000000..611c4de2 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ConsumerImpl.java @@ -0,0 +1,79 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import com.alibaba.fastjson.JSON; +import io.kimmking.javacourse.kafka.Order; +import org.apache.kafka.clients.consumer.*; +import org.apache.kafka.common.TopicPartition; + +import java.time.Duration; +import java.util.*; + +public class ConsumerImpl implements Consumer { + private Properties properties; + private KafkaConsumer consumer; + private final String topic = "order-test1"; + private Map currentOffsets = new HashMap<>(); + private Set orderSet = new HashSet<>(); + private volatile boolean flag = true; + + public ConsumerImpl() { + properties = new Properties(); +// properties.put("enable.auto.commit", false); +// properties.put("isolation.level", "read_committed"); +// properties.put("auto.offset.reset", "latest"); + properties.put("group.id", "java1-kimmking"); + properties.put("bootstrap.servers", "localhost:9092"); + properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); + properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); + consumer = new KafkaConsumer(properties); + } + + @Override + public void consumeOrder() { + + consumer.subscribe(Collections.singletonList(topic)); + + try { + while (true) { //拉取数据 + ConsumerRecords poll = consumer.poll(Duration.ofSeconds(1)); + + for (ConsumerRecord o : poll) { + ConsumerRecord record = (ConsumerRecord) o; + Order order = JSON.parseObject(record.value(), Order.class); + System.out.println(" order = " + order); +// deduplicationOrder(order); +// currentOffsets.put(new TopicPartition(record.topic(), record.partition()), +// new OffsetAndMetadata(record.offset() + 1, "no matadata")); +// consumer.commitAsync(currentOffsets, new OffsetCommitCallback() { +// @Override +// public void onComplete(Map offsets, Exception exception) { +// if (exception != null) { +// exception.printStackTrace(); +// } +// } +// }); + } + } + } catch (CommitFailedException e) { + e.printStackTrace(); + } finally { + try { + consumer.commitSync();//currentOffsets); + } catch (Exception e) { + consumer.close(); + } + } + } + + @Override + public void close() { + if (this.flag) { + this.flag = false; + } + consumer.close(); + } + + private void deduplicationOrder(Order order) { + orderSet.add(order.getId().toString()); + } +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Producer.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Producer.java new file mode 100644 index 00000000..2bf2d937 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Producer.java @@ -0,0 +1,15 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import io.kimmking.javacourse.kafka.Order; + +public interface Producer { + + void send(Order order); + + void close(); + + // add your interface method here + + // and then implement it + +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ProducerImpl.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ProducerImpl.java new file mode 100644 index 00000000..1c069263 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ProducerImpl.java @@ -0,0 +1,55 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import io.kimmking.javacourse.kafka.Order; + +import com.alibaba.fastjson.JSON; +import kafka.common.KafkaException; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerRecord; + +import java.util.Properties; + +public class ProducerImpl implements Producer { + private Properties properties; + private KafkaProducer producer; + private final String topic = "order-test1"; + + public ProducerImpl() { + properties = new Properties(); + properties.put("bootstrap.servers", "localhost:9092"); +// properties.put("queue.enqueue.timeout.ms", -1); +// properties.put("enable.idempotence", true); +// properties.put("transactional.id", "transactional_1"); +// properties.put("acks", "all"); +// properties.put("retries", "3"); +// properties.put("max.in.flight.requests.per.connection", 1); + properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); + properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); + producer = new KafkaProducer(properties); + //producer.initTransactions(); + } + + @Override + public void send(Order order) { + try { + //producer.beginTransaction(); + ProducerRecord record = new ProducerRecord(topic, order.getId().toString(), JSON.toJSONString(order)); + producer.send(record, (metadata, exception) -> { +// if (exception != null) { +// producer.abortTransaction(); +// throw new KafkaException(exception.getMessage() + " , data: " + record); +// } + }); + //producer.commitTransaction(); + + } catch (Throwable e) { + //producer.abortTransaction(); + } + //System.out.println("************" + json + "************"); + } + + @Override + public void close() { + producer.close(); + } +} \ No newline at end of file diff --git a/09mq/kafka-demo/src/main/resources/log4j.xml b/09mq/kafka-demo/src/main/resources/log4j.xml new file mode 100644 index 00000000..e7915d2e --- /dev/null +++ b/09mq/kafka-demo/src/main/resources/log4j.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/09mq/kmq-core/.gitignore b/09mq/kmq-core/.gitignore new file mode 100644 index 00000000..14fb3721 --- /dev/null +++ b/09mq/kmq-core/.gitignore @@ -0,0 +1,35 @@ + +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +*.dat diff --git a/09mq/kmq-core/README.md b/09mq/kmq-core/README.md new file mode 100644 index 00000000..5af5a6fd --- /dev/null +++ b/09mq/kmq-core/README.md @@ -0,0 +1,9 @@ +## 说明 + +第一个版本,完全基于内存queue的消息队列,实现了最基础的三个功能: + +- 创建topic +- 订阅topic和poll消息 +- 发送消息到topic + +具体参见demo.KmqDemo \ No newline at end of file diff --git a/09mq/kmq-core/pom.xml b/09mq/kmq-core/pom.xml new file mode 100644 index 00000000..f0dc62ef --- /dev/null +++ b/09mq/kmq-core/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.kmq + kmq-core + 0.0.1-SNAPSHOT + kmq-core + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + true + + + + com.alibaba + fastjson + 1.2.58 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/KmqApplication.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/KmqApplication.java new file mode 100644 index 00000000..76c7ed8e --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/KmqApplication.java @@ -0,0 +1,13 @@ +package io.kimmking.kmq; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class KmqApplication { + + public static void main(String[] args) { + SpringApplication.run(KmqApplication.class, args); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java new file mode 100644 index 00000000..8989faab --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java @@ -0,0 +1,55 @@ +package io.kimmking.kmq.core; + +import lombok.SneakyThrows; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +public final class Kmq { + + public Kmq(String topic, int capacity) { + this.topic = topic; + this.capacity = capacity; + this.queue = new LinkedBlockingQueue(capacity); + } + +// public List consumers = new ArrayList<>(); + + private List listeners = new ArrayList<>(); + + private final String topic; + + private final int capacity; + + private LinkedBlockingQueue queue; + + public boolean send(KmqMessage message) { + boolean offered = queue.offer(message); + if(offered) { + listeners.forEach(listener -> { + try { + listener.onMessage(message); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + return offered; + } + + public KmqMessage poll() { + return queue.poll(); + } + + @SneakyThrows + public KmqMessage poll(long timeout) { + return queue.poll(timeout, TimeUnit.MILLISECONDS); + } + + public void addListener(MessageListener listener) { + listeners.add(listener); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java new file mode 100644 index 00000000..c0aa344f --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java @@ -0,0 +1,30 @@ +package io.kimmking.kmq.core; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +public final class KmqBroker { // Broker+Connection + + public static final int CAPACITY = 10000; + + private final Map kmqMap = new ConcurrentHashMap<>(64); + + public void createTopic(String name){ + kmqMap.putIfAbsent(name, new Kmq(name,CAPACITY)); + } + + public Kmq findKmq(String topic) { + return this.kmqMap.get(topic); + } + + public KmqProducer createProducer() { + return new KmqProducer(this); + } + + final AtomicInteger consumerId = new AtomicInteger(0); + public KmqConsumer createConsumer() { + return new KmqConsumer("CID" + consumerId.getAndIncrement(), this); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java new file mode 100644 index 00000000..83ae7245 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java @@ -0,0 +1,34 @@ +package io.kimmking.kmq.core; + +import lombok.Getter; + +public class KmqConsumer { + + private final KmqBroker broker; + + @Getter + private final String id; + + private Kmq kmq; + + public KmqConsumer(String id, KmqBroker broker) { + this.id = id; + this.broker = broker; + } + + public void subscribe(String topic) { + this.kmq = this.broker.findKmq(topic); + if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); + } + + public void subscribe(String topic, MessageListener listener) { + this.kmq = this.broker.findKmq(topic); + if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); + this.kmq.addListener(listener); + } + + public KmqMessage poll(long timeout) { + return kmq.poll(timeout); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java new file mode 100644 index 00000000..73823c9d --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java @@ -0,0 +1,32 @@ +package io.kimmking.kmq.core; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.HashMap; +import java.util.concurrent.atomic.AtomicLong; + +@AllArgsConstructor +@NoArgsConstructor +@Data +public class KmqMessage { + + static AtomicLong MID = new AtomicLong(0); + + private HashMap headers = new HashMap<>(); + private String topic; + private Long id; + private T body; + + public KmqMessage(String topic, T body) { + this.topic = topic; + this.body = body; + this.id = MID.getAndIncrement(); + } + + public static KmqMessage from(String topic, T body) { + return new KmqMessage<>(topic, body); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqProducer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqProducer.java new file mode 100644 index 00000000..0adadd71 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqProducer.java @@ -0,0 +1,16 @@ +package io.kimmking.kmq.core; + +public class KmqProducer { + + private KmqBroker broker; + + public KmqProducer(KmqBroker broker) { + this.broker = broker; + } + + public boolean send(String topic, KmqMessage message) { + Kmq kmq = this.broker.findKmq(topic); + if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); + return kmq.send(message); + } +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java new file mode 100644 index 00000000..4cfdbe08 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java @@ -0,0 +1,13 @@ +package io.kimmking.kmq.core; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/13 下午3:29 + */ +public interface MessageListener { + + void onMessage(KmqMessage message) throws Exception; + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java new file mode 100644 index 00000000..9921af3d --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java @@ -0,0 +1,58 @@ +package io.kimmking.kmq.demo; + +import io.kimmking.kmq.core.KmqBroker; +import io.kimmking.kmq.core.KmqConsumer; +import io.kimmking.kmq.core.KmqMessage; +import io.kimmking.kmq.core.KmqProducer; + +import lombok.SneakyThrows; + +public class KmqDemo { + + @SneakyThrows + public static void main(String[] args) { + + String topic = "kk.test"; + KmqBroker broker = new KmqBroker(); + broker.createTopic(topic); + + KmqConsumer subscriber = broker.createConsumer(); + subscriber.subscribe(topic, (message) -> { + System.out.println(subscriber.getId() + " : " + message.getBody()); + }); + + KmqConsumer consumer = broker.createConsumer(); + consumer.subscribe(topic); + final boolean[] flag = new boolean[1]; + flag[0] = true; + new Thread(() -> { + while (flag[0]) { + KmqMessage message = consumer.poll(100); + if(null != message) { + System.out.println(consumer.getId() + " : " + message.getBody()); + } + } + System.out.println("程序退出。"); + }).start(); + + KmqProducer producer = broker.createProducer(); + for (int i = 0; i < 10; i++) { + Order order = new Order(1000L + i, System.currentTimeMillis(), "USD2CNY", 6.51d); + producer.send(topic, new KmqMessage(null, order)); + } + Thread.sleep(500); + System.out.println("点击任何键,发送一条消息;点击q或e,退出程序。"); + while (true) { + char c = (char) System.in.read(); + if(c > 20) { + System.out.println(c); + producer.send(topic, new KmqMessage(null, new Order(100000L + c, System.currentTimeMillis(), "USD2CNY", 6.52d))); + } + + if( c == 'q' || c == 'e') break; + } + + flag[0] = false; + + } +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/Order.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/Order.java new file mode 100644 index 00000000..2d81ec85 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/Order.java @@ -0,0 +1,17 @@ +package io.kimmking.kmq.demo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Order { + + private Long id; + private Long ts; + private String symbol; + private Double price; + +} \ No newline at end of file diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java new file mode 100644 index 00000000..baccdaff --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java @@ -0,0 +1,97 @@ +package io.kimmking.kmq.store; + +import com.alibaba.fastjson.JSON; +import io.kimmking.kmq.core.KmqMessage; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Scanner; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/13 下午9:59 + */ +public class FileDemo { + + public static void main(String[] args) throws IOException { + + String content = "this is a good file.\r\n" + + "that is a new line.\r\n"; + String topic = "topicA"; + System.out.println(content.length()); + + File file = new File("store001.dat"); + if (!file.exists()) { + file.createNewFile(); + } + Path path = Paths.get(file.toURI()); + try(FileChannel channel = (FileChannel) Files.newByteChannel(path, + StandardOpenOption.READ, + StandardOpenOption.WRITE)) { + + MappedByteBuffer mappedByteBuffer = channel + .map(FileChannel.MapMode.READ_WRITE, 0, 10240); + + if (mappedByteBuffer != null) { + + System.out.println(Charset.forName("utf-8") + .decode(mappedByteBuffer.asReadOnlyBuffer())); + + for (int i = 0; i < 100; i++) { + KmqMessage km = KmqMessage.from(topic, content); + String message = encodeMessage(km); + Indexer.addEntry(topic, km.getId(), mappedByteBuffer.position(), message.length()); + int pos = write(mappedByteBuffer, message); + System.out.println("POS = " + pos); + } + + System.out.println(" ======== indexer ========= "); + System.out.println(Indexer.getEntries(topic)); + } + + ByteBuffer readOnlyBuffer = mappedByteBuffer.asReadOnlyBuffer(); + Scanner sc = new Scanner(System.in); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + if (line.equals("exit")) { + break; + } + System.out.println("IN = "+line); + Long id = Long.valueOf(line); + Indexer.Entry entry = Indexer.getEntry(id); + System.out.println("EN = " + entry); + if(entry == null) { + System.out.println("!!!No entry for id=" + id); + } else { + readOnlyBuffer.position(entry.offset); + byte[] bytes = new byte[entry.length]; + readOnlyBuffer.get(bytes, 0, entry.length); + System.out.println("MSG = " + new String(bytes)); + } + } + + } + } + + private static String encodeMessage(KmqMessage message) { + return JSON.toJSONString(message); + } + + public static int write(MappedByteBuffer buffer, String content) throws IOException { + buffer.put( + Charset.forName("utf-8") + .encode(content)); + return buffer.position(); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java new file mode 100644 index 00000000..792ac400 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java @@ -0,0 +1,53 @@ +package io.kimmking.kmq.store; + +import lombok.Data; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/14 下午6:19 + */ + +@Data +public class Indexer { + + static Map> index = new HashMap<>(); + static Map mappings = new HashMap<>(); + + @Data + public static class Entry { + long id; + int offset; + int length; + + public Entry(long id, int offset, int length) { + this.offset = offset; + this.length = length; + } + } + + public static void addEntry(String topic, long id, int offset, int length) { + List entries = index.get(topic); + if(entries == null) { + entries = new java.util.ArrayList<>(); + index.put(topic, entries); + } + Entry e = new Entry(id, offset, length); + entries.add(e); + mappings.put(id, e); + } + + public static List getEntries(String topic) { + return index.get(topic); + } + + public static Entry getEntry(long id) { + return mappings.get(id); + } + +} diff --git a/09mq/kmq-core/src/main/resources/application.properties b/09mq/kmq-core/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/kmq-core/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/kmq-core/src/test/java/io/kimmking/kmq/core/KmqApplicationTests.java b/09mq/kmq-core/src/test/java/io/kimmking/kmq/core/KmqApplicationTests.java new file mode 100644 index 00000000..19ef07aa --- /dev/null +++ b/09mq/kmq-core/src/test/java/io/kimmking/kmq/core/KmqApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.kmq.core; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class KmqApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/pulsar/.gitignore b/09mq/pulsar/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/pulsar/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/pulsar/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/pulsar/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/pulsar/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/pulsar/.mvn/wrapper/maven-wrapper.jar b/09mq/pulsar/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/pulsar/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/pulsar/.mvn/wrapper/maven-wrapper.properties b/09mq/pulsar/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/pulsar/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/pulsar/mvnw b/09mq/pulsar/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/pulsar/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/pulsar/mvnw.cmd b/09mq/pulsar/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/pulsar/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/pulsar/pom.xml b/09mq/pulsar/pom.xml new file mode 100644 index 00000000..6382a1b4 --- /dev/null +++ b/09mq/pulsar/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.mq + pulsar + 0.0.1-SNAPSHOT + pulsar + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.apache.pulsar + pulsar-client + 2.7.0 + + + + + + + + + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/Config.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/Config.java new file mode 100644 index 00000000..7d62c9c3 --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/Config.java @@ -0,0 +1,15 @@ +package io.kimmking.mq.pulsar; + +import lombok.SneakyThrows; +import org.apache.pulsar.client.api.PulsarClient; + +public class Config { + + @SneakyThrows + public static PulsarClient createClient() { + return PulsarClient.builder() + .serviceUrl("pulsar://localhost:6650") + .build(); + } + +} diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ConsumerDemo.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ConsumerDemo.java new file mode 100644 index 00000000..7a1ac56d --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ConsumerDemo.java @@ -0,0 +1,136 @@ +package io.kimmking.mq.pulsar; + +import lombok.SneakyThrows; +import org.apache.pulsar.client.api.Consumer; +import org.apache.pulsar.client.api.Message; +import org.springframework.stereotype.Component; + +@Component +public class ConsumerDemo { + + @SneakyThrows + public void consume() { + + Consumer consumer = Config.createClient().newConsumer() + .topic("my-kk") + .subscriptionName("my-subscription") + .subscribe(); + + while (true) { + // Wait for a message + Message msg = consumer.receive(); + + try { + // Do something with the message + System.out.printf("Message received from pulsar: %s \n", new String(msg.getData())); + + // Acknowledge the message so that it can be deleted by the message broker + consumer.acknowledge(msg); + } catch (Exception e) { + // Message failed to process, redeliver later + consumer.negativeAcknowledge(msg); + } + } + +// +// client.newConsumer() +// .deadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(10) +// .deadLetterTopic("your-topic-name").build()) +// .subscribe(); + + +// Consumer consumer = client.newConsumer() +// .topic("my-topic") +// .subscriptionName("my-subscription") +// .ackTimeout(10, TimeUnit.SECONDS) +// .subscriptionType(SubscriptionType.Exclusive) +// .subscribe(); + + +// +// CompletableFuture asyncMessage = consumer.receiveAsync(); +// +// Messages messages = consumer.batchReceive(); +// for (Object message : messages) { +// // do something +// } +// consumer.acknowledge(messages); +// +// BatchReceivePolicy.builder() +// .maxNumMessage(-1) +// .maxNumBytes(10 * 1024 * 1024) +// .timeout(100, TimeUnit.MILLISECONDS) +// .build(); +// +// Consumer consumer = client.newConsumer().topic("my-topic").subscriptionName("my-subscription") +// .batchReceivePolicy(BatchReceivePolicy.builder(). +// maxNumMessages(100).maxNumBytes(1024 * 1024) +// .timeout(200, TimeUnit.MILLISECONDS) +// .build()).subscribe(); + + +// +// ConsumerBuilder consumerBuilder = pulsarClient.newConsumer() +// .subscriptionName(subscription); +// +//// Subscribe to all topics in a namespace +// Pattern allTopicsInNamespace = Pattern.compile("public/default/.*"); +// Consumer allTopicsConsumer = consumerBuilder +// .topicsPattern(allTopicsInNamespace) +// .subscribe(); +// +//// Subscribe to a subsets of topics in a namespace, based on regex +// Pattern someTopicsInNamespace = Pattern.compile("public/default/foo.*"); +// Consumer allTopicsConsumer = consumerBuilder +// .topicsPattern(someTopicsInNamespace) +// .subscribe(); +// In the above example, the consumer subscribes to the persistent topics that can match the topic name pattern. If you want the consumer subscribes to all persistent and non-persistent topics that can match the topic name pattern, set subscriptionTopicsMode to RegexSubscriptionMode.AllTopics. +// +// Pattern pattern = Pattern.compile("public/default/.*"); +// pulsarClient.newConsumer() +// .subscriptionName("my-sub") +// .topicsPattern(pattern) +// .subscriptionTopicsMode(RegexSubscriptionMode.AllTopics) +// .subscribe(); +// Note +// By default, the subscriptionTopicsMode of the consumer is PersistentOnly. Available options of subscriptionTopicsMode are PersistentOnly, NonPersistentOnly, and AllTopics. +// +// 你还可以订阅明确的主题列表 (如果愿意, 可跨命名空间): +// +// List topics = Arrays.asList( +// "topic-1", +// "topic-2", +// "topic-3" +// ); +// +// Consumer multiTopicConsumer = consumerBuilder +// .topics(topics) +// .subscribe(); +// +//// Alternatively: +// Consumer multiTopicConsumer = consumerBuilder +// .topic( +// "topic-1", +// "topic-2", +// "topic-3" +// ) +// .subscribe(); +// You can also subscribe to multiple topics asynchronously using the subscribeAsync method rather than the synchronous subscribe method. The following is an example. +// +// Pattern allTopicsInNamespace = Pattern.compile("persistent://public/default.*"); +// consumerBuilder +// .topics(topics) +// .subscribeAsync() +// .thenAccept(this::receiveMessageFromConsumer); +// +// private void receiveMessageFromConsumer(Object consumer) { +// ((Consumer)consumer).receiveAsync().thenAccept(message -> { +// // Do something with the received message +// receiveMessageFromConsumer(consumer); +// }); +// } + + + } + +} diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ProducerDemo.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ProducerDemo.java new file mode 100644 index 00000000..9c1c6bf4 --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ProducerDemo.java @@ -0,0 +1,60 @@ +package io.kimmking.mq.pulsar; + +import lombok.SneakyThrows; +import org.apache.pulsar.client.api.Producer; +import org.apache.pulsar.client.api.Schema; +import org.springframework.stereotype.Component; + +@Component +public class ProducerDemo { + Producer stringProducer; + + @SneakyThrows + public ProducerDemo(){ + stringProducer = Config.createClient().newProducer(Schema.STRING) + .topic("my-kk") + .create(); + } + + @SneakyThrows + public void sendMessage() { + for (int i = 0; i < 1000; i++) { + stringProducer.send(i + " message from pulsar."); + } + } + + +// //关闭操作也可以是异步的: +// +// producer.closeAsync() +// .thenRun(() -> System.out.println("Producer closed")); +// .exceptionally((ex) -> { +// System.err.println("Failed to close producer: " + ex); +// return ex; +// }); + + // 控制发送行为 +// Producer producer = client.newProducer() +// .topic("my-topic") +// .batchingMaxPublishDelay(10, TimeUnit.MILLISECONDS) +// .sendTimeout(10, TimeUnit.SECONDS) +// .blockIfQueueFull(true) +// .create(); + + + //异步发送 +// producer.sendAsync("my-async-message".getBytes()).thenAccept(msgId -> { +// System.out.printf("Message with ID %s successfully sent", msgId); +// }); + + + // 添加参数 +// producer.newMessage() +// .key("my-message-key") +// .value("my-async-message".getBytes()) +// .property("my-key", "my-value") +// .property("my-other-key", "my-other-value") +// .send(); + + +} diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/PulsarApplication.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/PulsarApplication.java new file mode 100644 index 00000000..89938c6a --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/PulsarApplication.java @@ -0,0 +1,36 @@ +package io.kimmking.mq.pulsar; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class PulsarApplication { + + public static void main(String[] args) { + SpringApplication.run(PulsarApplication.class, args); + } + + + @Autowired + ProducerDemo producer; + + @Autowired + ConsumerDemo consumer; + + @Bean + ApplicationRunner run() { + return args -> { + new Thread(() -> { + consumer.consume(); + }).start(); + + producer.sendMessage(); + + }; + } + + +} diff --git a/09mq/pulsar/src/main/resources/application.properties b/09mq/pulsar/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/pulsar/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/pulsar/src/test/java/io/kimmking/mq/pulsar/PulsarApplicationTests.java b/09mq/pulsar/src/test/java/io/kimmking/mq/pulsar/PulsarApplicationTests.java new file mode 100644 index 00000000..8d9659b7 --- /dev/null +++ b/09mq/pulsar/src/test/java/io/kimmking/mq/pulsar/PulsarApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.mq.pulsar; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PulsarApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/rabbit/.gitignore b/09mq/rabbit/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/rabbit/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/rabbit/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/rabbit/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/rabbit/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/rabbit/.mvn/wrapper/maven-wrapper.jar b/09mq/rabbit/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/rabbit/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/rabbit/.mvn/wrapper/maven-wrapper.properties b/09mq/rabbit/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/rabbit/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/rabbit/mvnw b/09mq/rabbit/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/rabbit/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/rabbit/mvnw.cmd b/09mq/rabbit/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/rabbit/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/rabbit/pom.xml b/09mq/rabbit/pom.xml new file mode 100644 index 00000000..ae17ee14 --- /dev/null +++ b/09mq/rabbit/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.mq + rabbit + 0.0.1-SNAPSHOT + rabbit + Demo project for Spring Boot + + + 1.8 + + + + + + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + + + + + + + + + + + + + + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.amqp + spring-rabbit-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/camel/README.md b/09mq/rabbit/src/main/java/io/kimmking/mq/camel/README.md new file mode 100644 index 00000000..29f5cd93 --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/camel/README.md @@ -0,0 +1 @@ +使用代码方式,实现camel的操作。 diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageProducer.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageProducer.java new file mode 100644 index 00000000..2480046b --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageProducer.java @@ -0,0 +1,43 @@ +package io.kimmking.mq.rabbit; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +@Component +@Slf4j +public class MessageProducer implements RabbitTemplate.ConfirmCallback { + + //由于rabbitTemplate的scope属性设置为ConfigurableBeanFactory.SCOPE_PROTOTYPE,所以不能自动注入 + private RabbitTemplate rabbitTemplate; + /** + * 构造方法注入rabbitTemplate + */ + @Autowired + public MessageProducer(RabbitTemplate rabbitTemplate) { + this.rabbitTemplate = rabbitTemplate; + rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话,那回调就是最后设置的内容 + } + + public void sendMessage(String content) { + CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString()); + //把消息放入ROUTINGKEY_A对应的队列当中去,对应的是队列A + rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_A, RabbitConfig.ROUTINGKEY_B, content, correlationId); + } + /** + * 回调 + */ + @Override + public void confirm(CorrelationData correlationData, boolean ack, String cause) { + log.info(" 回调id:" + correlationData); + if (ack) { + log.info("消息成功消费!!!!!" + correlationData); + } else { + log.info("消息消费失败:" + cause + correlationData); + } + } +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverA.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverA.java new file mode 100644 index 00000000..6cea0d3a --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverA.java @@ -0,0 +1,18 @@ +package io.kimmking.mq.rabbit; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +@Component +@RabbitListener(queues = RabbitConfig.QUEUE_A) +@Slf4j +public class MessageReceiverA { //guava , EventBus 的一些语法糖 + + @RabbitHandler + public void process(String content) { + log.info("接收处理队列A当中的消息: " + content); + } + +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverB.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverB.java new file mode 100644 index 00000000..7b0e81db --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverB.java @@ -0,0 +1,18 @@ +package io.kimmking.mq.rabbit; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +@Component +@RabbitListener(queues = RabbitConfig.QUEUE_B) +@Slf4j +public class MessageReceiverB { + + @RabbitHandler + public void process(String content) { + log.info("接收处理队列B当中的消息: " + content); + } + +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitApplication.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitApplication.java new file mode 100644 index 00000000..787c57a3 --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitApplication.java @@ -0,0 +1,28 @@ +package io.kimmking.mq.rabbit; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class RabbitApplication { + + public static void main(String[] args) { + SpringApplication.run(RabbitApplication.class, args); + } + + @Autowired + MessageProducer producer; + + @Bean + ApplicationRunner run() { + return args -> { + for (int i = 0; i < 1000; i++) { + producer.sendMessage(i+" message by cuicuilaoshi."); + } + }; + } + +} diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitConfig.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitConfig.java new file mode 100644 index 00000000..440db166 --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitConfig.java @@ -0,0 +1,108 @@ +package io.kimmking.mq.rabbit; + +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.BindingBuilder; +import org.springframework.amqp.core.DirectExchange; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration +public class RabbitConfig { + + @Value("${spring.rabbitmq.host}") + private String host; + + @Value("${spring.rabbitmq.port}") + private int port; + + @Value("${spring.rabbitmq.username}") + private String username; + + @Value("${spring.rabbitmq.password}") + private String password; + + + public static final String EXCHANGE_A = "my-mq-exchange_A"; + public static final String EXCHANGE_B = "my-mq-exchange_B"; + public static final String EXCHANGE_C = "my-mq-exchange_C"; + + + public static final String QUEUE_A = "QUEUE_A"; + public static final String QUEUE_B = "QUEUE_B"; + public static final String QUEUE_C = "QUEUE_C"; + + public static final String ROUTINGKEY_A = "spring-boot-routingKey_A"; + public static final String ROUTINGKEY_B = "spring-boot-routingKey_B"; + public static final String ROUTINGKEY_C = "spring-boot-routingKey_C"; + + @Bean + public ConnectionFactory connectionFactory() { + CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port); + connectionFactory.setUsername(username); + connectionFactory.setPassword(password); + connectionFactory.setVirtualHost("/"); + connectionFactory.setPublisherConfirms(true); + return connectionFactory; + } + + @Bean + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public RabbitTemplate rabbitTemplate() { + RabbitTemplate template = new RabbitTemplate(connectionFactory()); + return template; + } + + /** + * 针对消费者配置 + * 1. 设置交换机类型 + * 2. 将队列绑定到交换机 + FanoutExchange: 将消息分发到所有的绑定队列,无routingkey的概念 + HeadersExchange :通过添加属性key-value匹配 + DirectExchange:按照routingkey分发到指定队列 + TopicExchange:多关键字匹配 + */ + @Bean + public DirectExchange defaultExchange() { + return new DirectExchange(EXCHANGE_A); + } + + @Bean + public Queue queueA() { + return new Queue(QUEUE_A, true); //队列持久 + } + + @Bean + public Queue queueB() { + return new Queue(QUEUE_B, true); //队列持久 + } + + @Bean + public Queue queueC() { + return new Queue(QUEUE_C, true); //队列持久 + } + + @Bean + public Binding bindingA() { + return BindingBuilder.bind(queueA()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_A); + } + @Bean + public Binding bindingB() { + return BindingBuilder.bind(queueB()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_B); + } + @Bean + public Binding bindingC(){ + return BindingBuilder.bind(queueC()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_C); + } + + // 详细的使用可以参考: + // https://blog.csdn.net/qq_38455201/article/details/80308771 + // https://www.cnblogs.com/handsomeye/p/9135623.html + +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/resources/application.yaml b/09mq/rabbit/src/main/resources/application.yaml new file mode 100644 index 00000000..48574487 --- /dev/null +++ b/09mq/rabbit/src/main/resources/application.yaml @@ -0,0 +1,7 @@ +spring: + rabbitmq: + host: localhost + username: admin + password: admin + port: 5672 + diff --git a/09mq/rabbit/src/test/java/io/kimmking/mq/rabbit/RabbitApplicationTests.java b/09mq/rabbit/src/test/java/io/kimmking/mq/rabbit/RabbitApplicationTests.java new file mode 100644 index 00000000..adf34994 --- /dev/null +++ b/09mq/rabbit/src/test/java/io/kimmking/mq/rabbit/RabbitApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.mq.rabbit; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class RabbitApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/rocket/.gitignore b/09mq/rocket/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/rocket/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/rocket/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/rocket/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/rocket/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/rocket/.mvn/wrapper/maven-wrapper.jar b/09mq/rocket/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/rocket/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/rocket/.mvn/wrapper/maven-wrapper.properties b/09mq/rocket/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/rocket/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/rocket/mvnw b/09mq/rocket/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/rocket/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/rocket/mvnw.cmd b/09mq/rocket/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/rocket/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/rocket/pom.xml b/09mq/rocket/pom.xml new file mode 100644 index 00000000..d2931a7e --- /dev/null +++ b/09mq/rocket/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.mq + rocket + 0.0.1-SNAPSHOT + rocket + Demo project for Spring Boot + + + 1.8 + + + + + + + + + + org.apache.rocketmq + rocketmq-spring-boot-starter + 2.1.1 + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/Order.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/Order.java new file mode 100644 index 00000000..02aec9f7 --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/Order.java @@ -0,0 +1,16 @@ +package io.kimmking.mq.rocket; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Order { + + private long id; + private String symbol; + private double price; + +} diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/OrderConsumerDemo.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/OrderConsumerDemo.java new file mode 100644 index 00000000..2c4ba4ed --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/OrderConsumerDemo.java @@ -0,0 +1,17 @@ +package io.kimmking.mq.rocket; + +import org.apache.rocketmq.spring.annotation.ConsumeMode; +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQReplyListener; +import org.springframework.stereotype.Component; + +@Component +@RocketMQMessageListener(consumerGroup = "test2", topic = "test-k2") +public class OrderConsumerDemo implements RocketMQReplyListener { + + @Override + public String onMessage(Order order) { // request-response + System.out.println(this.getClass().getName() + " -> " + order); + return "Process&Return [" + order + "]."; + } +} diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/RocketApplication.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/RocketApplication.java new file mode 100644 index 00000000..13d6c89c --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/RocketApplication.java @@ -0,0 +1,60 @@ +package io.kimmking.mq.rocket; + +import org.apache.rocketmq.client.producer.SendCallback; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.spring.core.RocketMQTemplate; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.messaging.MessageHeaders; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.util.MimeTypeUtils; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.util.List; + +@SpringBootApplication +public class RocketApplication implements CommandLineRunner { + + public static void main(String[] args) { + SpringApplication.run(RocketApplication.class, args); + } + + @Resource + private RocketMQTemplate rocketMQTemplate; + + @Override + public void run(String... args) throws Exception { + + String topic = "test-k1"; + SendResult sendResult = rocketMQTemplate.syncSend(topic, "Hello, World!" + System.currentTimeMillis()); + System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult); + + sendResult = rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload( + new Order(System.currentTimeMillis(),"CNY2USD", 0.1501d)) + .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE).build()); + System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult); + + String topic1 = "test-k2"; + + String result = rocketMQTemplate.sendAndReceive(topic1, new Order(System.currentTimeMillis(),"CNY2USD", 0.1502d), String.class); + System.out.println(" consumer result => " + result); + +// rocketMQTemplate.asyncSend(topic1, new Order(System.currentTimeMillis(),"CNY2USD", 0.1502d), new SendCallback() { +// @Override +// public void onSuccess(SendResult result) { +// System.out.printf("async onSucess SendResult=%s %n", result); +// } +// +// @Override +// public void onException(Throwable throwable) { +// System.out.printf("async onException Throwable=%s %n", throwable); +// } +// +// }); + + + } + +} diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/StringConsumerDemo.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/StringConsumerDemo.java new file mode 100644 index 00000000..f2534bdb --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/StringConsumerDemo.java @@ -0,0 +1,15 @@ +package io.kimmking.mq.rocket; + +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQListener; +import org.springframework.stereotype.Component; + +@Component +@RocketMQMessageListener(consumerGroup = "test1", topic = "test-k1") +public class StringConsumerDemo implements RocketMQListener { + + @Override + public void onMessage(String message) { // one way + System.out.println(this.getClass().getName() + " -> " + message); + } +} diff --git a/09mq/rocket/src/main/resources/application.properties b/09mq/rocket/src/main/resources/application.properties new file mode 100644 index 00000000..513b78b4 --- /dev/null +++ b/09mq/rocket/src/main/resources/application.properties @@ -0,0 +1,5 @@ + +rocketmq.name-server=localhost:9876 +rocketmq.producer.group=my-group1 +rocketmq.producer.sendMessageTimeout=300000 + diff --git a/09mq/rocket/src/test/java/io/kimmking/mq/rocket/RocketApplicationTests.java b/09mq/rocket/src/test/java/io/kimmking/mq/rocket/RocketApplicationTests.java new file mode 100644 index 00000000..80c1f88e --- /dev/null +++ b/09mq/rocket/src/test/java/io/kimmking/mq/rocket/RocketApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.mq.rocket; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class RocketApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/spring-kafka-demo/.gitignore b/09mq/spring-kafka-demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/spring-kafka-demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/spring-kafka-demo/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/spring-kafka-demo/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/spring-kafka-demo/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.jar b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.properties b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/spring-kafka-demo/mvnw b/09mq/spring-kafka-demo/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/spring-kafka-demo/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/spring-kafka-demo/mvnw.cmd b/09mq/spring-kafka-demo/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/spring-kafka-demo/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/spring-kafka-demo/pom.xml b/09mq/spring-kafka-demo/pom.xml new file mode 100644 index 00000000..7c3d6f57 --- /dev/null +++ b/09mq/spring-kafka-demo/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.javacourse + kafka-demo + 0.0.1-SNAPSHOT + kafka-demo + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.kafka + spring-kafka + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.kafka + spring-kafka-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/spring-kafka-demo/src/main/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplication.java b/09mq/spring-kafka-demo/src/main/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplication.java new file mode 100644 index 00000000..738ba47c --- /dev/null +++ b/09mq/spring-kafka-demo/src/main/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplication.java @@ -0,0 +1,13 @@ +package io.kimmking.javacourse.kafkademo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class KafkaDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(KafkaDemoApplication.class, args); + } + +} diff --git a/09mq/spring-kafka-demo/src/main/resources/application.properties b/09mq/spring-kafka-demo/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/spring-kafka-demo/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/spring-kafka-demo/src/test/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplicationTests.java b/09mq/spring-kafka-demo/src/test/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplicationTests.java new file mode 100644 index 00000000..e70ed750 --- /dev/null +++ b/09mq/spring-kafka-demo/src/test/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.javacourse.kafkademo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class KafkaDemoApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/11dfs/dfs/.gitignore b/11dfs/dfs/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/11dfs/dfs/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/11dfs/dfs/.mvn/wrapper/maven-wrapper.jar b/11dfs/dfs/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..c1dd12f1 Binary files /dev/null and b/11dfs/dfs/.mvn/wrapper/maven-wrapper.jar differ diff --git a/11dfs/dfs/.mvn/wrapper/maven-wrapper.properties b/11dfs/dfs/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..b7cb93e7 --- /dev/null +++ b/11dfs/dfs/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar diff --git a/11dfs/dfs/app/pic/1.txt b/11dfs/dfs/app/pic/1.txt new file mode 100644 index 00000000..9d07aa0d --- /dev/null +++ b/11dfs/dfs/app/pic/1.txt @@ -0,0 +1 @@ +111 \ No newline at end of file diff --git a/11dfs/dfs/app/pic/1644038685842-moon.jpeg b/11dfs/dfs/app/pic/1644038685842-moon.jpeg new file mode 100644 index 00000000..39a70e03 Binary files /dev/null and b/11dfs/dfs/app/pic/1644038685842-moon.jpeg differ diff --git a/11dfs/dfs/app/pic/1644048593861-moon.jpeg b/11dfs/dfs/app/pic/1644048593861-moon.jpeg new file mode 100644 index 00000000..39a70e03 Binary files /dev/null and b/11dfs/dfs/app/pic/1644048593861-moon.jpeg differ diff --git a/11dfs/dfs/app/pic/1644051049289-IMG_0987.JPG b/11dfs/dfs/app/pic/1644051049289-IMG_0987.JPG new file mode 100644 index 00000000..e6d9aae4 Binary files /dev/null and b/11dfs/dfs/app/pic/1644051049289-IMG_0987.JPG differ diff --git a/11dfs/dfs/app/pic/1644051171701-houyiqibing.jpeg b/11dfs/dfs/app/pic/1644051171701-houyiqibing.jpeg new file mode 100644 index 00000000..1a21b07f Binary files /dev/null and b/11dfs/dfs/app/pic/1644051171701-houyiqibing.jpeg differ diff --git a/11dfs/dfs/app/pic/1644051228263-houyiqibing.jpeg b/11dfs/dfs/app/pic/1644051228263-houyiqibing.jpeg new file mode 100644 index 00000000..1a21b07f Binary files /dev/null and b/11dfs/dfs/app/pic/1644051228263-houyiqibing.jpeg differ diff --git a/11dfs/dfs/app/pic/1644051550427-houyiqibing.jpeg b/11dfs/dfs/app/pic/1644051550427-houyiqibing.jpeg new file mode 100644 index 00000000..1a21b07f Binary files /dev/null and b/11dfs/dfs/app/pic/1644051550427-houyiqibing.jpeg differ diff --git a/11dfs/dfs/app/pic/1646313820045-image1.png b/11dfs/dfs/app/pic/1646313820045-image1.png new file mode 100644 index 00000000..54495982 Binary files /dev/null and b/11dfs/dfs/app/pic/1646313820045-image1.png differ diff --git a/11dfs/dfs/app/pic/1646485079290-renshijian.jpeg b/11dfs/dfs/app/pic/1646485079290-renshijian.jpeg new file mode 100644 index 00000000..78d018a0 Binary files /dev/null and b/11dfs/dfs/app/pic/1646485079290-renshijian.jpeg differ diff --git a/11dfs/dfs/app/pic/1646485131332-renshijian.jpeg b/11dfs/dfs/app/pic/1646485131332-renshijian.jpeg new file mode 100644 index 00000000..78d018a0 Binary files /dev/null and b/11dfs/dfs/app/pic/1646485131332-renshijian.jpeg differ diff --git a/11dfs/dfs/app/pic/1646490487023-renshijian.jpeg b/11dfs/dfs/app/pic/1646490487023-renshijian.jpeg new file mode 100644 index 00000000..78d018a0 Binary files /dev/null and b/11dfs/dfs/app/pic/1646490487023-renshijian.jpeg differ diff --git a/11dfs/dfs/app/pic/1646490663637-yushengjiexuan7.jpeg b/11dfs/dfs/app/pic/1646490663637-yushengjiexuan7.jpeg new file mode 100644 index 00000000..61366514 Binary files /dev/null and b/11dfs/dfs/app/pic/1646490663637-yushengjiexuan7.jpeg differ diff --git a/11dfs/dfs/app/pic/1646491143115-yushengjiexuan7.jpeg b/11dfs/dfs/app/pic/1646491143115-yushengjiexuan7.jpeg new file mode 100644 index 00000000..61366514 Binary files /dev/null and b/11dfs/dfs/app/pic/1646491143115-yushengjiexuan7.jpeg differ diff --git a/11dfs/dfs/app/pic/jiajia.txt b/11dfs/dfs/app/pic/jiajia.txt new file mode 100644 index 00000000..fc139c7d --- /dev/null +++ b/11dfs/dfs/app/pic/jiajia.txt @@ -0,0 +1 @@ +jiajia,nihao \ No newline at end of file diff --git a/11dfs/dfs/mvnw b/11dfs/dfs/mvnw new file mode 100755 index 00000000..8a8fb228 --- /dev/null +++ b/11dfs/dfs/mvnw @@ -0,0 +1,316 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`\\unset -f command; \\command -v java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/11dfs/dfs/mvnw.cmd b/11dfs/dfs/mvnw.cmd new file mode 100644 index 00000000..1d8ab018 --- /dev/null +++ b/11dfs/dfs/mvnw.cmd @@ -0,0 +1,188 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% ^ + %JVM_CONFIG_MAVEN_PROPS% ^ + %MAVEN_OPTS% ^ + %MAVEN_DEBUG_OPTS% ^ + -classpath %WRAPPER_JAR% ^ + "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ + %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%"=="on" pause + +if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% + +cmd /C exit /B %ERROR_CODE% diff --git a/11dfs/dfs/pom.xml b/11dfs/dfs/pom.xml new file mode 100644 index 00000000..92586ab7 --- /dev/null +++ b/11dfs/dfs/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + io.github.kimmking.javacourse + dfs + 0.0.1-SNAPSHOT + dfs + DFS Demo project for Spring Boot + + 11 + + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.alibaba + druid-spring-boot-starter + 1.1.17 + + + + org.springframework + spring-jdbc + + + + com.github.tobato + fastdfs-client + 1.26.4 + + + + commons-fileupload + commons-fileupload + 1.4 + + + + mysql + mysql-connector-java + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/DfsApplication.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/DfsApplication.java new file mode 100644 index 00000000..9ace7029 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/DfsApplication.java @@ -0,0 +1,15 @@ +package io.github.kimmking.javacourse.dfs; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@MapperScan("io.github.kimmking.javacourse.dfs.mapper") +@SpringBootApplication +public class DfsApplication { + + public static void main(String[] args) { + SpringApplication.run(DfsApplication.class, args); + } + +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FastDfsClientConfig.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FastDfsClientConfig.java new file mode 100644 index 00000000..7ee6d867 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FastDfsClientConfig.java @@ -0,0 +1,14 @@ +package io.github.kimmking.javacourse.dfs; + +import com.github.tobato.fastdfs.FdfsClientConfig; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableMBeanExport; +import org.springframework.context.annotation.Import; +import org.springframework.jmx.support.RegistrationPolicy; + +@Configuration +@Import(FdfsClientConfig.class) +@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) +public class FastDfsClientConfig { + +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FileConfig.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FileConfig.java new file mode 100644 index 00000000..79bc164a --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FileConfig.java @@ -0,0 +1,40 @@ +package io.github.kimmking.javacourse.dfs; + +import org.springframework.context.annotation.Configuration; +import org.springframework.util.ResourceUtils; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +import java.io.File; +import java.io.FileNotFoundException; + +@Configuration +public class FileConfig extends WebMvcConfigurationSupport { + + public static final String PATH = new File("app").getAbsolutePath(); + public static final String PIC_PATH=PATH + "/pic/"; + static { + File picFile = new File(PIC_PATH); + if (!picFile.exists()) { + picFile.mkdirs(); + } + System.out.println("PIC_PATH => " + PIC_PATH); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { +// File path = null; +// try { +// path = new File(ResourceUtils.getURL("classpath:").getPath()); +// System.out.println("ResourceUtils Path: "+ path); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } +// String picPath = path.getParentFile().getParentFile().getParent() + File.separator + "app" + File.separator + "pic" + File.separator; + String picPath = PIC_PATH; + System.out.println("addResource for: "+picPath); + registry.addResourceHandler("/pic/**").addResourceLocations("file:"+picPath); + registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/"); + super.addResourceHandlers(registry); + } +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/JavaConfig.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/JavaConfig.java new file mode 100644 index 00000000..2672afac --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/JavaConfig.java @@ -0,0 +1,17 @@ +package io.github.kimmking.javacourse.dfs; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; + +@Configuration +public class JavaConfig { + @Bean + CommonsMultipartResolver createCommonsMultipartResolver() { + CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); + commonsMultipartResolver.setMaxUploadSize(4*1024*1024); + commonsMultipartResolver.setMaxInMemorySize(4*1024*1024); + commonsMultipartResolver.setDefaultEncoding("UTF-8"); + return commonsMultipartResolver; + } +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/controller/FileController.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/controller/FileController.java new file mode 100644 index 00000000..a7a0fb6f --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/controller/FileController.java @@ -0,0 +1,76 @@ +package io.github.kimmking.javacourse.dfs.controller; + +import io.github.kimmking.javacourse.dfs.FileConfig; +import io.github.kimmking.javacourse.dfs.JavaConfig; +import io.github.kimmking.javacourse.dfs.model.User; +import io.github.kimmking.javacourse.dfs.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.commons.CommonsMultipartFile; +import com.github.tobato.fastdfs.domain.StorePath; +import com.github.tobato.fastdfs.service.FastFileStorageClient; + +import java.io.File; +import java.io.IOException; +import java.io.FileInputStream; + +@RestController +@RequestMapping("/file") +public class FileController { + + @Autowired + UserService userService; + + @Autowired + FastFileStorageClient storageClient; + + @GetMapping("/test") + public User createUser() { + return new User(1L,"KK01", "null-"+System.currentTimeMillis()); + } + + @PostMapping("/") + public User upload(@RequestParam("file") CommonsMultipartFile file, @RequestParam("name") String name) throws IOException { + long startTime=System.currentTimeMillis(); + System.out.println("文件名称:" + file.getOriginalFilename()); + String fileName= startTime + "-" + file.getOriginalFilename(); + File newFile=new File(FileConfig.PIC_PATH + "/" +fileName); + System.out.println("文件路径:" + newFile.getAbsolutePath()); + file.transferTo(newFile); + long endTime=System.currentTimeMillis(); + System.out.println("文件处理时间:"+(endTime-startTime)+"ms"); + User user = new User(startTime, name, "http://localhost:8011/pic/"+fileName); + return userService.create(user); + } + + @PostMapping("/uploadDfs") + public User uploadDfs(@RequestParam("file") CommonsMultipartFile file, @RequestParam("name") String name) throws IOException { + long startTime=System.currentTimeMillis(); + System.out.println("文件名称:" + file.getOriginalFilename()); + String fileName= startTime + "-" + file.getOriginalFilename(); + File newFile=new File(FileConfig.PIC_PATH + "/" +fileName); + System.out.println("文件路径:" + newFile.getAbsolutePath()); + file.transferTo(newFile); + + FileInputStream is = new FileInputStream(newFile); + StorePath storePath = storageClient.uploadFile(is, newFile.length(), org.apache.commons.io.FilenameUtils.getExtension(newFile.getName()), null); + is.close(); + String fullPath = storePath.getFullPath(); + System.out.println("FastDFS Path = " + fullPath); + + long endTime=System.currentTimeMillis(); + System.out.println("文件处理时间:"+(endTime-startTime)+"ms"); + User user = new User(startTime, name, fullPath); + return userService.create(user); + } + + @RequestMapping("/findById") + public User findById(@RequestParam("id") Long id){ + return userService.findById(id); + } + + // get pic + + + +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/mapper/UserMapper.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/mapper/UserMapper.java new file mode 100644 index 00000000..11ece03b --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/mapper/UserMapper.java @@ -0,0 +1,10 @@ +package io.github.kimmking.javacourse.dfs.mapper; + +import io.github.kimmking.javacourse.dfs.model.User; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserMapper { + int create(User user); + User findById(Long id); +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/model/User.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/model/User.java new file mode 100644 index 00000000..bb601bb6 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/model/User.java @@ -0,0 +1,14 @@ +package io.github.kimmking.javacourse.dfs.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class User { + private Long id; + private String name; + private String picPath; +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/service/UserService.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/service/UserService.java new file mode 100644 index 00000000..595cc672 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/service/UserService.java @@ -0,0 +1,23 @@ +package io.github.kimmking.javacourse.dfs.service; + +import io.github.kimmking.javacourse.dfs.mapper.UserMapper; +import io.github.kimmking.javacourse.dfs.model.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + @Autowired + UserMapper userMapper; + + public User create(User user){ + int result = this.userMapper.create(user); + System.out.println("create user[" + user + "],result=" + result); + return result>0 ? user : null; + } + + public User findById(Long id){ + return this.userMapper.findById(id); + } +} diff --git a/11dfs/dfs/src/main/resources/application.yml b/11dfs/dfs/src/main/resources/application.yml new file mode 100644 index 00000000..d662bf93 --- /dev/null +++ b/11dfs/dfs/src/main/resources/application.yml @@ -0,0 +1,32 @@ +server: + port: 8011 +# tomcat: +# basedir: app + +spring: +# mvc: +# static-path-pattern: /** +# web: +# resources: +# static-locations: file:/Users/kimmking/kimmking/JavaCourseCodes/11dfs/dfs/pic/ +# # ## classpath:/META-INF/static/,classpath:/META-INF/public/,classpath:/META-INF/resources/, + datasource: + url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useServerPrepStmts=true&cachePrepStmts=true + username: root + password: + druid: + initial-size: 5 + max-active: 5 + +mybatis: + mapper-locations: classpath:mapper/*Mapper.xml + +logging: + level: + root: info + +fdfs: + so-timeout: 3000 + connect-timeout: 1000 + tracker-list: + - 62.234.122.77:22122 diff --git a/11dfs/dfs/src/main/resources/mapper/userMapper.xml b/11dfs/dfs/src/main/resources/mapper/userMapper.xml new file mode 100644 index 00000000..9008ebb8 --- /dev/null +++ b/11dfs/dfs/src/main/resources/mapper/userMapper.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + insert into user(id,name,pic_path) values(#{id},#{name},#{picPath}); + + + \ No newline at end of file diff --git a/11dfs/dfs/src/main/resources/static/index-dfs.html b/11dfs/dfs/src/main/resources/static/index-dfs.html new file mode 100644 index 00000000..72427468 --- /dev/null +++ b/11dfs/dfs/src/main/resources/static/index-dfs.html @@ -0,0 +1,17 @@ + + + + + 测试上传文件 + + +

+

请选择上传文件

+ +
+ +
+ +
+ + \ No newline at end of file diff --git a/11dfs/dfs/src/main/resources/static/index.html b/11dfs/dfs/src/main/resources/static/index.html new file mode 100644 index 00000000..36710e1e --- /dev/null +++ b/11dfs/dfs/src/main/resources/static/index.html @@ -0,0 +1,17 @@ + + + + + 测试上传文件 + + +
+

请选择上传文件

+ +
+ +
+ +
+ + \ No newline at end of file diff --git a/11dfs/dfs/src/test/java/io/github/kimmking/javacourse/dfs/DfsApplicationTests.java b/11dfs/dfs/src/test/java/io/github/kimmking/javacourse/dfs/DfsApplicationTests.java new file mode 100644 index 00000000..2d184891 --- /dev/null +++ b/11dfs/dfs/src/test/java/io/github/kimmking/javacourse/dfs/DfsApplicationTests.java @@ -0,0 +1,13 @@ +package io.github.kimmking.javacourse.dfs; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DfsApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/README.md b/README.md index e6a677d6..252447c6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,14 @@ # JavaCourse -JavaCourse +Java进阶训练营示例代码。此处代码,一方面作为作业的基本版本,另一方面需要大家通过调整加深自己对技术点的认识。 + +本课程的三个要素: + +1)40%是课程,包括预习、听课、复习总结,形成自己对知识体系的认识和经验,这是最基本的学习方法。 + +2)30%是作业,作业包括基础版本的必做作业,补充的选做作业,高难度的挑战作业。基本上完成必做可以通过P6的技术面试,完成选做可以通过P7的技术面试,高难度的话可以达到P7+/P8的技术面试水平。这是通过练习,得到第一手的体验。 + +3)30%是活动,包括且不限于源码分析学习小组,技术文章活动,读书活动,线下技术沙龙,线上技术分享等。通过一群愿意学习的人,在更好的学习氛围中,实现长期深入学习。 + +## 挑战作业 + +每个模块的挑战作业:[homework2.0.md](./homework2.0.md) , 能做出来70%的题目,直接联系我,给你推荐一线大厂工作。 \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/app/.mvn/wrapper/MavenWrapperDownloader.java b/app/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/app/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/app/.mvn/wrapper/maven-wrapper.jar b/app/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/app/.mvn/wrapper/maven-wrapper.jar differ diff --git a/app/.mvn/wrapper/maven-wrapper.properties b/app/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/app/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/app/mvnw b/app/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/app/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/app/mvnw.cmd b/app/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/app/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/app/pom.xml b/app/pom.xml new file mode 100644 index 00000000..3d06aa6c --- /dev/null +++ b/app/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + com.example + app + 0.0.1-SNAPSHOT + app + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/app/src/main/java/com/example/app/App.java b/app/src/main/java/com/example/app/App.java new file mode 100644 index 00000000..bcd641a1 --- /dev/null +++ b/app/src/main/java/com/example/app/App.java @@ -0,0 +1,26 @@ +package com.example.app; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class App { + + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } + + + + // ==== 测试自动配置 ==== + @Autowired + WebInfo info; + + @Bean + public void printInfo(){ + System.out.println(info.getName()); + } + +} diff --git a/app/src/main/java/com/example/app/WebAutoConfiguration.java b/app/src/main/java/com/example/app/WebAutoConfiguration.java new file mode 100644 index 00000000..e63d295f --- /dev/null +++ b/app/src/main/java/com/example/app/WebAutoConfiguration.java @@ -0,0 +1,25 @@ +package com.example.app; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@Import(WebConfiguration.class) +@EnableConfigurationProperties(WebProperties.class) +public class WebAutoConfiguration { + + @Autowired + WebProperties properties; + + @Autowired + WebConfiguration configuration; + + @Bean + public WebInfo creatInfo(){ + return new WebInfo(configuration.name + "-"+properties.getA()); + } + +} diff --git a/app/src/main/java/com/example/app/WebConfiguration.java b/app/src/main/java/com/example/app/WebConfiguration.java new file mode 100644 index 00000000..d645d77b --- /dev/null +++ b/app/src/main/java/com/example/app/WebConfiguration.java @@ -0,0 +1,10 @@ +package com.example.app; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class WebConfiguration { + + public String name = "java"; + +} diff --git a/app/src/main/java/com/example/app/WebInfo.java b/app/src/main/java/com/example/app/WebInfo.java new file mode 100644 index 00000000..5fd4345b --- /dev/null +++ b/app/src/main/java/com/example/app/WebInfo.java @@ -0,0 +1,19 @@ +package com.example.app; + +public class WebInfo { + + public WebInfo(String name) { + this.name = name; + } + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/app/src/main/java/com/example/app/WebProperties.java b/app/src/main/java/com/example/app/WebProperties.java new file mode 100644 index 00000000..713fbeed --- /dev/null +++ b/app/src/main/java/com/example/app/WebProperties.java @@ -0,0 +1,17 @@ +package com.example.app; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "web") +public class WebProperties { + + private String a = "aaa"; + + public String getA() { + return a; + } + + public void setA(String a) { + this.a = a; + } +} diff --git a/app/src/main/resources/META-INF/spring.factories b/app/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..e69de29b diff --git a/app/src/main/resources/application.properties b/app/src/main/resources/application.properties new file mode 100644 index 00000000..5167366f --- /dev/null +++ b/app/src/main/resources/application.properties @@ -0,0 +1 @@ +web.a=kimmking diff --git a/app/src/test/java/com/example/app/AppApplicationTests.java b/app/src/test/java/com/example/app/AppApplicationTests.java new file mode 100644 index 00000000..a9afa13c --- /dev/null +++ b/app/src/test/java/com/example/app/AppApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.app; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class AppApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demo/.gitignore b/demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/demo/.mvn/wrapper/MavenWrapperDownloader.java b/demo/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..e76d1f32 --- /dev/null +++ b/demo/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,117 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if(mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if(mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if(!outputFile.getParentFile().exists()) { + if(!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/demo/.mvn/wrapper/maven-wrapper.jar b/demo/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/demo/.mvn/wrapper/maven-wrapper.jar differ diff --git a/demo/.mvn/wrapper/maven-wrapper.properties b/demo/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/demo/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/demo/mvnw b/demo/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/demo/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/demo/mvnw.cmd b/demo/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/demo/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/demo/pom.xml b/demo/pom.xml new file mode 100644 index 00000000..52076da9 --- /dev/null +++ b/demo/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demo/src/main/java/com/example/demo/DemoApplication.java b/demo/src/main/java/com/example/demo/DemoApplication.java new file mode 100644 index 00000000..64b538a1 --- /dev/null +++ b/demo/src/main/java/com/example/demo/DemoApplication.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/demo/src/main/java/com/example/demo/controller/DemoController.java b/demo/src/main/java/com/example/demo/controller/DemoController.java new file mode 100644 index 00000000..10cac5a7 --- /dev/null +++ b/demo/src/main/java/com/example/demo/controller/DemoController.java @@ -0,0 +1,17 @@ +package com.example.demo.controller; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@EnableAutoConfiguration +@RequestMapping("/demo") +public class DemoController { + + @RequestMapping("/hello") + public String hello() { + return "KK-" + System.currentTimeMillis(); + } + +} diff --git a/demo/src/main/java/com/example/demo/controller/UserController.java b/demo/src/main/java/com/example/demo/controller/UserController.java new file mode 100644 index 00000000..6a9e87f9 --- /dev/null +++ b/demo/src/main/java/com/example/demo/controller/UserController.java @@ -0,0 +1,17 @@ +package com.example.demo.controller; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@EnableAutoConfiguration +@RequestMapping("/user") +public class UserController { + + @RequestMapping("/list") + public String list() { + return "KK-" + System.currentTimeMillis(); + } + +} diff --git a/demo/src/main/resources/application.yml b/demo/src/main/resources/application.yml new file mode 100644 index 00000000..a7afc92b --- /dev/null +++ b/demo/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 8080 diff --git a/demo/src/test/java/com/example/demo/DemoApplicationTests.java b/demo/src/test/java/com/example/demo/DemoApplicationTests.java new file mode 100644 index 00000000..2778a6a7 --- /dev/null +++ b/demo/src/test/java/com/example/demo/DemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DemoApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demoidea/.gitignore b/demoidea/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/demoidea/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/demoidea/.mvn/wrapper/MavenWrapperDownloader.java b/demoidea/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/demoidea/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/demoidea/.mvn/wrapper/maven-wrapper.jar b/demoidea/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/demoidea/.mvn/wrapper/maven-wrapper.jar differ diff --git a/demoidea/.mvn/wrapper/maven-wrapper.properties b/demoidea/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/demoidea/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/demoidea/mvnw b/demoidea/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/demoidea/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/demoidea/mvnw.cmd b/demoidea/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/demoidea/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/demoidea/pom.xml b/demoidea/pom.xml new file mode 100644 index 00000000..4ca50254 --- /dev/null +++ b/demoidea/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + io.github.kimmking + demoidea + 0.0.1-SNAPSHOT + demoidea + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter-activemq + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demoidea/src/main/java/io/github/kimmking/demoidea/DemoideaApplication.java b/demoidea/src/main/java/io/github/kimmking/demoidea/DemoideaApplication.java new file mode 100644 index 00000000..ba5c61cf --- /dev/null +++ b/demoidea/src/main/java/io/github/kimmking/demoidea/DemoideaApplication.java @@ -0,0 +1,13 @@ +package io.github.kimmking.demoidea; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoideaApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoideaApplication.class, args); + } + +} diff --git a/demoidea/src/main/resources/application.properties b/demoidea/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/demoidea/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/demoidea/src/test/java/io/github/kimmking/demoidea/DemoideaApplicationTests.java b/demoidea/src/test/java/io/github/kimmking/demoidea/DemoideaApplicationTests.java new file mode 100644 index 00000000..428c4548 --- /dev/null +++ b/demoidea/src/test/java/io/github/kimmking/demoidea/DemoideaApplicationTests.java @@ -0,0 +1,13 @@ +package io.github.kimmking.demoidea; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DemoideaApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/homework2.0.md b/homework2.0.md index 0077b243..ab6193f2 100644 --- a/homework2.0.md +++ b/homework2.0.md @@ -31,19 +31,167 @@ #### 3.2-侧重应用: -1. 10-根据课程提供的场景,实现一个订单处理Service,模拟处理100万订单:后面提供模拟数据。 +1. 10-根据课程提供的场景,实现一个订单处理Service,模拟处理100万订单:后面提供模拟数据 2. 20-使用多线程方法优化订单处理,对比处理性能 3. 30-使用并发工具和集合类改进订单Service,对比处理性能 -4. 30-使用分布式集群+分库分表方式处理拆分订单,对比处理性能:第6模块讲解分库分表。 -5. 30-使用读写分离和分布式缓存优化订单的读性能:第6、8模块讲解读写分离和缓存。 +4. 30-使用分布式集群+分库分表方式处理拆分订单,对比处理性能:第6模块讲解分库分表 +5. 30-使用读写分离和分布式缓存优化订单的读性能:第6、8模块讲解读写分离和缓存 ### 4. 框架 #### 4.1 Spring AOP -1. 10-讲网关的frontend/backend/filter/router/线程池都改造成Spring配置方式; -2. 20-基于AOP改造Netty网关,filter和router使用AOP方式实现; -3. 30-基于前述改造,将网关请求前后端分离,中级使用JMS传递消息; -4. 30-尝试使用ByteBuddy实现一个简单的基于类的AOP; -5. 30-尝试使用ByteBuddy与Instrument实现一个简单JavaAgent实现无侵入下的AOP; +1. 10-讲网关的frontend/backend/filter/router/线程池都改造成Spring配置方式 +2. 20-基于AOP改造Netty网关,filter和router使用AOP方式实现 +3. 30-基于前述改造,将网关请求前后端分离,中级使用JMS传递消息 +4. 30-尝试使用ByteBuddy实现一个简单的基于类的AOP +5. 30-尝试使用ByteBuddy与Instrument实现一个简单JavaAgent实现无侵入下的AOP +#### 4.2 Spring ORM + +1. 基于AOP和自定义注解,实现@MyCache(60)对于指定方法返回值缓存60秒 +2. 自定义实现一个数据库连接池,并整合Hibernate/Mybatis/Spring/SpringBoot +3. 基于MyBatis实现一个简单的分库分表+读写分离+分布式ID生成方案 + +### 5. 数据库与性能 + +1. 模拟1000万订单数据,测试不同方式下导入导出(数据备份还原)MySQL的速度,包括jdbc程序处理和命令行处理,思考和实践,如何提升处理效率 +2. 对MySQL配置不同的数据库连接池(DBCP、C3P0、Druid、Hikari),测试增删改查100万次,对比性能,生成报告 +3. 尝试自己做一个ID生成器(可以模拟Seq或Snowflake) +4. 尝试实现或改造一个非精确分页的组件,思考是否可以用于改造自己的业务系统 +5. 基于必做作业2.0版本,实现读写分离-数据库中间件版本3.0 + +### 6. 分库分表 + +1. 思考总结常用的数据拆分和数据迁移同步方案,以及它们的优势劣势,适用场景,考虑是否可以引入到自己的工作中 +2. 设计实现一个简单的XA分布式事务框架demo,只需要能管理和调用2个MySQL的本地事务即可,不需要考虑全局事务的持久化和恢复、高可用等 +3. 设计实现一个TCC分布式事务框架的简单Demo,需要实现事务管理器,不需要实现全局事务的持久化和恢复、高可用等 +4. 设计实现一个AT分布式事务框架的简单Demo,仅需要支持根据主键id进行的单个删改操作的SQL或插入操作的事务 + +### 7. RPC与分布式服务化 + +#### 7.1 RPC与Dubbo + +1. 升级作业中的自定义RPC程序: +- 尝试使用压测并分析优化RPC性能 +- 尝试使用Netty+TCP作为两端传输方式 +- 尝试自定义二进制序列化或者使用kyro/fst等 +- 尝试压测改进后的RPC并分析优化,有问题欢迎群里讨论 +- 尝试将fastjson改成xstream +- 尝试使用字节码生成方式代替服务端反射 + +2. 尝试扩展Dubbo +- 基于上次作业的自定义序列化,实现Dubbo的序列化扩展; +- 基于上次作业的自定义RPC,实现Dubbo的RPC扩展; +- 在Dubbo的filter机制上,实现REST权限控制,可参考dubbox; +- 实现自定义Dubbo的Cluster/Loadbalance扩展,如果一分钟内调用某个服务/提供者超过10次,则拒绝提供服务直到下一分钟; +- 整合Dubbo+Sentinel,实现限流功能; +- 整合Dubbo与Skywalking,实现全链路性能监控。 + +#### 7.2 自定义RPC + +1. rpcfx1.1: 给自定义RPC实现简单的分组(group)和版本(version)。 + +2. rpcfx2.0: 给自定义RPC实现: + - 基于zookeeper的注册中心,消费者和生产者可以根据注册中心查找可用服务进行调用(直接选择列表里的最后一个)。 + - 当有生产者启动或者下线时,通过zookeeper通知并更新各个消费者,使得各个消费者可以调用新生产者或者不调用下线生产者。 + +3. 在2.0的基础上继续增强rpcfx实现: +- 3.0: 实现基于zookeeper的配置中心,消费者和生产者可以根据配置中心配置参数(分组,版本,线程池大小等)。 +- 3.1:实现基于zookeeper的元数据中心,将服务描述元数据保存到元数据中心。 +- 3.2:实现基于etcd/nacos/apollo等基座的配置/注册/元数据中心。 + +4. 在3.2的基础上继续增强rpcfx实现: +- 4.0:实现基于tag的简单路由; +- 4.1:实现基于Weight/ConsistentHash的负载均衡; +- 4.2:实现基于IP黑名单的简单流控; +- 4.3:完善RPC框架里的超时处理,增加重试参数; + +5. 在4.3的基础上继续增强rpcfx实现: +- 5.0:实现利用HTTP头跨进程传递Context参数(隐式传参); +- 5.1:实现消费端mock一个指定对象的功能(Mock功能); +- 5.2:实现消费端可以通过一个泛化接口调用不同服务(泛化调用); +- 5.3:实现基于Weight/ConsistentHash的负载均衡; +- 5.4:实现基于单位时间调用次数的流控,可以基于令牌桶等算法; + +6. 实现最终版本6.0:压测并分析调优5.4版本。 + +### 8. 分布式缓存 + +1. 基于其他各类场景,设计并在示例代码中实现简单demo: +- 实现分数排名或者排行榜; +- 实现全局ID生成; +- 基于Bitmap实现id去重; +- 基于HLL实现点击量计数。 +- 以redis作为数据库,模拟使用lua脚本实现前面课程的外汇交易事务。 + +2. 升级改造项目: +- 实现guava cache的spring cache适配; +- 替换jackson序列化为fastjson或者fst,kryo; +- 对项目进行分析和性能调优。 + +3. 以redis作为基础实现上个模块的自定义rpc的注册中心; +4. 练习redission的各种功能; +5. 练习hazelcast的各种功能; +6. 搭建hazelcast 3节点集群,写入100万数据到一个map,模拟和演示高可用,测试一下性能。 + +### 9. 分布式消息 + +#### 9.1 消息队列原理与应用 + +1. 基于数据库的订单表,模拟消息队列处理订单: +- 一个程序往表里写新订单,标记状态为未处理(status=0); +- 另一个程序每隔100ms定时从表里读取所有status=0的订单,打印一下订单数据,然后改成完成status=1; +- 考虑失败重试策略,考虑多个消费程序如何协作; +- 将上述订单处理场景,改成使用ActiveMQ发送消息处理模式; +- 使用java代码,创建一个ActiveMQ Broker Server,并测试它; + +2. ActiveMQ/RabbitMQ作业 +- 搭建ActiveMQ的network集群和master-slave主从结构; +- 基于ActiveMQ的MQTT实现简单的聊天功能或者Android消息推送; +- 创建一个RabbitMQ,用Java代码实现简单的AMQP协议操作; +- 搭建RabbitMQ集群,重新实现前面的订单处理; +- 使用Apache Camel打通上述ActiveMQ集群和RabbitMQ集群,实现所有写入到ActiveMQ上的一个队列q24的消息,自动转发到RabbitMQ; +- 压测ActiveMQ和RabbitMQ的性能; + +3. 演练本课提及的各种生产者和消费者特性。 + +4. Kafka金融领域实战:在证券或者外汇、数字货币类金融核心交易系统里,对于订单的处理,大概可以分为收单、定序、撮合、清算等步骤。其中我们一般可以用mq来实现订单定序,然后将订单发送给撮合模块。 +- 收单:请实现一个订单的rest接口,能够接收一个订单Order对象; +- 定序:将Order对象写入到kafka集群的order.usd2cny队列,要求数据有序并且不丢失; +- 撮合:模拟撮合程序(不需要实现撮合逻辑),从kafka获取order数据,并打印订单信息,要求可重放, 顺序消费, 消息仅处理一次。 + +#### 9.2 自定义消息中间件 + +1. v1.0-内存队列:基于内存Queue实现生产和消费API(示例代码已经完成) +- 创建内存BlockingQueue,作为底层消息存储 +- 定义Topic,支持多个Topic +- 定义Producer,支持Send消息 +- 定义Consumer,支持Poll消息 + +2. v2.0-自定义队列:去掉内存Queue,设计自定义Queue,实现消息确认和消费offset +- 自定义内存Message数组模拟Queue。 +- 使用指针记录当前消息写入位置。 +- 对于每个命名消费者,用指针记录消费位置。 + +3. v3.0-基于SpringMVC实现MQServer:拆分broker和client(包括producer和consumer),从单机走向服务器模式。 +- 将Queue保存到web server端 +- 设计消息读写API接口,确认接口,提交offset接口 +- producer和consumer通过httpclient访问Queue +- 实现消息确认,offset提交 +- 实现consumer从offset增量拉取 + +4. v4.0-功能全面:增加多种策略(各条之间没有关系,可以任意选择实现),基于TCP实现server->client,从而实现 PUSH模式 +- 考虑实现消息过期,消息重试,消息定时投递等策略 +- 考虑批量操作,包括读写,可以打包和压缩 +- 考虑消息清理策略,包括定时清理,按容量清理、LRU等 +- 考虑消息持久化,存入数据库,或WAL日志文件,或BookKeeper +- 考虑将spring mvc替换成netty下的tcp传输协议,rsocket/websocket + +5. v5.0-优化完善:对接各种技术(各条之间没有关系,可以任意选择实现) +- 考虑封装 JMS 1.1 接口规范 +- 考虑实现 STOMP 消息规范 +- 考虑实现消息事务机制与事务管理器 +- 对接Spring +- 对接Camel或Spring Integration +- 优化内存和磁盘的使用 diff --git a/java11/.gitignore b/java11/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/java11/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/java11/.mvn/wrapper/MavenWrapperDownloader.java b/java11/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/java11/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/java11/.mvn/wrapper/maven-wrapper.jar b/java11/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/java11/.mvn/wrapper/maven-wrapper.jar differ diff --git a/java11/.mvn/wrapper/maven-wrapper.properties b/java11/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/java11/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/java11/klass.png b/java11/klass.png new file mode 100644 index 00000000..264750de Binary files /dev/null and b/java11/klass.png differ diff --git a/java11/list.png b/java11/list.png new file mode 100644 index 00000000..02b2f4f7 Binary files /dev/null and b/java11/list.png differ diff --git a/java11/map.png b/java11/map.png new file mode 100644 index 00000000..034e4917 Binary files /dev/null and b/java11/map.png differ diff --git a/java11/mvnw b/java11/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/java11/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/java11/mvnw.cmd b/java11/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/java11/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/java11/pom.xml b/java11/pom.xml new file mode 100644 index 00000000..19b803dc --- /dev/null +++ b/java11/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 11 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.openjdk.jol + jol-core + 0.16 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/java11/src/main/java/com/example/demo/DemoApplication.java b/java11/src/main/java/com/example/demo/DemoApplication.java new file mode 100644 index 00000000..f314b278 --- /dev/null +++ b/java11/src/main/java/com/example/demo/DemoApplication.java @@ -0,0 +1,146 @@ +package com.example.demo; + +import org.openjdk.jol.info.ClassLayout; +import org.openjdk.jol.info.GraphLayout; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.io.IOException; +import java.util.*; + +@SpringBootApplication +public class DemoApplication implements ApplicationRunner { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + @Override + public void run(ApplicationArguments args) throws Exception { + +// ClassLoader app = this.getClass().getClassLoader(); +// System.out.println(" APP Classloader => " + app.getName()); +// for (Package definedPackage : app.getDefinedPackages()) { +// System.out.println(definedPackage.getName()); +// } + + + printObject(); + printString(); + printIntArray(); + + printUser(); + + printListGraph(); + } + + private void printListGraph() { + + Random random = new Random(); + List list = new ArrayList<>(128); + for (int i = 0; i < 128; i++) { + list.add(i, random.nextInt(128)); + } + + try { + System.out.println(" ===> GraphLayout.parseInstance(list).toImage(\"list.png\")"); + GraphLayout.parseInstance(list).toImage("list.png"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Map map = new HashMap(256); + for (int i = 0; i < 128; i++) { + map.put("K" + i, i); + } + + try { + System.out.println(" ===> GraphLayout.parseInstance(map).toImage(\"map.png\"))"); + GraphLayout.parseInstance(map).toImage("map.png"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + private void printUser() { + + System.out.println(" ===> ClassLayout.parseInstance(user).toPrintable()"); + User user = new User(true, (byte) 65,12345, 8888, 12345678L, 9999999L, "a"); + System.out.println(ClassLayout.parseInstance(user).toPrintable()); + + System.out.println(" ===> GraphLayout.parseInstance(user).toPrintable()"); + System.out.println(GraphLayout.parseInstance(user).toPrintable()); + + System.out.println(" ===> GraphLayout.parseInstance(user).toFootprint()"); + System.out.println(GraphLayout.parseInstance(user).toFootprint()); + +// try { +// GraphLayout.parseInstance(user).toImage("user.png"); +// } catch (IOException e) { +// e.printStackTrace(); +// } + + User user1 = new User(false, (byte) 66,12346, 5555, 12345679L, 888888L, "b"); + Klass klass = new Klass("Klass1", Arrays.asList(user,user1)); + + System.out.println(" ===> ClassLayout.parseInstance(klass).toPrintable()"); + System.out.println(ClassLayout.parseInstance(klass).toPrintable()); + + System.out.println(" ===> GraphLayout.parseInstance(klass).toImage(\"klass.png\")"); + try { + GraphLayout.parseInstance(klass,user,user1).toImage("klass.png"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + private void printObject() { + System.out.println(ClassLayout.parseInstance(new Object()).toPrintable()); + } + + private void printString() { + System.out.println(ClassLayout.parseInstance("abcde12345").toPrintable()); + } + + private void printIntArray() { + System.out.println(ClassLayout.parseInstance(new int[2]).toPrintable()); + System.out.println(ClassLayout.parseInstance(new int[12]).toPrintable()); + System.out.println(ClassLayout.parseInstance(new int[2][50]).toPrintable()); + System.out.println(ClassLayout.parseInstance(new int[50][2]).toPrintable()); + } + + public static class User { + public User(boolean b1, byte b2, int a, Integer b, long c, Long d, String s) { + this.b1 = b1; + this.b2 = b2; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.s = s; + } + + private boolean b1; + private byte b2; + private int a; + private Integer b; + private long c; + private Long d; + private String s; + } + + public static class Klass { + private String klassName; + private List users; + + public Klass(String klassName, List users) { + this.klassName = klassName; + this.users = users; + } + } + +} diff --git a/java11/src/main/resources/application.properties b/java11/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/java11/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/java11/src/test/java/com/example/demo/DemoApplicationTests.java b/java11/src/test/java/com/example/demo/DemoApplicationTests.java new file mode 100644 index 00000000..de52af5b --- /dev/null +++ b/java11/src/test/java/com/example/demo/DemoApplicationTests.java @@ -0,0 +1,13 @@ +//package com.example.demo; +// +//import org.junit.jupiter.api.Test; +//import org.springframework.boot.test.context.SpringBootTest; +// +//@SpringBootTest +//class DemoApplicationTests { +// +// @Test +// void contextLoads() { +// } +// +//} diff --git a/java11/user.png b/java11/user.png new file mode 100644 index 00000000..c7c20997 Binary files /dev/null and b/java11/user.png differ diff --git a/test.txt b/test.txt new file mode 100644 index 00000000..880d7876 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +nohup java -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnSafepointTimeout -XX:+ShowMessageBoxOnError -Xmx1g -Xms1g -XX:+SafepointTimeout -XX:SafepointTimeoutDelay=100 -Xlog:gc*=trace,heap*=trace,safepoint*=trace,logging*=trace:file=gc-%p-%t.log:pid,utctime,level,tags:filecount=100,filesize=10m -jar gateway-server-0.0.1-SNAPSHOT.jar & \ No newline at end of file diff --git "a/\344\275\234\344\270\232\346\263\250\346\204\217\344\272\213\351\241\271.md" "b/\344\275\234\344\270\232\346\263\250\346\204\217\344\272\213\351\241\271.md" new file mode 100644 index 00000000..46e0b9aa --- /dev/null +++ "b/\344\275\234\344\270\232\346\263\250\346\204\217\344\272\213\351\241\271.md" @@ -0,0 +1,315 @@ +# 作业注意事项 + + +> 课程内容: + +课程1、Java中的多线程与并发编程,某一线公司技术专家任富飞。 +Part1:11-22,周一,18:45-19:45,主要内容: + +Java多线程原理、各种锁的机制和应用(重量级锁、轻量级锁,可重入锁,偏向锁,公平锁,自旋锁,读写锁,乐观锁等)、四种线程池的原理、类型、配置参数和经验,Spring中线程池的应用,几种常用原子类的原理和使用场景。 + +Part2:11-26,周五,18:45-19:45,主要内容:AQS原理,三种常用并发工具类(CountdownLatch/Semaphore/CyclicBarrier)使用场景示例,线程安全编程和优化经验。 + +课程2、JVM的体系发展与优化经验,长亮科技北京平台团队负责人秦金卫。 +Part1:11-29,周一,18:45-19:45,主要内容:JVM基础(字节码、内存模型、类加载器),各种不同的GC(串行GC、并行GC、CMS、G1、ZGC)的基本原理、常用参数和分析经验。 +Part2:12-03,周五,18:45-19:45,主要内容:常见JVM命令行工具、可视化工具,以及高级工具的使用,通过GC日志、线程堆栈、飞行记录以及可视化分析等方式,实现JVM运行状态的分析和优化,常见的JVM问题排查。 + +课程3:事务原理与Spring事务机制,京东科技技术专家、Apache Shenyu项目负责人、《分布式事务原理与实战》作者肖宇。 +Part1:12-06,周一,18:45-19:45,主要内容:关系数据库的事务机制和隔离级别(读未提交/读已提交/可重复读/串行化),Spring TX事务管理器的功能特性、使用方式。 +Part2:12-10,周五,18:45-19:45,主要内容:Spring TX事务管理器的原理设计,相关的API和注解,传播特性,跟其他框架的集成,以及常见问题处理经验。 + +课程4、网络编程NIO与Netty实践,长亮科技北京平台团队负责人秦金卫。 +Part1:12-13,周一,18:45-19:45,主要内容:Java网络编程模型,BIO/NIO/AIO,Reactor/Proactor网络模型,select/epoll机制; +Part2:12-17,周五,18:45-19:45,主要内容:Netty的基本原理、常见用法与网络编程优化经验,Channel/EventLoop/Handler/Adapter + + +> 批阅原则: + +- 1. 认可与表扬认真学习的同学, 尽量给与正面反馈, 加油打气。 +- 2. 对一些问题和不足进行友善的提示, 都是成年人, 大家都要面子。 +- 3. 引导同学们学习各种进阶知识,拓展知识点。 + + +简介: + +``` +铁锚:系统架构师,Java性能调优专家。 +CSDN博客专家: https://renfufei.blog.csdn.net/ +热爱程序开发和设计; 积极应对各种情境和挑战; +喜欢钻研新技术, 闲暇时喜欢翻译和分析英文文档/技术博客。 +技术文章翻译仓库: https://github.com/cncounter/translation/ +``` + + + +## 1周 + + +提示: +- 尝试自己从头绘制一幅图形,完善后向自己的同事或者朋友介绍,加深自己的印象和理解 +- JDK7及之前的版本是永久代, 对应到JDK8及以后是Meta区, class信息保存着方法区之中。 +- 常量池位于方法区/Meta区之中, Mata区归属于非堆内存(Non-Heap),不是堆外内存,也不是直接内存 +- CCS是开启指针压缩时才有的, 关闭指针压缩则此空间的大小为0 +- 直接内存(Direct)属于堆外内存,不属于非堆 +- 堆外内存可简单划分为(Direct, Native), 本次作业对应的所有内存区域都在JVM进程内部 +- JIT编译后的机器代码缓存到 code cache/非堆之中; +- `-Xmx` 和 `-Xms` 设置的是整个堆内存的大小,不是老年代。 +- 和年轻代(young/nursery)不一样,新生代实际上是 Eden 区, 由 `-Xmn` 与存活区的比例来控制 +- 栈内存不在堆内存之中,也不属于堆外内存,是一个独立的部分。 +- 注意区分线程栈以及内部的栈帧结构; `-Xss` 控制单个线程的栈空间最大大小 +- 编码风格: 建议增加中间变量,增加可读性,可读性和可维护性是构建大型复杂系统的基石。 +- 注意关闭输入输出流,最好是谁控制着打开的,就由谁在同一个方法内将其关闭。 +- 在开发中可以打断点调试,查看class加载时的调用栈和调用关系。 +- 作业参考链接: https://github.com/JavaCourse00/JavaCourseCodes/tree/main/01jvm +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的翻译仓库: + + + +## 2周 + +认真完成作业和整理笔记 + +- 在finally中关闭输入输出流是一个好习惯; 每次提交认真写提交日志也是一个好习惯。 +- 分辨HTTP1.0与HTTP1.1的区别; +- 可以尝试分析G1中混合模式 mixed GC的威力; 选择哪种垃圾收集器,主要考虑的是业务特征(实时/非实时),跟内存大小关系并不太大。 +- 与外部系统的交互,在允许的情况下,最好是输出详细的交互日志, 包括: [uri,请求参数/body,响应信息,耗时信息]等等。 +- 性能测试时需要排除各种干扰,只改变单个变量然后进行对比,例如: 客户端线程数,并发连接数/用户数,去除随机数的随机性(指定random种子)等等。 +- 编码风格: 建议增加中间变量,增加可读性,可读性和可维护性是构建大型复杂系统的基石。 +- 建议: 适当使用一些通用工具类,例如 apache的commons-io等等,里面的 IOUtils之类的工具很好用。 +- 建议:趁机使用和学习 markdown 文件,做好目录和链接引用,在互联网上很方便,而且可以导出为各种格式。常用工具: Typora 等。 + +- 往期优秀作业链接: +- 作业参考链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的翻译仓库: 【可以找感兴趣的技术文章看看】 + + + + + +## 3周 + + +提示: + +- 梳理一下网关需要执行的操作和处理逻辑,对比一下Nginx是怎么处理的。 +- 使用自己的包名, 来标识自己的作品 +- 通过 .gitignore 文件来忽略某些文件。 +- 性能测试时需要排除各种干扰,只改变单个因素, 然后进行对比,例如: 客户端线程数,并发连接数/用户数等等。 +- 不要用实例变量来存储某些信息,避免多线程环境下的出错,以及多次请求互相污染。 +- 适当增加必要的注释来增加可读性,说明某段代码的作用和实现逻辑,但不要太多或者太少 ^_^ +- 响应头之中也可以加上某些 X-开始的header,方便调试和追踪,更像一个网关 +- 响应消息写入完成后, 记得 flush 和 close。推荐在finally中关闭,避免中间过程抛异常导致资源不关闭,引发内存泄漏, 在高并发场景中造成问题。 +- 可以了解一下 ByteArrayOutputStream,挺好用. +- 注意Java输入流的特征,以及基于buffer的读取和写入方法, 不要写多了, 也不要读漏了. +- 注意区分 HTTP1.0 和 HTTP1.1的差异 +- 所有使用 getBytes 和String相关的方法都需要指定具体的编码, 避免默认语言问题。 + +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: + + + +## 4周 + +笔记整理。 + +提示: +- 请复习创建线程池的方法, 分辨 cached 方式的线程池是先加队列还是先创将cache线程? 提示: core,queue,cache,reject +- 多线程代码中,共享状态的变量需要特殊处理,比如使用 volatile 来强制从内存读取最新值,或者直接使用原子类来保存。 +- 思考各种线程协同方式的本质是什么? +- 想想怎么让线程处于各种不同的状态, 以及怎么获取这些状态信息 +- 线程最重要的属性是哪些? +- 尝试使用反射方式在main方法中将这些方法全部调用起来 +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: + +并发相关的面试题,在【第一课】【预习资料以及环境准备】里面的压缩包之中。 + + + + +## 5周 + + +提示: +- 枚举实际上是可以通过反射往里面加东西的,当然,在编译期间不可见,属于运行时hack修改。 +- 反编译工具可以使用: jd-gui, 或者 jclasslib +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 深入系列: InnoDB存储引擎: + + + + +## 6 周 + +提示: +- 建议: 在Table级别使用 utf8mb4 字符集 +- 设计合理的SQL语句和注释,并通过Git形成版本化。 +- 使用BIGINT来存储Long类型的时间戳timeMillis,减少对时区的依赖。 +- 可以根据业务需要, 增加联合索引。 +- 可以结合自己在网上购物的经历, 思考如何设计订单表。 +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- [InnoDB的锁和事务模型](https://github.com/cncounter/translation/blob/master/tiemao_2020/44_innodb-storage-engine/14.7_innodb-locking-transaction-model_CN.md) + + +通过上周的课程学习, 经过本次作业实践, 请思考以下问题: +1. MySQL数据库中, int(2) 和 int(10) 有什么区别? +2. 订单表中的【创建时间】字段采用以下类型, 各自的优缺点是什么? +- A、 datetime +- B、 timestamp +- C、 bigint +- D、 int +- E、 varchar + + +## 7周 + + +提示: +0. 想一想限制数据库TPS的瓶颈点有哪些? 分析整个请求执行链路上的各个环节, 并尝试解决各种环节下的瓶颈。 +1. 可以使用aop来做动态切换,根据sql类型来做数据源切换不能很好支持强制走主库查询的情况 +2. 注解时考虑使用数据源名称,而不是master-slave;应用内可能有多个数据库组(比如账号数据一套主从,订单数据一套主从) +3. 考虑多个从库的情况 +4. 注解加到mapper(dao层),而不是service层;具体实现可以考虑各种情景确定是否采用注解赋予的建议。 +5. aop可以看下AbstractPointcutAdvisor +6. 《Java资料链接汇总》: +7. 可以参考的作业实现: + - + - +8. 秦老师推荐书单: +9. 铁锚的技术文章翻译: + + + +## 8周 + + +提示: + +- 想想什么情况下适合使用分表,什么情况下适合使用分库,各有什么优缺点? +- 注意分库与分表的表达式, 避免分布不均匀或者导致空表 +- 适当增减注释来说明每一块代码的意图。 +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: +- CSDN博客: https://renfufei.blog.csdn.net/ +- 优秀作业参考: + - + - + - + + + + +## 9周 + + +提示: + +- id 可以使用 BIGINT(20), 更加专业一些。 +- 根据实际情况, 可以在某些情况下, 使用 BIGINT 来存储金额,单位则约定为"分", 由程序进行换算, 避免程序层面出现精度丢失。 +- 如果使用BigDecimal计算, 则一定要指定精度和四舍五入规则; 并且与数据库字段的精度对应。 +- 《Java资料链接汇总》: +- 往期优秀作业链接: +- 李睿老师的完整RPC实现: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: + + + + + +## 10周 + + +请思考以下问题: + +- 通过这个作业有哪些收获? +- RPC通信中有哪些需要注意的问题? +- 序列化和反序列化框架有哪些?需要注意什么? +- 往期优秀作业链接: +- 秦老师推荐书单: +- Java资料链接汇总: +- 李睿老师的完整实现: +- 铁锚的技术文章翻译: + + + +## 11周 + + +提示: +1. 锁的实现需要考虑加锁时的失败自动重试机制以及自动过期 +2. 正常释放锁时要判断lock值是否一致,最好是通过 lua 脚本来保证操作的原子性。 +3. 推荐使用 redisson,封装的 api 简单易用。 +4. 分布式减库存操作需要加锁,而且需要在库存上加锁,不能使用 userId 等 +5. 推荐阅读 《Redis 操作手册; 基于Redis 5版本,微信读书可以免费阅读 +6. 往期优秀作业链接: https://github.com/lw1243925457/JAVA-000 +7. 《Java资料链接汇总: https://shimo.im/docs/YGjGgTWwgD6V3wkp/ +8. 秦老师推荐书单: https://kimmking.github.io/ +9. Redis集群入门简介[系列文章]: https://github.com/cncounter/translation/tree/master/tiemao_2021/31_redis_cluster +10. 免费好用的Redis图形界面客户端: Another: https://gitee.com/qishibo/AnotherRedisDesktopManager/releases + + + +## 12周 + +锻炼动手能力, 加油! + +提示: +1. 尝试分析一下本地缓存与分布式缓存的适用场景。 +2. 尝试分析缓存数据的特征: 变化频率, 数据量, 不一致的容忍时间, 网络带宽占用压力,延迟以及并发请求数... +3. 总结消息队列的优缺点: 异步解耦、削峰填谷、增强并发性能和横向扩展能力... + +- 往期优秀作业链接: +- 《Java资料链接汇总: +- Redis集群入门教程(中文翻译): +- 免费好用的Redis图形界面客户端: Another: https://gitee.com/qishibo/AnotherRedisDesktopManager/releases + + + + +## 13周 + + +请思考以下问题: + +- 生产端如何保证消息的顺序性? +- 消费端如何保证消息的顺序性? +- 如何防止消息的重复消费? +- 如何防止消费失败或者消息丢失? +- 幂等性是怎么回事?如何保证?在哪个层级保证? + +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: https://kimmking.github.io/ +- 铁锚的翻译仓库: + + + +## 14周 + + +- 往期优秀作业链接: +- 《Java资料链接汇总: +- 秦老师推荐书单: https://kimmking.github.io/ +- 铁锚的翻译仓库: + + + +## 毕业总结: + +恭喜你完成学业,祝未来的工作和学习旅程越来越顺利! +认真总结,这份坚持和努力值得肯定。 +提示: 很多东西用自己的语言来转述一遍,收获和理解会更深刻。 聚沙成塔的同时,也需要梳理知识体系,形成自己的核心脉络与钢筋铁骨。