微服务模板工具

在微服务架构下,我们经常需要按业务领域进行拆分,新建微服务。

频繁的创建新服务,十分繁琐,本文介绍一种微服务创建的模板工具。

在Maven架构下,我们可以用ArchType)快速生成新项目。

但在本文所选的Gradle构建工具下,尚未有类似工具。

我们使用模板替换的方式,新建服务。

构建模板微服务

首先,我们构建模板微服务,代码放到了这里

我们看下目录结构:

  1. .
  2. ├── build.gradle
  3. ├── gradle
  4. └── wrapper
  5. ├── gradle-wrapper.jar
  6. └── gradle-wrapper.properties
  7. ├── gradlew
  8. ├── gradlew.bat
  9. ├── homs-template-client
  10. ├── build.gradle
  11. └── src
  12. └── main
  13. ├── java
  14. └── com
  15. └── coder4
  16. └── homs
  17. └── template
  18. ├── HomsTemplate.proto
  19. ├── HomsTemplateGrpc.java
  20. ├── HomsTemplateProto.java
  21. ├── client
  22. ├── AbstractGrpcClientManager.java
  23. ├── HSGrpcClient.java
  24. ├── HomsAbcGrpcClient.java
  25. └── SimpleGrpcClientManager.java
  26. └── constant
  27. └── HomsAbcConstant.java
  28. └── resources
  29. └── META-INF
  30. └── spring.factories
  31. ├── homs-template-server
  32. ├── build.gradle
  33. ├── lombok.config
  34. └── src
  35. ├── main
  36. ├── java
  37. └── com
  38. └── coder4
  39. └── homs
  40. └── template
  41. └── server
  42. └── server
  43. ├── HomsRpcServer.java
  44. ├── HomsTemplateApplication.java
  45. ├── configuration
  46. ├── RpcBindableServiceConfiguration.java
  47. └── RpcServerConfiguration.java
  48. ├── grpc
  49. └── HomsTemplateGrpcImpl.java
  50. └── web
  51. ├── ctrl
  52. └── BaseController.java
  53. ├── logic
  54. ├── impl
  55. └── spi
  56. └── vo
  57. └── resources
  58. └── application.yaml
  59. └── test
  60. └── java
  61. └── com
  62. └── coder4
  63. └── homs
  64. ├── demo
  65. └── template
  66. └── server
  67. └── server
  68. └── Test.java
  69. ├── settings.gradle
  70. └── tool
  71. ├── compile_grpc.sh
  72. └── test_curl.sh

如上图所示,这是一个多模块的子项目,分为client、server两部分。与我们在前文中介绍的保持一致。为了简单起见,这里去掉了MySQL、Redis等依赖。

服务生成工具

接下来,我们开发服务生成工具,脚本如下:

  1. #!/bin/bash
  2. if [ x"$#" != x"1" ];then
  3. echo "Usage $0 <project-name>"
  4. exit -1
  5. fi
  6. PROJECT_NAME=$1
  7. PROJECT_NAME_CAMEL=$(echo $PROJECT_NAME | gsed -r 's/(^|-)([a-z])/\U\2/g')
  8. PROJECT_P1=$(echo $PROJECT_NAME | awk -F '-' '{print $1}')
  9. PROJECT_P2=$(echo $PROJECT_NAME | awk -F '-' '{print $2}')
  10. rm -rf $PROJECT_NAME
  11. cp -rf homs-template $PROJECT_NAME
  12. # move files
  13. mv $PROJECT_NAME/homs-template-client $PROJECT_NAME/${PROJECT_NAME}-client
  14. mkdir -p $PROJECT_NAME/${PROJECT_NAME}-client/src/main/java/com/coder4/$PROJECT_P1/$PROJECT_P2
  15. mv $PROJECT_NAME/${PROJECT_NAME}-client/src/main/java/com/coder4/homs/template/* $PROJECT_NAME/${PROJECT_NAME}-client/src/main/java/com/coder4/$PROJECT_P1/$PROJECT_P2
  16. rm -rf $PROJECT_NAME/${PROJECT_NAME}-client/src/main/java/com/coder4/homs/template
  17. mv $PROJECT_NAME/homs-template-server $PROJECT_NAME/${PROJECT_NAME}-server
  18. mkdir -p $PROJECT_NAME/${PROJECT_NAME}-server/src/main/java/com/coder4/$PROJECT_P1/$PROJECT_P2
  19. mv $PROJECT_NAME/${PROJECT_NAME}-server/src/main/java/com/coder4/homs/template/* $PROJECT_NAME/${PROJECT_NAME}-server/src/main/java/com/coder4/$PROJECT_P1/$PROJECT_P2
  20. rm -rf $PROJECT_NAME/${PROJECT_NAME}-server/src/main/java/com/coder4/homs/template
  21. find $PROJECT_NAME -type file -exec gsed -i "s/HomsTemplate/$PROJECT_NAME_CAMEL/g" {} +
  22. find $PROJECT_NAME -type file -exec gsed -i "s/homs\.template/$PROJECT_P1\.$PROJECT_P2/g" {} +
  23. find $PROJECT_NAME -type file -exec gsed -i "s/homs-template/$PROJECT_P1-$PROJECT_P2/g" {} +
  24. for file in $(find $PROJECT_NAME -type file);do
  25. target=$(echo $file|sed -e "s/HomsTemplate/$PROJECT_NAME_CAMEL/g")
  26. mv $file $target
  27. done

如上所示:

  • 输入项目”homs-abc”后,会获取其驼峰命名如”HomsAbc”

  • 拷贝上述template项目后,会对文件夹进行重命名

  • 接着,对文件中的template进行替换

  • 最后,对部分文件名进行替换

我们试着这运行下:

  1. ./generate.sh homs-abc
  2. ├── build.gradle
  3. ├── gradle
  4. └── wrapper
  5. ├── gradle-wrapper.jar
  6. └── gradle-wrapper.properties
  7. ├── gradlew
  8. ├── gradlew.bat
  9. ├── homs-abc-client
  10. ├── build.gradle
  11. └── src
  12. └── main
  13. ├── java
  14. └── com
  15. └── coder4
  16. └── homs
  17. └── abc
  18. ├── HomsAbc.proto
  19. ├── HomsAbcGrpc.java
  20. ├── HomsAbcProto.java
  21. ├── client
  22. ├── AbstractGrpcClientManager.java
  23. ├── HSGrpcClient.java
  24. ├── HomsAbcGrpcClient.java
  25. └── SimpleGrpcClientManager.java
  26. └── constant
  27. └── HomsAbcConstant.java
  28. └── resources
  29. └── META-INF
  30. └── spring.factories
  31. ├── homs-abc-server
  32. ├── build.gradle
  33. ├── lombok.config
  34. └── src
  35. ├── main
  36. ├── java
  37. └── com
  38. └── coder4
  39. └── homs
  40. └── abc
  41. └── server
  42. └── server
  43. ├── HomsAbcApplication.java
  44. ├── HomsRpcServer.java
  45. ├── configuration
  46. ├── RpcBindableServiceConfiguration.java
  47. └── RpcServerConfiguration.java
  48. ├── grpc
  49. └── HomsAbcGrpcImpl.java
  50. └── web
  51. ├── ctrl
  52. └── BaseController.java
  53. ├── logic
  54. ├── impl
  55. └── spi
  56. └── vo
  57. └── resources
  58. └── application.yaml
  59. └── test
  60. └── java
  61. └── com
  62. └── coder4
  63. └── homs
  64. ├── demo
  65. └── template
  66. └── server
  67. └── server
  68. └── Test.java
  69. ├── settings.gradle
  70. └── tool
  71. ├── compile_grpc.sh
  72. └── test_curl.sh

如上,非常快速的生成了新的微服务!

在实际项目中,你还可以在初始化脚本中,集成如下功能:

  • 自动创建远程的git repo

  • 创建jenkins打包项目

  • 创建监控项

由于篇幅所限,这里不再讨论上述功能改进。