解决Springcloud微服务注册到Eureka上遇到的com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException的两种方法。
目录
问题说明:
1.首先导入eureka客户端和服务端的依赖如下
2.编写eureka端的yml文件如下
3.编写了eureka的启动代码
4.配置微服务端的yml文件如下
5.编写服务端的启动代码
6.先启动eurekaServer端
7.在浏览器中输入localhost:7001
8.启动微服务端,然后出现了错误com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect,意思是微服务无法连接上7001端口的eureka
9.查找资料发现配资的yml是没有问题的
10.仔细观察报错的信息,发现在错误信息上面有一个红色ERROR如下
解决方法一:
1.将eureka的端口号设置为8761
2.微服务访问的端口号也设置为8761
3.按照上面的顺序重新启动,在浏览器中输入localhost:8761回车
解决方法二:
1.看上面我写的defaultzone全是小写的,将defaultzone改成defalutZone,端口号设置为7001,重新尝试连接
2.在浏览器中输入localhost:7001回车,如下图所示
问题说明:
今天将一个微服务模块入驻eurekaServer的过程中出现了下面的错误。
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server。
下面展示我的这个微服务的代码和配置文件以及eureka的代码和配置文件
1.首先导入eureka客户端和服务端的依赖如下
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.编写eureka端的yml文件如下
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
注意服务端的端口号是7001
3.编写了eureka的启动代码
@SpringBootApplication
@EnableEurekaServer
public class Eureka7001 {
public static void main(String[] args) {
SpringApplication.run(Eureka7001.class,args);
}
}
4.配置微服务端的yml文件如下
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultzone: http://localhost:7001/eureka
5.编写服务端的启动代码
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
6.先启动eurekaServer端
7.在浏览器中输入localhost:7001
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultzone: http://localhost:7001/eureka
5.编写服务端的启动代码
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
6.先启动eurekaServer端
7.在浏览器中输入localhost:7001
7.在浏览器中输入localhost:7001
上面说明eureka是没问题的
8.启动微服务端,然后出现了错误com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect,意思是微服务无法连接上7001端口的eureka
9.查找资料发现配资的yml是没有问题的
10.仔细观察报错的信息,发现在错误信息上面有一个红色ERROR如下
10.仔细观察报错的信息,发现在错误信息上面有一个红色ERROR如下
发现这里的服务端口号是8761,可能是eureka默认的端口号吧。
下面展示我的两种解决方法
解决方法一:
1.将eureka的端口号设置为8761
2.微服务访问的端口号也设置为8761
3.按照上面的顺序重新启动,在浏览器中输入localhost:8761回车
解决方法二:
1.看上面我写的defaultzone全是小写的,将defaultzone改成defalutZone,端口号设置为7001,重新尝试连接
2.在浏览器中输入localhost:7001回车,如下图所示
上面两种解决方法都挺神奇的,希望能够帮助到你。



