一、首先在idea中建立一个多模块应用,为了工程的干净整洁,web工程最好只集成了springmvc和spring的web应用,service工程最好只集成了spring。web
二、配置dubbo服务提供者spring
2.一、服务器提供者web.xml配置服务器
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
2.二、服务器提供者applicationContext.xml配置mvc
<!-- 普通bean扫描配置--> <context:annotation-config /> <!-- 消费方应用名(通常取工程名),用于计算依赖关系,不是匹配条件,不要与消费方同样 --> <dubbo:application name="service" /> <!-- zookeeper注册中心 --> <dubbo:registry protocol="zookeeper" address="192.168.188.128:2181" /> <!-- 暴露的接口 --> <dubbo:service interface="com.mall.service.UserService" ref="userService" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- dubbo简易监控中心 --> <dubbo:monitor protocol="registry"></dubbo:monitor> <context:component-scan base-package="com"></context:component-scan>
三、配置dubbo服务消费者app
3.一、服务消费者web.xml配置ide
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 能够自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
3.二、服务消费者applicationContext.xml配置 url
<context:annotation-config /> <dubbo:application name="web" /> <dubbo:registry address="zookeeper://192.168.188.128:2181" /> <dubbo:reference interface="com.mall.service.UserService" id="userService" check="true" /> <dubbo:monitor protocol="registry"></dubbo:monitor> <context:component-scan base-package="com"></context:component-scan>
具体的每一个配置是什么意思就自行百度了,加深记忆idea
四、使用方法spa
4.一、Controller(服务消费者)中调用服务提供者component
@Controller @RequestMapping("/user") public class UserController extends BaseController{ @Autowired private UserService userService; @RequestMapping("/test.do") public void test(){ userService.test(); System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaa"); } }
4.二、服务提供者
@Service("userService") public class UserServiceImpl implements UserService { public void test(){ System.out.println("啦啦啦啦啦啦啦啦啦啦 我是Service服务啦"); } }
五、在启动的时候,要先启动服务提供者,若是先启动服务器消费者,在zookeeper注册中心找服务提供者时找不到的话就会报错(前提是消费方<dubbo:reference interface="com.mall.service.UserService" id="userService" check="true" />这里配置了true)。
六、dubbo管理控制中心和简易监控中心还有zookeeper的配置安装就自行百度了,配置基本上都同样,通常不会出问题。