Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use With Feign #1181

Open
NumFive5 opened this issue Dec 21, 2024 · 4 comments
Open

Use With Feign #1181

NumFive5 opened this issue Dec 21, 2024 · 4 comments
Milestone

Comments

@NumFive5
Copy link

Describe the proposal

@cicoyle
Copy link
Contributor

cicoyle commented Jan 31, 2025

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?

@cicoyle cicoyle added this to the v1.15 milestone Jan 31, 2025
@lony2003
Copy link

lony2003 commented Mar 3, 2025

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
Copy link
Contributor

salaboy commented Mar 19, 2025

@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
Copy link

lony2003 commented Mar 20, 2025

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants