# 第三方工具API集成
# 环境准备
参考 开发环境搭建
# 第三方工具集成开发
# 新建项目
DevOps项目右键》创建》构建包
更新构建包名,显示名,版本号
eg.
在新建的项目中更新 pom.xml 文件内容 重命名 pom.xml 中对应的属性 注释的信息为原格式(注释信息不需要保留,请自行删除),改为新内容
为了下文演示代码不报错 可以添加common模块和api模块 <dependencies> <dependency> <groupId>com.primeton.devops</groupId> <artifactId>devops-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.primeton.devops</groupId> <artifactId>devops-api</artifactId> <version>${project.version}</version> </dependency> </dependencies>
eg.
'com.primeton.devops.integration.demo'->'devops-integration-demo'
更新主项目 pom.xml 中新项目位置 保证构建顺序政策
eg.
更新boot启动项目 pom.xml 中新项目依赖信息
eg.
<!--<dependency> <groupId>com.primeton.devops</groupId> <artifactId>com.primeton.devops.integration.demo</artifactId> <version>6.7.0</version> </dependency>--> 更新为下方代码 <dependency> <groupId>com.primeton.devops</groupId> <artifactId>devops-integration-demo</artifactId> <version>${project.version}</version> </dependency>
更新maven依赖
# 后端集成开发
在新建的项目中创建包
eg.
'com.primeton.devops.integration.demo'
创建集成服务连通性测试类:'DemoServiceTest'
更新'DemoServiceTest'测试类,需要实现'com.primeton.devops.specs.api.pcm.ISystemServiceTest'接口的test方法,如下为示例写法:
在配置路径中创建文件夹'services',并在'services'文件夹中增加测试配置文件'com.primeton.devops.specs.api.pcm.ISystemServiceTest'
更新配置文件'com.primeton.devops.specs.api.pcm.ISystemServiceTest'内容
说明:SystemType以及Category请参考'com.primeton.devops.specs.constant.pcm.SystemType'枚举类,支持用户自定义
对应业务字典如下,如果需要自定义请按已有规则添加字典项
在新创建的包中创建集成服务类以及接口
eg.
'IntegrationDemoController'、'DemoService'
'IntegrationDemoController'示例如下
package com.primeton.devops.integration.demo.controller; import org.apache.calcite.Demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.primeton.components.rest.annotation.MultiRequestBody; import com.primeton.devops.integration.demo.service.DemoService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; /** * IntegrationDemoController. * */ @RestController @RequestMapping(value = "/api/integration/demos", consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE}) @Api(tags="IntegrationDemoController", description = "Demo服务集成管理接口") public class IntegrationDemoController { @Autowired private DemoService demoService; /** * 根据ID查询Demo信息 * * @param demoId * @return Demo信息 * @throws Exception */ @GetMapping(value = "/{demoId}") @ApiOperation("根据ID查询Demo信息") Demo getDemo( @PathVariable(name = "demoId", required = false) @ApiParam("DemoID") String demoId, @RequestParam(name = "systemId", required = false) @ApiParam("系统Id") String systemId) throws Exception { return demoService.getDemo(systemId, demoId); } /** * 新建Demo * * @param demo Demo信息 * @return * @throws Exception */ @PostMapping @ApiOperation("新建Demo") Demo createDemo(@MultiRequestBody("demo") @ApiParam("Demo") Demo demo) throws Exception { return demoService.createDemo(demo); } /** * 修改Demo * * @param demoId DemoID * @param demo Demo信息 * @return * @throws Exception */ @PutMapping(value = "/{demoId}") @ApiOperation("修改Demo") Demo modifyDemo( @PathVariable(name = "demoId", required = false) @ApiParam("DemoID") String demoId, @MultiRequestBody("demo") @ApiParam("Demo") Demo demo) throws Exception { return demoService.modifyDemo(demo); } /** * 删除Demo * * @param demoId DemoID * @throws Exception */ @DeleteMapping("/{demoId}") @ApiOperation("删除Demo") Demo removeDemo( @PathVariable(name = "demoId", required = false) @ApiParam("DemoID") String demoId) throws Exception { return demoService.removeDemo(demoId); } }
'DemoService'示例如下
package com.primeton.devops.integration.demo.service; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.calcite.Demo; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.primeton.devops.common.dao.DevOpsDao; import com.primeton.devops.common.util.HttpClient; import com.primeton.devops.common.util.JsonUtil; import com.primeton.devops.specs.api.pcm.service.ISysConfService; import com.primeton.devops.specs.model.pcm.Infra3rdSystem; import com.primeton.ext.system.utility.HttpClient.Response; @Service public class DemoService { private static final String TOKEN_NAME = "Authorization"; @Autowired private ISysConfService sysConfService; @Autowired private DevOpsDao baseDao; private <T> T get(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.GET, null, retClass); } private <T> T post(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Object body, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.POST, body, retClass); } private <T> T put(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Object body, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.PUT, body, retClass); } private <T> T delete(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, Object body, Class<T> retClass) throws Exception { return request(rootUrl, apiToken, restUri, queryParams, HttpClient.DELETE, body, retClass); } private <T> T request(String rootUrl, String apiToken, String restUri, Map<String, ?> queryParams, String requestMethod, Object body, Class<T> retClass) throws Exception { try { HttpClient httpClient = new HttpClient(rootUrl + restUri).head(TOKEN_NAME, apiToken); if (queryParams != null && !queryParams.isEmpty()) { for (Entry<String, ?> entry : queryParams.entrySet().toArray(new Entry[0])) { if (entry.getValue() == null || StringUtils.isBlank(String.valueOf(entry.getValue()))) { queryParams.remove(entry.getKey()); } } } httpClient = httpClient.queryParams(queryParams).method(requestMethod); if (body != null) { httpClient.body(JsonUtil.toJson(body)); } if (retClass != null && retClass == byte[].class) { ByteArrayOutputStream responseOutput = new ByteArrayOutputStream(); httpClient.setResponseOutput(responseOutput); httpClient.request(-1); return (T)responseOutput.toByteArray(); } Response response = httpClient.request(-1); if (retClass == null) { return null; } return response.getBodyObject(retClass); } catch (Exception e) { if ((HttpClient.GET.equals(requestMethod) || HttpClient.DELETE.equals(requestMethod)) && (e.getMessage().contains("[statusCode=400]") || e.getCause() instanceof FileNotFoundException)) { if (retClass == null) { return null; } if (retClass.isArray()) { return (T)Array.newInstance(retClass.getComponentType(), 0); } else if (List.class.isAssignableFrom(retClass)) { return (T)new ArrayList<>(); } return null; } throw e; } } public Demo getDemo(String systemId, String demoId) throws Exception { Infra3rdSystem sysConf = sysConfService.getSysConf(systemId); Map queryParams = new HashMap<>(); return toDemo(get(sysConf.getAddress(), sysConf.getApiToken(), "", queryParams, Map.class)); } private Demo toDemo(Map map) throws Exception { return null; } public Demo createDemo(Demo demo) throws Exception { baseDao.insertEntity(demo); return demo; } public Demo modifyDemo(Demo demo) throws Exception { baseDao.updateEntity(demo); return demo; } public Demo removeDemo(String demoId) throws Exception { Demo demo = baseDao.getEntityByKey(Demo.class.getName(), demoId); baseDao.deleteEntityByKey(Demo.class.getName(), demoId); return demo; } }
# 前端集成开发
1、修改 scripts/buildApi.js 中 swagger.json 的在线地址
(function () {
// 读取 在线 swagger
var url = "http://10.15.15.109:8080/swagger/swagger.json";
execute(url).then((data) => {
console.log(`read ${url} ...`)
mapAction(data)
}).then(() => {
console.log('create controller')
createController()
})
})()
2、运行 npm run build:api
以'IBomController'为示例:
运行 npm run build:api 后自动生成下列'IBomController'内容