spring-gateway如何配置feign调用的url,需要从euruka动态获取目标ip端口信息。
时间: 2024-02-23 15:02:39 浏览: 32
在Spring Cloud Gateway中,可以通过配置Route来将请求路由到对应的服务上。在配置Route时,可以使用Eureka中的服务名作为目标服务的标识符,但是需要注意的是,Eureka中的服务名只是一个逻辑名字,并不是具体的IP地址和端口号。
如果要从Eureka中动态获取目标IP地址和端口信息,可以使用Spring Cloud的DiscoveryClient来获取服务的实例信息。在获取到实例信息后,可以通过Feign的@FeignClient注解配置URL,从而实现对目标服务的调用。具体的步骤如下:
1. 引入Spring Cloud Eureka和Feign的依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
```
2. 在Spring Cloud Gateway中配置Route,使用服务名作为目标服务的标识符:
```yaml
spring:
cloud:
gateway:
routes:
- id: service-a-route
uri: lb://service-a
predicates:
- Path=/service-a/**
```
3. 在Feign的@FeignClient注解中配置URL,使用DiscoveryClient获取服务实例信息并构造URL:
```java
@FeignClient(name = "service-a", url = "${service-a.url}")
public interface ServiceAClient {
// ...
}
@Configuration
public class ServiceAConfiguration {
private final DiscoveryClient discoveryClient;
public ServiceAConfiguration(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@Bean
public ServiceInstanceListSupplier serviceInstanceListSupplier() {
return ServiceInstanceListSupplier.builder()
.withDiscoveryClient(discoveryClient)
.withServiceName("service-a")
.build();
}
@Bean
public IRule ribbonRule() {
return new WeightedResponseTimeRule();
}
@Bean
@ConditionalOnMissingBean
public RestClient restClient(RibbonClientFactory ribbonClientFactory) {
return new RibbonRestClient(ribbonClientFactory);
}
@Bean
@ConditionalOnMissingBean
public RibbonLoadBalancerContext ribbonLoadBalancerContext(SpringClientFactory clientFactory) {
return clientFactory.getLoadBalancerContext("default");
}
@Bean
@ConditionalOnMissingBean
public SpringClientFactory clientFactory() {
return new SpringClientFactory();
}
@Bean
public LoadBalancerProperties loadBalancerProperties() {
return new LoadBalancerProperties();
}
@Bean
public Request.Options options() {
return new Request.Options(5000, 5000);
}
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public Decoder decoder() {
return new JacksonDecoder();
}
@Bean
public Encoder encoder() {
return new JacksonEncoder();
}
@Bean
public Retryer retryer() {
return new Retryer.Default();
}
@Bean
public Contract contract() {
return new SpringMvcContract();
}
@Value("${service-a.port}")
private Integer port;
@Value("${service-a.context-path}")
private String contextPath;
@Bean
public String serviceAUrl() {
ServiceInstance instance = discoveryClient.getInstances("service-a").get(0);
String host = instance.getHost();
String url = String.format("http://%s:%d/%s", host, port, contextPath);
return url;
}
}
```
在上述代码中,我们首先通过DiscoveryClient获取服务实例信息,然后根据实例信息构造URL,并将URL注入到Feign的@FeignClient注解中。需要注意的是,在配置Feign的时候,需要手动构造一些Bean,例如ServiceInstanceListSupplier、IRule等,以便让Feign能够正确地调用目标服务。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)