Dapr Java Http 调用
版本介绍
- Java 版本:8
- Dapr Java SKD 版本:0.9.2
Dapr Java-SDK HTTP 调用文档 有个先决条件,内容如下:
- Dapr and Dapr CLI.
- Java JDK 11 (or greater): Oracle JDK or OpenJDK.
- Apache Maven version 3.x.
大家看到 Java JDK 版本最低要求是 11,但是本文显示使用的 JDK 8,这么做的原因是什么呢,可以参考 Java-SDK Issues,Issues 中回答如下:
We want to validate that the SDK is built with Java 8 and apps can use it with Java 11.
意思是他们想通过 Java 11 写的应用程序验证 Java 8 写的 SDK 是否能正常使用。本文不需要验证 Java 11 能否使用 Java-SDK ,因此本文将使用 Java 8 构建应用程序。
工程结构
3 个子工程,一个 client,两个 service。新建两个 service 的意义在于展示 http 链路调用使用 dapr 如何实现。3 个工程项目都集成了 Spring Boot。Spring Boot 启动后会自动注册 Controller、Config 之类的 bean。
java-client-a--1-->java-service-b;
java-service-b--2-->java-service-c;
java-service-c--3-->java-service-b;
java-service-b--4-->java-client-a;
- java-client-a 做为客户端调用 java-service-b;
- java-service-b 接收请求,并调用 java-service-c;
- java-service-c 接收请求,并响应;
- java-service-b 收到 java-service-c 应答,并响应 java-client-a 请求。
java-service-c
java-service-c 做为 http 调用链路末端,只需监听 http 请求即可。
package com.dapr.service;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
/**
* @author Zhang_Xiang
* @since 2020/11/7 10:51:22
*/
public class ServiceC {
/**
* Starts the service.
*
* @param args Expects the port: -p PORT
* @throws Exception If cannot start service.
*/
public static void main(String[] args) throws Exception {
Options options = new Options();
options.addRequiredOption("p", "port", true, "Port to listen to.");
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
// If port string is not valid, it will throw an exception.
int port = Integer.parseInt(cmd.getOptionValue("port"));
DaprApplication.start(port);
}
}
DaprApplication.start(port);
集成 SpringBoot 启动。
package com.dapr.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Dapr's HTTP callback implementation via SpringBoot.
* Scanning package io.dapr.springboot is required.
*
* @author zhangxiang
*/
@SpringBootApplication(scanBasePackages = {"com.dapr.service"})
public class DaprApplication {
/**
* Starts Dapr's callback in a given port.
*
* @param port Port to listen to.
*/
public static void start(int port) {
SpringApplication app = new SpringApplication(DaprApplication.class);
app.run(String.format("--server.port=%d", port));
}
}
启动命令:
dapr run --app-id java-service-c --app-port 9100 --dapr-http-port 3510 -- java -jar target/dapr-java-service-exec.jar com.dapr.service.ServiceC -p 9100
java-service-b
java-service-b 需要配置一个 DaprClient Bean,以在需要使用 Http 客户端的地方注入。
package com.dapr.service.config;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Zhang_Xiang
* @since 2020/11/8 08:46:49
*/
@Configuration
public class Client {
@Bean
public DaprClient getClient(){
return (new DaprClientBuilder()).build();
}
}
接下来在需要调用的 Controller 中添加构造器注入。
/**
* SpringBoot Controller to handle input binding.
*
* @author zhangxiang
*/
@RestController
public class HelloController {
private final DaprClient client;
...
public HelloController(DaprClient client) {
this.client = client;
}
...
}
发起 http 请求。
...
byte[] response = client.invokeService(SERVICE_APP_ID, "say", message, HttpExtension.POST, null,
byte[].class).block();
if (response != null) {
...
}
...
启动命令:
dapr run --app-id java-service-b --app-port 9101 --dapr-http-port 3511 -- java -jar target/dapr-java-service-exec.jar com.dapr.service.ServiceB -p 9101
java-client-a
对于 java-client-a 来说,集成 Springboot 是可选项,此处构造一个每隔 5 秒发起一次请求的客户端。
package com.dapr.client;
import com.alibaba.fastjson.JSON;
import com.common.ResponseResult;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.HttpExtension;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
/**
* @author Zhang_Xiang
* @since 2020/11/7 17:30:26
*/
public class ClientA {
/**
* Identifier in Dapr for the service this client will invoke.
*/
private static final String SERVICE_APP_ID = "java-service-b";
/**
* Format to output date and time.
*/
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
/**
* Starts the invoke client.
*
* @param args Messages to be sent as request for the invoke API.
*/
public static void main(String[] args) throws IOException {
try (DaprClient client = (new DaprClientBuilder()).build()) {
while (true) {
Calendar utcNow = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
String utcNowAsString = DATE_FORMAT.format(utcNow.getTime());
String msg = String.format("%s:this this java client A", utcNowAsString);
byte[] response = client.invokeService(SERVICE_APP_ID, "say", msg.getBytes(), HttpExtension.POST, null,
byte[].class).block();
if (response != null) {
String responseResultStr = new String(response);
ResponseResult responseResult = JSON.parseObject(responseResultStr, ResponseResult.class);
System.out.println(responseResult.getMessage());
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
启动命令:
dapr run --app-id java-client-a --dapr-http-port 3006 -- java -jar target/dapr-java-client-exec.jar com.dapr.client.ClientA
总结
各个模块的启动顺序应为:
java-service-c-->java-service-b;
java-service-b-->java-client-a;
这里限定顺序的原因是,如果先启动 java-client-a ,java-client-a 会立刻通过 dapr 开始发起请求到 java-service-b ,而这时 java-service-b 并未启动。这将触发 dapr 的重试机制。
重试
服务调用在事件调用失败和瞬态错误时,将执行带避退时间间隔(backoff time periods)的自动重试。
引起重试的错误:
- 网络错误,包括终端不可用和拒绝连接。
- 身份认证错误,由于在调用方/被调用方的 dapr 边车证书更新导致。
每次重试都以 1 秒的时间避退时间为间隔,最大重试次数为 3 次。和目的地边车通过 gRPC 建立连接 5 秒超时。
java-client-a 打印:
== APP == This is java-service-b,receive the message:2020-11-08 14:21:14.336:this this java client A,and request java-service-c get the response:{"message":"This is java-service-c,receive the message:\"2020-11-08 14:21:14.336:this this java client A\""}
java-service-b 打印:
== APP == This is java-service-b,receive the message:2020-11-08 14:21:44.454:this this java client A
java-service-c 打印:
== APP == This is java-service-c,receive the message:"2020-11-08 14:22:19.571:this this java client A"
打开新的命令行窗口,输入 dapr list
启动示例
源码地址:https://github.com/ZhangX-Byte/dapr-java
克隆仓库
git clone https://github.com/ZhangX-Byte/dapr-java.git
cd dapr-java
构建 dapr-java 项目
mvn install
然后各个项目各自 install 就能正常启动了。
Dapr Java Http 调用的更多相关文章
- Java jacob调用打印机打印word文档
前面说了Java如何生成复杂的Word文档,今年记录下Java如何调用打印机打印word文档. 起初用的是自带的PrintJob,但是系统提供的打印机制并不成熟完整.网上的代码也是千篇一律,在我的打印 ...
- 使用Runtime.getRuntime().exec()在java中调用python脚本
举例有一个Python脚本叫test.py,现在想要在Java里调用这个脚本.假定这个test.py里面使用了拓展的包,使得pythoninterpreter之类内嵌的编译器无法使用,那么只能采用ja ...
- java servlet调用带有多个返回结果集的存储过程
一.mysql存储过程 这里我先说下我这个功能实现的逻辑及途中遇到的一些问题.这个存储过程一共带两个输入参数,一共关联到两张表的查询,每个参数都对应查询表中的一个判断,所以一共返回了两个结果集(当然要 ...
- java程序调用存储过程
java程序调用存储过程 PL/SQL子程序,很多情况下是给应用程序来调用的,所有我们要掌握使用其他编程语言来调用我们写好的存储过程.下面我们介绍下使用java调用Oracle的存储过程. ...
- JAVA如何调用C/C++方法
JAVA如何调用C/C++方法 2013-05-27 JAVA以其跨平台的特性深受人们喜爱,而又正由于它的跨平台的目的,使得它和本地机器的各种内部联系变得很少,约束了它的功能.解决JAVA对本地操作的 ...
- Java中调用c/c++语言出现Exception in thread "main" java.lang.UnsatisfiedLinkError: Test.testPrint(Ljava/lang/String;)V...错误
错误: Exception in thread "main" java.lang.UnsatisfiedLinkError: Test.testPrint(Ljava/lang/S ...
- OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用OpenCV的示例
转自: http://blog.csdn.net/yanzi1225627/article/details/16917961 开发环境:windows+ADT Bundle+CDT+OpenCV-2 ...
- asyn4j -- java 异步方法调用框架
asyn4j 是一个java异步方法调用框架,基于消费者与生产者模式.包括了异步方法执行,异步回调执行,异步工作缓存模块.支持Spring. 让我们写异步方法不再写很多的相关多线程代码.用asyn4j ...
- java中调用dll文件的两种方法
一中是用JNA方法,另外是用JNative方法,两种都是转载来的, JNA地址:http://blog.csdn.net/shendl/article/details/3589676 JNativ ...
随机推荐
- JS寄快递地址智能解析
JS寄快递地址智能解析--2020年7月15日 去年做了些前端内容,最近在整理一些稍微有点用的内容,比如智能解析地址,用户只要输入:张三1351111111江苏省扬州市广陵区XX小区X楼xxx室,就能 ...
- js undefined和null的判断
function isUndef(value ){ return value === undefined || value === null; }
- 01 sublime text3下配置c/c++ 的编译运行环境
安装mingw,在线安装经常出问题,推荐离线安装 参考: https://blog.csdn.net/qq_34432960/article/details/106124496?utm_medium= ...
- 执行新增和修改操作报错connection is read-only. Queries leading to data modification are not allowed
出现这个问题的原因是默认事务只有只读权限,因此要添加下面的每一个add*,del*,update*等等. 分别给予访问数据库的权限. 方法名的前缀有该关键字设置了read-only=true,将其改为 ...
- docker-管理镜像常用命令
1. docker管理镜像常用命令 指令 说明 docker run [容器 CONTAINER ID] 创建一个新的容器并运行一个命令 docker start [容器 CONT ...
- 54.Qt-将界面程序封装成动态库DLL
1.生成dll 然后选择创建共享库: 创建好后,修改pro文件,改为下面两句(这样就可以创建界面了): 然后修改sharedlib.h: #ifndef SHAREDLIB_H #define SH ...
- ARM-Linux S5PV210 UART驱动(转)
ARM-Linux S5PV210 UART驱动(3)----串口核心层.关键结构体.接口关系 尽管一个特定的UART设备驱动完全可以按照tty驱动的设计方法来设计,即定义tty_driver并实现t ...
- 如何让矢量瓦片配图神器maputnik支持 geoserver
关键词:maputnik.geoserver.矢量地图.矢量瓦片.mapbox.mapboxgl.地图配图.地图配色 一直想搞一个类似百度.高德地图那样的矢量地图配图工具 百度个性化地图配图工具: 高 ...
- 在linux下搭建l2tp隧道
搭一个l2tp隧道,拓扑如下 两台机器是CentOS5,内核选上CONFIG_LEGACY_PTYS选项后自己编译的,l2tp是已经停更的l2tpd-0.69.先在LS上配置IP地址,iptables ...
- 多测师讲解jmeter _接口请求_(003)高级讲师肖sir
1.简单接口的请求 2. 3. 正则查看: 正则提取:在后置处理器中正则请求 设置:正则表达式 JSESSIONID提取器: Debug sampler 总结: