Dubbo是什么?
dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
简单来说dubbo是一个服务框架,如果没有分布式需求是不需要用的。
其核心包括
1.远程通讯:提供对多种基于长连接的NIO框架抽象封装,包括多线程模型,序列化,以及“请求-响应”模式的信息交换方式。
2.集群容错:提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
3.自动发现:基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。
4.Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
官方网站:dubbo.io
HelloWorld
依赖包:
ServiceProvider(服务提供方):
目录结构:
SimpleSerrvice:
public interface SimpleService {
String sayHello(String name);
public List getUsers();
}
SimpleServiceImpl:
public class SimpleServiceImpl implements SimpleService {
@Override
public String sayHello(String name) {
//
return "Hello " + name;
}
@Override
public List<User> getUsers() {
List<User> list = new ArrayList<User>();
User u1 = new User();
u1.setName("zhang1");
u1.setAge(18);
User u2 = new User();
u2.setName("zhang2");
u2.setAge(20);
list.add(u2);
list.add(u1);
return list;
}
}
simple-provider.xml:
<?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
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 具体实现的bean -->
<bean id="simpleService" class="com.zhang.simple.provider.impl.SimpleServiceImpl"/>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="simple-provider"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务 -->
<dubbo:registry address="zookeeper://192.168.0.3:2181" />
<!-- 用dubbo协议在2080端口暴露 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 声明需要暴露的服务接口 写操作设置retries=0避免重复调用soa -->
<dubbo:service retries="0" interface="com.zhang.simple.provider.SimpleService" ref="simpleService"/>
</beans>
Provider:
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"simple-provider.xml"});
context.start();
System.in.read();
}
Consumer(消费方):
目录结构:
simple-Consumer.xml:
<?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
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名 用于计算依赖关系 与服务提供方不一样-->
<dubbo:application name="simple-consumer"/>
<dubbo:registry address="zookeeper://192.168.0.3:2181"/>
<dubbo:reference id="simpleService" check="false" interface="com.zhang.simple.provider.SimpleService"/>
</beans>
SimpleService:
public interface SimpleService {
String sayHello(String name);
public List getUsers();
}
Consumer:
public static void main(String[] args) throws IOException {
@SuppressWarnings("resource")
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"simple-Consumer.xml"});
context.start();
SimpleService simpleService = (SimpleService)context.getBean("simpleService");
String hello = simpleService.sayHello("zhang");
System.out.println(hello);
System.in.read();
}
搭建好zookeeper服务器后,先运行Provider,再运行Consumer,即可打印hello zhang
zookeeper服务器中多出了/dubbo
节点,
[zk:192.168.0.3:2181]ls /dubbo
[com.zhang.simple.provider.SimpleService]
服务提供方和消费方中,SimpleService所在的包名必须一致,
如:都为com.zhang.simple.provider.SimpleService