DanielLiu1123/httpexchange-spring-boot-starter

Configured so that each channel can use different clients

sjh836 opened this issue · 5 comments

sjh836 commented

This library is really well made.
It is a spring feature that can be easily applied right away.

I have to use both the normal return type and the return type of Reactive Stream through the http interface.
Also, I would like to adjust the timeout dynamically.

Therefore, I hope it works as follows.
I wish each channel could have a separate remote client.

http-exchange:
  enabled: true
  refresh:
    enabled: false
  read-timeout: 2000
  connect-timeout: 500
  #client-type: WEB_CLIENT
  channels:
    - base-url: 'https://jsonplaceholder.typicode.com'
      #client-type: REST_CLIENT #here
      #read-timeout: 500
      #connect-timeout: 200
      clients:
        - com.devljh.module.http.*HttpClient
    - base-url: 'https://jsonplaceholder.typicode.com'
      #client-type: WEB_CLIENT #here
      clients:
        - com.devljh.module.http.*ReactiveHttpClient

It doesn't work now, and an error occurs because the rest client cannot convert Mono/Flux messages.

I have to use both the normal return type and the return type of Reactive Stream through the http interface.

I think this feature should work fine.

Also, I would like to adjust the timeout dynamically.

I've also been trying to implement this feature recently. However, it seems that Spring doesn't provide the corresponding API, so this issue is a bit tricky at the moment. If you have any good ideas, please let me know!

See #32

Therefore, I hope it works as follows.
I wish each channel could have a separate remote client.

As for your request, the issue here is: a client that matches the com.devljh.module.http.*ReactiveHttpClient pattern will definitely match the com.devljh.module.http.*HttpClient as well, and it will use the first configuration that it hits. Swapping their order should meet your needs.

http-exchange:
  channels:
    - base-url: 'https://jsonplaceholder.typicode.com'
      client-type: WEB_CLIENT
      clients:
        - com.devljh.module.http.*ReactiveHttpClient
    - base-url: 'https://jsonplaceholder.typicode.com'
      client-type: REST_CLIENT
      read-timeout: 500
      connect-timeout: 200
      clients:
        - com.devljh.module.http.*HttpClient

Hi, I have some progress for dynamic adjustment of timeout settings, please refer to #38.

Use your Http Client to extend RequestConfigurator:

@HttpExchange("/users")  
interface UserApi extends RequestConfigurator<UserApi> {      
    @HttpExchange      
    List<User> list();  
}    

UserApi userApi = ...  
userApi.withTimeout(1000).list();

If you have a better idea, please let me know!

sjh836 commented

this is really well made.

It works well with separate client types for each channel.

I'm not sure if I understood it well.
Does each channel have a singleton RestClient or WebClient?
For example, if there are 100 channels, are there 50 RestClient instances and 50 WebClient instances?

In a similar problem, I used ConcurrentHashMap to group and inject the same configuration(read-timeout, ... etc).

In the same way, WebClient also dynamically supported read-timeout and connect-timeout.

For example, if there are 100 channels, are there 50 RestClient instances and 50 WebClient instances?

Yes, but perhaps we can reuse the HTTP client.

Does each channel have a singleton RestClient or WebClient?

Yes, for a long period in the past, a single channel configuration would only create one HTTP client, regardless of how many HTTP exchange interfaces the channel corresponded to. However, the same configuration can actually create different types of HTTP clients. For example, if a method in an HTTP exchange interface contains Reactive types, then only WebClient can be used.

In summary: Each HTTP exchange interface will create one HTTP client.

Yes, but perhaps we can reuse the HTTP client.

Fixed by #49