标签:
其核心部分包含
Dubbo能做什么
透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。
主要核心部件
Remoting: 网络通信框架,实现了sync-over-async 和 request-response 消息机制.
RPC: 一个远程过程调用的抽象,支持负载均衡、容灾和集群功能
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
不会搭建maven项目的可以参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51286373
本例我搭建了两个项目:dubbo-provider和dubbo-customer
在pom.xml文件中增加dubbo、zookeeper、zkclient的jar包:
<!-- http://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- http://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.8</version> </dependency> <!-- http://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> <!-- <type>pom</type> --> </dependency>
因为要作为web项目启动,web.xml文件中需要增加:
必须有ContextLoaderListener监听器,applicationContext.xml才会成功加载
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>下面是DemoService和DemoServiceImpl的内容
public interface DemoService { String getName(String firstName,String lastName); }
public class DemoServiceImpl implements DemoService{ @Override public String getName(String firstName, String lastName) { return "hello, "+firstName+" " +lastName; } }
<?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 "> <!-- 具体的实现bean --> <bean id="demoService" class="com.cn.provider.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="provider" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://127.0.0.1:1234" /> --> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.cn.provider.DemoService" ref="demoService"/> </beans>
public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); context.start(); System.out.println("dubbo-provider启动"); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟 } }
因为dubbo-customer需要引入dubbo-provider项目中DemoService的jar包,pom.xml文件内容要加上:
<!-- http://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- http://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.8</version> </dependency> <!-- http://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> <!-- <type>pom</type> --> </dependency> <dependency> <groupId>javabuilder</groupId> <artifactId>javabuilder</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/dubbo-provider.jar</systemPath> </dependency>
记得把dubbo-provider.jar放到项目WEB-INF/lib下,生成jar包的方法可参考我的博客:http://blog.csdn.net/aqsunkai/article/details/51711580
整个项目我想既可以用main方法启动加载配置文件,也可以作为web项目用tomcat启动,在浏览器中看到结果,那么我一定需要在pom.xml中引入spring的jar包吗,答案是no,我只需要写servlet,直接进入doGet方法即可验证,那么就需要修改web.xml
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>servletDemo</servlet-name> <servlet-class>com.cn.customer.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletDemo</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping>
<?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="customer" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.cn.provider.DemoService"/> <!-- 目的是用ApplicationContext获取bean,与dubbo项目无关 --> <bean class="com.cn.customer.AppContext"/> </beans>
public class Servlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; //初始化 public void init() throws ServletException { System.out.println("我是init()方法!用来进行初始化工作"); } //处理GET请求 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是doGet()方法!用来处理GET请求"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<HTML>"); out.println("<BODY>"); /* * 通过Spring提供的工具类获取ApplicationContext对象 */ //ServletContext sc = this.getServletContext(); //和下面一行一样,都能获取ServletContext ServletContext sc = request.getSession().getServletContext(); //第一种获取bean方法,获取失败时抛出异常 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); DemoService demoService1 = (DemoService)ac1.getBean("demoService"); String name1 = demoService1.getName("tom", "Edison"); out.println(name1); out.println("<br>"); //第二种获取bean方法,获取失败时返回null ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc); DemoService demoService2 = (DemoService)ac2.getBean("demoService"); String name2 = demoService2.getName("tom", "Edison"); out.println(name2); out.println("<br>"); //第三种获取bean方法 WebApplicationContext wac = (WebApplicationContext)sc.getAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); DemoService demoService3 = (DemoService)wac.getBean("demoService"); String name3 = demoService3.getName("tom", "Edison"); out.println(name3); out.println("<br>"); //第四种获取bean方法,实现ApplicationContextAware接口 AppContext aContext = new AppContext(); DemoService demoService4 = (DemoService)aContext.getBean("demoService"); String name4 = demoService4.getName("tom", "Edison"); out.println(name4); out.println("</BODY>"); out.println("</HTML>"); } //处理POST请求 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("我是doPost()方法!用来处理POST请求"); doGet(request, response); } //销毁实例 public void destroy() { super.destroy(); System.out.println("我是destroy()方法!用来进行销毁实例的工作"); } }
public class AppContext implements ApplicationContextAware{ private static ApplicationContext applicationContext; /** * 当继承了ApplicationContextAware类之后,那么程序在调用 * getBean(String)的时候会自动调用该方法,不用自己操作 */ @Override public void setApplicationContext( org.springframework.context.ApplicationContext applicationContext) throws BeansException { this.applicationContext= applicationContext; } public Object getBean(String beanName){ return this.applicationContext.getBean(beanName); } }
public class Customer{ public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); String name = demoService.getName("tom", "Edison"); System.out.println(name); System.in.read(); } }
标签:
原文地址:http://blog.csdn.net/aqsunkai/article/details/51703052