一、首先在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的配置安裝就自行百度了,配置基本上都同樣,通常不會出問題。