WebGIS

MapGIS IGServer(Java)10.6扩展服务插件指南

文章
扩展服务
头像
发布时间:2022-07-29 17:37:42
76299
0
0


依赖

插件中依赖已经存在于igserver-webapp进程中,将其设置为 provided ,因为插件可以共享框架中已存在的依赖,如果将重复的依赖加入到插件,只会增加插件的大小。igserver框架的所有依赖可将igserver-webapp-*.jar(路径:MapGIS 10\igserver_for_java\lib)解压 后查看目录BOOT-INF/lib下的jar包。

依赖声明

插件必须有如下依赖,其中mapgis-igs-api需要手动注册,该包位于BOOT-INF/lib下。

注册命令:mvn install:install-file -DgroupId=com.zondy.mapgis.igs -DartifactId=mapgis-igs-api -Dversion=10.6.0.10 -Dpackaging=jar -Dfile=D:\mapgis-igs-api-10.6.x.x.jar

<dependencies>

    <!--插件主框架-->

    <dependency>

        <groupId>org.pf4j</groupId>

          <artifactId>pf4j</artifactId>

        <version>3.4.0</version>

        <scope>provided</scope>

    </dependency>

    <dependency>

          <groupId>org.pf4j</groupId>

          <artifactId>pf4j-spring</artifactId>

        <version>0.7.0</version>

        <scope>provided</scope>

    </dependency>

    <!--igserver扩展接口-->

    <dependency>

          <groupId>com.zondy.mapgis.igs</groupId>

          <artifactId>mapgis-igs-api</artifactId>

          <version>10.6.0.10</version>

        <scope>provided</scope>

    </dependency>

    <!--springmvc标准-->

    <dependency>

          <groupId>org.springframework</groupId>

          <artifactId>spring-webmvc</artifactId>

        <version>5.1.20.RELEASE</version>

        <scope>provided</scope>

        <!--open api3标准-->

    </dependency>

    <dependency>

          <groupId>io.swagger.core.v3</groupId>

          <artifactId>swagger-annotations</artifactId>

        <version>2.2.0</version>

        <scope>provided</scope>

    </dependency>

    <!--java servelet标准-->

    <dependency>

          <groupId>jakarta.servlet</groupId>

          <artifactId>jakarta.servlet-api</artifactId>

        <version>4.0.3</version>

        <scope>provided</scope>

    </dependency>

    <!--推荐日志框架-->

    <dependency>

          <groupId>org.slf4j</groupId>

          <artifactId>slf4j-api</artifactId>

        <version>1.7.30</version>

        <scope>provided</scope>

    </dependency>

</dependencies>

打包

推荐插件打包使用maven-assembly-plugin,打包为one-jar,如下

<build>

        <plugins>

            <plugin>

                  <groupId>org.apache.maven.plugins</groupId>

                  <artifactId>maven-assembly-plugin</artifactId>

                  <version>3.1.0</version>

                <configuration>

                    <descriptorRefs>

                          <descriptorRef>jar-with-dependencies</descriptorRef>

                    </descriptorRefs>

                      <finalName>mapgis-plugin</finalName>

                    <appendAssemblyId>false</appendAssemblyId>

                      <attach>false</attach>

                    <archive>

                        <manifest>

                              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>

                              <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>

                        </manifest>

                        <manifestEntries>

                              <Plugin-Id>${plugin.id}</Plugin-Id>

                              <Plugin-Version>${plugin.version}</Plugin-Version>

                              <Plugin-Provider>${plugin.provider}</Plugin-Provider>

                            <Plugin-Class>${plugin.class}</Plugin-Class>

                            <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>

                          </manifestEntries>

                    </archive>

                </configuration>

                <executions>

                    <execution>

                          <id>make-assembly</id>

                          <phase>package</phase>

                        <goals>

                              <goal>single</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

          </plugins>

    </build>

其中必须指定插件的属性,注意插件id必须保证唯一性,class为Plugin实现类,如果是扩展的 SpringMvc REST服务,服务承载到IGServer中时将添加/igs/rest/services/{pluginid}/ExtensionServer,示例如下

<properties>
      <!-- Override below properties   in each plugin's pom.xml -->

<!-- 插件实现中必须包括如下配置项-->
      <plugin.id>igs-demo-plugin</plugin.id>
        <plugin.class>com.xxx.server.DemoPlugin</plugin.class>
        <plugin.version>0.0.1</plugin.version>
        <plugin.provider>MapGIS</plugin.provider>
      <plugin.dependencies/>
  </properties>

在插件项目目录下执行

mvn clean   package

将在target目录下生成插件文件 mapgis-plugin.jar

pom.xml参考

<?xml version="1.0"   encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.xxx.server</groupId>
        <artifactId>cxf-service-extension-test</artifactId>
        <version>10.6.0.10</version>
      <properties>
          <!-- Override below   properties in each plugin's pom.xml -->
          <!-- 插件实现中必须包括如下配置项-->
            <plugin.id>igs-demo-plugin</plugin.id>
            <plugin.class>com.xxx.server.DemoPlugin</plugin.class>
            <plugin.version>0.0.1</plugin.version>
          <plugin.provider>MapGIS</plugin.provider>
          <plugin.dependencies/>
      </properties>
      <dependencies>
          <!--插件主框架-->
          <dependency>
                <groupId>org.pf4j</groupId>
                <artifactId>pf4j</artifactId>
                <version>3.4.0</version>
                <scope>provided</scope>
          </dependency>
          <dependency>
                <groupId>org.pf4j</groupId>
                <artifactId>pf4j-spring</artifactId>
                <version>0.7.0</version>
                <scope>provided</scope>
          </dependency>
          <!--igserver扩展接口-->
          <dependency>
                <groupId>com.zondy.mapgis.igs</groupId>
                <artifactId>mapgis-igs-api</artifactId>
                <version>10.6.0.10</version>
                <scope>provided</scope>
          </dependency>
          <!--springmvc标准-->
          <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.20.RELEASE</version>
                <scope>provided</scope>
              <!--open api3标准-->
          </dependency>
          <dependency>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>2.2.0</version>
                <scope>provided</scope>
          </dependency>
          <!--java servelet标准-->
          <dependency>
                <groupId>jakarta.servlet</groupId>
                <artifactId>jakarta.servlet-api</artifactId>
                <version>4.0.3</version>
                <scope>provided</scope>
          </dependency>
          <!--推荐日志框架-->
          <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.30</version>
              <scope>provided</scope>
          </dependency>
          <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.2</version>
          </dependency>
      </dependencies>
      <build>
          <plugins>
              <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>3.1.0</version>
                  <configuration>
                        <descriptorRefs>
                          <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>mapgis-plugin</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                      <attach>false</attach>
                      <archive>
                            <manifest>                              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>                              <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            </manifest>
                            <manifestEntries>
                                <Plugin-Id>${plugin.id}</Plugin-Id>
                                <Plugin-Version>${plugin.version}</Plugin-Version>
                              <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                                <Plugin-Class>${plugin.class}</Plugin-Class>
                                <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
                          </manifestEntries>
                      </archive>
                  </configuration>
                  <executions>
                      <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                          <goals>
                                <goal>single</goal>
                          </goals>
                      </execution>
                  </executions>
              </plugin>
          </plugins>
      </build>
  </project>

 

开发

创建plugin类,即上文中的plugin.class,示例如下

public class   DemoPlugin extends SpringPlugin {

 

    public DemoPlugin(PluginWrapper wrapper)   {

        super(wrapper);

    }

 

    @Override

    public void start() {

        log.info("DemoPlugin.start,mode:{}",   wrapper.getRuntimeMode());

    }

 

    @Override

    public void stop() {

          log.info("DemoPlugin.stop()");

    }

 

    @Override

    protected ApplicationContext   createApplicationContext() {

        AnnotationConfigApplicationContext   applicationContext = new AnnotationConfigApplicationContext();

          applicationContext.setClassLoader(getWrapper().getPluginClassLoader());

        applicationContext.register(SpringConfiguration.class);

        applicationContext.refresh();

        return applicationContext;

    }

}

 

创建SpringMvc Controller扩展服务实现类,添加spring的REST服务注解@RestController或 @Controller,同时必须添加注解@Extension和实现Rest扩展服务接口ControllerExtension,可选 择实现FrameworkContextCapable接口来获取框架的IOC容器,示例如下

public class   TestController implements ControllerExtension, FrameworkContextCapable {
 
      private ApplicationContext   applicationContext;
      @Autowired
      private MessageProvider   messageProvider;
 
      @Override
      public void   setFrameworkApplicationContext(ApplicationContext applicationContext) {
          this.applicationContext =   applicationContext;
      }
 
      @Override
      @Operation(summary = "服务信息")
      public ResponseEntity<?>   getServiceInfo() {
          return ResponseEntity.ok("这是插件igs-demo-plugin提供的REST扩展服务");
      }
 
      @Operation(summary = "获取结果信息")
      @RequestMapping(value =   "/test", method = {RequestMethod.GET})
      public ResObj getRes() {
          String msg =   messageProvider.getMessage();
          ResObj resObj = new ResObj();
          resObj.setMessage(msg);
          return resObj;
      }
  }

其中注解 @Tag , @Operation 为OpenApi3注解,用于生成swagger文档

 

上述示例REST服务的访问基地址为 http://localhost:8089/igs/rest/services/igs-demo plugin/ExtensionServer ,其中demo-plugin为插件id,IGServer自动为扩展服务添加的REST服务前 缀,接口ControllerExtension默认实现了服务自述信息方法

public interface   ControllerExtension extends ExtensionPoint {

 /** * 当前REST扩展服务的自述信息、

  * 用于在IGServer服务管理中调用服务基地址时,返回的服务信息  

* 开发者可根据实际需要,实现该方法,将扩展服务的详细自述信息通过该接口提供给使用者

  *

* @return 返回text的信息  

*/

@GetMapping("")

default ResponseEntity   getServiceInfo() {

return ResponseEntity.ok("这是一个基于SpringMVC的REST扩展服务:" +   this.getClass().getName());

}

}

 

发布扩展服务

在IGServer服务管理界面,点击服务发布-功能服务-扩展服务

3.png

       选择打包生成的jar包上传即可完成扩展服务发布

4.png

重启IGServer服务(扩展服务插件添加、删除后必须重启服务)

访问服务

请求服务基地址 http://localhost:8089/igs/rest/services/igs-demo-   plugin/ExtensionServer ,将返回如下信息:

    1.png

开发者也可实现接口,实现自定义的服务自述信息接口,示例如下:

@Override

@Operation(summary = "服务信息")

public ResponseEntity   getServiceInfo() {

return   ResponseEntity.ok("这是插件igs-demo-plugin提供的REST扩展服务");

}

swagger支持,访问 http://localhost:8089/webjars/swagger-ui/index.html ,可查看所有扩展服务的REST接口文档。

2.png

 

 


评论区
暂无评论
暂无评论,快来抢占第一个沙发吧~
发布评论