Dubbo
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和spring框架无缝集成。
1.主要核心部件
Remoting:网络通信框架,实现了sync-over-async和request-reponse消息机制
RPC:一个远程过程调用的一个抽象,支持负载均衡、容灾和集群功能
Registry:服务目录框架用于服务的注册和服务事件发布和订阅
2.工作原理
Provider:暴露服务方称之为“服务提供者”。
Consumer:调用远程服务方称之为“服务消费者”。
Registry:服务注册与发现的中心目录服务称之为“服务注册中心”。
Monitor:统计服务的调用次数和调用时间的日志服务称之为“服务监控中心”。
3.demo
3.1创建一个maven web工程
3.2在web.xml中添加如下代码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
3.3创建接口和实现类
3.4定义生产者和消费者
在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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="hello-world-app"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.alibaba.hello.api.HelloService"ref="helloService"/>
<bean id="helloService" class="com.alibaba.hello.impl.HelloServiceImpl"/>
</beans>
在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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="consumer-of-helloworld-app"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService"/>
</beans>
3.5编写测试代码进行测试
创建provider.java文件
packagecom.alibaba.hello.test;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
public classProvider
{
publicstatic void main(String[] args)
{
ClassPathXmlApplicationContext context = newClassPathXmlApplicationContext(new String[]{"config/provider.xml"});
// 启动成功,监听端口为20880System.in.read();//按任意键退出
}
}
创建consumer.java文件
packagecom.alibaba.hello.test;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.alibaba.hello.api.HelloService;
public classConsumer
{
public static void main(String[] args){
ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext(newString[]{"config/consumer.xml"});
HelloService helloService= (HelloService)context.getBean("helloService");
//getserviceinvocationproxyStringhello=helloService.sayHello("world");
//doinvoke!System.out.println(hello);
//cool,howareyou~
}
}