@FeignClient(name = "test", url="http://xxxx")
如何在运行时更改假名URL(url =“http:// xxxx”)?因为URL只能在运行时确定。
答案 0 :(得分:8)
Feign可以在运行时提供动态URL和端点。
必须遵循以下步骤:
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);
}
FeignClient
。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);
}
}
答案 1 :(得分: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);
这将向POST
(source)发送一个https://my-determined-host.com/create
请求。
答案 2 :(得分:2)
您可以手动创建客户端:
@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");
}
}
请参阅文档:http://cloud.spring.io/spring-cloud-static/Camden.SR6/#_creating_feign_clients_manually
答案 3 :(得分:2)
使用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)
答案 4 :(得分:1)
我不知道您是否使用spring取决于多个配置文件。 例如:like(dev,beta,prod等)
如果您依赖于不同的yml或属性。您可以像这样定义FeignClient
:(@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)。
......
感谢上帝。
答案 5 :(得分: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);
}
答案 6 :(得分:-1)
package commxx;
import java.net.URI;
import java.net.URISyntaxException;
import feign.Client;
import feign.Feign;
import feign.RequestLine;
import feign.Retryer;
import feign.Target;
import feign.codec.Encoder;
import feign.codec.Encoder.Default;
import feign.codec.StringDecoder;
public class FeignTest {
public interface someItfs {
@RequestLine("GET")
String getx(URI baseUri);
}
public static void main(String[] args) throws URISyntaxException {
String url = "http://www.baidu.com/s?wd=ddd"; //ok..
someItfs someItfs1 = Feign.builder()
// .logger(new FeignInfoLogger()) // 自定义日志类,继承 feign.Logger
// .logLevel(Logger.Level.BASIC)// 日志级别
// Default(long period, long maxPeriod, int maxAttempts)
.client(new Client.Default(null, null))// 默认 http
.retryer(new Retryer.Default(5000, 5000, 1))// 5s超时,仅1次重试
// .encoder(Encoder)
// .decoder(new StringDecoder())
.target(Target.EmptyTarget.create(someItfs.class));
// String url = "http://localhost:9104/";
//
System.out.println(someItfs1.getx(new URI(url)));
}
}