Skip to content

Commit 6498a09

Browse files
committed
docs(springboot openfeign): Update doc for Dapr OpenFeign
Signed-off-by: lony2003 <zhangke200377@outlook.com>
1 parent d7831b0 commit 6498a09

File tree

1 file changed

+90
-0
lines changed
  • daprdocs/content/en/java-sdk-docs/spring-boot

1 file changed

+90
-0
lines changed

daprdocs/content/en/java-sdk-docs/spring-boot/_index.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,47 @@ By adding these dependencies you can:
4040
- Use the Spring Data and Messaging abstractions and programming model that uses the Dapr APIs under the hood
4141
- Improve your inner-development loop by relying on [Testcontainers](https://testcontainers.com/) to bootstrap Dapr Control plane services and default components
4242

43+
___(Optional)___ And if you want to enable openfeign support, you will also need to add the dependencies to your project:
44+
45+
```
46+
<dependency>
47+
<groupId>org.springframework.cloud</groupId>
48+
<artifactId>spring-cloud-starter-openfeign</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.dapr.spring</groupId>
52+
<artifactId>dapr-spring-openfeign</artifactId>
53+
<version>0.15.0-SNAPSHOT</version>
54+
</dependency>
55+
```
56+
57+
By adding these dependencies you can:
58+
- Invoke Method and Bindings with OpenFeign, just like other HTTP endpoints.
59+
60+
___Note that Spring Cloud dependencies will require a different dependencyManagement setup from normal SpringBoot Application,
61+
please check the [Official Documentation](https://spring.io/projects/spring-cloud) for more information.___
62+
63+
___(Optional)___ If you want to use OpenFeign with Dapr from a non-SpringBoot project, you can add this dependency to your project:
64+
65+
```
66+
<dependency>
67+
<groupId>io.dapr.spring</groupId>
68+
<artifactId>dapr-openfeign-client</artifactId>
69+
<version>0.15.0-SNAPSHOT</version>
70+
</dependency>
71+
```
72+
73+
It mainly provides a Client for OpenFeign to receive OpenFeign requests and send them using Dapr.
74+
75+
You can use the client like this:
76+
77+
```java
78+
MyAppData response = Feign.builder().client(new DaprFeignClient()).target(MyAppData.class,
79+
"http://binding.myBinding/create");
80+
```
81+
82+
___Note that you don't have to add this dependency to your SpringBoot project directly, `dapr-spring-openfeign` has already included it.___
83+
4384
Once these dependencies are in your application, you can rely on Spring Boot autoconfiguration to autowire a `DaprClient` instance:
4485

4586
```java
@@ -323,6 +364,55 @@ daprWorkflowClient.raiseEvent(instanceId, "MyEvenet", event);
323364

324365
Check the [Dapr Workflow documentation](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-overview/) for more information about how to work with Dapr Workflows.
325366

367+
## Invoke Methods and Bindings registered in Dapr with Spring Cloud OpenFeign
368+
369+
First you should follow the official Spring Cloud OpenFeign steps to enable FeignClient features,
370+
mainly by adding a `@UseFeignClient` annotation in your SpringBoot Application of Configurations.
371+
372+
Define a FeignClient using DaprClient is very easy, you can just define a regular FeignClient, and add a `@UseFeignClient` to the interface, just like that:
373+
374+
```java
375+
@FeignClient(value = "producer-client", url = "http://method.producer-app/")
376+
@UseDaprClient
377+
public interface ProducerClient {
378+
379+
@PostMapping("/orders")
380+
String storeOrder(@RequestBody Order order);
381+
382+
@GetMapping(value = "/orders", produces = "application/json")
383+
Iterable<Order> getAll();
384+
385+
@GetMapping(value = "/orders/byItem/", produces = "application/json")
386+
Iterable<Order> getAllByItem(@RequestParam("item") String item);
387+
}
388+
```
389+
390+
There you go! now when you call the ProducerClient methods, it will call the DaprClient to handle that.
391+
392+
>___Note: because of the design of DaprClient, you won't get any headers from Dapr.___
393+
>
394+
>___So you need to add `produces = "application/json"`
395+
to your RequestMapping in order to parse the response body which return type is other than `String`.___
396+
>
397+
> ___The `produces` field will generate an `Accept` header to the request,
398+
the client will read it and create a fake `Content-Type` header to the response,
399+
and Spring Cloud Openfeign will read the `Content-Type` header of the response to parse values.___
400+
401+
You may have noticed that the `url` field of `@FeignClient` is strange, here is the schema of it:
402+
403+
The following content is from the Java Doc of DaprInvokeFeignClient.
404+
405+
> Dapr currently supports two methods of invocation: invokeBinding (output binding) and invokeMethod. This client supports two modes: http://binding.xxx or http://method.xxx. The http scheme at the beginning is just to make Spring Boot Openfeign work properly.
406+
>
407+
> For invokeMethod, the URL contains two types of information, similar to the format of an HTTP URL. The difference lies in the conversion of the host in the HTTP URL to appId, and the path (excluding “/”) to methodName. For example, if you have a method with the appId “myApp” and the methodName “getAll/demo”, then the URL for this request would be http://method.myApp/getAll/demo. You can also set HTTP headers if you wish, and the client will handle them. Currently, only HTTP calls are supported, but grpc calls may be supported in the future, with possible URLs like http://method_grpc.myApp/getAll or similar.
408+
>
409+
> For invokeBinding, the URL also contains two types of information: the host is the bindingName, and the path is the operation. Note that different bindings support different operations, so you must consult the Dapr documentation. For example, if you have a binding with the bindingName “myBinding” and the supported operation is “create”, then the URL for this request would be http://binding.myBinding/create. You can put some metadata in the headers of the Feign request, and the client will handle them.
410+
>
411+
> As for the response, the result code is always 200 OK. If the client encounters any errors, it will throw an IOException.
412+
>
413+
> Currently, we have no method to gain metadata from server as Dapr Client doesn’t have methods to do that, so headers will be blank. If Accept header has set in request, a fake Content-Type header will be created in response, and it will be the first value of Accept header.
414+
415+
___Note that not all bindings are recommended to use FeignClient to query directly, you can try `dapr-spring-data` for databases, or `dapr-spring-messaging` for pubsubs___
326416

327417
## Next steps
328418

0 commit comments

Comments
 (0)