spring-cloud-netflix icon indicating copy to clipboard operation
spring-cloud-netflix copied to clipboard

Invoking a Ribbon(loadbalanced) call at Zuul Startup using feign/restemplates leads to StackOverFlow

Open hodeh opened this issue 9 years ago • 3 comments

I wanted to do a custom logic in DiscoveryClientRouteLocator.locateRoutes so I have overriden that method, the custom logic I wanted to do is related to this ( https://github.com/spring-cloud/spring-cloud-netflix/issues/2034#issuecomment-307822197 )

in the overriden locateroutes I tried to use a feign and a RestTemplate to execute a ribbon call to service, the call has led to generating those logs continiously until it led to a stackoverflow. I am afraid that I am calling it in early stage before a complete initalization of the loadbalancers , is there a way to overcome this ?

2017-06-14 19:55:43.278  INFO [public-api-gateway,,,] 2757 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@523198f0: startup date [Wed Jun 14 19:55:43 GST 2017]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@436390f4
2017-06-14 19:55:43.278  INFO [public-api-gateway,,,] 2757 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@523198f0: startup date [Wed Jun 14 19:55:43 GST 2017]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@436390f4
2017-06-14 19:55:43.296  INFO [public-api-gateway,,,] 2757 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-06-14 19:55:43.296  INFO [public-api-gateway,,,] 2757 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-06-14 19:55:43.325  INFO [public-api-gateway,,,] 2757 --- [           main] c.netflix.config.ChainedDynamicProperty  : Flipping property: user-identity.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2017-06-14 19:55:43.325  INFO [public-api-gateway,,,] 2757 --- [           main] c.netflix.config.ChainedDynamicProperty  : Flipping property: user-identity.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2017-06-14 19:55:43.330  INFO [public-api-gateway,,,] 2757 --- [           main] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook installed for: NFLoadBalancer-PingTimer-user-identity
2017-06-14 19:55:43.330  INFO [public-api-gateway,,,] 2757 --- [           main] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook installed for: NFLoadBalancer-PingTimer-user-identity
2017-06-14 19:55:43.331  INFO [public-api-gateway,,,] 2757 --- [           main] c.netflix.loadbalancer.BaseLoadBalancer  : Client:user-identity instantiated a LoadBalancer:DynamicServerListLoadBalancer:{NFLoadBalancer:name=user-identity,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2017-06-14 19:55:43.331  INFO [public-api-gateway,,,] 2757 --- [           main] c.netflix.loadbalancer.BaseLoadBalancer  : Client:user-identity instantiated a LoadBalancer:DynamicServerListLoadBalancer:{NFLoadBalancer:name=user-identity,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2017-06-14 19:55:43.333  INFO [public-api-gateway,,,] 2757 --- [           main] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2017-06-14 19:55:43.333  INFO [public-api-gateway,,,] 2757 --- [           main] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2017-06-14 19:55:43.338  INFO [public-api-gateway,,,] 2757 --- [           main] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client user-identity initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=user-identity,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@630ae6ce
2017-06-14 19:55:43.338  INFO [public-api-gateway,,,] 2757 --- [           main] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client user-identity initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=user-identity,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@630ae6ce

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.netflix.loadbalancer.IRule]: Factory method 'ribbonRule' threw exception; nested exception is java.lang.StackOverflowError

hodeh avatar Jun 14 '17 15:06 hodeh

it can be due to the fact that invoking a Ribbon call will cause a ContextRefreshEvent that will cause refresh that ends up with calling the same overriden method DiscoveryClientRouteLocator.locateroutes that it self has the ribbon invocation. hence the stackoverflow, not sure how to overcome this

hodeh avatar Jun 14 '17 17:06 hodeh

Solved by preventing Ribbon client from being invoked again using a flag that determines if Ribbon call is being executed or not.

hodeh avatar Jun 14 '17 21:06 hodeh

I am new in spring cloud and I'm getting the following exception when I hit Zuul server to access the microservice.

Servers:

  1. Eureka Server (for service registry and discovery)
  2. Zuul Server (For Routing and load balancing)
  3. Notification Service (business microservice)

Zuul and notification service both connect with Eureka Server.

Requirement:

  1. Access notification service via Zuul and Zuul also handle load balancing on itself.

If anyone know how to resolve this issue and handle routes with load balancing on Zuul Server then please let me know.

Zuul Server Exception:


2019-02-13 16:43:52.296  INFO 1840 --- [nio-8079-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-02-13 16:43:52.303  INFO 1840 --- [nio-8079-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 7 ms
Request Method : POST Request URL : http://192.168.11.27:8079/SIMS/notification-service/v1/endpointhandler/get/56/0
Using Route Filter
2019-02-13 16:43:52.507  INFO 1840 --- [nio-8079-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: NOTIFICATION-SERVICE-V1.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-02-13 16:43:52.527  INFO 1840 --- [nio-8079-exec-1] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook installed for: NFLoadBalancer-PingTimer-NOTIFICATION-SERVICE-V1
2019-02-13 16:43:52.528  INFO 1840 --- [nio-8079-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client: NOTIFICATION-SERVICE-V1 instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=NOTIFICATION-SERVICE-V1,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2019-02-13 16:43:52.533  INFO 1840 --- [nio-8079-exec-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2019-02-13 16:43:52.550  INFO 1840 --- [nio-8079-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: NOTIFICATION-SERVICE-V1.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-02-13 16:43:52.551  INFO 1840 --- [nio-8079-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client NOTIFICATION-SERVICE-V1 initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=NOTIFICATION-SERVICE-V1,current list of Servers=[192.168.11.27:2020],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;	Instance count:1;	Active connections count: 0;	Circuit breaker tripped count: 0;	Active connections per server: 0.0;]
},Server stats: [[Server:192.168.11.27:2020;	Zone:defaultZone;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 05:30:00 IST 1970;	First connection made: Thu Jan 01 05:30:00 IST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@4507e1dd
Using Post Filter
2019-02-13 16:43:53.536  INFO 1840 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: NOTIFICATION-SERVICE-V1.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2019-02-13 16:48:47.489  INFO 1840 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

Zuul Configruation :


eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
#     New
    healthcheck:
      enabled: true
  instance:
    preferIpAddress: true
    #     New
    statusPageUrlPath: ${server.servlet.context-path}/actuator/info
    healthCheckUrlPath: ${server.servlet.context-path}/actuator/health    
    
server:
  port: 8079
  servlet:
    context-path: /SIMS
    
spring:
  application:
    name: ROUTER-SERVICE
zuul:
  routes:
    notification-service:
      path: '/notification-service/v1/**/**/**/**'
      service-id: NOTIFICATION-SERVICE-V1

Notification Service Config: (Which I want to access via Zuul)


eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
#     New
    healthcheck:
      enabled: true
  instance:
    preferIpAddress: true
    #     New
    statusPageUrlPath: ${server.servlet.context-path}/actuator/info
    healthCheckUrlPath: ${server.servlet.context-path}/actuator/health

Notification Service Controller:


@RestController
@RefreshScope
@RequestMapping("/endpointhandler")
public class NSController {

	@RequestMapping(value="/get/{queryId}/{tenantId}" , method=RequestMethod.POST)
	public ServiceResponse get(@RequestBody String data, @PathVariable("queryId") long queryId, @PathVariable("tenantId") long tenantId) { 

		ServiceResponse response = new ServiceResponse();
		try {
			// Bussiness Login
			response.setStatus(ResponseConstants.SUCCESS);
			response.setData( requestHandler.getResultList());
					
		} catch (Exception e) {
			response.setStatus(ResponseConstants.FAIL);
			response.setErrorMsg(e.getMessage());
		}
		return response;
	}
}

GursharanKhalsa avatar Feb 13 '19 11:02 GursharanKhalsa