spring cloud中服务注册成功,但不能通过服务名来调用IP地址

lzw95 2018-07-12 05:10:48
最近在学习spring cloud,按照网上的教程建立了hello-service服务,但由服务消费者调用时显示No instances available for HELLO-SERVICE

但是服务注册已经成功了

这是为什么?我直接调用服务IP地址就可以得到返回值

服务提供者的controller类和入口类如下:
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index() {
List<ServiceInstance> instances = client.getInstances("hello-service");
for (int i = 0; i < instances.size(); i++) {
logger.info("/hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
}
return "Hello World";
}
}

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}


服务消费者的controller类和入口类:
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
public String helloController() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
...全文
6520 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京天很蓝 2020-06-19
  • 打赏
  • 举报
回复
引用 楼主 lzw95 的回复:
最近在学习spring cloud,按照网上的教程建立了hello-service服务,但由服务消费者调用时显示No instances available for HELLO-SERVICE

但是服务注册已经成功了

这是为什么?我直接调用服务IP地址就可以得到返回值

服务提供者的controller类和入口类如下:
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index() {
List<ServiceInstance> instances = client.getInstances("hello-service");
for (int i = 0; i < instances.size(); i++) {
logger.info("/hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
}
return "Hello World";
}
}

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}


服务消费者的controller类和入口类:
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
public String helloController() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
用nacos+sentinel+feign吧,直接feign远程调用不香么。
樛木33 2020-06-15
  • 打赏
  • 举报
回复
12楼正解,我用的是Hoxton.SR3版本
红姬茄子 2019-07-15
  • 打赏
  • 举报
回复
大佬,请问你是怎么解决的,缺了什么依赖。我现在eureka使用主机名注册后,无法调用我的服务接口,求解
斌小哥 2019-05-14
  • 打赏
  • 举报
回复
引用 16 楼 changerzhuo 的回复:
[quote=引用 12 楼 斌小哥 的回复:] RestTemplate注入有问题 新版的需要这样注入: @Bean @LoadBalanced RestOperations restTemplate(RestTemplateBuilder builder) { return builder.build(); } @Autowired RestOperations restTemplate;
大佬, 你的版本是什么?我用了你这种方式, 仍然有问题。 我在想会不会是版本的问题?[/quote] SpringBoot版本 2.0.4.RELEASE SpringCloud版本 Finchley.SR1 上面注入方法还可以简写一下: @Bean @LoadBalanced public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); }
Fay_Q 2019-05-14
  • 打赏
  • 举报
回复 1
https://blog.csdn.net/o_darling/article/details/82147350
jumpheightway 2019-05-14
  • 打赏
  • 举报
回复
prefer-ip-address: true
changerzhuo 2019-05-14
  • 打赏
  • 举报
回复
引用 18 楼 斌小哥 的回复:
[quote=引用 16 楼 changerzhuo 的回复:] [quote=引用 12 楼 斌小哥 的回复:] RestTemplate注入有问题 新版的需要这样注入: @Bean @LoadBalanced RestOperations restTemplate(RestTemplateBuilder builder) { return builder.build(); } @Autowired RestOperations restTemplate;
大佬, 你的版本是什么?我用了你这种方式, 仍然有问题。 我在想会不会是版本的问题?[/quote] SpringBoot版本 2.0.4.RELEASE SpringCloud版本 Finchley.SR1 上面注入方法还可以简写一下: @Bean @LoadBalanced public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } [/quote] 我的问题解决了, 我是缺了eureka的依赖。。。尴尬。 谢谢大佬
changerzhuo 2019-05-13
  • 打赏
  • 举报
回复
引用 12 楼 斌小哥 的回复:
RestTemplate注入有问题 新版的需要这样注入: @Bean @LoadBalanced RestOperations restTemplate(RestTemplateBuilder builder) { return builder.build(); } @Autowired RestOperations restTemplate;
大佬, 你的版本是什么?我用了你这种方式, 仍然有问题。 我在想会不会是版本的问题?
  • 打赏
  • 举报
回复
为什么不用feign老哥
qq_37816174 2019-05-09
  • 打赏
  • 举报
回复
请问你解决了吗,我和你出现了一样的问题。13楼说12楼是正解,你试了吗,我不知道public RestOperations restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
RestTemplateBuilder 这个参数是什么
A941250 2019-02-22
  • 打赏
  • 举报
回复
12楼是正解,折腾了好多天
斌小哥 2019-01-28
  • 打赏
  • 举报
回复
RestTemplate注入有问题
新版的需要这样注入:
@Bean
@LoadBalanced
RestOperations restTemplate(RestTemplateBuilder builder) {
return builder.build();
}

@Autowired
RestOperations restTemplate;
Insist_on_progress 2019-01-08
  • 打赏
  • 举报
回复

eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: jih-manage
prefer-ip-address: true
server:
enable-self-preservation: false
这是发布者的eureka配置。源代码在https://github.com/qpdyg/jih
默凡_lt 2019-01-06
  • 打赏
  • 举报
回复
我也遇到了,刚解决 在消费端访问服务时写spring.application.name的对外暴露名称就好了
fanxl12 2018-10-28
  • 打赏
  • 举报
回复
eureka.instance.hostname配置有没有,有的话删除掉再试下
蒹葭残辉 2018-08-18
  • 打赏
  • 举报
回复
之前我也遇到过,是不是和我的一样呢?https://blog.csdn.net/a729913162/article/details/81123078#comments
qq_41220113 2018-08-14
  • 打赏
  • 举报
回复
因为你的 @RequestMapping 里的 value 写的 hello
JunneYang 2018-07-12
  • 打赏
  • 举报
回复
消费端
eureka的配置对吗?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧