帮酷LOGO

JS implementation of a client for Eureka (https

JS实现尤里卡客户端( https

将光标移到/点击文章中的句子上,可以查看译文。 显示原文      显示译文      双语对照

  • 显示内容
文章标签:IMP  Https  HTTP  Implementation  
JS implementation of a client for Eureka (https://github.com/Netflix/eureka), the Netflix OSS service registry.



  • Source Code:eureka-js-client
  • WebSite:http://www.github.com/jquatier/eureka-js-client
  • eureka-js-client Documents
  • eureka-js-client Download
  • eureka-js-client

    npm versionBuild StatusCoverage StatusDependency StatusbitHound Overall Score

    A JavaScript implementation of a client for Eureka ( https://github.com/Netflix/eureka ), the Netflix OSS service registry.

    Usage

    First, install the module into your node project :

    npm install eureka-js-client --save

    Add Eureka client to a Node application.

    The Eureka module exports a JavaScript function that can be constructed.

    importEurekafrom'eureka-js-client';// Or, if you're not using a transpiler:constEureka=require('eureka-js-client').Eureka;// example configurationconstclient=newEureka({
     // application instance information instance: {
     app:'jqservice',
     hostName:'localhost',
     ipAddr:'127.0.0.1',
     port:8080,
     vipAddress:'jq.test.something.com',
     dataCenterInfo: {
     name:'MyOwn',
     },
     },
     eureka: {
     // eureka server host/port host:'192.168.99.100',
     port:32768,
     },
    });

    The Eureka client searches for the YAML file eureka-client.yml in the current working directory.it further searches for environment specific overrides in the environment specific YAML files (e.g.eureka-client-test.yml ). the environment is typically development or production, and is determined by the NODE_ENV environment variable.the options passed to the constructor overwrite any values that are set in configuration files.

    You can configure a custom directory to load the configuration files from by specifying a cwd option in the object passed to the Eureka constructor.

    constclient=newEureka({
     cwd:`${__dirname}/config`,
    });

    If you wish, you can also overwrite the name of the file that is loaded with the filename property.you can mix the cwd and filename options.

    constclient=newEureka({
     filename:'eureka',
     cwd:`${__dirname}/config`,
    });

    Register with Eureka & start application heartbeats

    client.start();

    De-register with Eureka & stop application heartbeats

    client.stop();

    get Instances by App ID

    constinstances=client.getInstancesByAppId('YOURSERVICE');

    get Instances by Vip Address

    constinstances=client.getInstancesByVipAddress('YOURSERVICEVIP');

    Providing Custom Request Middleware

    The client exposes the ability to modify the outgoing request options object prior to a eureka call.this is useful when adding authentication methods such as OAuth, or other custom headers.this will be called on every eureka request, so it highly suggested that any long-lived external calls made in the middleware are cached or memoized.if the middleware returns anything other than an object, the eureka request will immediately fail and perform a retry if configured.

    // example using middleware to set-up HTTP authenticationconstclient=newEureka({
     requestMiddleware: (requestOpts, done) => {
     requestOpts.auth= {
     user:'username',
     password:'somepassword' };
     done(requestOpts);
     }
    });

    Configuring for AWS environments

    For AWS environments ( dataCenterInfo.name == 'Amazon' ) the client has built-in logic to request the AWS metadata that the Eureka server requires.see Eureka REST schema for more information.

    // example configuration for AWSconstclient=newEureka({
     // application instance information instance: {
     app:'jqservice',
     port:8080,
     vipAddress:'jq.test.something.com',
     statusPageUrl:'http://__HOST__:8080/info',
     healthCheckUrl:'http://__HOST__:8077/healthcheck',
     homePageUrl:'http://__HOST__:8080/',
     dataCenterInfo: {
     name:'Amazon',
     },
     },
     eureka: {
     // eureka server host/port/EC2 region host:'eureka.test.mydomain.com',
     port:80,
     },
    });

    Notes :

    • Under this configuration, the instance hostName and ipAddr will be set to the public host and public IP that the AWS metadata provides.you can set eureka.useLocalMetadata to true to use the private host and private IP address instead.
    • If you want to register using the IP address as the hostname, set eureka.preferIpAddress to true.this may be used in combination with eureka.useLocalMetadata for selecting the private or public IP.
    • For status and healthcheck URLs, you may use the replacement key of __HOST__ to use the host from the metadata.
    • Metadata fetching can be disabled by setting config.eureka.fetchMetadata to false if you want to provide your own metadata in AWS environments.

    looking up Eureka Servers using DNS

    If your have multiple availability zones and your DNS entries set up according to the Wiki article Deploying Eureka Servers in EC2, you'll want to set config.eureka.useDns to true and set config.eureka.ec2Region to the current region (usually this can be pulled into your application via an environment variable, or passed in directly at startup).

    This will cause the client to perform a DNS lookup using config.eureka.host and config.eureka.ec2Region.the naming convention for the DNS TXT records required for this to function is also described in the Wiki article above.this feature will also work in non-EC2 environments as long as the DNS records conform to the same convention.the results of the DNS resolution are cached in memory and refreshed every 5 minutes by default (setconfig.eureka.clusterRefreshIntervalto override).

    Zone Affinity

    By default, the client will first try to connect to the Eureka server located in the same availability-zone as it's currently in.if availability-zone is not set in the instance metadata, a random server will be chosen.this also applies when statically configuring the cluster (mapped by zone, see below).to disable this feature, set config.eureka.preferSameZone to false, and a random server will be chosen.

    Statically configuring Eureka server list

    While the recommended approach for resolving the Eureka cluster is using DNS (see above), you can also statically configure the list of Eureka servers by zone or just using a simple default list.Make sure to provide the full protocol, host, port, and path to the Eureka REST service (usually /apps/ ) when using this approach.

    Static cluster configuration (map by zone )
    // example configuration for AWS (static map of Eureka cluster by availability-zone)constclient=newEureka({
     instance: {
     ...// application instance information },
     eureka: {
     availabilityZones: {
     'us-east-1': ['us-east-1c', 'us-east-1d', 'us-east-1e']
     },
     serviceUrls: {
     'us-east-1c': [
     'http://ec2-fake-552-627-568-165.compute-1.amazonaws.com:7001/eureka/v2/apps/', 'http://ec2-fake-368-101-182-134.compute-1.amazonaws.com:7001/eureka/v2/apps/' ],
     'us-east-1d': [...],
     'us-east-1e': [...]
     }
     },
    });
    Static cluster configuration (list )
    // example configuration (static list of Eureka cluster servers)constclient=newEureka({
     instance: {
     ...// application instance information },
     eureka: {
     serviceUrls: {
     default: [
     'http://ec2-fake-552-627-568-165.compute-1.amazonaws.com:7001/eureka/v2/apps/', 'http://ec2-fake-368-101-182-134.compute-1.amazonaws.com:7001/eureka/v2/apps/' ]
     }
     },
    });

    Advanced Configuration Options

    option default value description
    requestMiddlewarenoopCustom middleware function to modify the outgoing request to eureka
    loggerconsole logginglogger implementation for the client to use
    shouldUseDeltafalseExperimental mode to fetch deltas from eureka instead of full registry on update
    eureka.maxRetries3Number of times to retry all requests to eureka
    eureka.requestRetryDelay500milliseconds to wait between retries.this will be multiplied by the # of failed retries.
    eureka.heartbeatInterval30000milliseconds to wait between heartbeats
    eureka.registryFetchInterval30000milliseconds to wait between registry fetches
    eureka.registerWithEurekatrueenable/disable Eureka registration
    eureka.fetchRegistrytrueenable/disable registry fetching
    eureka.filterUpInstancestrueenable/disable filtering of instances with status === UP
    eureka.servicePath/eureka/v2/apps/path to eureka REST service
    eureka.sslfalseenable SSL communication with Eureka server
    eureka.useDnsfalselook up Eureka server using DNS, see looking up Eureka Servers in AWS using DNS
    eureka.preferSameZonetrueenable/disable zone affinity when locating a Eureka server
    eureka.clusterRefreshInterval300000milliseconds to wait between refreshing cluster hosts (DNS resolution only )
    eureka.fetchMetadatatruefetch AWS metadata when in AWS environment, see Configuring for AWS environments
    eureka.useLocalMetadatafalseuse local IP and local hostname from metadata when in an AWS environment.
    eureka.preferIpAddressfalseuse IP address (local or public) as the hostname for registration when in an AWS environment.

    Events

    Eureka client is an instance of EventEmitter and provides the following events for consumption :

    event data provided description
    startedN/AFired when eureka client is fully registered and all registries have been updated.
    registeredN/AFired when the eureka client is registered with eureka.
    deregisteredN/AFired when the eureka client is deregistered with eureka.
    heartbeatN/AFired when the eureka client has successfully renewed it's lease with eureka.
    registryUpdatedN/AFired when the eureka client has successfully update it's registries.

    Debugging

    The library uses request for all service calls, and debugging can be turned on by passing NODE_DEBUG=request when you start node.this allows you you double-check the URL being called as well as other request properties.

    NODE_DEBUG=request node example.js

    You can also turn on debugging within the library by setting the log level to debug :

    client.logger.level('debug');

    known Issues

    400 Bad Request Errors from Eureka Server

    Later versions of Eureka require a slightly different JSON POST body on registration.if you are seeing 400 errors on registration it's probably an issue with your configuration and it could be the formatting differences below.the history behind this is unclear and there's a discussion here.the main differences are :

    • port is now an object with 2 required fields $ and @enabled.
    • dataCenterInfo has an @class property.

    See below for an example :

    constclient=newEureka({
     // application instance information instance: {
     app:'jqservice',
     hostName:'localhost',
     ipAddr:'127.0.0.1',
     port: {
     '$':8080,
     '@enabled':true,
     },
     vipAddress:'jq.test.something.com',
     dataCenterInfo: {
     '@class':'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
     name:'MyOwn',
     },
     },
     eureka: {
     // eureka server host/port host:'192.168.99.100',
     port:32768,
     },
    });

    If you are planning on connecting to a eureka service in AWS you will need to add the corresponding dataCenterInfo information :

     dataCenterInfo: {
     '@class':'com.netflix.appinfo.AmazonInfo',
     name:'Amazon',
     }

    404 not Found Errors from Eureka Server

    This probably means that the Eureka REST service is located on a different path in your environment.the default ishttp://<EUREKA_HOST>/eureka/v2/apps, but depending on your setup you may need to set eureka.servicePath in your configuration to another path.the REST service could be hung under /eureka/apps/ or possibly /apps/.

    Usage with Spring Cloud

    If you are using Spring Cloud you'll likely need the following settings :

    • Set eureka.servicePath in your config to /eureka/apps/.
    • Use the newer style of the configuration here or Spring Cloud Eureka will throw a 500 error.
    • Set statusPageUrl to a valid URL for your application, Spring Cloud seems to require this when the instance information is parsed.
    • Put single quotes around boolean @enabled.Unfortunately, a 500 error regarding parsing seems to occur without that.

    Below is an example configuration that should work with Spring Cloud Eureka server :

    constclient=newEureka({
     instance: {
     app:'jqservice',
     hostName:'localhost',
     ipAddr:'127.0.0.1',
     statusPageUrl:'http://localhost:8080/info',
     port: {
     '$':8080,
     '@enabled':'true',
     },
     vipAddress:'jq.test.something.com',
     dataCenterInfo: {
     '@class':'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
     name:'MyOwn',
     },
     },
     eureka: {
     host:'192.168.99.100',
     port:32768,
     servicePath:'/eureka/apps/' },
    });

    Tests

    The test for the module are written using mocha and chai.to run the unit tests, you can use the gulp test task :

    gulp test

    If you wish to have the tests watch the src/ and test/ directories for changes, you can use the test:watch gulp task :

    gulp test:watch


    文章标签:IMP  Implementation  HTTP  Https  

    Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语