Java基于Dapr的服务调用
在进行该Demo程序前,应保证本地环境Dapr已正确安装
Server端
1.创建SpringBoot应用【略】
2.POM引入Dapr
<!-- https://mvnrepository.com/artifact/io.dapr/dapr-sdk-springboot -->
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk-springboot</artifactId>
<version>1.8.0</version>
</dependency>
3.编写一个简单的控制器,返回简单的内容消息
package cn.coreqi.server_demo.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class ServerController {
@PostMapping("/hello")
public Map<String,Object> sayHello(@RequestBody Map<String,Object> params){
String name = params.get("name").toString();
String msg = "Hello " + name;
Map<String,Object> response = new HashMap<>();
response.put("code",200);
response.put("status",true);
response.put("data",msg);
return response;
}
}
4.application.properties配置服务端的运行端口
server.port=7777
5.配置注入Dapr边车的调试模式
1.新建Shell Script配置
2.修改名称,指定工作目录,在脚本执行前添加Maven构建
-DskipTests=true package
3.添加shell脚本,保存
dapr run --app-id server_demo --app-port 7777 --dapr-http-port 7001 -- java -jar target/server_demo-0.0.1-SNAPSHOT.jar
6.运行测试
Client端
1.创建SpringBoot应用【略】
2.POM引入Dapr
<!-- https://mvnrepository.com/artifact/io.dapr/dapr-sdk-springboot -->
<dependency>
<groupId>io.dapr</groupId>
<artifactId>dapr-sdk-springboot</artifactId>
<version>1.8.0</version>
</dependency>
3.配置Dapr
package cn.coreqi.client_demo.config;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DaprConfig {
private static final DaprClientBuilder BUILDER = new DaprClientBuilder();
@Bean
public DaprClient buildDaprClient() {
return BUILDER.build();
}
}
4.新建控制器并调用服务端的接口
package cn.coreqi.client_demo.controller;
import io.dapr.client.DaprClient;
import io.dapr.client.domain.HttpExtension;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@RestController
public class ClientController {
@Resource
private DaprClient client;
private static final String SERVICE_APP_ID = "server_demo";
private static final String METHOD_NAME = "hello";
@GetMapping("/say")
public String say() {
Map<String,Object> params = new HashMap<>();
params.put("name","fanqi");
HashMap response = client.invokeMethod(
SERVICE_APP_ID,
METHOD_NAME,
params,
HttpExtension.POST,
HashMap.class).block();
return response.get("data").toString();
}
}
5.application.properties配置服务端的运行端口
server.port=8888
6.配置注入Dapr边车的调试模式【略】
7.运行测试【略】
调用
1.使用Dapr Dashboard查看服务注册状态
2.访问http://localhost:8888/say
3.查看链路追踪详情
Dapr在Kubernetes下,名称解析组件使用 Kubernetes DNS 服务来解析在集群中运行的其他应用程序的位置。 对于本地和多个物理机器,这将使用 mDNS 协议。同时也可以使用其他的服务注册中间件,例如Concul,具体信息请参考官网介绍:
https://docs.dapr.io/zh-hans/reference/components-reference/supported-name-resolution/setup-nr-consul/