日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

记录一次dubbo项目实战

發布時間:2024/4/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记录一次dubbo项目实战 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



一、案例說明

存在2個系統,A系統和B系統,A系統調用B系統的接口獲取數據,用于查詢用戶列表。

二、環境搭建

安裝zookeeper,解壓(zookeeper-3.4.8.tar.gz)得到如下:

然后進入conf將zoo_sample.cfg改名成zoo.cfg。并相關如下內容:

該目錄為存放數據的目錄。然后啟動,在bin目錄下:

三、工程創建

1、搭建B工程

1.導入依賴

<dependencies> <!-- dubbo采用spring配置方式,所以需要導入spring容器依賴 --> <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> <!-- zookeeper依賴 --> <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> </dependencies> 復制代碼

2.創建對象

public class User implements Serializable{//序列化自動生成 private static final long serialVersionUID = 1749666453251148943L;private Long id; private String username; private String password; private Integer age; //getter and setter } 復制代碼


3.創建服務

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; } } 復制代碼

4.編寫Dubbo的配置文件

位置我放在根目錄下dubbo/dubbo-server.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="com.shen.dubbo.service. UserService" ref="userServiceImpl" /> <!-- 將具體的實現類加入到Spring容器中 --> <bean id="userServiceImpl" class="com.shen.dubbo.service.impl.UserServiceImpl" /> </beans> 復制代碼

5.編寫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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>dubbo-b</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dubbo/dubbo-*.xml</param-value> </context-param> <!--Spring的ApplicationContext 載入 --> <listener> <listener-class>org.springframework.web.context. ContextLoaderListener</listener-class> </listener> </web-app> 復制代碼


6.啟動tomcat

在控制臺中將會看到如下內容:

可以看到,已經將UserService服務注冊到zookeeper注冊中心,協議采用的是dubbo。


2、搭建A工程

1.拷貝基本文件

從b系統中拷貝User對象、UserService接口到a系統

.

2.編寫Dubbo的配置文件

<!-- 提供方應用信息,用于計算依賴關系 --> <dubbo:application name="dubbo-a-consumer" /> <!-- 這里使用的注冊中心是zookeeper --> <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/> <!-- 從注冊中心中查找服務 --> <dubbo:reference id="userService" interface="com.shen.dubbo.service.UserService"/> 復制代碼

3.編寫UserService測試用例

public class UserServiceTest { private UserService userService; @Before public void setUp() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:dubbo/*.xml"); this.userService = applicationContext.getBean(UserService.class); } @Test public void testQueryAll() { List<User> users = this.userService.queryAll(); for (User user : users) { System.out.println(user); } } } 復制代碼


查看效果如下:

可以看到,已經查詢到10條數據,那么,也就是說A系統通過B系統提供的服務獲取到了數據。

在此我向大家推薦一個架構學習交流群。交流學習群號:478030634 里面會分享一些資深架構師錄制的視頻錄像:有Spring,MyBatis,Netty源碼分析,高并發、高性能、分布式、微服務架構的原理,JVM性能優化、分布式架構等這些成為架構師必備的知識體系。還能領取免費的學習資源,目前受益良多

3、解決代碼重復問題

我們可以看到,在上面的案例中User實體和服務接口兩個項目都需要使用,代碼復用不高。那么我們可以將該部分代碼抽取出來打成包,以供所有系統使用。故可以在創建一個工程項目名為dubbo-b-api。然后將相關的代碼都放到該項目中,再在其它項目中導入該項目依賴即可。這也是我們在真實項目中應該做的事情,因為調用方未必知道細節。


總結

以上是生活随笔為你收集整理的记录一次dubbo项目实战的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。