随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行。
当越来越的的接口与实现类的增加后,duboo的xml配置会越来越多,为了防止几百几千行的代码,减少开发人员配置xml的工作量,使用duboo的注解模式,减少配置多出问题多的可能性!
二、Dubbo使用案例
Duboo注解
接口类项目:DubboServiceInterface
仅仅是一个接口类项目!接口是普通接口!
注意:将接口类项目打包成jar分别放入服务端项目跟客户端项目!
服务端项目:DubboServiceProvider
实现类fooserviceImpl.java
package com.alibaba.dubbo.demo.imp;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.demo.DemoService;
@Service(version="1.0")
public class FooServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
web.xml 配置扫描内容
applicationContext.xml 配置
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">
测试类Provider
package com.alibaba.dubbo.test;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
@Before
public void setUp() throws Exception {
}
@Test
public void testMain() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
System.in.read();// 按任意键退出
}
}
lib下的jar包
客户端项目:DubboServiceConsumer
web.xml 配置扫描内容
applicationContext.xml文件
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">
测试类:Consumer
package com.unj.dubbotest.action;
import java.io.IOException;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.demo.DemoService;
public class Consumer{
@Reference(version = "1.0")
private DemoService demoService;
@Test
public void mainTest() throws IOException {
ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"});
context.start();
demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
}
lib下的jar包
获取海量视频