在使用SpringCloud搭建微服务的时候,消费者应用通过RestTemplate来向服务提供者发送GET请求,但是报Connection Refusedc错误,如下:
我的配置文件如下:
注册中心:
server.port=1111
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.server.enable-self-preservation=false
服务提供者:
server.port=${random.int[10000,19999]}
spring.application.name=eureka-service
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
eureka.instance.instance-id=${spring.application.name}:${random.int}
消费者:
server.port=${random.int[10000,19999]}
server.servlet.context-path=/eclient
spring.application.name=eclient
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
eureka.instance.instance-id=${spring.application.name}:${random.int}
调用方式:
@RestController
@RequestMapping("/client")
public class ClientController {
@Autowired
RestTemplate template;
@GetMapping("/calc")
public void calc() {
System.out.println("~~~begin~~~");
Integer result = template.getForObject("http://EUREKA-SERVICE/service/calc?day={1}", Integer.class, 20);
System.out.println(result);
}
}