eureka-node-client是对eureka-js-client做的封装,是对Eureka 客户端的JS实现,安装 eureka-node-client:npm install eureka-node-client --save。
地址:https://github.com/arthas001/eureka-node-client
eureka-node-client集成项目代码案例
const os = require('os');
let hostname = os.hostname();
console.log('hostname=' + hostname)
let port = 3000;
const Eureka = require("eureka-node-client");
let eureka_address = process.env.EUREKA_ADDRESS || 'localhost';
let eureka_port = process.env.EUREKA_PORT || '7001';
let eureka_username = process.env.EUREKA_USERNAME || 'admin';
let eureka_password = process.env.EUREKA_PWD || 'admin';
const eureka_client = new Eureka({
eureka: {
host: `${eureka_username}:${eureka_password}@${eureka_address}`,
port: eureka_port,
servicePath: "/eureka/apps/"
},
instance: {
app: 'project-nodejs',
port: { '#39;: port, '@enabled': 'true' },
homePageUrl: `http://${hostname}:${port}/`,
healthCheckUrl: `http://${hostname}:${port}/health`,
statusPageUrl: `http://${hostname}:${port}/static/index.html`,
metadata: {
zone: 'primary',
env: process.env.NODE_ENV,
version: '1.0'
}
}
});
/**
* 设置日志级别
*/
eureka_client.logger.level('debug');
//******************** 测试监听 ********************//
let updatedListener = function (apps) {
console.log("更新:" + JSON.stringify(apps));
}
eureka_client.onUpdated(updatedListener);
eureka_client.start(function (error) {
console.log(error || '启动成功!');
});eureka-js-client
A JavaScript implementation of a client for Eureka (https://github.com/Netflix/eureka), the Netflix OSS service registry.
地址:https://www.npmjs.com/package/eureka-js-client
代码案例1:
const Eureka = require('eureka-js-client').Eureka;
const client = new Eureka({
instance: {
app: 'nodejs-demo',
hostName: 'localhost',
ipAddr: '127.0.0.1',
port: {
'#39;: 3000,
'@enabled': true
},
vipAddress: 'ftrybe',
statusPageUrl: 'http://localhost:3000/info', //404
dataCenterInfo: {
'@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo', //缺少会导致404错误
name: 'MyOwn', // 'Netflix' | 'Amazon' | 'MyOwn'
}
},
eureka: {
// eureka server host / port
host: 'localhost',
port: 7001,
servicePath: '/eureka/apps' //缺少会导致404错误
},
});
client.start(function (error) {
console.log(error || 'complete');
});代码案例2:
const Eureka = require("eureka-js-client").Eureka;
const client = new Eureka({
filename: 'eureka-client',
cwd: __dirname
});
client.logger.level('debug');
client.start(function (error) {
console.log(error || 'complete');
});
eureka-client.yml
eureka:
host: 'localhost'
port: 7001
servicePath: '/eureka/apps/'
heartbeatInterval: 30000
registryFetchInterval: 30000
instance:
app: 'node-client'
hostName: 'localhost'
ipAddr: 'localhost'
statusPageUrl: 'http://localhost:3000'
port:
'#39;: 3000
'@enabled': 'true'
vipAddress: 'razer-node'
dataCenterInfo:
'@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo'
name: 'MyOwn'Eureka
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。
地址: Java,SpringCloud,微服务注册中心Eureka
代码案例
pom.xml
eureka-7001
1.0.0
Eureka注册中心-7001端口
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
ch.qos.logback
logback-core
1.1.3
ch.qos.logback
logback-access
1.1.3
ch.qos.logback
logback-classic
1.1.3
src/main/java
**/*.*
src/main/resources
**/*.*
org.springframework.boot
spring-boot-maven-plugin
2.3.10.RELEASE
com.what21.eureka.EurekaApplication7001
repackage
org.apache.maven.plugins
maven-compiler-plugin
3.1
${java.version}
${java.version}
${project.build.sourceEncoding}
true
EurekaApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication7001.class);
}
}application.yml
server:
port: 7001
eureka:
instance:
# Eureka服务实例名
hostname: EUREKA7001
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false表示自己端就是注册中心,职责是维护服务实例,不需要去检索服务
fetch-registry: false
service-url:
# 设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://eureka7002.com:7002/eureka/
# 指向自己(单机)
defaultZone: http://localhost:7001/eureka/
server:
# 开启自我保护模式(开启状态下服务停掉eureka不会立即清除掉宕掉的服务)
enable-self-preservation: true
# 清理无效节点,默认60*1000毫秒,即60秒
eviction-interval-timer-in-ms: 30000logback.xml
<?xml version="1.0" encoding="UTF-8"?>
%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){red}---%magenta([%thread])---%clr(%-5level)---%blue([%c])---%green([%L])---[traceId:%X{traceId}]---[%msg]%n
${LOG_HOME}/eureka.log.%d{yyyy-MM-dd}.log
30
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
10MB
| 留言与评论(共有 0 条评论) “” |