{"id":15068910,"url":"https://github.com/leihuazhe/java11-netty-demo","last_synced_at":"2025-04-10T17:11:49.151Z","repository":{"id":50356836,"uuid":"150584174","full_name":"leihuazhe/Java11-Netty-Demo","owner":"leihuazhe","description":"A mini RPC client server demo based on Java 11, using its modularization features.","archived":false,"fork":false,"pushed_at":"2023-12-05T22:19:20.000Z","size":33,"stargazers_count":19,"open_issues_count":1,"forks_count":7,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-24T14:51:23.265Z","etag":null,"topics":["java","java11","netty"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leihuazhe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-27T12:35:26.000Z","updated_at":"2024-09-30T00:30:40.000Z","dependencies_parsed_at":"2024-09-29T13:40:58.041Z","dependency_job_id":"31f21c46-1be2-4129-9c43-cfec98fe2d55","html_url":"https://github.com/leihuazhe/Java11-Netty-Demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leihuazhe%2FJava11-Netty-Demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leihuazhe%2FJava11-Netty-Demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leihuazhe%2FJava11-Netty-Demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leihuazhe%2FJava11-Netty-Demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leihuazhe","download_url":"https://codeload.github.com/leihuazhe/Java11-Netty-Demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248261770,"owners_count":21074223,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","java11","netty"],"created_at":"2024-09-25T01:39:42.385Z","updated_at":"2025-04-10T17:11:49.115Z","avatar_url":"https://github.com/leihuazhe.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Java11-Netty-Demo\n\nJava11 基于maven构建的简单的服务端客户端分离模块调用的例子\n\n## Java 11 \n\u003e 从 Java9 开始引入了模块化的概念。使用Java11 也需要以模块化的方式进行项目的构建和编译。\n\n### `Maven` 项目支持\n\u003e 为支持 Java9 之后的模块化， Maven 很多插件需要升级到比较新的版本。\n\n#### 编译器插件\n\n为了能够使用JDK 9 的模块系统 maven-compiler-plugin 版本3.6.1或更高版本是必需的。\n```xml\n\u003cplugin\u003e\n     \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n     \u003cartifactId\u003emaven-compiler-plugin\u003c/artifactId\u003e\n     \u003cversion\u003e3.8.0\u003c/version\u003e\n     \u003cconfiguration\u003e\n           \u003csource\u003e11\u003c/source\u003e\n           \u003ctarget\u003e11\u003c/target\u003e\n           \u003cencoding\u003eutf8\u003c/encoding\u003e\n     \u003c/configuration\u003e\n\u003c/plugin\u003e\n\n```\n\n#### 工具链插件\n这或多或少是可选的，但是我强烈建议您使用它。\n由于现在Java版本更新很快，但是大部分项目还是基于 Java8 甚至更低版本。不适宜更改项目所有的环境变量，并将其指向JDK11的主目录。\n使用 maven-toolchains-plugin使您能够轻松地使用各种环境。\n\n创建$HOME/.m2/toolchains.xml（或 %USERPROFILE%\\.m2\\toolchains.xml在Windows上），如果你还没有的话。\n```xml\n\u003ctoolchains\u003e\n  \u003ctoolchain\u003e\n    \u003ctype\u003ejdk\u003c/type\u003e\n    \u003cprovides\u003e\n      \u003cversion\u003e11\u003c/version\u003e\n      \u003cvendor\u003eoracle\u003c/vendor\u003e\n    \u003c/provides\u003e\n    \u003cconfiguration\u003e\n      \u003c!-- Change path to JDK9 --\u003e\n      \u003cjdkHome\u003e/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home\u003c/jdkHome\u003e\n    \u003c/configuration\u003e\n\u003c/toolchain\u003e\n\n\u003ctoolchain\u003e\n    \u003ctype\u003ejdk\u003c/type\u003e\n    \u003cprovides\u003e\n      \u003cversion\u003e8\u003c/version\u003e\n      \u003cvendor\u003eoracle\u003c/vendor\u003e\n    \u003c/provides\u003e\n    \u003cconfiguration\u003e\n      \u003cjdkHome\u003e/Library/Java/JavaVirtualMachines/jdk-8.jdk/Contents/Home\u003c/jdkHome\u003e\n    \u003c/configuration\u003e\n  \u003c/toolchain\u003e\n\u003c/toolchains\u003e\n\n```\n\n- 注意：将配置文件中 \u003cjdkHome\u003e  更改为实际的JDK安装HOME。\n\n\n##### 项目主POM 文件 添加 工具链插件\n```xml\n\u003cplugin\u003e\n  \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n  \u003cartifactId\u003emaven-toolchains-plugin\u003c/artifactId\u003e\n  \u003cversion\u003e1.1\u003c/version\u003e\n  \u003cconfiguration\u003e\n    \u003ctoolchains\u003e\n        \u003cjdk\u003e\n            \u003cversion\u003e11\u003c/version\u003e\n            \u003cvendor\u003eoracle\u003c/vendor\u003e\n        \u003c/jdk\u003e\n    \u003c/toolchains\u003e\n  \u003c/configuration\u003e\n  \u003cexecutions\u003e\n    \u003cexecution\u003e\n          \u003cgoals\u003e\n            \u003cgoal\u003etoolchain\u003c/goal\u003e\n        \u003c/goals\u003e\n    \u003c/execution\u003e\n  \u003c/executions\u003e\n\u003c/plugin\u003e\n\n```\n\n\n### 启用Java 11 （9）语言支持\n\n```xml\n\u003cproperties\u003e\n  \u003cmaven.compiler.release\u003e11\u003c/maven.compiler.release\u003e\n  \u003cmaven.compiler.source\u003e11\u003c/maven.compiler.source\u003e\n  \u003cmaven.compiler.target\u003e11\u003c/maven.compiler.target\u003e\n\u003c/properties\u003e\n\n```\n\n属性 maven.compiler.release直接映射到该--release标志javac\n另外两个属性只对IntelliJ有必要 ，用来了解源码兼容性。\n\n```xml\n\u003cplugin\u003e\n  \u003cgroupId\u003eorg.apache.maven.plugins\u003c/groupId\u003e\n  \u003cartifactId\u003emaven-compiler-plugin\u003c/artifactId\u003e\n  \u003cversion\u003e${maven-compiler-plugin.version}\u003c/version\u003e\n  \u003c!--\n    Fix breaking change introduced by JDK-8178012: Finish removal of -Xmodule\n    Reference:  http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8178012\n  --\u003e\n  \u003cexecutions\u003e\n    \u003cexecution\u003e\n      \u003cid\u003edefault-testCompile\u003c/id\u003e\n      \u003cphase\u003etest-compile\u003c/phase\u003e\n      \u003cgoals\u003e\n        \u003cgoal\u003etestCompile\u003c/goal\u003e\n      \u003c/goals\u003e\n      \u003cconfiguration\u003e\n        \u003cskip\u003etrue\u003c/skip\u003e\n      \u003c/configuration\u003e\n    \u003c/execution\u003e\n  \u003c/executions\u003e\n  \u003cconfiguration\u003e\n    \u003cshowWarnings\u003etrue\u003c/showWarnings\u003e\n    \u003cshowDeprecation\u003etrue\u003c/showDeprecation\u003e\n  \u003c/configuration\u003e\n\u003c/plugin\u003e\n```\n\n\n### Netty with  java 11\n\n项目结构，包含 4 大模块，分别是：\n- hello-api\n- hello-common\n- hello-client\n- hello-service\n\n每个模块src根目录下都有一个 `module-info.java` 文件用来定义模块\n\n```\n\n├── hello-api\n│   ├── src\n│   │   ├── main\n│   │   │   ├── java\n│   │   │   │   ├── com\n│   │   │   │   │   └── maple\n│   │   │   │   │       └── hello\n│   │   │   │   │           ├── HelloRequest.java\n│   │   │   │   │           └── HelloResponse.java\n│   │   │   │   └── module-info.java\n│   │   │   └── resources\n│   \n├── hello-client\n│   ├── src\n│   │   ├── main\n│   │   │   ├── java\n│   │   │   │   ├── com\n│   │   │   │   │   └── maple\n│   │   │   │   │       └── hello\n│   │   │   │   │           └── client\n│   │   │   │   │               ├── AppClient.java\n│   │   │   │   │               ├── Main.java\n│   │   │   │   │               ├── netty\n│   │   │   │   │               │   ├── NettyClient.java\n│   │   │   │   │               │   └── handler\n│   │   │   │   │               │       ├── RpcClientHandler.java\n│   │   │   │   │               │       ├── RpcClientMsgDecoder.java\n│   │   │   │   │               │       └── RpcClientMsgEncoder.java\n│   │   │   │   │               └── service\n│   │   │   │   │                   └── HelloClient.java\n│   │   │   │   └── module-info.java\n│   │   │   └── resources\n│   │   │       └── logback.xml\n│   │   └── test\n│   │       └── java\n│   \n├── hello-common\n│   ├── src\n│   │   ├── main\n│   │   │   ├── java\n│   │   │   │   ├── com\n│   │   │   │   │   └── maple\n│   │   │   │   │       └── hello\n│   │   │   │   │           └── common\n│   │   │   │   │               ├── Constants.java\n│   │   │   │   │               ├── DumpUtil.java\n│   │   │   │   │               ├── RpcException.java\n│   │   │   │   │               └── netty\n│   │   │   │   │                   └── RpcFrameDecoder.java\n│   │   │   │   └── module-info.java\n│   │   │   └── resources\n│   └── \n├── hello-service\n│   ├── hello-service.iml\n│   ├── pom.xml\n│   ├── src\n│   │   ├── main\n│   │   │   ├── java\n│   │   │   │   ├── com\n│   │   │   │   │   └── maple\n│   │   │   │   │       ├── AppServer.java\n│   │   │   │   │       ├── hello\n│   │   │   │   │       │   └── service\n│   │   │   │   │       │       ├── HelloService.java\n│   │   │   │   │       │       └── Person.java\n│   │   │   │   │       └── netty\n│   │   │   │   │           ├── NettySimpleServer.java\n│   │   │   │   │           └── handler\n│   │   │   │   │               ├── RpcLogHandler.java\n│   │   │   │   │               ├── RpcMsgDecoder.java\n│   │   │   │   │               ├── RpcMsgEncoder.java\n│   │   │   │   │               └── ServerHandler.java\n│   │   │   │   └── module-info.java\n│   │   │   └── resources\n│   │   │       └── logback.xml\n\n```\n\nDEBUG 错误1:\n\n- Reflective setAccessible(true) disabled\n\nTODO\n\n## 迁移注意事项\n### 1. JavaEE 模块被移除\nJava11 移除了 JavaEE 模块,所以很多诸如 javax JAXB 等已经被移除。\n如果旧版本的项目有依赖 Javaee的组件，需要单独加入 javaee-api\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ejavax\u003c/groupId\u003e\n    \u003cartifactId\u003ejavaee-api\u003c/artifactId\u003e\n    \u003cversion\u003e8.0\u003c/version\u003e\n    \n\u003c/dependency\u003e\n```\n\n### 2.模块化 api\nexports 和 exports to 指令\nexports 指令用于指定一个模块中哪些包对外是可访问的，而 exports…to 指令则用来限定哪些模块可以访问导出类，允许开发者通过逗号分隔的列表指定哪些模块及模块的哪些代码可以访问导出的包，这种方式也称为限定导出(qualified export)。\n\nuse 指令\nuse 指令用于指定一个模块所使用的服务，使模块成为服务的消费者，服务其实就是一个实现了某个接口或抽象类的对象。\n\nprovides…with 指令\n该指令用于说明模块提供了某个服务的实现，因此模块也称为服务提供者。provides 后面跟接口名或抽象类名，与 use 指令后的名称一致，with 后面跟实现类该接口或抽象类的类名。\n\nopen, opens, opens…to 指令\n在 Java 9 之前，我们可以通过反射技术来获取某个包下所有的类及其内部乘员的信息，即使是 private 类型我们也能获取到，所以类信息并不是真的与外界完全隔离的。而模块系统的主要目标之一就是实现强封装，默认情况下，除非显式地导出或声明某个类为 public 类型，那么模块中的类对外部都是不可见的，模块化要求我们对外部模块应最小限度地暴露包的范围。open 相关的指令就是用来限制在运行时哪些类可以被反射技术探测到。\n\n首先我们先看 opens 指令，语法如下：\nopens package\nopens 指令用于指定某个包下所有的 public 类都只能在运行时可被别的模块进行反射，并且该包下的所有的类及其乘员都可以通过反射进行访问。\n\nopens…to 指令，语法如下：\nopens package to modules\n该指令用于指定某些特定的模块才能在运行时对该模块下特定包下的 public 类进行反射操作，to 后面跟逗号分隔的模块名称。\n\nopen 指令，语法如下：\nopen module moduleName{\n}\n该指令用于指定外部模块可以对该模块下所有的类在运行时进行反射操作。\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleihuazhe%2Fjava11-netty-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleihuazhe%2Fjava11-netty-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleihuazhe%2Fjava11-netty-demo/lists"}