如何用功能区了解主机?

时间:2018-02-22 21:50:46

标签: spring-cloud netflix-eureka spring-cloud-netflix netflix-feign netflix-ribbon

我有eureka,ribbon和feign的应用程序。我有一个假的RequestInterceptor,但问题是我需要知道哪个是我正在进行呼叫的主机。到目前为止,使用我当前的代码,我可以使用RequestTemplate获取路径,但不能获取主机。

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

我也是新手,但我相信我刚学到了与你的问题相关的东西。

在您正在创建的服务中,可以为每个实例提供一些与其实例配置文件中包含的属性相关联的唯一标识符。下面是一些.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

此服务作为设计的示例将显示,可以发送@Value带注释的属性以供Ribbon应用程序使用:

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

只是为了完成该示例,可能使用这些属性的功能区应用程序可能如下所示:

@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创建一个动态属性标识符将是理想的选择。