dubbo的介绍
创建Provider
四个文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dubbo.testerhome.com</groupId>
<artifactId>Provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</project>
访问zookeeper的注册中心
dubbo验证
ls -l
ls -l com.alibaba.dubbo.demo.DemoService
//todo: 找到正确的语法
invoke com.alibaba.dubbo.demo.DemoService.sayHello({"name": "xxxx"})
consumer消费
构建测试用例
import com.alibaba.dubbo.demo.DemoService;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
public class TestProvider {
public static DemoService demoService;
@BeforeClass
public static void beforeClass(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"classpath:consumer.xml"});
context.start();
demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
}
@Test
public void sayHello(){
assertThat(demoService.sayHello(""), equalTo("Hello "));
assertThat(demoService.sayHello("world"), equalTo("Hello world"));
assertThat(demoService.sayHello("hello"), equalTo("Hello hello"));
assertThat(demoService.sayHello("seveniruby"), equalTo("hello seveniruby"));
}
}
泛化调用
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.rpc.service.GenericService;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class TestConsumerGeneric {
static GenericService genericService;
@BeforeClass
public static void beforeClass() {
// 引用远程服务
// 该实例很重量,里面封装了所有与注册中心及服务提供方连接,请缓存
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
// 弱类型接口名
reference.setInterface("com.alibaba.dubbo.demo.DemoService");
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("consumer-generic");
reference.setApplication(applicationConfig);
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
reference.setRegistry(registryConfig);
//reference.setVersion("1.0.0");
// 声明为泛化接口
reference.setGeneric(true);
// 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用
genericService = reference.get();
}
@Test
public void sayHello() {
Object result = genericService.$invoke(
"sayHello",
new String[]{"java.lang.String"}, new Object[]{"world"});
assertThat(result.toString(), equalTo("Hello world"));
Object result2 = genericService.$invoke(
"sayHello",
new String[]{"java.lang.String"}, new Object[]{"seveniruby"});
assertThat(result2.toString(), equalTo("Hello world"));
}
}
jsonrpc
可以让多语言或者使用RestAssured来测试dubbo服务
curl -i \\
-H 'content-type: application/json' \\
-X POST \\
-d '{"jsonrpc": "2.0", "method": "sayHello", "params": [ "MOBILE"],
"id": 1 }' 'http://127.0.0.1:20881/com.alibaba.dubbo.demo.DemoService'
调用结果
curl -i -H 'content-type: application/json' -X POST -d '{"jsonrpc": "2.0", "method": "sayHello", "params": [ "MOBILE"],"id": 1 }' 'http://127.0.0.1:20881/com.alibaba.dubbo.demo.DemoService'
HTTP/1.1 200 OK
Content-Length: 49
Server: Jetty(6.1.26)
{"jsonrpc":"2.0","id":1,"result":"Hello MOBILE"}
dubbo相关的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
\thttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbotest-provider"/>
<!-- <dubbo:registry address="zookeeper://10.0.0.88:2281" check="false"></dubbo:registry> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="1200000"/>
<!-- <dubbo:registry address="zookeeper://10.0.0.16:2281" check="false"></dubbo:registry> -->
<dubbo:protocol name="dubbo" port="20881"/>
<!-- 用接口定义服务 -->
<dubbo:service interface="com.ceba.provide.service.IGameinfo" ref="gameinfoService" >
\t<dubbo:method name="getDicPlaninfo" async="false" timeout="3000"></dubbo:method>
</dubbo:service>
<!-- 用接口的实现定义bean -->
<bean id="gameinfoService" class="com.ceba.provide.impl.GameinfoImpl" />
<dubbo:service interface="com.ceba.provide.service.IUserinfo" ref="userinfoService" />
<bean id="userinfoService" class="com.ceba.provide.impl.UserinfoImpl" />
</beans>