Skip to content
Permalink
Browse files
add:demo 与翻译师相结合
  • Loading branch information
creasy2010 committed Apr 19, 2018
1 parent d22e707 commit e70eb6e6056f9bfa386d20dc9434afc188c03d39
Show file tree
Hide file tree
Showing 19 changed files with 362 additions and 185 deletions.
@@ -0,0 +1,7 @@
{
"output": "./packages/dubbo/src/__tests__/providers",
"dubboVersion": "2.0.0",
"entry": "com.alibaba.dubbo.demo",
"entryJarPath": "./java/dubbo-demo/dubbo-demo-api/target/dubbo-demo-api-2.5.7.jar",
"libDirPath": "./java/dubbo-demo/dubbo-demo-api/target/dependency/"
}
@@ -1,6 +1,6 @@
package com.alibaba.dubbo.demo;

public interface BasicTypeService {
public interface BasicTypeProvider {
TypeRequest testBasicType(TypeRequest request);

}
@@ -1,28 +1,28 @@
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.demo;

public interface DemoService {

String sayHello(String name);

String echo() ;

void test();

UserResponse getUserInfo(UserRequest request);

/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.demo;

public interface DemoProvider {

String sayHello(String name);

String echo() ;

void test();

UserResponse getUserInfo(UserRequest request);

}
@@ -1,5 +1,5 @@
package com.alibaba.dubbo.demo;

public interface ErrorService {
public interface ErrorProvider {
void errorTest() throws CustomizeException;
}
@@ -13,7 +13,7 @@ public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();

// DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理
// DemoProvider demoService = (DemoProvider) context.getBean("demoService"); // 获取远程服务代理
//// String hello = demoService.sayHello("world"); // 执行远程方法
//
//// System.out.println(hello); // 显示调用结果
@@ -30,7 +30,7 @@ public static void main(String[] args) {
// UserResponse userInfo = demoService.getUserInfo(request);
// System.out.println(userInfo);

BasicTypeService basicTypeService = (BasicTypeService) context.getBean("basicTypeService");
BasicTypeProvider basicTypeService = (BasicTypeProvider) context.getBean("basicTypeService");
System.out.println(basicTypeService.testBasicType(new TypeRequest()));

try {
@@ -27,7 +27,9 @@
<dubbo:registry address="localhost:2181" protocol="zookeeper"/>

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" url="dubbo://localhost:20880" check="false" interface="com.alibaba.dubbo.demo.DemoService" version="1.0.0"/>
<dubbo:reference id="demoService" url="dubbo://localhost:20880" check="false"
interface="com.alibaba.dubbo.demo.DemoProvider" version="1.0.0"/>

<dubbo:reference id="basicTypeService" check="false" interface="com.alibaba.dubbo.demo.BasicTypeService" version="2.0.0"/>
<dubbo:reference id="basicTypeService" check="false" interface="com.alibaba.dubbo.demo.BasicTypeProvider"
version="2.0.0"/>
</beans>
@@ -1,11 +1,9 @@
package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.BasicTypeService;
import com.alibaba.dubbo.demo.BasicTypeProvider;
import com.alibaba.dubbo.demo.TypeRequest;

import java.util.Map;

public class BasicTypeServiceImpl implements BasicTypeService{
public class BasicTypeServiceImpl implements BasicTypeProvider {
@Override
public TypeRequest testBasicType(TypeRequest request) {
return request;
@@ -0,0 +1,58 @@
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.DemoProvider;
import com.alibaba.dubbo.demo.UserRequest;
import com.alibaba.dubbo.demo.UserResponse;
import com.alibaba.dubbo.rpc.RpcContext;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DemoProviderImpl implements DemoProvider {

public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
}

@Override
public String echo() {
System.out.println("receive....");
return "pang";
}

@Override
public void test() {
System.out.println("test");
}

@Override
public UserResponse getUserInfo(UserRequest request) {
System.out.println(request);
UserResponse response = new UserResponse();
response.setStatus("ok");
Map<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("name", "test");
response.setInfo(map);
return response;
}

}
@@ -0,0 +1,11 @@
package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.CustomizeException;
import com.alibaba.dubbo.demo.ErrorProvider;

public class ErrorProviderImpl implements ErrorProvider {
@Override
public void errorTest() throws CustomizeException {
throw new CustomizeException("exception");
}
}
@@ -30,13 +30,13 @@
<dubbo:protocol name="dubbo" port="20881"/>

<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoProviderImpl"/>
<bean id="basicTypeService" class="com.alibaba.dubbo.demo.provider.BasicTypeServiceImpl"/>
<bean id="errorService" class="com.alibaba.dubbo.demo.provider.ErrorServiceImpl"/>
<bean id="errorService" class="com.alibaba.dubbo.demo.provider.ErrorProviderImpl"/>

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" version="1.0.0"/>
<dubbo:service interface="com.alibaba.dubbo.demo.BasicTypeService" ref="basicTypeService" version="2.0.0"/>
<dubbo:service interface="com.alibaba.dubbo.demo.ErrorService" ref="errorService" version="1.0.0"/>
<dubbo:service interface="com.alibaba.dubbo.demo.DemoProvider" ref="demoService" version="1.0.0"/>
<dubbo:service interface="com.alibaba.dubbo.demo.BasicTypeProvider" ref="basicTypeService" version="2.0.0"/>
<dubbo:service interface="com.alibaba.dubbo.demo.ErrorProvider" ref="errorService" version="1.0.0"/>

</beans>
@@ -18,5 +18,8 @@
"ip": "1.1.5",
"js-to-java": "2.4.0",
"node-zookeeper-client": "0.2.2"
},
"devDependencies": {
"interpret-util": "0.0.2"
}
}
@@ -15,44 +15,22 @@
* limitations under the License.
*/
import java from 'js-to-java';
import log4js from 'log4js';
import {Dubbo} from '../dubbo';
import {TDubboCallResult} from '../types';

interface IDemoService {
sayHello(name: string): TDubboCallResult<string>;

echo(): TDubboCallResult<string>;

test(): TDubboCallResult<void>;

getUserInfo(): TDubboCallResult<{
status: string;
info: {id: number; name: string};
}>;
}

interface IBasicTypeService {
testBasicType(
typeRequest: Map<string, string>,
): TDubboCallResult<{
hello: string;
email: string;
}>;
}

interface IErrorService {
errorTest();
}
import {DemoProvider} from './providers/com/alibaba/dubbo/demo/DemoProvider';
import {BasicTypeProvider} from './providers/com/alibaba/dubbo/demo/BasicTypeProvider';
import {ErrorProvider} from './providers/com/alibaba/dubbo/demo/ErrorProvider';
import {UserRequest} from './providers/com/alibaba/dubbo/demo/UserRequest';
import {TypeRequest} from './providers/com/alibaba/dubbo/demo/TypeRequest';

const dubbo = new Dubbo({
application: {name: '@qianmi/node-dubbo'},
register: 'localhost:2181',
dubboVersion: '2.0.0',
interfaces: [
'com.alibaba.dubbo.demo.DemoService',
'com.alibaba.dubbo.demo.BasicTypeService',
'com.alibaba.dubbo.demo.ErrorService',
'com.alibaba.dubbo.demo.DemoProvider',
'com.alibaba.dubbo.demo.BasicTypeProvider',
'com.alibaba.dubbo.demo.ErrorProvider',
],
});

@@ -61,9 +39,7 @@ dubbo.use(async function test(ctx, next) {
const startTime = Date.now();
await next();
const endTime = Date.now();
const {
request: {dubboInterface, methodName},
} = ctx;
const {request: {dubboInterface, methodName}} = ctx;
console.log(
`invoke ${dubboInterface}#${methodName} costTime: ${endTime - startTime}`,
);
@@ -81,33 +57,11 @@ dubbo.subscribe({
},
});

const demoService = dubbo.proxyService<IDemoService>({
dubboInterface: 'com.alibaba.dubbo.demo.DemoService',
version: '1.0.0',
methods: {
sayHello(name) {
return [java.String(name)];
},

echo() {},

test() {},

getUserInfo() {
return [
java.combine('com.alibaba.dubbo.demo.UserRequest', {
id: 1,
name: 'nodejs',
email: 'node@qianmi.com',
}),
];
},
},
});
const demoService = DemoProvider(dubbo);

describe('demoService', () => {
it('test sayHello', async () => {
const {res, err} = await demoService.sayHello('node');
const {res, err} = await demoService.sayHello(java.String('node'));
expect(err).toEqual(null);
expect(res.includes('Hello node, response form provider')).toEqual(true);
});
@@ -121,35 +75,29 @@ describe('demoService', () => {
});

it('test getUserInfo', async () => {
const res = await demoService.getUserInfo();
const res = await demoService.getUserInfo(
new UserRequest({name: 'nodejs', email: 'email'}),
);
expect(res).toEqual({
err: null,
res: {status: 'ok', info: {id: '1', name: 'test'}},
});
});
});

const basicTypeService = dubbo.proxyService<IBasicTypeService>({
dubboInterface: 'com.alibaba.dubbo.demo.BasicTypeService',
version: '2.0.0',
methods: {
testBasicType(typeRequest) {
return [
java.combine('com.alibaba.dubbo.demo.TypeRequest', {
map: typeRequest,
bigDecimal: java.BigDecimal('100.00'),
}),
];
},
},
});
const basicTypeService = BasicTypeProvider(dubbo);

describe('typeBasicServer', () => {
it('testBasicType', async () => {
const map = new Map();
map.set('hello', 'hello world');
map.set('email', 'email@qianmi.com');
const reuslt = await basicTypeService.testBasicType(map);
const reuslt = await basicTypeService.testBasicType(
new TypeRequest({
map: {
hello: 'hello world',
email: 'email@qianmi.com',
},
bigDecimal: {value: '100.00'},
}),
);
expect(reuslt).toEqual({
err: null,
res: {
@@ -163,15 +111,7 @@ describe('typeBasicServer', () => {
});
});

const errorService = dubbo.proxyService<IErrorService>({
dubboInterface: 'com.alibaba.dubbo.demo.ErrorService',
version: '1.0.0',
methods: {
errorTest() {
return [];
},
},
});
const errorService = ErrorProvider(dubbo);

describe('error test', () => {
it('test errorTest', async () => {

0 comments on commit e70eb6e

Please sign in to comment.