Spring Eureka 是 Spring Cloud Netflix 堆栈的一部分,是一个服务注册表,允许服务自行注册并发现其他已注册的服务。将 Node.js 应用程序与 Spring Eureka 集成涉及使用 Eureka 注册 Node.js 服务并发现使用 Eureka 注册的其他服务。
本指南将逐步指导您完成整个过程。
先决条件
- 对 Node.js 和 Java Spring Boot 有基本的了解
- 已安装 Node.js 和 npm
- 使用 Eureka 服务器设置的 Spring Boot 项目
- Eureka 服务器正在运行
设置Eureka服务器
首先,确保您的 Eureka 服务器已设置并正在运行。如果没有,请按照以下步骤操作:
1、使用 Eureka Server 创建 Spring Boot 项目:
<!-- pom.xml --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
|
2、在Spring Boot应用程序中启用Eureka服务器:
// EurekaServerApplication.java @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
|
3、配置Eureka服务器属性:
# application.properties spring.application.name=eureka-server server.port=8761
eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
|
4、运行Eureka服务器:
mvn spring-boot:run
Eureka 仪表板应该可以通过 http://localhost:8761 访问。
使用 Eureka 注册 Node.js 应用程序
步骤 1:安装依赖项
为 Eureka 客户端安装所需的 npm 软件包。
npm install eureka-js-client axios
第 2 步:配置 Eureka 客户端
创建 eureka-client.js 文件并配置 Eureka 客户端。
const Eureka = require('eureka-js-client').Eureka;
// Configure Eureka client const client = new Eureka({ instance: { app: 'nodejs-service', hostName: 'localhost', ipAddr: '127.0.0.1', port: { '$': 3000, '@enabled': 'true', }, vipAddress: 'nodejs-service', dataCenterInfo: { '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo', name: 'MyOwn', }, }, eureka: { host: 'localhost', port: 8761, servicePath: '/eureka/apps/' } });
// Start Eureka client client.start(error => { console.log('Eureka client started with error:', error); }); Step 3: Create a Simple Node.js Service Create a simple Express application in app.js. const express = require('express'); const app = express(); const port = 3000;
app.get('/', (req, res) => { res.send('Hello from Node.js service!'); });
app.listen(port, () => { console.log(<code>Node.js service listening at http://localhost:${port}</code>); });
// Import and start Eureka client require('./eureka-client');
|
第 4 步:运行 Node.js 服务
node app.js
您的 Node.js 服务现在应该已在 Eureka 服务器注册。您可以访问 http://localhost:8761 的 Eureka 面板来验证这一点。
发现已在 Eureka 注册的服务
第 1 步:安装用于 HTTP 请求的 Axios
npm install axios
第 2 步:创建服务发现函数
在 eureka-client.js 中,添加一个发现服务的函数。
const axios = require('axios');
function getServiceUrl(serviceName) { return new Promise((resolve, reject) => { client.getInstancesByAppId(serviceName, (error, instances) => { if (error) { return reject(error); } if (instances.length === 0) { return reject(new Error('No instances available')); } const instance = instances[0]; const serviceUrl = <code>http://${instance.hostName}:${instance.port.$}</code>; resolve(serviceUrl); }); }); }
module.exports = { getServiceUrl }; Step 3: Use Service Discovery in Your Application Modify app.js to use the service discovery function. const express = require('express'); const { getServiceUrl } = require('./eureka-client'); const axios = require('axios');
const app = express(); const port = 3000;
app.get('/', async (req, res) => { try { const serviceUrl = await getServiceUrl('another-service'); const response = await axios.get(<code>${serviceUrl}/api</code>); res.send(<code>Response from another service: ${response.data}</code>); } catch (error) { res.status(500).send('Error discovering service: ' + error.message); } });
app.listen(port, () => { console.log(<code>Node.js service listening at http://localhost:${port}</code>); });
// Import and start Eureka client require('./eureka-client');
|
这种设置允许您的 Node.js 服务发现在 Eureka 注册的其他服务并与之通信,从而使您的微服务架构更具可扩展性和弹性!
祝您在编程之旅中好运,欢迎在评论区分享您的想法和经验。