如果你的applicationContext.xml没有配置dubbo.xsd可能会报以下错误
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'.
- schema_reference.4: Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not
find the document; 2) the document could not be read; 3) the root element of the document is not .1234 解决办法(我使用的是eclipse):
windows->preferrence->xml->xmlcatalog
点击Add
package com.usercenter.service.test;
import java.util.List;
import com.usercenter.service.impl.test.UserInfo;
public interface DemoService {
String sayHello(String name);
public List getUsers();
} 测试用的service接口实现类(对应applicationContext.xml中暴露的service具体实现接口)
package com.usercenter.service.impl.test;
import java.util.ArrayList;
import java.util.List;
import com.usercenter.service.test.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
public List getUsers() {
List list = new ArrayList();
UserInfo user = new UserInfo();
user.setId(1);
user.setName("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~zjm~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
user.setPassword("000000");
list.add(user);
return list;
}
}
启动“提供者”的类
package com.usercenter.service.impl.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "test/applicationContext.xml" });
context.start();
// 为保证服务一直开着,一直循环等待
synchronized (Provider.class) {
while (true) {
try {
Provider.class.wait();
} catch (Throwable e) {
System.out.println("服务异常:" + e.toString());
context.close();
}
}
}
}
} 最后需要把该项目打包成jar,放到“消费者”的项目中(maven直接打包成jar并上传到你的本地仓库或私服仓库就好了,每次修改只需要重新deploy就好了)
消费者
pom.xml
renai.usercenter
renai-usercenter-service
1.0
com.alibaba
dubbo
2.5.3
org.apache.zookeeper
zookeeper
3.4.6
com.github.sgroschupf
zkclient
0.1
applicationContext.xml
测试用的远程调用类(消费者)
package com.renai.card.manage.consumer;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.usercenter.service.impl.test.UserInfo;
import com.usercenter.service.test.DemoService;
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "spring/renai-dubbo.xml" });
context.start();
DemoService demoService = (DemoService) context.getBean("demoService");
String hello = demoService.sayHello("~~~~~~~~~~~~~炸酱面~~~~~~~~~~~~~");
System.out.println(hello);
List list = demoService.getUsers();
System.out.println(list.get(0).getName());
}
} 以上就完成了dubbo的基本功能
如果想实现远程调用,首先需要启动提供者的Provider(我自己写的那个)类
启动后在zookeeper管理控制台上可以看到有了一个“提供者”