我有一个 Spring Boot 应用程序试图通过 Eureka/Ribbon 获取服务实例:
@LoadBalanced
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hi(@RequestParam(value="name", defaultValue="superuser") String name) {
AccountRest account = this.restTemplate.getForObject("http://AUTHENTICATION-SERVICE/authservice/v1/accounts/userId/"+name,
AccountRest.class);
return "hi, " + account.getId();
}
我是手动注册“AUTHENTICATION-SERVICE”,注意这个服务只需要注册自己,不需要查询eureka:
public class ServiceDiscoveryManager {
private ApplicationInfoManager applicationInfoManager;
private EurekaClient eurekaClient;
public void start() {
MyDataCenterInstanceConfig instanceConfig = new MyDataCenterInstanceConfig();
DefaultEurekaClientConfig clientConfig = new DefaultEurekaClientConfig();
InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo);
eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);
applicationInfoManager.getInfo();
eurekaClient.getApplications();
instanceInfo.setStatus(InstanceStatus.UP);
}
public void stop() {
eurekaClient.shutdown();
}
}
ServiceDiscoveryManager 的 Spring 接线:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="serviceDiscoveryManager" class="com.embotics.authervice.serviceDiscovery.ServiceDiscoveryManager" init-method="start"/>
</beans>
身份验证服务 eureka-client.properties 文件:
eureka.name=AUTHENTICATION-SERVICE
eureka.appGroup=AUTHENTICATION-SERVICE
eureka.vipAddress=http://localhost:9080
eureka.port.enabled=true
eureka.port=9080
eureka.traffic.enabled=true
eureka.preferSameZone=true
eureka.serviceUrl.default=http://localhost:8000/eureka/
eureka.decoderName=JacksonJson
eureka.healthCheckUrl=http://localhost:9080/authservice/health
eureka.healthCheckPath=/authservice/health
Eureka Dashboard 显示我的身份验证服务已注册:
当我尝试使用负载均衡器获取身份验证服务的实例时,我收到以下异常:
java.lang.IllegalStateException: No instances available for AUTHENTICATION-SERVICE
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:90) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
我已经确认负载均衡器知道身份验证服务,但 DynamicServerListLoadBalancer 的 allServerList 属性为空。
我已经玩了几个小时了,想知道是否某些配置无效。谢谢你的帮助。