一个简单的测试:使用eureka+ribbon访问服务。配置都配好了,实在不知道问题出在哪儿。。。
1.服务端
1.1服务端注解:@EnableEurekaClient
1.2服务端配置
server:
port: ${PORT:31001}
spring:
application:
name: xc-service-manage-cms
eureka:
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: ${EUREKA_SERVER:http://localhost:50101/eureka/}
instance:
prefer-ip-address: true
ip-address: ${IP_ADDRESS:127.0.0.1}
instance-id: ${spring.application.name} :${server.port}
2.客户端
2.1注解
2.2配置
server:
port: 31200
spring:
application:
name: xc-service-manage-course
eureka:
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: ${EUREKA_SERVER:http://localhost:50101/eureka/}
instance:
prefer-ip-address: true
ip-address: ${IP_ADDRESS:127.0.0.1}
instance-id: ${spring.application.name} :${server.port}
ribbon:
MaxAutoRetries: 2
MaxAutoRetriesNextServer: 3
OkToRetryOnAllOperations: false
ConnectTimeout: 5000
ReadTimeout: 6000
3.Eureka注册中心页面
服务和客户端都注册进去了
4.测试代码
@SpringBootTest(classes = ManageCourseApplication.class)
@RunWith(SpringRunner.class)
public class TestRibbon {
@Autowired
RestTemplate restTemplate;
@Test
public void test(){
String serviceId="XC-SERVICE-MANAGE-CMS";
ResponseEntity<CmsPage> forEntity = restTemplate.getForEntity("http://"+serviceId+"/cms/manage/get/5a754adf6abb500ad05688d9", CmsPage.class);
CmsPage body = forEntity.getBody();
System.out.println(body);
}
}
报错:
java.lang.IllegalStateException: No instances available for XC-SERVICE-MANAGE-CMS
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:89)
at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:55)
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92)
at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:76)
<p>大兄弟,eureka服务端注解是@EnableEurekaServer,客户端注解是@EnableEurekaClient。</p>
大兄弟注意区别!
我也在做这个项目,也是在Spring Cloud卡主,你bug我知道原因,我的bug就不知道什么原因了!
……做这个项目都是坎,不容易,大兄弟加油……
你好,我在eureka这块也卡住了,服务者注册到了eureka,消费者可以通过discoveryClient.getServices()获取到serviceNames,但是discoveryClient.getInstances(serviceName)却获取不到服务实例,是空,可能是那些方面的原因?
<p>我和你一样在这里出错了,解决了吗?</p>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。