You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// userService.jsconstSeneca=require('seneca');constuserService=Seneca();letusers=[];userService.add({role: 'user',cmd: 'register'},(msg,reply)=>{const{ username, password }=msg;if(!username||!password){returnreply(null,{error: 'Username and password are required'});}constuser={id: users.length+1, username, password };users.push(user);reply(null,user);});userService.add({role: 'user',cmd: 'login'},(msg,reply)=>{const{ username, password }=msg;constuser=users.find(u=>u.username===username&&u.password===password);if(!user){returnreply(null,{error: 'Invalid username or password'});}reply(null,{success: true,userId: user.id});});userService.listen({type: 'http',port: 3001,pin: 'role:user'});
商品服务
// productService.jsconstSeneca=require('seneca');constproductService=Seneca();letproducts=[];productService.add({role: 'product',cmd: 'create'},(msg,reply)=>{const{ name }=msg;if(!name){returnreply(null,{error: 'Product name is required'});}constproduct={id: products.length+1, name };products.push(product);reply(null,product);});productService.listen({type: 'http',port: 3002,pin: 'role:product'});
订单服务
// orderService.jsconstSeneca=require('seneca');constorderService=Seneca();letorders=[];orderService.add({role: 'order',cmd: 'create'},(msg,reply)=>{const{ userId, productId }=msg;if(!userId||!productId){returnreply(null,{error: 'User ID and Product ID are required'});}orderService.act({role: 'user',cmd: 'get',id: userId},(err,user)=>{if(err||!user){returnreply(null,{error: 'Invalid User ID'});}orderService.act({role: 'product',cmd: 'get',id: productId},(err,product)=>{if(err||!product){returnreply(null,{error: 'Invalid Product ID'});}constorder={id: orders.length+1, userId, productId };orders.push(order);reply(null,order);});});});orderService.listen({type: 'http',port: 3003,pin: 'role:order'});
库存服务
// inventoryService.jsconstSeneca=require('seneca');constinventoryService=Seneca();letinventory=[];inventoryService.add({role: 'inventory',cmd: 'update'},(msg,reply)=>{const{ productId, quantity }=msg;if(!productId||quantity===undefined){returnreply(null,{error: 'Product ID and Quantity are required'});}constitem=inventory.find(i=>i.productId===productId);if(item){item.quantity=quantity;}else{inventory.push({ productId, quantity });}reply(null,{success: true});});inventoryService.listen({type: 'http',port: 3004,pin: 'role:inventory'});
网关服务
// gatewayService.jsconstSeneca=require('seneca');constexpress=require('express');constbodyParser=require('body-parser');constapp=express();app.use(bodyParser.json());constgatewayService=Seneca();gatewayService.client({type: 'http',port: 3001,pin: 'role:user'});gatewayService.client({type: 'http',port: 3002,pin: 'role:product'});gatewayService.client({type: 'http',port: 3003,pin: 'role:order'});gatewayService.client({type: 'http',port: 3004,pin: 'role:inventory'});app.post('/api/:role/:cmd',(req,res)=>{const{ role, cmd }=req.params;constmsg={ role, cmd, ...req.body};gatewayService.act(msg,(err,result)=>{if(err){returnres.status(500).json({error: err.message});}res.json(result);});});app.listen(3000,()=>{console.log('Gateway service listening on port 3000');});
示例代码
以下是使用 Seneca 构建微服务架构的示例代码,包括用户服务、商品服务、订单服务、库存服务和网关服务。
用户服务
商品服务
订单服务
库存服务
网关服务
请求示例
以下是如何通过 HTTP 请求调用网关服务的示例。
URL: http://localhost:3000/api/user/register
URL: http://localhost:3000/api/user/login
说明
网关服务使用 Express 框架接收 HTTP 请求,并通过动态路由和模式匹配将请求转发到相应的微服务。
请求通过 HTTP POST 方法发送到网关服务的 /api/:role/:cmd 路径,网关服务接收到请求后,通过 Seneca 的 act 方法将请求转发到相应的微服务,并返回结果。
通过这种方式,可以简化网关服务的维护,并且更容易扩展。每次新增接口时,只需要在相应的微服务中添加处理器,而不需要修改网关服务。
The text was updated successfully, but these errors were encountered: