Comparing version 1.2.1 to 1.3.0
@@ -5,2 +5,10 @@ # Changelog | ||
## [1.3.0](https://github.com/edvardchen/grpclb/compare/v1.2.1...v1.3.0) (2019-08-18) | ||
### Features | ||
* export createClientPool ([f5343e7](https://github.com/edvardchen/grpclb/commit/f5343e7)) | ||
* support static-genereated client service ([b7788cc](https://github.com/edvardchen/grpclb/commit/b7788cc)) | ||
### [1.2.1](https://github.com/edvardchen/grpclb/compare/v1.2.0...v1.2.1) (2019-07-31) | ||
@@ -7,0 +15,0 @@ |
import register from './register'; | ||
import createGrpcProxy from './client'; | ||
export { register, createGrpcProxy }; | ||
export * from './client'; | ||
export { register }; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -8,4 +11,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.register = register_1.default; | ||
const client_1 = __importDefault(require("./client")); | ||
exports.createGrpcProxy = client_1.default; | ||
__export(require("./client")); | ||
//# sourceMappingURL=index.js.map |
@@ -16,3 +16,3 @@ import { Server } from 'grpc'; | ||
export default function register({ server, ttl, // 10 seconds, | ||
etcdKV: { key: _key, value: _value }, etcdHosts, }: RegisterOptions): Promise<() => void>; | ||
etcdKV: { key: _key, value: _value }, etcdHosts, }: RegisterOptions): Promise<() => Promise<void>>; | ||
export {}; |
{ | ||
"name": "grpclb", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "grpc load balancer for Node.js", | ||
@@ -12,3 +12,5 @@ "main": "lib/index.js", | ||
"prepublish": "npm run build", | ||
"test": "jest" | ||
"test": "jest -i", | ||
"lint": "eslint src/ --ext ts && tsc --noEmit", | ||
"pretest": "npm run lint" | ||
}, | ||
@@ -26,2 +28,18 @@ "repository": { | ||
"homepage": "https://github.com/edvardchen/grpclb#readme", | ||
"husky": { | ||
"hooks": { | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS", | ||
"pre-push": "npm t", | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.{tsx,ts}": [ | ||
"eslint" | ||
], | ||
"*.{ts,tsx,js,json}": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"devDependencies": { | ||
@@ -39,2 +57,4 @@ "@commitlint/cli": "^8.1.0", | ||
"eslint-config-prettier": "^4.3.0", | ||
"google-protobuf": "^3.9.0", | ||
"grpc": "^1.22.2", | ||
"husky": "^1.2.0", | ||
@@ -47,23 +67,10 @@ "jest": "^24.8.0", | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS", | ||
"pre-commit": "lint-staged" | ||
} | ||
"peerDependencies": { | ||
"grpc": "^1.22.2" | ||
}, | ||
"lint-staged": { | ||
"*.{tsx,ts}": [ | ||
"eslint" | ||
], | ||
"*.{ts,tsx,js,json}": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"dependencies": { | ||
"debug": "^4.1.1", | ||
"etcd3": "^0.2.13", | ||
"grpc": "^1.22.2", | ||
"lodash": "^4.17.15" | ||
} | ||
} |
@@ -8,8 +8,8 @@ # grpclb | ||
```bash | ||
npm i grpclb | ||
npm i grpclb grpc | ||
``` | ||
## Usage | ||
> `grpclb` lists `grpc` as its **`peerDependency`** not `dependency` because [here](#Notes) | ||
### Server side | ||
## Server side | ||
@@ -34,7 +34,7 @@ ```typescript | ||
### Client side | ||
## Client side | ||
- client-side load balancing with `round-robin strategy` | ||
We can't register custom service resolver util [the `c` library exposes the api](https://github.com/grpc/grpc-node/issues/719). | ||
We can't register custom service resolver util [the `C` library exposes the api](https://github.com/grpc/grpc-node/issues/719). | ||
@@ -45,4 +45,11 @@ So we can implement client-side load-balancing on the other way: [javascript Proxy](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy) | ||
import { createGrpcProxy } from 'grpclb'; | ||
import { loadSync } from '@grpc/proto-loader'; | ||
import { loadPackageDefinition } from 'grpc'; | ||
proxy = await createGrpcProxy({ | ||
// load .proto file | ||
const packageDefinition = loadSync(PROTO_PATH); | ||
// initialize into javascript object | ||
const pkgDef = loadPackageDefinition(packageDefinition); | ||
const proxy = await createGrpcProxy({ | ||
etcdHosts: hosts, // etcd hosts, or you can set as env var ETCD_HOSTS | ||
@@ -54,9 +61,59 @@ target: pkgDef.helloworld, // your gRPC object, MUST be the package definition object | ||
// Every time you access the service object, you get the new servant address. | ||
proxy.Greeter; | ||
const servant = proxy.Greeter; | ||
// The service was already initialized and | ||
// you can just call the service method to send request | ||
proxy.Greeter.sayHello({ name }, (error, response) => {}); | ||
servant.sayHello({ name }, (error, response) => {}); | ||
``` | ||
### For **static** generated grpc javascript codes | ||
The `Proxy` way is not convenient. So `grpclb` also provides another api to do the load balancing: | ||
```typescript | ||
import { createClientPool } from 'grpclb'; | ||
import { GreeterClient } from 'helloworld/static_codegen/helloworld_grpc_pb'; | ||
import { HelloRequest } from 'helloworld/static_codegen/helloworld_pb'; | ||
const pool = await createClientPool({ | ||
Client: GreeterClient, // your client service | ||
parseKV, // how to extract service name, host, port from etcd key and value | ||
etcdHosts: hosts, // etcd hosts, or you can set as env var ETCD_HOSTS | ||
}); | ||
// Every time you access the service object, you get the new servant address. | ||
const servant = pool.get(); | ||
// The service was already initialized and | ||
// you can just call the service method to send request | ||
servant.sayHello(new HelloRequest(), (error, response) => {}); | ||
``` | ||
## Notes | ||
### `grpc` as peerDependency, not dependency | ||
Image you have two copies of `grpc`, it would look like: | ||
```bash | ||
├── node_modules | ||
│ ├── grpclb | ||
│ │ └── node_modules | ||
│ │ └── grpc | ||
│ └── grpc | ||
└── src | ||
└── static_codegen | ||
├── helloworld_grpc_pb.js | ||
├── helloworld_pb.d.ts | ||
└── helloworld_pb.js | ||
``` | ||
- `require('grpc')` in src directory, no matter dynamic generated gRPC javascript code or static generated, would resolve to `node_modules/grpc` | ||
- `require('grpc')` in `grpclb` package would resolve to `node_modules/grpclb/node_modules/grpc` | ||
Then initialization would throw error | ||
`TypeError: Channel's second argument must be a ChannelCredentials`. See details for [this issue](https://github.com/grpc/grpc/issues/10786) | ||
List `grpc` as peerDependency can avoid this situation. | ||
[etcd]: https://github.com/etcd-io/etcd |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
30257
30
372
116
19
1
- Removedgrpc@^1.22.2