在此感谢
http://blog.youkuaiyun.com/lzxadsl/article/details/48494917
http://blog.youkuaiyun.com/yeness/article/details/52823503
http://blog.youkuaiyun.com/hejingyuan6/article/details/47403299
http://doc.okbase.net/congcong68/archive/112508.html
本文大部分内容将与网上的资料一致,网上给的资料挺全,但在使用过程中往往会出现个人遇到的问题,而网上没有解决方案或者需要查询其它资料,我搭建完后,整理了一下几点心得,与大家分享:
1、工具准备,这部分包括zookeeper的下载,tomcat下载,dubbo-admin的环境搭建或者下载。关于dubbo-admin这部分,其实这个东西是dubbo的一个管理后台,可以自己到网上下载dubbo的源码,当前2017-12-08号更新到了2.5.8的版本,我这里提供我自己下载源码打包的下载地址http://download.youkuaiyun.com/download/perfectprogramming/10153462,欢迎大家下载使用,zookeeper下载就更简单了,大家可以自行百度搜索,我此处只是单机运行zookeeper,因此,没有涉及到集群的知识,那么zookeeper的搭建,我在此不赘述了。
2、结构分析:rpc框架的核心三大块就是api接口项目,impl接口实现层项目,还有就是消费者,一般我们先定义接口,然后开发实现层,然后进行消费者程序调用,代码在https://gitee.com/superlcf/rpc下,大家可以去下载,互相学习
在我使用过程中,主要注意到,service工程下,使用spring的包扫描时,如果不使用java7编译,会一直报错,并且导致工程无法进行dubbo的service服务扫描,网上有说法说是因为spring的扫描导致了dubbo的失效,但是后面我将java8改成java7后,可以直接进行两个包的扫描,主要代码如下:
<context:component-scan base-package="com.lcf"/>此处扫描我的实现类里面的java类,后面再
<import resource="dubbo.xml"/>
<dubbo:application name="dubbo_provider"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>
<!-- 用dubbo协议在21000端口暴露服务 -->
<dubbo:protocol name="dubbo" port="21000" />
<!-- 要暴露的服务接口 -->
<!--<bean id='hello' class="com.lcf.HelloImpl"></bean>
<dubbo:service interface="com.lcf.Hello" ref="hello" /> -->
<dubbo:annotation package="com.lcf"/> <!--此处进行dubbo的扫描,此处两次扫描不会进行冲突-->
实现类的代码如下:
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
@Component("hello")////这是java的包扫描,相当于applicationContext.xml中进行bean的配置
@Service///这里的注解是dubbo的扫描,将该服务注册到zookeeper中,声明该服务
public class HelloImpl implements Hello{
public void say(String name) {
System.out.println("你好,"+name);
}
}
3、消费者方面:
applicationContext.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" default-autowire="byName">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<!-- <dubbo:application name="hjy_consumer" /> -->
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" /> -->
<!-- <dubbo:annotation package="com.lcf.controller" /> -->
<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<!-- <dubbo:reference id='hello' interface="com.lcf.Hello" /> -->
<context:component-scan base-package="com.lcf.controller"/>
</beans>
由于使用到了springmvc,因此,还有springmvc的配置文件<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 启用spring mvc 注解-->
<context:annotation-config/>
<!-- MVC转换 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<import resource="dubbo.xml"/>
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.lcf.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<context:annotation-config />
</beans>
springmvc中存在导入dubbo的配置文件,dubbo.xml必须在springmvc的配置文件中加载,如果在spring的文件中加载,会出现注入接口出现空指针的情况。
dubbo.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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-lazy-init="false" default-autowire="byName">
<dubbo:application name="basic_dubbo_consumer" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:annotation package="com.lcf.controller" />
<!-- <dubbo:reference id='hello' interface="com.lcf.Hello" /> -->
</beans>
package com.lcf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.dubbo.config.annotation.Reference;
import com.lcf.Hello;
@Controller
@RequestMapping("/")
public class HelloController {
@Reference////此处对应的是zookeeper中注册的服务的接口id
private Hello hello;
@ResponseBody
@RequestMapping("hello")
public void hello(String name){
hello.say(name);
}
}
package com.lcf;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Start {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "classpath:applicationContext.xml" });
context.start();
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
以上代码启动,顺序无关紧要,但是在访问消费者时,服务方必须处于服务状态。
都启动完成之后,可以看到dubbo-admin中出现