Skip to content

Use With Feign #1181

Open
Open
@NumFive5

Description

@NumFive5

Describe the proposal

Activity

cicoyle

cicoyle commented on Jan 31, 2025

@cicoyle
Contributor

Hi @NumFive5 – thanks for opening the issue! Are you looking to integrate the Java SDK with Feign? If so, could you provide more details on what you're trying to achieve?

added this to the v1.15 milestone on Jan 31, 2025
lony2003

lony2003 commented on Mar 3, 2025

@lony2003

I wrote a frign client for dapr and springboot, you can try this: https://github.com/fangkehou-team/dapr-spring

Edit: I have just checking out the code of Dapr java-sdk and found that It changed a lot for dapr-spring-boot-starter, so it will be not usable for the repo above. So the repo is now archived.

salaboy

salaboy commented on Mar 19, 2025

@salaboy
Collaborator

@lony2003 can you please share here some examples on how Feign clients would be used? I like the idea of simplifying the service to service communication with a declarative client, but I just don't see how this will work in practice. This might be my lack of experience with Feign, so a code example in this issue would help a lot.

lony2003

lony2003 commented on Mar 20, 2025

@lony2003

@salaboy just like this example in my project:

Feign client defined here:

@UseDaprClient
@FeignClient(name = "grid-feign", url = "http://method.nep-back-gfb-spring/gfb")
public interface GridFeedbackFeignClient {

    @GetMapping(path = "/list/byUser/{pageSize}/{page}", consumes = "application/json;charset=utf-8", produces = "application/json;charset=utf-8")
    Result<PageResult<List<GridFeedbackDTO>>> getAllByUser(
            @PathVariable Integer page,
            @PathVariable Integer pageSize,
            @RequestHeader HttpHeaders headers
    );

    @PostMapping(path = "/update", consumes = "application/json;charset=utf-8", produces = "application/json;charset=utf-8")
    Result<GridFeedbackDTO> updateGridFeedback(
            @RequestBody UpdateGridFeedbackDTO command,
            @RequestHeader HttpHeaders headers
    );

}

And usage of the client is here:

@Service
public class GfbServiceImpl implements IGfbService {

    private final GridFeedbackFeignClient gridFeedbackFeignClient;

    public GfbServiceImpl(GridFeedbackFeignClient gridFeedbackFeignClient) {
        this.gridFeedbackFeignClient = gridFeedbackFeignClient;
    }

    @Override
    public PageResult<List<GridFeedbackDTO>> getAllByUser(Integer page, Integer pageSize, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);

        Result<PageResult<List<GridFeedbackDTO>>> result = gridFeedbackFeignClient.getAllByUser(page, pageSize, headers);

        if (!result.isSuccess()) {
            throw new ResultException("can not get GFB by user", ResultCode.getByValue(result.getCode()));
        }

        return result.getData();
    }

    @Override
    public GridFeedbackDTO updateGridFeedback(UpdateGridFeedbackDTO command, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);

        Result<GridFeedbackDTO> result = gridFeedbackFeignClient.updateGridFeedback(command, headers);

        if (!result.isSuccess()) {
            throw new ResultException("can not get GFB by user", ResultCode.getByValue(result.getCode()));
        }

        return result.getData();
    }
}

As Spring Cloud Openfeign has a check of http:// and https:// protocol, the url of dapr feign client must be start with http:// or https://, as there is no need to specify TLS for Dapr RPC, I'd like to use http:// protocol for that (in fact, it's just a meaningless placeholder, so never mind that problem).

I defined the protocol like http://method.<appId>/<methodName> for invokeMethod and http://binding.<bindingName>/<operation> for invokeBinding , please check https://dapr-spring.fangkehou.icu/en/feign/getting-started.html#about-feign-dapr-client for more details.

It can also be used like https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#creating-feign-clients-manually , just replace the client to DaprInvokeFeignClient.

Note that the path parameters like ?a=a&b=b not work, as dapr grpc will urlencode it, don't know if it's my problem for that.

To get Dapr Invoke works with Spring Cloud Openfeign, we would create a Targeter (a interface in Spring Cloud Openfeign) to handle @UseDaprClient annoation, and use @Bean @Primary or @Bean to cover the default Targeter defined in Spring Cloud Openfeign as it defined with @Bean @ConditionOnMissingBean, but I think it may not be suitable for a library (especially for a official library) to have a bean that replace a bean from other libraries, so I wonder your opinion.

linked a pull request that will close this issue on Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @salaboy@cicoyle@lony2003@NumFive5

      Issue actions

        Use With Feign · Issue #1181 · dapr/java-sdk