How to Develop A New Microservice

This article introduces how to develop, debug and deploy a new microservice in the local area based on the existing Linkis microservice architecture, so as to facilitate the need for logs of new applications.

Mind mapping:

mind-Mapping

This article introduces the new microservice linkis-new-microservice as an example. How to create and register a new microservice belonging to linkis in IDEA

Software requirements

  • jdk1.8
  • maven3.5+

Note: The new sub-module under which module is not fixed and depends on the situation. Generally, it is divided and confirmed by service group. Here is just an example.

  • Right click under the linkis-public-enhancements module

new-module

  • Select maven and click Nex to next step

maven-module

  • Enter the module name and click Finsh

name-module

  • Created successfully

created-successfully

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

  1. ## Add the public dependency module of linkis and the mybatis module dependency (if it does not involve database operations, you can not add 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>

The configuration file is named according to linkis-service name.properties, and placed in the linkis-dist/package/conf/ directory. When the service starts, the linkis.properties general configuration file and the linkis-service name.properties configuration file will be loaded

Add linkis-new-microservice.properties configuration file

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. ## If you do not need to provide interface Api, you do not need to add this configuration
  16. ##restful
  17. wds.linkis.server.restful.scan.packages=org.apache.linkis.newmicroservice.server.restful
  18. ## mybatis Configuration of data manipulation items
  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. ## Never use the same port as other services
  23. spring.server.port=9208

It is convenient to debug the interface, no need to verify the login status

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

test-mode

  1. wds.linkis.test.mode=true # Turn on test mode
  2. wds.linkis.test.user=hadoop # Specify which user to proxy all requests to in test mode

To make it easier for everyone to learn, let’s take creating a simple API interface as an example.

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. }

The specific guidelines for this step have been written in the Debugging Guidelines document and can be directly accessed, so I won’t introduce too much here

Set the startup Application of linkis-new-microservice

commissioning-service

parameter explanation:

  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. By checking Include dependencies with Provided scope, you can introduce provided-level dependency packages during debugging.

After the above settings are completed, the Application can be run directly. After running successfully, open the browser and enter the url of the eureka registration center

  1. http://ip:port/

new-service

When the linkis-new-microservice service appears in the eureka registration center, the local registration of the new microservice is successful.

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

postman-test

Packaging and deployment mainly has two stages. The first step is that after the module is packaged by maven, the dependencies required by the module will be packaged into the corresponding target directory of the module linkis-new-microservice/target/out/lib. The second step is to assemble the complete final deployment installation package, you need to automatically copy linkis-new-microservice/target/out/lib to linkis-dist/target/apache-linkis-x.x.x-incubating-bin/linkis - under package/lib

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

new-distribution

Since there are many dependencies that need to be excluded, only part of the code is posted here

  1. <excludes> <!-- does not include -->
  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>

Here is an explanation of why you need to add excludes, because the service startup script linkis-dist/package/sbin/ext/linkis-common-start generally loads the general lib by default

common-start

Therefore, when packaging service dependencies, existing lib packages can be excluded. For details, please refer to linkis-computation-governance/linkis-entrance/src/main/assembly/distribution.xml

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

Add fileSet configuration, changing the configuration is mainly to control the output linkis-new-microservice service package when compiling and packaging

fileset

Only the configuration content that needs to be added is posted here.

  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

Only the configuration content that needs to be added is posted here.

  1. ## startApp
  2. #linkis-new-microservice
  3. SERVER_NAME="new-microservice"
  4. startApp

detection-script

Only the configuration content that needs to be added is posted here.

  1. ##checkServer
  2. #linkis-new-microservice
  3. SERVER_NAME="new-microservice"
  4. checkServer

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

stop-script

Only the configuration content that needs to be added is posted here.

  1. ## stopApp
  2. #linkis-new-microservice
  3. export SERVER_NAME="new-microservice"
  4. stopApp

The specific guidelines for this step have been written in the backend compilation document and can be directly accessed, so I won’t introduce too much here

Here is an example of single-machine deployment, and the specific guidance of this step has been written in the Single-machine deployment document and can be accessed directly, so I won’t introduce it here

After the installation and deployment is successful, you can directly visit the eureka registration center in the browser to see if the center has successfully registered the linkis-new-microservice service. If the registration is successful, the creation of a new microservice is successful.

new-service