最近在学习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();
}
}
@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();
}
}
解决方案 »
- weblogic8.1,mstr8.02,jdk1.4 点第一次时登录不了,点第二次以上才能登录成功
- 无刷新分页
- ajax返回集合取值问题....
- 怎么替换文件中的字符啊
- Spring Freemarker 相关问题
- EJB3 问题(entityManager.getTransaction无法提交事务)
- 笑话```java多线程
- 问一下各位朋友,私有的注册中心如何建立?juddi怎么使用?不甚感谢
- java中如何实现按队列执行任务?
- 请教高手,急!
- springboot关联jsp报错java.lang.ClassNotFoundException: org.apache.tools.ant.Project
- jrebel 2018 不能激活的解决办法
eureka的配置对吗?
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
新版的需要这样注入:
@Bean
@LoadBalanced
RestOperations restTemplate(RestTemplateBuilder builder) {
return builder.build();
} @Autowired
RestOperations restTemplate;
return builder.build();
}
RestTemplateBuilder 这个参数是什么
SpringBoot版本 2.0.4.RELEASE
SpringCloud版本 Finchley.SR1
上面注入方法还可以简写一下: @Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
SpringBoot版本 2.0.4.RELEASE
SpringCloud版本 Finchley.SR1
上面注入方法还可以简写一下: @Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
我的问题解决了, 我是缺了eureka的依赖尴尬。 谢谢大佬