在 Spring Boot 中,可以使用 @ConfigurationProperties 注解来实现可配置 URL 的 Feign 调用。此外,还需要使用 @FeignClient 注解来指定 Feign 客户端的名称和配置。

首先,在 application.properties 文件中添加 Feign 客户端的配置:

my.feign.client.url=http://localhost:8080/api

然后,创建一个 Feign 客户端接口,并在接口上使用 @FeignClient 注解来指定 Feign 客户端的名称和配置:

@FeignClient(name = 'myFeignClient', url = '${my.feign.client.url}')
public interface MyFeignClient {

    @GetMapping('/users/{id}')
    User getUserById(@PathVariable('id') Long id);

}

在这个例子中,MyFeignClient 是 Feign 客户端的接口,它使用 @FeignClient 注解来指定 Feign 客户端的名称和配置。url 属性从配置文件中读取,并指定为 Feign 客户端的 URL。

最后,可以使用 MyFeignClient 接口来进行 Feign 调用:

@RestController
public class UserController {

    @Autowired
    private MyFeignClient myFeignClient;

    @GetMapping('/users/{id}')
    public User getUserById(@PathVariable('id') Long id) {
        return myFeignClient.getUserById(id);
    }

}

在这个例子中,UserController 中注入了 MyFeignClient,并使用 getUserById 方法来进行 Feign 调用。Feign 客户端的 URL 从配置文件中读取,并可以在运行时进行修改。

Spring Boot 可配置 URL 的 Feign 调用实现

原文地址: https://www.cveoy.top/t/topic/oYH7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录