如何在运行时更改假名URL?

问题描述 投票:3回答:5

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

如何在运行时更改假名URL(url =“http:// xxxx”)?因为URL只能在运行时确定。

spring-cloud spring-cloud-feign
5个回答
10
投票

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);
}
  1. 在Rest Controller中,您必须导入FeignClientsConfiguration
  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);
    }
}

3
投票

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

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

然后使用将是:

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

这将向POSThttps://my-determined-host.com/create)发送source请求。


3
投票

您可以手动创建客户端:

@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");
     }
}

请参阅文档:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html#_creating_feign_clients_manually


1
投票

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

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

然后

限定

feign:
  client:
    url:
      TestUrl: http://dev:dev

在你的application-dev.yml中

限定

feign:
  client:
    url:
      TestUrl: http://beta:beta

在你的应用程序-beta.yml(我更喜欢yml)。

......

谢谢god.enjoy。


0
投票

在界面中,您可以通过Spring注释更改URL。基URI在yml Spring配置中配置。

   @FeignClient(
            name = "some.client",
            url = "${some.serviceUrl:}",
            configuration = FeignClientConfiguration.class
    )

public interface SomeClient {

    @GetMapping("/metadata/search")
    String search(@RequestBody SearchCriteria criteria);

    @GetMapping("/files/{id}")
    StreamingResponseBody downloadFileById(@PathVariable("id") UUID id);

}
© www.soinside.com 2019 - 2024. All rights reserved.