如何使用 netflix-eureka 获取已注册的微服务
how can I get registered micro services by using netflix-eureka
我使用 Netflix 的 Eureka 注册了很多微服务,我也在使用 Spring Cloud Config。但是,当我更新 application.properties
时,我需要重新启动应用程序才能应用新属性。
重新启动应用程序有点烦人,所以谷歌了一下,发现使用Netflix Eureka注册微服务并使用spring启动执行器刷新它,但我没有注册微服务。
简而言之:如何使用 Netflix Eureka 注册微服务?
如果您尝试使用 eureka 来识别服务位置,那么我强烈建议您在客户端使用 ribbon。因此,您无需对应用程序进行任何其他更改。请看一看这个。
https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html
您可以使用 com.netflix.discovery.EurekaClient
界面以编程方式访问 Eureka 的注册表。
例如,要向某些服务的所有实例发送刷新:
@RestController
public class EurekaOperationsController {
@Autowired
private EurekaClient eurekaClient;
@Autowired
private RestTemplate restTemplate;
@PostMapping("/{serviceName}")
public void refreshAllInstancesOf(@RequestParam String serviceName) {
Application application = eurekaClient.getApplications().getRegisteredApplications(serviceName);
application.getInstances().forEach(instanceInfo -> {
restTemplate.postForEntity(instanceInfo.getHomePageUrl() + "actuator/refresh", null, Void.class);
});
}
}
这可以在任何 Eureka 客户端中,包括 Eureka 服务本身,因为它应该自行注册为客户端。
我使用 Netflix 的 Eureka 注册了很多微服务,我也在使用 Spring Cloud Config。但是,当我更新 application.properties
时,我需要重新启动应用程序才能应用新属性。
重新启动应用程序有点烦人,所以谷歌了一下,发现使用Netflix Eureka注册微服务并使用spring启动执行器刷新它,但我没有注册微服务。
简而言之:如何使用 Netflix Eureka 注册微服务?
如果您尝试使用 eureka 来识别服务位置,那么我强烈建议您在客户端使用 ribbon。因此,您无需对应用程序进行任何其他更改。请看一看这个。 https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html
您可以使用 com.netflix.discovery.EurekaClient
界面以编程方式访问 Eureka 的注册表。
例如,要向某些服务的所有实例发送刷新:
@RestController
public class EurekaOperationsController {
@Autowired
private EurekaClient eurekaClient;
@Autowired
private RestTemplate restTemplate;
@PostMapping("/{serviceName}")
public void refreshAllInstancesOf(@RequestParam String serviceName) {
Application application = eurekaClient.getApplications().getRegisteredApplications(serviceName);
application.getInstances().forEach(instanceInfo -> {
restTemplate.postForEntity(instanceInfo.getHomePageUrl() + "actuator/refresh", null, Void.class);
});
}
}
这可以在任何 Eureka 客户端中,包括 Eureka 服务本身,因为它应该自行注册为客户端。