一、连接MongoDB
1、修改application.properties配置文件,增加连接MongoDB的信息
spring.data.mongodb.host=10.238.3.xx
spring.data.mongodb.port=20000
spring.data.mongodb.database=admin
spring.data.mongodb.username=username
spring.data.mongodb.password=password
二、使用
1、创建 ModuleInfo 类
@Document(collection = "module_info")
@Getter
@Setter
@EntityScan
public class ModuleInfo implements java.io.Serializable {
@Id
private Long id;
private Date gmtCreate;
private Date gmtModified;
private String appName;
private String environment;
private String ip;
private String port;
private String version;
private String status;
}
2、创建service类,ModuleInfoService
public interface ModuleInfoService {
ModuleInfo findByAppNameAndEnv(String appName, String environment);
}
3、创建service 实现类,ModuleInfoServiceImpl
@Service
public class ModuleInfoServiceImpl implements ModuleInfoService {
@Autowired
MongoTemplate mongoTemplate;
@Override
public ModuleInfo findByAppNameAndEnv(String appName, String environment) {
Query query = new Query(Criteria.where("appName").is(appName).and("environment").is(environment)).limit(1);
return mongoTemplate.findOne(query, ModuleInfo.class);
}
}
4、controller类
@RestController
@RequestMapping(value = "/api/interfacecase")
public class InterfaceCaseController {
@Autowired
SingleCaseDebugAPI singleCaseDebugAPI;
@Autowired
GatewayCaseService gatewayCaseService;
@Autowired
WebAPICaseService webAPICaseService;
@Autowired
MicroServiceCaseService microServiceCaseService;
@Autowired
RecordService recordService;
@Autowired
ModuleInfoService moduleInfoService;
@Autowired
BaseRedisDao baseRedisDao;
@RequestMapping(value = "/debugInterfaceCase", method = RequestMethod.GET)
public boolean debugInterfaceCase(@RequestParam(value = "id") Integer id, @RequestParam(value = "interfaceType") String interfaceType, @RequestParam(value = "username") String username){
System.out.println("前端传递过来的用例ID:" + id + " 用例接口类型:" + interfaceType);
ResMsg resMsg = new ResMsg();
JSONObject jsonObject = new JSONObject();
boolean result = true;
if(interfaceType.equals("Gateway")){
GatewayCase gatewayCase = gatewayCaseService.findByPrimaryKey(id);
jsonObject = JSONObject.parseObject(JSONObject.toJSONString(gatewayCase), JSONObject.class);
}else if(interfaceType.equals("WebAPI")){
WebAPICase webAPICase = webAPICaseService.findByPrimaryKey(id);
jsonObject = JSONObject.parseObject(JSONObject.toJSONString(webAPICase), JSONObject.class);
}else {
// 微服务
MicroServiceCase microServiceCase = microServiceCaseService.findByPrimaryKey(id);
String entranceDesc = "dubbo://" + microServiceCase.getClassName() + "/" + microServiceCase.getMethodName();
String appName = "umehome-manager";
//动态获取接口请求参数
Record record = recordService.findByAppNameAndEntranceDesc(appName, entranceDesc);
// System.out.println("record: " + JSONObject.toJSONString(record));
//获取回放的ip
ModuleInfo moduleInfo = moduleInfoService.findByAppNameAndEnv(appName, "dockerGray");
System.out.println("moduleInfo: " + JSONObject.toJSONString(moduleInfo));
}
return result;
}
}