公司项目用到Dubbo框架 分享一下自己的测试结果

首先创建一个服务器端maven项目,结构如下


建一个接口以及实现类

spring配置文件内容如下

<?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.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">
	<dubbo:application name="dubbo-app"/>
	
	<dubbo:registry address="multicast://224.5.6.7:1234"/>
	
	<dubbo:protocol name="dubbo" port="20880"/>
	
	<dubbo:service interface="com.tc.dubbo_service.ProcessData" ref="processDataService"/>

	<bean id="processDataService" class="com.tc.dubbo_service.ProcessDataImpl"/>
</beans>
编写一个测试类,用于启动服务

package com.tc.dubbo_service;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import junit.framework.TestCase;

public class TestProcess extends TestCase{
	public void testProcess() throws IOException{
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		System.in.read();
	}
}
再新建一个客户端maven项目,并且新建spring配置文件,代码如下:

<?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.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">
	<dubbo:application name="dubbo-app1"/>
	
	<dubbo:registry address="multicast://224.5.6.7:1234"/>
	
	<dubbo:reference id="processDataService" interface="com.tc.dubbo_service.ProcessData" />
	
</beans>
测试类,测试

package com.tc.dubbo_client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tc.dubbo_service.ProcessData;

import junit.framework.TestCase;

public class TestClient extends TestCase{
	
	public void testClient(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		ProcessData pro = (ProcessData) context.getBean("processDataService");
		pro.test();
	}
}
结果如下 

调用成功




扫描二维码关注公众号,回复: 2282509 查看本文章


猜你喜欢

转载自blog.csdn.net/qq812908087/article/details/49783367
今日推荐