Skip to content

Commit

Permalink
feat: add sofa-rpc showcase
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer committed Jun 12, 2018
1 parent 5605916 commit a90142a
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 0 deletions.
30 changes: 30 additions & 0 deletions sofa-rpc/.autod.conf.js
@@ -0,0 +1,30 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'egg-ci',
'egg-bin',
'egg-mock',
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
'webstorm-disable-index',
],
exclude: [
'./test/fixtures',
'./dist',
],
};

2 changes: 2 additions & 0 deletions sofa-rpc/.eslintignore
@@ -0,0 +1,2 @@
coverage
app/proxy/*
3 changes: 3 additions & 0 deletions sofa-rpc/.eslintrc
@@ -0,0 +1,3 @@
{
"extends": "eslint-config-egg"
}
12 changes: 12 additions & 0 deletions sofa-rpc/.gitignore
@@ -0,0 +1,12 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
.DS_Store
*.sw*
*.un~
10 changes: 10 additions & 0 deletions sofa-rpc/.travis.yml
@@ -0,0 +1,10 @@
sudo: false
language: node_js
node_js:
- '8'
install:
- npm i npminstall && npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
33 changes: 33 additions & 0 deletions sofa-rpc/README.md
@@ -0,0 +1,33 @@
# sofa-rpc

sofa rpc demo

## QuickStart

<!-- add docs here for user -->

see [egg-sofa-rpc tutorial][https://github.com/eggjs/egg-sofa-rpc/wiki/Eggjs-%E5%92%8C-SOFA-%E7%9A%84%E8%B7%A8%E8%AF%AD%E8%A8%80%E4%BA%92%E8%B0%83] for more detail.

### Development

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

### Deploy

```bash
$ npm start
$ npm stop
```

### npm scripts

- Use `npm run lint` to check code style.
- Use `npm test` to run unit test.
- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.


[egg]: https://eggjs.org
39 changes: 39 additions & 0 deletions sofa-rpc/README.zh-CN.md
@@ -0,0 +1,39 @@
# sofa-rpc

sofa rpc demo

## 快速入门

<!-- 在此次添加使用文档 -->

如需进一步了解,参见 [Eggjs 和 SOFA 的跨语言互调][https://github.com/eggjs/egg-sofa-rpc/wiki/Eggjs-%E5%92%8C-SOFA-%E7%9A%84%E8%B7%A8%E8%AF%AD%E8%A8%80%E4%BA%92%E8%B0%83]

### 本地开发

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

### 部署

```bash
$ npm start
$ npm stop
```

### 单元测试

- [egg-bin] 内置了 [mocha], [thunk-mocha], [power-assert], [istanbul] 等框架,让你可以专注于写单元测试,无需理会配套工具。
- 断言库非常推荐使用 [power-assert]
- 具体参见 [egg 文档 - 单元测试](https://eggjs.org/zh-cn/core/unittest)

### 内置指令

- 使用 `npm run lint` 来做代码风格检查。
- 使用 `npm test` 来执行单元测试。
- 使用 `npm run autod` 来自动检测依赖更新,详细参见 [autod](https://www.npmjs.com/package/autod)


[egg]: https://eggjs.org
16 changes: 16 additions & 0 deletions sofa-rpc/app/controller/home.js
@@ -0,0 +1,16 @@
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
async index() {
const { ctx } = this;
const res = await ctx.proxy.protoService.echoObj({
name: 'gxcsoccer',
group: 'A',
});
ctx.body = res;
}
}

module.exports = HomeController;
9 changes: 9 additions & 0 deletions sofa-rpc/app/router.js
@@ -0,0 +1,9 @@
'use strict';

/**
* @param {Egg.Application} app - egg application
*/
module.exports = app => {
const { router, controller } = app;
router.get('/', controller.home.index);
};
8 changes: 8 additions & 0 deletions sofa-rpc/app/rpc/ProtoService.js
@@ -0,0 +1,8 @@
'use strict';

exports.echoObj = async function(req) {
return {
code: 200,
message: 'hello ' + req.name + ', you are in ' + req.group,
};
};
14 changes: 14 additions & 0 deletions sofa-rpc/appveyor.yml
@@ -0,0 +1,14 @@
environment:
matrix:
- nodejs_version: '8'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run test

build: off
22 changes: 22 additions & 0 deletions sofa-rpc/config/config.default.js
@@ -0,0 +1,22 @@
'use strict';

module.exports = appInfo => {
const config = exports = {};

// use for cookie sign key, should change to your own and keep security
config.keys = appInfo.name + '_1528713428903_3456';

// add your config here
config.middleware = [];

config.sofaRpc = {
registry: {
address: '127.0.0.1:2181', // 根据实际情况配置
},
server: {
namespace: 'com.alipay.sofa.rpc.protobuf',
},
};

return config;
};
6 changes: 6 additions & 0 deletions sofa-rpc/config/plugin.js
@@ -0,0 +1,6 @@
'use strict';

exports.sofaRpc = {
enable: true,
package: 'egg-sofa-rpc',
};
10 changes: 10 additions & 0 deletions sofa-rpc/config/proxy.js
@@ -0,0 +1,10 @@
'use strict';

module.exports = {
services: [{
appName: 'sofarpc',
api: {
ProtoService: 'com.alipay.sofa.rpc.protobuf.ProtoService',
},
}],
};
47 changes: 47 additions & 0 deletions sofa-rpc/package.json
@@ -0,0 +1,47 @@
{
"name": "sofa-rpc",
"version": "1.0.0",
"description": "sofa rpc demo",
"private": true,
"dependencies": {
"egg": "^2.2.1",
"egg-scripts": "^2.5.0",
"egg-sofa-rpc": "^1.0.0"
},
"devDependencies": {
"autod": "^3.0.1",
"autod-egg": "^1.0.0",
"egg-bin": "^4.3.5",
"egg-ci": "^1.8.0",
"egg-mock": "^3.14.0",
"egg-rpc-generator": "^1.0.0",
"eslint": "^4.11.0",
"eslint-config-egg": "^6.0.0",
"webstorm-disable-index": "^1.2.0"
},
"engines": {
"node": ">=8.9.0"
},
"scripts": {
"start": "egg-scripts start --daemon --title=egg-server-sofa-rpc",
"stop": "egg-scripts stop --title=egg-server-sofa-rpc",
"dev": "egg-bin dev",
"debug": "egg-bin debug",
"test": "npm run rpc && npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
"cov": "npm run rpc && egg-bin cov",
"lint": "eslint .",
"ci": "npm run lint && npm run cov",
"autod": "autod",
"rpc": "egg-rpc-generator"
},
"ci": {
"version": "8"
},
"repository": {
"type": "git",
"url": ""
},
"author": "gxcsoccer",
"license": "MIT"
}
24 changes: 24 additions & 0 deletions sofa-rpc/proto/ProtoService.proto
@@ -0,0 +1,24 @@
syntax = "proto3";

package com.alipay.sofa.rpc.protobuf;
option java_multiple_files = true; // 可选
option java_outer_classname = "ProtoServiceModels"; // 可选

service ProtoService {
rpc echoObj (EchoRequest) returns (EchoResponse) {}
}

message EchoRequest {
string name = 1;
Group group = 2;
}

message EchoResponse {
int32 code = 1;
string message = 2;
}

enum Group {
A = 0;
B = 1;
}
21 changes: 21 additions & 0 deletions sofa-rpc/test/app/controller/home.test.js
@@ -0,0 +1,21 @@
'use strict';

const { app, assert } = require('egg-mock/bootstrap');

describe('test/app/controller/home.test.js', () => {

it('should assert', function* () {
const pkg = require('../../../package.json');
assert(app.config.keys.startsWith(pkg.name));

// const ctx = app.mockContext({});
// yield ctx.service.xx();
});

it('should GET /', () => {
return app.httpRequest()
.get('/')
.expect('{"code":200,"message":"hello gxcsoccer, you are in 0"}')
.expect(200);
});
});
27 changes: 27 additions & 0 deletions sofa-rpc/test/server.test.js
@@ -0,0 +1,27 @@
'use strict';

const mm = require('egg-mock');

describe('test/server.test.js', () => {
let app;
before(async function() {
app = mm.app();
await app.ready();
});
after(async function() {
await app.close();
});

it('should invoke echoObj ok', done => {
app.rpcRequest('ProtoService')
.invoke('echoObj')
.send([{
name: 'gxcsoccer',
group: 'B',
}])
.expect({
code: 200,
message: 'hello gxcsoccer, you are in 1',
}, done);
});
});

0 comments on commit a90142a

Please sign in to comment.