## 部署Grape-Server > 注意: > > - Grape-Server需要JDK8及以上运行环境。 > - Grape-Server需要依赖 Redis 2.6+ 存储,Redis环境可参考[redis-docker](https://github.com/dinstone/grape/tree/master/redis-docker)构建。 - 下载源代码: ```shell git clone https://github.com/dinstone/grape.git ``` - 构建打包: ```shell cd grape maven package ``` - 编辑配置 ```shell cd grape/grape-server/target unzip grape-server-1.3.0.zip cd grape-server-1.3.0/config/ ``` 在配置文件config.json中编辑Redis节点的配置项。 ```json "redis": { "nodes": [ { "host": "127.0.0.1", "port": 6379 } ], "model": "pooled", "maxTotal": 8, "minIdle": 1, "timeout": 2000, "maxWaitMillis": 3000, "numTestsPerEvictionRun": -1, "minEvictableIdleTimeMillis": 60000, "timeBetweenEvictionRunsMillis": 30000 } ``` 在配置文件user.json中编辑用户密码和角色。 ```json { "admin": { "password": "grape", "roles": [ "writer", "reader" ] }, "guid": { "password": "grape", "roles": [ "reader" ] } } ``` - 启动服务 ```shell cd grape-server-1.3.1/bin ./start.sh ``` ## 生产和消费延迟任务 ### 生产延迟任务 业务系统通常调用 Rest API 向延迟队列提交一个延迟任务。如下例子是使用OkhttpClient调用API(/api/job/produce),向test队列提交了一个任务id为j004,延迟时间1000毫秒,预留时间10000毫秒(tube=test&jid=j004&dtr=1000&ttr=10000)的延迟任务,该任务的内容包含了一段JSON串。 ```java OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, "{\n \"orderId\": \"j001\",\n \"sumMoney\": 2000,\n \"count\": 10000\n}"); Request request = new Request.Builder() .url("http://localhost:9521/api/job/produce?tube=test&jid=j004&dtr=1000&ttr=10000") .method("POST", body) .addHeader("Content-Type", "application/json") .build(); Response response = client.newCall(request).execute(); ``` ### 消费延迟任务 业务系统通常调用 Rest API 从延迟队列消费一批延迟任务。如下例子是使用OkhttpClient调用API(/api/job/consume),向test队列提交了一个最大批量为10(tube=test&max=10)的消费申请,响应结果为延迟任务的JSON数组。 ```java OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("http://localhost:9521/api/job/consume?tube=test&max=10") .method("GET", body) .build(); Response response = client.newCall(request).execute(); ``` 响应结果: ```json [ { "id": "j004", "dtr": 1000, "ttr": 10000, "noe": 0, "data": "ewogICAgIm9yZGVySWQiOiAiajAwMSIsCiAgICAic3VtTW9uZXkiOiAyMDAwLAogICAgImNvdW50IjogMTAwMDAKfQ" } ] ``` 现在消费者就可以遍历延迟任务,执行延迟任务的业务逻辑了。 - 任务执行很顺利,拿到了期望的结果,那么就可以调用API(/api/job/finish)结束掉该延迟任务了,这样任务(j004)就真的从延迟队列中移除了。 ```java OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("http://localhost:9521/api/job/finish?tube=test&jid=j004") .method("DELETE", body) .build(); Response response = client.newCall(request).execute(); ``` - 任务执行很顺利,但还没有拿到期望的结果,希望能延迟一段时间后再试,那么可以调用API(/api/job/release)指定任务延时(tube=test&jid=j004&dtr=20000),这样任务就能回到延迟队列中等待调度了. ```java OkHttpClient client = new OkHttpClient().newBuilder().build(); MediaType mediaType = MediaType.parse("text/plain"); RequestBody body = RequestBody.create(mediaType, ""); Request request = new Request.Builder() .url("http://localhost:9521/api/job/release?tube=test&jid=j001&dtr=20000") .method("PUT", body) .build(); Response response = client.newCall(request).execute(); ``` ### 其它业务场景探索 待添加