public static void main(String[] args) throws Exception {
try (DaprClient client = new DaprClientBuilder().build()) {
System.out.println("Waiting for Dapr sidecar ...");
client.waitForSidecar(3000).block();
System.out.println("Dapr sidecar is ready.");
String message = args.length == 0 ? "wang test string " : args[0];
MyClass myClass = new MyClass();
myClass.message = message;
MyClass secondState = new MyClass();
secondState.message = "test message";
client.saveState(STATE_STORE_NAME, FIRST_KEY_NAME, myClass).block();
System.out.println("Saving class with message 用消息保存类: " + message);
Mono<State<MyClass>> retrievedMessageMono = client.getState(STATE_STORE_NAME, FIRST_KEY_NAME, MyClass.class);
System.out.println("Retrieved class message from state 从状态检索到类消息: " + (retrievedMessageMono.block().getValue()).message);
System.out.println("Updating previous state and adding another state 'test state 更新以前的状态并添加另一个状态的测试状态'... ");
myClass.message = message + " updated";
System.out.println("Saving updated class with message 使用消息保存更新的类: " + myClass.message);
// execute transaction
List<TransactionalStateOperation<?>> operationList = new ArrayList<>();
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
new State<>(FIRST_KEY_NAME, myClass, "")));
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
new State<>(SECOND_KEY_NAME, secondState, "")));
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
// get multiple states
Mono<List<State<MyClass>>> retrievedMessagesMono = client.getBulkState(STATE_STORE_NAME,
Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class);
System.out.println("Retrieved messages using bulk get 使用批量获取检索到的消息:");
retrievedMessagesMono.block().forEach(System.out::println);
System.out.println("Deleting states 删除状态...");
System.out.println("Verify delete key request is aborted if an etag different from stored is passed 如果传递的etag不同于存储的etag,则验证删除密钥请求是否中止.");
// delete state API
try {
client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, "100", null).block();
} catch (DaprException ex) {
if (ex.getErrorCode().equals(Status.Code.ABORTED.toString())) {
// Expected error due to etag mismatch.
System.out.println(String.format("Expected failure 预期的失败。. %s", ex.getErrorCode()));
} else {
System.out.println("Unexpected exception 意外异常.");
throw ex;
}
}
Exception in thread "main" java.lang.NoSuchFieldError: Companion
at io.dapr.client.DaprHttp.(DaprHttp.java:117)
at io.dapr.client.DaprHttpBuilder.buildDaprHttp(DaprHttpBuilder.java:55)
at io.dapr.client.DaprHttpBuilder.build(DaprHttpBuilder.java:35)
at io.dapr.client.DaprClientBuilder.buildDaprClientHttp(DaprClientBuilder.java:159)
at io.dapr.client.DaprClientBuilder.buildDaprClient(DaprClientBuilder.java:126)
at io.dapr.client.DaprClientBuilder.build(DaprClientBuilder.java:106)
at com.dapeng.demodapr1.testone.TestOneController.main(TestOneController.java:44)
Disconnected from the target VM, address: '127.0.0.1:65365', transport: 'socket'
我对IDEA 也做了 dapr相关的配置,先跑Tools 下的External Tools 然后debg跑main方法,就报这个错误
Exception in thread "main" java.lang.NoSuchFieldError: Companion
at io.dapr.client.DaprHttp.<clinit>(DaprHttp.java:117)
at io.dapr.client.DaprHttpBuilder.buildDaprHttp(DaprHttpBuilder.java:55)
at io.dapr.client.DaprHttpBuilder.build(DaprHttpBuilder.java:35)
at io.dapr.client.DaprClientBuilder.buildDaprClientHttp(DaprClientBuilder.java:159)
at io.dapr.client.DaprClientBuilder.buildDaprClient(DaprClientBuilder.java:126)
at io.dapr.client.DaprClientBuilder.build(DaprClientBuilder.java:106)
at com.dapeng.demodapr1.testone.TestOneController.main(TestOneController.java:44)
Disconnected from the target VM, address: '127.0.0.1:65365', transport: 'socket'
通过Tools-》External Tools -> dapr for 执行结果是这样的。
C:\dapr\daprd.exe -app-id invokedemo -app-port 3005
time="2021-11-29T14:11:41.4688101+08:00" level=info msg="starting Dapr Runtime -- version 1.3.0 -- commit 4bab7576ed68a9ece1a4743a7925f18ef583775a" app_id=invokedemo instance=LAPTOP-ISP287H1 scope=dapr.runtime type=log ver=1.3.0
time="2021-11-29T14:11:41.4688101+08:00" level=info msg="log level set to: info" app_id=invokedemo instance=LAPTOP-ISP287H1 scope=dapr.runtime type=log ver=1.3.0
time="2021-11-29T14:11:41.4688101+08:00" level=info msg="metrics server started on :9090/" app_id=invokedemo instance=LAPTOP-ISP287H1 scope=dapr.metrics type=log ver=1.3.0
time="2021-11-29T14:11:41.4716367+08:00" level=fatal msg="failed to start metrics server: listen tcp :9090: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted." app_id=invokedemo instance=LAPTOP-ISP287H1 scope=dapr.metrics type=log ver=1.3.0
Process finished with exit code 1