首先咱們來看一下dubbo的架構:java
因此經過此圖,咱們看到就是服務的提供者將服務註冊到註冊中心,服務的消費者從註冊中心獲取服務,monitor監控服務的調用。git
關於dubbo的使用,咱們舉個簡單的例子:github
存在2個系統,A系統和B系統,A系統調用B系統的接口獲取數據,用於查詢用戶列表。web
廢話很少少,直接構建項目:spring
一、在Eclipse中點檢新建項目,選擇maven項目後,點擊「next」:數據庫
二、項目存儲位置,點擊「next」:apache
三、由於創建的是「jar」項目,選擇「maven-archetype-quickstart」,點擊「next」:api
四、填寫Group ID(項目組織惟一的標識符)和Artifact ID(項目的惟一的標識符) 點擊「Finish」,完成項目建立。tomcat
五、項目建立好後,在項目中增長pojo和service包:架構
六、在pojo包中添加User.java 類。
package cn.itcast.dubbo.pojo; import java.io.Serializable; public class User implements Serializable { /** * */ private static final long serialVersionUID = -8096919250664823274L; private Long id; private String username; private String password; private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]"; } }
七、在service包中UserService.java 接口類。
package cn.itcast.dubbo.service; import java.util.List; import cn.itcast.dubbo.pojo.User; public interface UserService { /** * 查詢全部的用戶數據 * * @return */ public List<User> queryAll(); }
七、將JAR包安裝到本地倉庫,供其餘Maven項目依賴使用。(若是有maven私倉,則將jar包添加到maven私倉中,供其餘項目調用)
一、在Eclipse中點檢新建項目,選擇maven項目後,點擊「next」:
二、項目存儲位置,點擊「next」:
三、創建web項目,選擇「maven-archetype-webapp」,點擊「next」:
四、填寫Group ID(項目組織惟一的標識符)和Artifact ID(項目的惟一的標識符) 點擊「Finish」,完成項目建立。
五、項目建立好後,在項目中增長cn.itcast.dubbo.service.impl包:
六、在「pom.xml」導入依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.itcast.dubbo</groupId> <artifactId>dubbo-b</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>dubbo-b Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>cn.itcast.dubbo</groupId> <artifactId>dubbo-ab-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <!-- 排除傳遞spring依賴 --> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8081</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
七、在cn.itcast.dubbo.service.impl包中建立UserServiceImpl實現類
package cn.itcast.dubbo.service.impl; import java.util.ArrayList; import java.util.List; import cn.itcast.dubbo.pojo.User; import cn.itcast.dubbo.service.UserService; public class UserServiceImpl implements UserService { /** * 實現查詢,這裏作模擬實現,不作具體的數據庫查詢 */ public List<User> queryAll() { List<User> list = new ArrayList<User>(); for (int i = 0; i < 10; i++) { User user = new User(); user.setAge(10 + i); user.setId(Long.valueOf(i + 1)); user.setPassword("123456"); user.setUsername("username_" + i); list.add(user); } return list; } }
八、在「src/main/resources」包中建立dubbo的配置文件「dubbo-applicationContext.xml」
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="dubbo-b-server" /> <!-- 這裏使用的註冊中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 將該接口暴露到dubbo中 --> <dubbo:service interface="cn.itcast.dubbo.service.UserService" ref="userServiceImpl" /> <!-- 將具體的實現類加入到Spring容器中 --> <bean id="userServiceImpl" class="cn.itcast.dubbo.service.impl.UserServiceImpl" /> <dubbo:monitor protocol="registry"></dubbo:monitor> </beans>
九、使用spring與dubbo進行了無縫整合,要在web.xml中將配置文件引入
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>dubbo-b</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo-applicationContext.xml</param-value> </context-param> <!--Spring的ApplicationContext 載入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
一、在Eclipse中點檢新建項目,選擇maven項目後,點擊「next」:
二、項目存儲位置,點擊「next」:
三、由於創建的是「jar」項目,選擇「maven-archetype-quickstart」,點擊「next」:
四、填寫Group ID(項目組織惟一的標識符)和Artifact ID(項目的惟一的標識符) 點擊「Finish」,完成項目建立。
五、在「pom.xml」導入依賴:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.itcast.dubbo</groupId> <artifactId>dubbo-a</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-a</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <!-- 排除傳遞spring依賴 --> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>cn.itcast.dubbo</groupId> <artifactId>dubbo-ab-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
六、在「src/main/resources」包中建立配置服務的消費者「dubbo-consumer.xml」 。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="dubbo-a-consumer" /> <!-- 這裏使用的註冊中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/> <!-- 從註冊中心中查找服務 --> <dubbo:reference id="userService" interface="cn.itcast.dubbo.service.UserService"/> </beans>
七、編寫測試代碼
public static void main( String[] args ) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:*.xml"); UserService userService = applicationContext.getBean(UserService.class); List<User> users = userService.queryAll(); for (User user : users) { System.out.println(user); } }
運行結果截圖: