@adastradev/serverless-discovery-sdk 中文文档教程
serverless-discovery-sdk-js
AWS 无服务器发现 SDK 与发现微服务交互,以发现为无服务器架构编写的微服务的端点。 这类似于集群服务,例如 Consul 或 ZooKeeper,但没有必须监视在线状态的实例或节点的概念。 该库旨在支持在服务器端(用于服务到服务查找)和浏览器/客户端的使用。
该项目包含发现服务的 Typescript/Javascript 绑定; 其他绑定可以在 GitHub 上的 AdAstraDev 组织中找到
Installation
npm install @adastradev/serverless-discovery-sdk
Usage
Service/Cloud Dependencies
Semver 版本控制由发现服务 1.1.x 支持。 在 lookupService
调用中传递 semver 兼容值以接收最新的兼容匹配版本。 服务及其所需版本也可以在 package.json
的 cloudDependencies
字段中指定。
{
"cloudDependencies": {
"service1": "1.x",
"service2": "^1.2.8-testbranch", // A pre-release version for development purposes
"service3": "3.x.x"
}
}
Version Postfix values
在某些测试环境中,修改查找版本以避免与生产环境发生冲突可能很有用。 如果 VERSION_POSTFIX
环境变量在运行时,它总是将此附加到查找调用的版本。
如果您要查找高度耦合或没有很好隔离的服务,并将它们用于系统测试,您应该:
- Set the
VERSION_POSTFIX
environment variable set to-staging
- Pass the environment variable through to the runtime where lookups are happening (lambda, docker, etc.)
如果查找 serviceA,版本 1.1.0,它只会与
1.1.0-staging
对话。 当环境变量存在时,所有查找调用都将遵循类似的模式。
TL;DR:如果您正在查找未很好隔离的服务,并且依赖暂存环境来避免对生产数据库/资源进行操作,请在暂存部署/测试中将以下内容添加到您的管道中步。
bitbucket-pipelines.yml:
- export VERSION_POSTFIX='-staging'
# Deployment steps follow...
serverless.yml
provider:
environment:
VERSION_POSTFIX: ${env:VERSION_POSTFIX, ''}
Code Example
我建议设置一个实用函数来处理 SDK 的构建和查找调用 - 请参见下面的示例。
import { DiscoverySdk } from '@adastradev/serverless-discovery-sdk';
export default async function lookup(serviceName) {
const sdk = new DiscoverySdk(
process.env.DISCOVERY_SERVICE_URL,
process.env.DISCOVERY_SERVICE_REGION,
// Non-versioned services will default to lookup via this stage
process.env.DEFAULT_STAGE,
undefined,
// Create map of cloudDependencies from package.json
new Map(Object.entries(require('../path/to/package.json')['cloudDependencies'])),
);
const endpoints = await sdk.lookupService(
serviceName
);
return endpoints[0];
}
serverless-discovery-sdk-js
The last serverless micro-service you'll ever wonder how to find
The AWS Serverless Discovery SDK interacts with a discovery microservice to discover endpoints for micro-services written for a serverless architecture. This is similar to clustered services such as Consul or ZooKeeper, but without the concept of instances or nodes that must be monitored for online state. This library is designed to support use both on the server side (for service-to-service lookups) and on the browser/client side.
This project contains the Typescript/Javascript bindings for the discovery service; Other bindings can be found in the AdAstraDev organization on GitHub
Installation
npm install @adastradev/serverless-discovery-sdk
Usage
Service/Cloud Dependencies
Semver versioning is supported by the discovery service 1.1.x. Pass a semver compatible value in the lookupService
call to receive the newest compatible matching version. Services and their desired versions can also be specified in the cloudDependencies
field of package.json
.
{
"cloudDependencies": {
"service1": "1.x",
"service2": "^1.2.8-testbranch", // A pre-release version for development purposes
"service3": "3.x.x"
}
}
Version Postfix values
In some testing environments, it can be useful to modify the lookup version to avoid collision with a production environment. If the VERSION_POSTFIX
environment variable at runtime, it will always append this to the version of a lookup call.
If you are looking up services which are highly coupled or are not well isolated, and using them for system tests, you should:
- Set the
VERSION_POSTFIX
environment variable set to-staging
- Pass the environment variable through to the runtime where lookups are happening (lambda, docker, etc.)
If there is a lookup for serviceA, version 1.1.0
, it will instead only talk to 1.1.0-staging
. All lookup calls will follow a similar pattern while the environment variable is present.
TL;DR: If you are looking up services which are not well isolated, and rely on a staging environment to avoid operations on prod databases/resources, add the following to your pipeline in a staging deployment/testing step.
bitbucket-pipelines.yml:
- export VERSION_POSTFIX='-staging'
# Deployment steps follow...
serverless.yml
provider:
environment:
VERSION_POSTFIX: ${env:VERSION_POSTFIX, ''}
Code Example
I recommend setting up a utility function to handle construction of the SDK, and the lookup call - see below example.
import { DiscoverySdk } from '@adastradev/serverless-discovery-sdk';
export default async function lookup(serviceName) {
const sdk = new DiscoverySdk(
process.env.DISCOVERY_SERVICE_URL,
process.env.DISCOVERY_SERVICE_REGION,
// Non-versioned services will default to lookup via this stage
process.env.DEFAULT_STAGE,
undefined,
// Create map of cloudDependencies from package.json
new Map(Object.entries(require('../path/to/package.json')['cloudDependencies'])),
);
const endpoints = await sdk.lookupService(
serviceName
);
return endpoints[0];
}
你可能也喜欢
- 2d-polygon-self-intersections 中文文档教程
- 3f335bcfd286030b4771414e30b0582c84dc12000efa5ad019765ec55339860d 中文文档教程
- 404studios-api 中文文档教程
- 4icloud-engine-testsss 中文文档教程
- @01js/cli 中文文档教程
- @0xgabi/1hive-ui 中文文档教程
- @14four/prettier-config 中文文档教程
- @3m/mmm 中文文档教程
- @40three/ngx-autofocus-directive 中文文档教程
- @4am-design/elements 中文文档教程