我有eureka,ribbon和feign的应用程序。我有一个假的RequestInterceptor,但问题是我需要知道哪个是我正在进行呼叫的主机。到目前为止,使用我当前的代码,我可以获得RequestTemplate的路径,但不能获取主机。
任何的想法?
我也是新手,但我相信我刚刚学到了与你的问题相关的东西。
在您正在创建的服务中,可以为每个实例提供一些与其实例配置文件中包含的属性相关联的唯一标识符。一些.properties示例如下:
application.properties(实例之间的共享属性)
spring.application.name=its-a-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
application-instance1.properties
server.port=5678
instance.uid=some-unique-property
application-instance2.properties
server.port=8765
instance.uid=other-unique-property
这个服务,作为一个非常人为的例子将显示,可以发送由Ribbon应用程序使用的@Value
注释属性:
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ItsAServiceApplication {
@Value("${server.port}")
private int port;
@Value("${instance.uid}")
private String instanceIdentifier;
public static void main(String[] args) {
SpringApplication.run(ItsAServiceApplication.class, args);
}
@RequestMapping
public String identifyMe() {
return "Instance: " + instanceIdentifier + ". Running on port: " + port + ".";
}
}
只是为了完成示例,可能使用这些属性的Ribbon应用程序可能如下所示:
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ServiceIdentifierAppApplication {
@Autowired
private RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(ServiceIdentifierAppApplication.class, args);
}
@GetMapping
public String identifyMe() {
return restTemplate.getForEntity("http://its-a-service", String.class).getBody();
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
结果:Reloading the rest template created by the services
正如我之前所说,我对此很陌生,并且刚学会了自己。希望这能让您了解如何发送这些属性!我想通过Spring Cloud Config创建一个动态属性标识符将是理想的选择。