3

I am now thinking how to combine spring cloud with nodejs. So I want to use nodeJS as middle tier, which is providing api to frontend angularjs application, but once nodeJS is getting api request, it will trigger other http request to backend spring api. I noticed spring cloud is providing a lot very good feature, such as service discovery, configure server, BUT these are all used for spring itself. Let's say,how can I load configuration server result in nodeJS, if that can be easily done through json parsing, how can I use service discovery from NodeJS? In spring project, I can use:

InstanceInfo instance = discoveryClient.getNextServerFromEureka("spirent", false);
String url = instance.getHomePageUrl();

But if I am using nodeJS, how can nodeJS easily find out next service instance from eureka service?

Hope to hear your advice and suggestions.

Frank

Based on the suggestion, I added

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-netflix-sidecar</artifactId>
        <version>1.0.2.RELEASE</version>
    </dependency>

To my current Eureka server POM, and then I have this in the mainApplication:

@SpringBootApplication
@EnableEurekaServer
@EnableSidecar

public class SpringCloudEurekaServerApplication {

    public static void main(String[] args) throws IOException {
        System.err.println(new ClassPathResource("static/eureka/css/wro.css").getURL());
        SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
    }
}

The Application.yml and bootstrap.yml at Eureka server is same as before.

    server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    waitTimeInMsWhenSyncEmpty: 0

and bootstrap.yml:

spring:
  application:
    name: eureka
  cloud:
    config:
      uri: ${CONFIG_SERVER_URL:http://localhost:8888}

Now, the Eureka clients can still register against this Eureka Server, but when I visit: http://localhost:8761/hosts/mysevice, I got empty response.

So my question is: Can sideCar automatically find out all the sevices registered with Eureka server?

user3006967
  • 3,291
  • 10
  • 47
  • 72

1 Answers1

3

Spring Cloud Netflix Sidecar is the way to do it. The Docs are here. You can do one of two things:

  1. You can use the automated zuul proxying enabled with sidecar. You would make requests to http://localhost:<sidecarport>/<servicename>
  2. You can get a list of available hosts for a service by requesting http://localhost:<sidecarport>/hosts/<servicename>
spencergibb
  • 24,471
  • 6
  • 69
  • 75
  • Thank you @spencergibb, I have upated the post as above, can you help to answer my question there? – user3006967 Jun 08 '15 at 20:15
  • You're app looks funny, `@EnableEurekaServer` and `@EnableSidecar` shouldn't be on the same app. The sidecar should run as a process on the same machine as the nodejs app. Eureka should run somewhere else. You'll need `@EnableDiscoveryClient` with sidecar. – spencergibb Jun 08 '15 at 23:27
  • Actually, this is my question, @spencergibb, my question is how can nodeJS app find out available micro-service instance from Eureka server? Let's say, we install sideCar in nodeJS server, and the register nodeJS service with SideCar, it is good, but if NodeJS wants to know other micro-service url, I don't know how to do that. I thought maybe I can create a micro-service in spring to be able to provide api to nodeJS and inside this micro-service, it will communicate with Eureka server to find out next available instance and then send it back to NodeJS app for NodeJS app to use. Is this good? – user3006967 Jun 09 '15 at 00:25
  • Hi @spencergibb, I want to use FeignClient, but I met a problem for feign/Logger not found, I have put it in another question, can you help to have a look? Thanks. http://stackoverflow.com/questions/30737885/ – user3006967 Jun 09 '15 at 16:36
  • @user3006967 side car is just what you described in your comment – spencergibb Jun 09 '15 at 16:45