天天看点

SkyWalking OAPServer接口新增流程说明

整体步骤如下所示:

  1. 定义graphqls文件(路径为server-query-plugin\query-graphql-plugin\src\main\resources\query-protocol)
# demo.graphqls
# !表示非空
type Demo {
    name: String
    address: String
}
 
extend type Query {
    queryPerson(pid: String!): [Demo]
}
           
  1. 到GraphQLQueryProvider完成处理类绑定
public void prepare() throws ServiceNotProvidedException, ModuleStartException {
      ...
        .file("query-protocol/demo.graphqls")
		.resolvers(new DemoFromEsQuery(getManager()))   
      ...
  }
           
  1. 定义查询逻辑处理类DemoFromEsQuery
public class DemoFromEsQuery implements GraphQLQueryResolver {
 
    private final ModuleManager moduleManager;
    private DemoFromEsQueryService demoFromEsQueryService;
 
    public DemoFromEsQuery(ModuleManager moduleManager){
        this.moduleManager = moduleManager;
    }
 
    private DemoFromEsQueryService getDemoFromEsQueryService(){
        if(null == demoFromEsQueryService){
            this.demoFromEsQueryService = moduleManager.find(CoreModule.NAME).provider().getService(DemoFromEsQueryService.class);
        }
        return demoFromEsQueryService;
    }
    public List<Demo> queryPerson(final String pid) throws IOException {
        return getDemoFromEsQueryService().queryPerson(pid);
    }
}
           
  1. 注册DemoFromEsQueryService
// 修改 CoreModule.java,在addQueryService加我们所需的service
classes.add(DemoFromEsQueryService.class);
// 修改 CoreModuleProvider.java 注册servcie,在prepare方法加添加如下代码
this.registerServiceImplementation(DemoFromEsQueryService.class, new DemoFromEsQueryService(getManager()));
           
  1. 完成DemoFromEsQueryService.java类编写
public class DemoFromEsQueryService implements Service {
    private final ModuleManager moduleManager;
    private IDemoDao demoDAO;
    
    public DemoFromEsQueryService(ModuleManager moduleManager) {
        this.moduleManager = moduleManager;
    }
 
     private IDemoDao getDemoDAO() {
        if (demoDAO == null) {
            demoDAO = moduleManager.find(StorageModule.NAME).provider().getService(IDemoDao.class);
        }
        return demoDAO;
    }
    
    public List<Demo> queryPerson(final String pid) throws IOException {
        List<Demo> demoList = new ArrayList<>();
        ...
        demoList = getDemoDAO().queryPerson(pid);
        ...
        return demoList;
    }
 
}
           
  1. 完成IDemoDao接口类编写
public interface IDemoDao extends DAO {

    List<Demo> queryPerson(final String pid) throws IOException;
    
}
           
  1. 完成IDemoDao实现类编写(存储使用ES7)
public class DemoDaoEs7DAO extends EsDAO implements IDemoDao {

    public DemoDaoEs7DAO(ElasticSearchClient client) {
        super(client);
    }

    List<Demo> queryPerson(final String pid) throws IOException {
        List<Demo> demoList = new ArrayList<>();
        ...
        return demoList;
    }
}
           
  1. 注册IDemoDao
// 在StorageModule.java services添加对应接口类
 	@Override
    public Class[] services() {
        return new Class[]{
                IBatchDAO.class,
         		...
                IDemoDao.class,
                ...
                IBrowserLogQueryDAO.class
        };
    }
// 在StorageModuleElasticsearch7Provider.java中完成实现类绑定(可能为其他Provider类)
this.registerServiceImplementation(IDemoDao.class, new DemoDaoEs7DAO(elasticSearch7Client));
           

继续阅读