环境:
win10 64bit
安装的JDK 1.6 32bit
绿色版JDK 1.8 64bit
STS 3.8.4.RELEASE,STS使用的JDK是1.8(启动和运行)
spring boot version:1.5.7
hosts:
# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 172.20.207.252 s1 172.20.207.252 s2 172.20.207.252 s3 172.20.207.252 s4 172.20.207.252 s5
git代码地址:https://github.com/yixiuquan/Eureka,详细请看EurekaRegisterServiceMaster,EurekaServiceProvider,EurekaServiceConsumer三个项目Eureka服务正常启动,并且服务也可以被发现,
但是使用Feign消费服务却报错,报错信息:java.lang.StackOverflowError: null具体如下:
2017-09-14 17:13:15.621 INFO 13624 --- [nio-8766-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 29 ms 2017-09-14 17:13:15.721 ERROR 13624 --- [nio-8766-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause java.lang.StackOverflowError: null at com.ccs.EurekaServiceService.getUser(EurekaServiceService.java:17) ~[classes/:na] at com.ccs.EurekaServiceService.getUser(EurekaServiceService.java:17) ~[classes/:na]
以下各项目的部分代码
Eureka服务注册中心:
application.xml:
eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.server.enable-self-preservation=false eureka.instance.hostname=s1 spring.application.name=master server.port=8761 eureka.client.serviceUrl.defaultZone=http://s2:8762/eureka/,http://s3:8763/eureka/
EurekaRegisterServiceMasterApplication.java:
@EnableEurekaServer @SpringBootApplication public class EurekaRegisterServiceMasterApplication { public static void main(String[] args) { SpringApplication.run(EurekaRegisterServiceMasterApplication.class, args); } }
Eureka服务provider:开启了两个provider端口分别是8764和8765,两个代码都一样只是端口不一样,下面是8764的配置
application.xml:
eureka.instance.hostname=s4 spring.application.name=serviceprovider server.port=8764 eureka.client.serviceUrl.defaultZone=http://s1:8761/eureka/
EurekaServiceProviderApplication.java:
@SpringBootApplication @RestController @EnableDiscoveryClient @RequestMapping(value="/v1",produces="application/json") public class EurekaServiceProviderApplication { @Value("${server.port}") private String port; public static void main(String[] args) { SpringApplication.run(EurekaServiceProviderApplication.class, args); } @RequestMapping(value="/getUser/{str}",method={RequestMethod.GET,RequestMethod.POST}) public User getUser(@PathVariable("str") String str){ User vo = new User(); String id = str.split(",")[0]; String name = str.split(",")[1]; vo.setId(id); vo.setName(name); vo.setPort(port); return vo; } }
Eureka服务consumer:
application.xml:
eureka.instance.hostname=s5 spring.application.name=serviceconsumer server.port=8766 eureka.client.serviceUrl.defaultZone=http://s1:8761/eureka/
EurekaServiceConsumerApplication.java:
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class EurekaServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServiceConsumerApplication.class, args); } }
EurekeServiceInterface.java:
@FeignClient("serviceprovider") public interface EurekeServiceInterface { @RequestMapping(value="/v3/getUser/{str}",method=RequestMethod.GET) public User getUser(@PathVariable("str") String str); }
EurekaServiceService.java:
@RestController public class EurekaServiceService { @Autowired EurekaServiceService eurekaServiceService; @RequestMapping(value="/v2/getUser/{str}",method=RequestMethod.GET) public String getUser(@PathVariable("str") String str){ return eurekaServiceService.getUser(str); } }
经QQ群大佬指点,我在EurekaServiceService里面居然引入了自己EurekaServiceService,导致调用时出现递归从而导致栈溢出,将引入的EurekaServiceService改为EurekeServiceInterface就好了,虽然很又爆了com.netflix.client.ClientException: Load balancer does not have available server for client: serviceprovider
public class EurekaServiceService {
@Autowired
EurekaServiceService eurekaServiceService;
@RequestMapping(value="/v2/getUser/{str}",method=RequestMethod.GET)
public String getUser(@PathVariable("str") String str){
return eurekaServiceService.getUser(str);
}
}