解决方法如下:
最后自己解决了。
问题出在URL地址上。我用的zookeeper版本是3.6.0。
PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
这种用全大写的方式已经不行了,zookeeper上显示的是小写:cloud-payment-service 【取自服务注册端的spring.application.name属性】
我们也要用小写才行PAYMENT_URL = "http://cloud-payment-service";
才能找到服务。
我的问题情况如下:
使用spring-cloud开发了2个服务,服务注册使用zookeeper。
在zookeeper的zkCli.sh中能够查看到两个服务
然后通过其中cloud-order-service的项目,通过在controller中的以下代码,访问CLOUD-PAYMENT-SERVICE服务:
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Resource
private DiscoveryClient discoveryClient;
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
log.info("***payment: {}", payment);
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
结果是:
{
"timestamp": "2020-04-18T12:52:50.103+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No instances available for CLOUD-PAYMENT-SERVICE",
"path": "/consumer/payment/create"
}
错误内容:
2020-04-18 20:52:50.092 ERROR 6656 --- [p-nio-80-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for CLOUD-PAYMENT-SERVICE] with root causej
ava.lang.IllegalStateException: No instances available for CLOUD-PAYMENT-SERVICE
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:119) ~[spring-cloud-netflix-ribbon-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:99) ~[spring-cloud-netflix-ribbon-2.2.1.RELEASE.jar:2.2.1.RELEASE]
文章评论