Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(consul): register to consul server and lookup service with balancer #949

Merged
merged 2 commits into from Apr 1, 2021

Conversation

boostbob
Copy link
Contributor

@boostbob boostbob commented Mar 25, 2021

commit message

register to consul server and deregister when shutdown, but both are optional, also lookup the services which registered on the consul server, get the service instance with injected balancer, the origin consul object is managed by the IoC container also.

dependency:

  • the hashicorp consul server(except unittest)
  • npm consul package
Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
  • coding style check with mwts check (4 unused var warning )

How to use
npm i @midwayjs/consul -S
npm i @types/consul -D
Support
  • register (optional)
  • deregister on the shutdown (optional)
  • service balancer (default random)
  • expose the origin consul object
Usage age
1. import component firstly.
import * as consul from '@midwayjs/consul'

@Configuration({
  imports: [
    consul
  ],
  importConfigs: [join(__dirname, 'config')]
})
export class ContainerConfiguration {}
2. config consul server and service definition.
consul: {
  provider: {
    // 注册本服务
    register: true,
    // 应用正常下线反注册
    deregister: true,
    // consul server 主机
    host: '192.168.0.10',
    // consul server 端口
    port: 8500,
    // 调用服务的策略(默认 random 具有随机性)
    strategy: 'random',
  },
  service: {
    address: '127.0.0.1',
    port: 7001,
    tags: ['tag1', 'tag2'],
    // others consul service definition
  }
}
3. lookup the service instance to call.
// 1. 注入的方式
@Inject('consul:balancerService')
balancerService: IConsulBalancer;
// 2. 编码的方式
const balancerService = await app.getApplicationContext().getAsync<IConsulBalancer>('consul:balancerService');

// 查询的 service 数据是 consul 返回的原生数据,因为组件并不知道应用层使用了 consul 的哪些元数据信息
// 注意下 select 在没有服务实例时会抛出 Error

// 1. 查询通过健康检查的服务
const service = await balancerService.getBalancer().select('the-service-name');
// 2. 第二个参数设定 false 可能取到不健康的服务(不推荐)
const service = await balancerService.getBalancer().select('the-service-name', false);
4. inject the origin consul object.
import * as Consul from 'consul';

// 使用 consul 官方包装的 API 接口
@Inject('consul:consul')
consul: Consul.Consul;

register to consul server and deregister when shutdown, but both are
optional, also lookup the services which registered on the consul
server, get the service instance with injected balancer, the origin
consul object is managed by the IoC container.

Dependency:
  1. the hashicorp consul server(except unittest)
  2. npm consul package

BREAKING CHANGE:
  NONE
@gitpod-io
Copy link

gitpod-io bot commented Mar 25, 2021

@codecov-io
Copy link

codecov-io commented Mar 25, 2021

Codecov Report

Merging #949 (1392734) into 2.x (8c88920) will increase coverage by 0.14%.
The diff coverage is 97.08%.

Impacted file tree graph

@@            Coverage Diff             @@
##              2.x     #949      +/-   ##
==========================================
+ Coverage   86.19%   86.33%   +0.14%     
==========================================
  Files         190      197       +7     
  Lines        7742     7845     +103     
  Branches     1498     1515      +17     
==========================================
+ Hits         6673     6773     +100     
- Misses       1018     1021       +3     
  Partials       51       51              
Impacted Files Coverage Δ
packages/consul/src/lib/balancer.ts 94.73% <94.73%> (ø)
packages/consul/src/configuration.ts 97.22% <97.22%> (ø)
packages/consul/src/config/config.default.ts 100.00% <100.00%> (ø)
packages/consul/src/controller/consul.ts 100.00% <100.00%> (ø)
packages/consul/src/index.ts 100.00% <100.00%> (ø)
packages/consul/src/lib/provider.ts 100.00% <100.00%> (ø)
packages/consul/src/service/balancer.ts 100.00% <100.00%> (ø)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c88920...1392734. Read the comment docs.

@czy88840616 czy88840616 self-assigned this Mar 26, 2021
@czy88840616
Copy link
Member

看起来差不多了,不需要再改了吧。

@boostbob
Copy link
Contributor Author

后面想支持多服务器注册,因为生产环境一般 service 会同时注册到多个 client 类型的 consul 服务器上,打算放在 1.0.1 版本,你看合适吗?

@czy88840616
Copy link
Member

czy88840616 commented Mar 27, 2021

额,放在 midway 主库里的版本就跟着主库升级啦 😸,现在是2.9.x了,不需要关心版本了。。发到 2.10.0 吧,毕竟是 feature。

@czy88840616
Copy link
Member

急用么?急的话我可以放到 2.9.x 里。

@boostbob
Copy link
Contributor Author

不急用,我现在直接放项目里的,深入学习 core 中。

@czy88840616 czy88840616 merged commit d5f9916 into midwayjs:2.x Apr 1, 2021
@boostbob boostbob deleted the feature/consul branch July 19, 2021 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants