Java Client

截止到目前,我们看到的都是通过concrete-support-jsr339发布服务,使用swagger调试服务。在本节,我们看看如果使用Java Client来调用服务。

开启调试信息

新建一个concrete.yml文件如下:

jaxrs:
  logger:
    level:
      # 可选值为TRACE,DEBUG,INFO,WARN,ERROR,不建议使用WARN以及ERROR级别
      # 可选值以外则表示不输出
      server: info #默认不开启,生产环境不建议开启
      client: info #默认DEBUG

Note

Profile cocnrete 在默认的Configuration下,在concrete工具链中可以进行有很多配置,后续单独说明

调用Jaxrs发布的服务

我们先约定,在demo-boot的测试作用域里调用。

  • 增加依赖,说明已经备注上了
        <!-- 调用jaxrs发布的concrete服务 -->
        <dependency>
            <groupId>org.coodex</groupId>
            <artifactId>concrete-jaxrs-client</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 使用jersey-client作为jaxrs实现 -->
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <scope>test</scope>
        </dependency>
  • 最简单的方式
package org.coodex.concrete.demo.client;

import org.coodex.concrete.Client;
import org.coodex.concrete.demo.api.SubjoinExampleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JaxRSClientDemo1 {
    private final static Logger log = LoggerFactory.getLogger(JaxRSClientDemo1.class);

    public static void main(String[] args) {
        log.info("restful service invoked: {}",
                Client.getInstance(SubjoinExampleService.class, "jaxrs").add(11, 12));
    }
}

说明一下,这种方式依赖配置信息,concrete默认的配置信息从Profile中读取,所以我们还要建一个client.jaxrs的Profile,优先使用yaml文件

client.jaxrs.yml

location: "http://localhost:8080/jaxrs"

Note

concrete-client在使用配置,命名空间为: client, moduleName。详见Configuration

ok,server端跑起来,然后跑client,大概能看到的信息如下:

server端:

2019-08-06 13:30:02.142  INFO 19312 --- [nio-8080-exec-6] o.c.concrete.jaxrs.logging.ServerLogger  : 3 * Server has received a request on thread http-nio-8080-exec-6
3 > POST http://localhost:8080/jaxrs/SubjoinExample/add
3 > accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
3 > accept-language: zh-CN
3 > connection: keep-alive
3 > content-length: 17
3 > content-type: application/json;charset=UTF-8
3 > host: localhost:8080
3 > user-agent: Jersey/2.27 (HttpUrlConnection 1.8.0_202)
3 > x-client-provider: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":11,"x2":12}

2019-08-06 13:30:02.521  INFO 19312 --- [vice.executor-3] o.c.concrete.jaxrs.logging.ServerLogger  : 3 * Server responded with a response on thread service.executor-3
3 < 200
3 < CONCRETE-WARNINGS: %5B%7B%22code%22%3A105001%2C%22message%22%3A%2211+%2B+12+%E5%A4%AA%E9%9A%BE%E4%BA%86+%3E_%3C+%28demoErrorCodes%29%22%7D%5D[decoded value:[{"code":105001,"message":"11 + 12 太难了 >_< (demoErrorCodes)"}]]
3 < Content-Type: application/json;charset=UTF-8
23

client端

13:30:02.136 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 1 * Sending client request on thread main
1 > POST http://localhost:8080/jaxrs/SubjoinExample/add
1 > Accept-Language: zh-CN
1 > Content-Type: application/json;charset=UTF-8
1 > X-CLIENT-PROVIDER: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":11,"x2":12}

13:30:02.542 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 1 * Client response received on thread main
1 < 200
1 < CONCRETE-WARNINGS: %5B%7B%22code%22%3A105001%2C%22message%22%3A%2211+%2B+12+%E5%A4%AA%E9%9A%BE%E4%BA%86+%3E_%3C+%28demoErrorCodes%29%22%7D%5D[decoded value:[{"code":105001,"message":"11 + 12 太难了 >_< (demoErrorCodes)"}]]
1 < Content-Length: 2
1 < Content-Type: application/json;charset=UTF-8
1 < Date: Tue, 06 Aug 2019 05:30:02 GMT
23

13:30:02.603 [main] INFO org.coodex.concrete.demo.client.JaxRSClientDemo1 - restful service invoked: 23

我们切换一下client端运行的语言环境: java 参数增加 -Duser.language=en -Duser.region=US

13:40:13.290 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 1 * Sending client request on thread main
1 > POST http://localhost:8080/jaxrs/SubjoinExample/add
1 > Accept-Language: en-US
1 > Content-Type: application/json;charset=UTF-8
1 > X-CLIENT-PROVIDER: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":11,"x2":12}

13:40:13.428 [main] INFO org.coodex.concrete.jaxrs.logging.ClientLogger - 1 * Client response received on thread main
1 < 200
1 < CONCRETE-WARNINGS: %5B%7B%22code%22%3A105001%2C%22message%22%3A%2211+%2B+12+is+too+hard+%3E_%3C+%28demoErrorCodes%29%22%7D%5D[decoded value:[{"code":105001,"message":"11 + 12 is too hard >_< (demoErrorCodes)"}]]
1 < Content-Length: 2
1 < Content-Type: application/json;charset=UTF-8
1 < Date: Tue, 06 Aug 2019 05:40:13 GMT
23

13:40:13.478 [main] INFO org.coodex.concrete.demo.client.JaxRSClientDemo1 - restful service invoked: 23

使用Desination来调用

package org.coodex.concrete.demo.client;

import org.coodex.concrete.Client;
import org.coodex.concrete.client.jaxrs.JaxRSDestination;
import org.coodex.concrete.demo.api.SubjoinExampleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JaxRSClientDemo2 {
    private final static Logger log = LoggerFactory.getLogger(JaxRSClientDemo2.class);

    public static void main(String[] args) {
        JaxRSDestination destination = new JaxRSDestination();
        destination.setLocation("http://localhost:8080/jaxrs");
        log.info("restful service invoked: {}",
                Client.getInstance(SubjoinExampleService.class, destination).add(11, 12));
    }
}

这种方式下,我们可以动态构建调用目标信息,减少外部配置。比方说,我们的系统可能需要向多个concrete服务节点推送信息,而且数量可能增加,那么,我们可以通过数据库管理目标节点信息,使用这种方式调用目标节点的数据服务。

使用DI

concrete-client也可以按照javax.inject规范来使用,依赖concrete-core-spring。我们在服务端演示。

示例改造为:客户端调用SubjoinExampleService的add方法,SubjoinExampleServiceImpl的add方法改为使用客户端调用AddWithIn10Service的add方法。

Note

这是我们第一次修改代码,示例代码会把之前部分的代码注释起来,你需要调试之前案例的时候,切换一下即可

demo-impl中增加依赖

        <!-- client调用演示 -->
        <dependency>
            <groupId>org.coodex</groupId>
            <artifactId>concrete-client</artifactId>
        </dependency>

SubjoinExampleServiceImpl中增加

    @Inject
    @ConcreteClient("jaxrs")// <-- Qualifier
    private AddService addServiceClient;

修改add方法为

    @Override
    public Integer add(Integer x1, Integer x2) {
        if (x1 < 0 || x1 > 9 || x2 < 0 || x2 > 9) {
            subjoin.putWarning(new WarningData(TOO_HARD, x1, x2));
        }
//        return x1 + x2;
        return addServiceClient.add(x1, x2);
    }

我们用客户端分别调用10以内和10以上的参数来试试:

情况1:

2019-08-06 14:20:34.983  INFO 18352 --- [nio-8080-exec-7] o.c.concrete.jaxrs.logging.ServerLogger  : 7 * Server has received a request on thread http-nio-8080-exec-7
7 > POST http://localhost:8080/jaxrs/SubjoinExample/add
7 > accept: application/json
7 > accept-encoding: gzip, deflate, br
7 > accept-language: zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7
7 > connection: keep-alive
7 > content-length: 15
7 > content-type: application/json
7 > cookie: _ga=GA1.1.587770346.1564740039
7 > host: localhost:8080
7 > origin: http://localhost:8080
7 > referer: http://localhost:8080/jaxrs/swagger/index.html
7 > user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
{"x1":0,"x2":1}

2019-08-06 14:20:35.428  INFO 18352 --- [vice.executor-1] o.c.concrete.jaxrs.logging.ClientLogger  : 1 * Sending client request on thread service.executor-1
1 > POST http://localhost:8080/jaxrs/OrgCoodexConcreteDemoApiAddService/add
1 > Accept-Language: zh-CN
1 > Content-Type: application/json;charset=UTF-8
1 > X-CLIENT-PROVIDER: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":0,"x2":1}

2019-08-06 14:20:35.431  INFO 18352 --- [nio-8080-exec-8] o.c.concrete.jaxrs.logging.ServerLogger  : 8 * Server has received a request on thread http-nio-8080-exec-8
8 > POST http://localhost:8080/jaxrs/OrgCoodexConcreteDemoApiAddService/add
8 > accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
8 > accept-language: zh-CN
8 > connection: keep-alive
8 > content-length: 15
8 > content-type: application/json;charset=UTF-8
8 > host: localhost:8080
8 > user-agent: Jersey/2.27 (HttpUrlConnection 1.8.0_202)
8 > x-client-provider: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":0,"x2":1}

2019-08-06 14:20:35.455  INFO 18352 --- [vice.executor-2] o.c.concrete.jaxrs.logging.ServerLogger  : 8 * Server responded with a response on thread service.executor-2
8 < 200
8 < Content-Type: application/json;charset=UTF-8
1

2019-08-06 14:20:35.463  INFO 18352 --- [vice.executor-1] o.c.concrete.jaxrs.logging.ClientLogger  : 1 * Client response received on thread service.executor-1
1 < 200
1 < Content-Length: 1
1 < Content-Type: application/json;charset=UTF-8
1 < Date: Tue, 06 Aug 2019 06:20:35 GMT
1

2019-08-06 14:20:35.486  INFO 18352 --- [vice.executor-1] o.c.concrete.jaxrs.logging.ServerLogger  : 7 * Server responded with a response on thread service.executor-1
7 < 200
7 < Content-Type: application/json;charset=UTF-8
1

根据上面的log,我们可以看到:7是真正客户端的请求响应信息;1是服务端使用client调用的请求信息;8是1的请求响应

超出10的结果

2019-08-06 15:55:49.479  INFO 4984 --- [nio-8080-exec-1] o.c.concrete.jaxrs.logging.ServerLogger  : 1 * Server has received a request on thread http-nio-8080-exec-1
1 > POST http://localhost:8080/jaxrs/SubjoinExample/add
1 > accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
1 > accept-language: en-US
1 > connection: keep-alive
1 > content-length: 17
1 > content-type: application/json;charset=UTF-8
1 > host: localhost:8080
1 > user-agent: Jersey/2.27 (HttpUrlConnection 1.8.0_202)
1 > x-client-provider: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":11,"x2":12}

2019-08-06 15:55:49.996  INFO 4984 --- [vice.executor-1] o.c.concrete.jaxrs.logging.ClientLogger  : 1 * Sending client request on thread service.executor-1
1 > POST http://localhost:8080/jaxrs/OrgCoodexConcreteDemoApiAddService/add
1 > Accept-Language: en-US
1 > Content-Type: application/json;charset=UTF-8
1 > X-CLIENT-PROVIDER: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":11,"x2":12}

2019-08-06 15:55:50.000  INFO 4984 --- [nio-8080-exec-2] o.c.concrete.jaxrs.logging.ServerLogger  : 2 * Server has received a request on thread http-nio-8080-exec-2
2 > POST http://localhost:8080/jaxrs/OrgCoodexConcreteDemoApiAddService/add
2 > accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
2 > accept-language: en-US
2 > connection: keep-alive
2 > content-length: 17
2 > content-type: application/json;charset=UTF-8
2 > host: localhost:8080
2 > user-agent: Jersey/2.27 (HttpUrlConnection 1.8.0_202)
2 > x-client-provider: cocnrete-jaxrs-client 0.4.0-SNAPSHOT
{"x1":11,"x2":12}

2019-08-06 15:55:50.033  INFO 4984 --- [vice.executor-2] o.c.concrete.jaxrs.logging.ServerLogger  : 2 * Server responded with a response on thread service.executor-2
2 < 200
2 < Content-Type: application/json;charset=UTF-8
23

2019-08-06 15:55:50.049  INFO 4984 --- [vice.executor-1] o.c.concrete.jaxrs.logging.ClientLogger  : 1 * Client response received on thread service.executor-1
1 < 200
1 < Content-Length: 2
1 < Content-Type: application/json;charset=UTF-8
1 < Date: Tue, 06 Aug 2019 07:55:50 GMT
23

2019-08-06 15:55:50.061  INFO 4984 --- [vice.executor-1] o.c.concrete.jaxrs.logging.ServerLogger  : 1 * Server responded with a response on thread service.executor-1
1 < 200
1 < CONCRETE-WARNINGS: %5B%7B%22code%22%3A105001%2C%22message%22%3A%2211+%2B+12+is+too+hard+%3E_%3C+%28demoErrorCodes%29%22%7D%5D[decoded value:[{"code":105001,"message":"11 + 12 is too hard >_< (demoErrorCodes)"}]]
1 < Content-Type: application/json;charset=UTF-8
23

注意看一下,语言环境是传递的

(ɔ) coodex.org 2016 - 2020            修订时间: 2020-03-09 02:56:39

results matching ""

    No results matching ""