如何在运行时更改假网址?

liu*_*cyu 5 spring-cloud spring-cloud-feign

@FeignClient(name ="test",url ="http:// xxxx")

如何在运行时更改feign url(url ="http:// xxxx")?因为网址只能在运行时确定.

小智 13

Feign有一种在运行时提供动态URL和端点的方法.

必须遵循以下步骤:

  1. FeignClient界面中,我们必须删除URL参数.我们必须使用@RequestLine注释来提及REST方法(GET,PUT,POST等):
@FeignClient(name="customerProfileAdapter")
public interface CustomerProfileAdaptor {

    // @RequestMapping(method=RequestMethod.GET, value="/get_all")
    @RequestLine("GET")
    public List<Customer> getAllCustomers(URI baseUri); 

    // @RequestMapping(method=RequestMethod.POST, value="/add")
    @RequestLine("POST")
    public ResponseEntity<CustomerProfileResponse> addCustomer(URI baseUri, Customer customer);

    @RequestLine("DELETE")
    public ResponseEntity<CustomerProfileResponse> deleteCustomer(URI baseUri, String mobile);
}
Run Code Online (Sandbox Code Playgroud)
  1. 在RestController中,您必须导入FeignClientConfiguration
  2. 您必须编写一个带编码器和解码器的RestController构造函数作为参数.
  3. 您需要FeignClient使用编码器,解码器构建.
  4. 在调用FeignClient方法时,提供URI(BaserUrl +端点)以及其他调用参数(如果有).
@RestController
@Import(FeignClientsConfiguration.class)
public class FeignDemoController {

    CustomerProfileAdaptor customerProfileAdaptor;

    @Autowired
    public FeignDemoController(Decoder decoder, Encoder encoder) {
        customerProfileAdaptor = Feign.builder().encoder(encoder).decoder(decoder) 
           .target(Target.EmptyTarget.create(CustomerProfileAdaptor.class));
    }

    @RequestMapping(value = "/get_all", method = RequestMethod.GET)
    public List<Customer> getAllCustomers() throws URISyntaxException {
        return customerProfileAdaptor
            .getAllCustomers(new URI("http://localhost:8090/customer-profile/get_all"));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResponseEntity<CustomerProfileResponse> addCustomer(@RequestBody Customer customer) 
            throws URISyntaxException {
        return customerProfileAdaptor
            .addCustomer(new URI("http://localhost:8090/customer-profile/add"), customer);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseEntity<CustomerProfileResponse> deleteCustomer(@RequestBody String mobile)
            throws URISyntaxException {
        return customerProfileAdaptor
            .deleteCustomer(new URI("http://localhost:8090/customer-profile/delete"), mobile);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这种方法改变了方法的签名,因此不是一个好的解决方案。请参阅下面 @Forest10 的答案,以获得使用 Spring 的更好解决方案。 (5认同)

Jef*_*eff 8

您可以手动创建客户端:

@Import(FeignClientsConfiguration.class)
class FooController {

    private FooClient fooClient;

    private FooClient adminClient;

    @Autowired
    public FooController(ResponseEntityDecoder decoder, SpringEncoder encoder, Client client) {
        this.fooClient = Feign.builder().client(client)
            .encoder(encoder)
            .decoder(decoder)
            .requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
            .target(FooClient.class, "http://PROD-SVC");
        this.adminClient = Feign.builder().client(client)
            .encoder(encoder)
            .decoder(decoder)
            .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
            .target(FooClient.class, "http://PROD-SVC");
     }
}
Run Code Online (Sandbox Code Playgroud)

来自文档:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html#_creating_feign_clients_manually


ash*_*bar 6

您可以添加一个未注释的URI参数(可以在运行时确定),它是将用于请求的基本路径。例如:

@FeignClient(name = "dummy-name", url = "https://this-is-a-placeholder.com")
public interface MyClient {
    @PostMapping(path = "/create")
    UserDto createUser(URI baseUrl, @RequestBody UserDto userDto);
}
Run Code Online (Sandbox Code Playgroud)

然后用法将是:

@Autowired 
private MyClient myClient;
...
URI determinedBasePathUri = URI.create("https://my-determined-host.com");
myClient.createUser(determinedBasePathUri, userDto);
Run Code Online (Sandbox Code Playgroud)

这将向()发送POST请求。https://my-determined-host.com/create


For*_*t10 5

我不知道你是否使用 spring 取决于多个配置文件。例如:like(dev,beta,prod 等等)

如果您依赖于不同的 yml 或属性。你可以定义FeignClient像:( @FeignClient(url = "${feign.client.url.TestUrl}", configuration = FeignConf.class))

然后

定义

feign:
  client:
    url:
      TestUrl: http://dev:dev
Run Code Online (Sandbox Code Playgroud)

在您的 application-dev.yml 中

定义

feign:
  client:
    url:
      TestUrl: http://beta:beta
Run Code Online (Sandbox Code Playgroud)

在您的 application-beta.yml 中(我更喜欢 yml)。

......

感谢上帝。享受。

  • 这是一个仅启动的解决方案,原始问题询问运行时,因此这是不相关的 (4认同)
  • 问题是关于运行时的,但这个答案只能在启动应用程序时使用,而不能在运行时使用。 (2认同)

小智 5

使用 feign.Target.EmptyTarget

@Bean
public BotRemoteClient botRemoteClient(){
    return Feign.builder().target(Target.EmptyTarget.create(BotRemoteClient.class));
}

public interface BotRemoteClient {

    @RequestLine("POST /message")
    @Headers("Content-Type: application/json")
    BotMessageRs sendMessage(URI url, BotMessageRq message);
}

botRemoteClient.sendMessage(new URI("http://google.com"), rq)
Run Code Online (Sandbox Code Playgroud)