如何实现新的微服务

这篇文章介绍如何基于现有的Linkis微服务架构下,在本地开发调试一个新的微服务以及部署,方便有需要新增微服务的二次开发的同学参考。

思维导图:

思维导图

本文以新增微服务linkis-new-microservice为示例,进行介绍。 如何在IDEA中创建和注册一个属于linkis新的微服务

软件要求

  • jdk1.8
  • maven3.5+

注意:在什么模块下新建子模块,这个并不是固定的因情况而定,一般按服务组来划分确认,这里只是举例子。

  • linkis-public-enhancements模块下 右键

new-module

  • 选择maven点击Nex下一步

maven-module

  • 输入模块名点击Finsh

name-module

  • 创建成功

created-successfully

path: linkis-public-enhancements/linkis-new-microservice/pom.xml

  1. ## 添加linkis的公共依赖模块和 mybatis模块依赖(如果不涉及数据库操作可以不添加mybatis)
  2. <dependency>
  3. <groupId>org.apache.linkis</groupId>
  4. <artifactId>linkis-module</artifactId>
  5. <version>${project.version}</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.ow2.asm</groupId>
  9. <artifactId>asm</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.linkis</groupId>
  15. <artifactId>linkis-mybatis</artifactId>
  16. <version>${project.version}</version>
  17. </dependency>

配置文件按 linkis-服务名.properties来命名,统一放在linkis-dist/package/conf/目录下,服务启动时候会加载linkis.properties 通用配置文件以及linkis-服务名.properties配置文件

新增linkis-new-microservice.properties配置文件

path: linkis-dist/package/conf/linkis-new-microservice.properties

  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. ## 如不需提供接口Api则无需添加此配置
  16. ##restful
  17. wds.linkis.server.restful.scan.packages=org.apache.linkis.newmicroservice.server.restful
  18. ## mybatis 数据操作项目的配置
  19. wds.linkis.server.mybatis.mapperLocations=classpath*:org/apache/linkis/newmicroservice/server/dao/mapper/*.xml
  20. wds.linkis.server.mybatis.typeAliasesPackage=org.apache.linkis.newmicroservice.server.domain
  21. wds.linkis.server.mybatis.BasePackage=org.apache.linkis.newmicroservice.server.dao
  22. ##切勿与其他服务的端口相同
  23. spring.server.port=9208

方便进行接口的调式,无需进行登陆态的验证

path: linkis-dist/package/conf/linkis.properties

test-mode

  1. wds.linkis.test.mode=true # 打开测试模式
  2. wds.linkis.test.user=hadoop # 指定测试模式下,所有请求都代理给哪个用户

为方便大家学习,现以创建一个简单的API接口为示例。

new-microservice

  1. package org.apache.linkis.newmicroservice.server.restful;
  2. import io.swagger.annotations.ApiOperation;
  3. import org.apache.linkis.server.Message;
  4. import org.springframework.web.bind.annotation.*;
  5. import io.swagger.annotations.Api;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. @Api(tags = "newmicroservice")
  9. @RestController
  10. @RequestMapping(path = "/newmicroservice")
  11. public class NewMicroservice {
  12. @ApiOperation(value = "establish", httpMethod = "GET")
  13. @RequestMapping(path = "establish", method = RequestMethod.GET)
  14. public Message list() {
  15. Map&lt;String,String&gt; map=new HashMap&lt;&gt;();
  16. map.put("NewMicroservice","Hello! This is a new microservice I registered(这是我注册的一个新的微服务)");
  17. return Message.ok("").data("map", map);
  18. }
  19. }

maven-module

  1. package org.apache.linkis.newmicroservice.server;
  2. import org.apache.linkis.LinkisBaseServerApp;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. public class LinkisNewMicroserviceApplication {
  6. private static final Log logger = LogFactory.getLog(LinkisNewMicroserviceApplication.class);
  7. public static void main(String[] args) throws ReflectiveOperationException {
  8. logger.info("Start to running LinkisNewmicroserviceApplication");
  9. LinkisBaseServerApp.main(args);
  10. }
  11. }

这一步骤的具体指引在 调试指引 文档中已有写 可直接访问,这里就不在过多介绍

设置linkis-new-microservice的启动Application

commissioning-service

参数解释:

  1. [Service Name]
  2. linkis-new-microservice
  3. [Module Name]
  4. linkis-new-microservice
  5. [VM Opitons]
  6. -DserviceName=linkis-new-microservice -Xbootclasspath/a:{YourPathPrefix}/linkis/linkis-dist/package/conf
  7. [main Class]
  8. org.apache.linkis.newmicroservice.server.LinkisNewmicroserviceApplication
  9. [Add provided scope to classpath]
  10. 通过勾选Include dependencies with Provided scope ,可以在调试时,引入provided级别的依赖包。

上述设置完成之后,可直接运行此Application。运行成功后打开浏览器输入eureka注册中心的url

  1. http://ip:port/

new-service

当eureka注册中心出现linkis-new-microservice服务即为本地注册新的微服务成功。

URL: http://ip:port/api/rest\_j/v1/newmicroservice/establish

postman-test

打包部署主要有有两个阶段 第一步是模块通过maven打包后 会将模块所需要的依赖 打包到模块对应的target目录下 linkis-new-microservice/target/out/lib。 第二步是 组装完整的最终部署安装包时,需要将 linkis-new-microservice/target/out/lib 自动拷贝至 linkis-dist/target/apache-linkis-x.x.x-bin/linkis-package/lib

path: linkis-public-enhancements/linkis-new-microservice/src/main/assembly/distribution.xml

new-distribution

由于需要排除的依赖比较多这里只贴部分代码

  1. <excludes> <!-- 不包括 -->
  2. <exclude>antlr:antlr:jar</exclude>
  3. <exclude>aopalliance:aopalliance:jar</exclude>
  4. <exclude>com.fasterxml.jackson.core:jackson-annotations:jar</exclude>
  5. <exclude>com.fasterxml.jackson.core:jackson-core:jar</exclude>
  6. </excludes>

这里解释下为什么需要加excludes,因为服务启动脚本 linkis-dist/package/sbin/ext/linkis-common-start 中一般会默认加载通用的lib

common-start

所以在打包服务依赖时候,可以排除已有的lib包.详细可以参考linkis-computation-governance/linkis-entrance/src/main/assembly/distribution.xml

path: linkis-dist/src/main/assembly/distribution.xml

添加fileSet 配置,改配置主要是控制编译打包时的能输出linkis-new-microservice服务包

fileset

这里只贴出来需要新增的配置内容。

  1. <fileSet>
  2. <directory>
  3. ../linkis-public-enhancements/linkis-new-microservice/target/out/lib
  4. </directory>
  5. <outputDirectory>
  6. linkis-package/lib/linkis-public-enhancements/linkis-new-microservice
  7. </outputDirectory>
  8. <includes>
  9. <include>**/*</include>
  10. </includes>
  11. </fileSet>

new-configuration

  1. #!/usr/bin/env bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # description: manager start cmd
  17. #
  18. # Modified for Linkis 1.0.0
  19. export SERVER_SUFFIX="linkis-public-enhancements/linkis-new-microservice"
  20. export SERVER_CLASS=org.apache.linkis.newmicroservice.server.LinkisNewMicroserviceApplication
  21. #export DEBUG_PORT=
  22. export COMMON_START_BIN=$LINKIS_HOME/sbin/ext/linkis-common-start
  23. if [[ ! -f "${COMMON_START_BIN}" ]]; then
  24. echo "The $COMMON_START_BIN does not exist!"
  25. exit 1
  26. else
  27. sh $COMMON_START_BIN
  28. fi

path: linkis-dist/package/sbin/linkis-start-all.sh

start-script

这里只贴出来需要新增的配置内容。

  1. ## 启动脚本
  2. #linkis-new-microservice
  3. SERVER_NAME="new-microservice"
  4. startApp

detection-script

这里只贴出来需要新增的配置内容。

  1. ##检测脚本
  2. #linkis-new-microservice
  3. SERVER_NAME="new-microservice"
  4. checkServer

path:linkis-dist/package/sbin/linkis-stop-all.sh

stop-script

这里只贴出来需要新增的配置内容。

  1. ## 停止脚本
  2. #linkis-new-microservice
  3. export SERVER_NAME="new-microservice"
  4. stopApp

这一步骤的具体指引在 后端编译 文档中已有写 可直接访问,这里就不在过多介绍

这里为单机部署做示例,而该步骤的具体指引在 单机部署 文档中已有写 可直接访问,这里就不在过多介绍 当安装部署成功后可直接在浏览器中访问eureka注册中心,看中心是否已经成功注册linkis-new-microservice服务,如注册成功即为创建新的微服务成功。

new-service