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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

【Java23】maven加强,分布式RPC框架Dubbo

發布時間:2024/4/24 java 124 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java23】maven加强,分布式RPC框架Dubbo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1.maven的繼承
  • 2.maven的聚合
  • 3.RPC
  • 4.軟件演進
  • 5.Dubbo和注冊中心zookeeper
  • 6.Dubbo創建接口工程
  • 7.Dubbo創建服務提供者(Provider)
  • 8.Dubbo創建服務消費者(Consumer)


1.maven的繼承

//pom.xml <?xml version="1.0" encoding="UTF-8"?> <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>com.itheima</groupId><artifactId>ssm_parent</artifactId><!-- 父工程必須打pom包--><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>ssm_child</module></modules><properties><!-- 依賴版本的統一管理 --><mybatis.version>3.4.6</mybatis.version></properties><!--dependencyManagement并不會真的去引入依賴,只是鎖定依賴的版本號一般用在父工程中,--><dependencyManagement><dependencies><!-- mybatis依賴--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><!--mysql驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><!--測試--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--spring的ioc--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.6.RELEASE</version></dependency><!--spring和mybatis整合--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><!-- 德魯伊連接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.9</version></dependency><!--spring的jdbc--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.6.RELEASE</version></dependency><!-- springMVC的依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.6.RELEASE</version></dependency><!--spring的web相關的依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.6.RELEASE</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--spring的事務--><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.6.RELEASE</version></dependency><!--spring的aop--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.0.6.RELEASE</version></dependency></dependencies></dependencyManagement><build><plugins><!-- java編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project> //pom.xml <?xml version="1.0" encoding="UTF-8"?> <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"><!-- 引入父工程的坐標--><parent><artifactId>ssm_parent</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ssm_child</artifactId><dependencies><!-- mybatis依賴--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></dependency><!--mysql驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies><build><plugins><!-- java編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project>

2.maven的聚合

所謂的橫向的拆分就是我們平常說的三層架構,將項目分成了web層,service層、dao層(web層也被叫做表現層, service層也被叫做業務層,dao層也被持久層),可以理解為將一個功能模塊的不同調用過程進行了水平方向的拆分。所謂的縱向拆分就是將一個項目的多個功能模塊進行了拆分,可以理解成為了完成一個系統,深度(縱向)分析 需要有哪些功能,然后將這些功能獨立出來,進行了(縱向)拆分。拆分后,每個功能模塊進行了單獨的開發之后,在整合項目時就需要有一個能夠整合這些項目或者模塊的工程, 這就是所謂聚合工程的意義。建立聚合工程需要注意:1.該聚合項目本身也做為一個Maven項目,它必須有自己的POM。2.它的打包方式必須為: pom3.引入了新的元素:module,子模塊


如下web工程要發布,所以打war包。

<?xml version="1.0" encoding="UTF-8"?> <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>com.itheima</groupId><artifactId>ssm_par</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><!-- 聲明所有的子模塊 --><modules><module>ssm_domain</module><module>ssm_mapper</module><module>ssm_service</module><module>ssm_web</module></modules><dependencies><!-- mybatis依賴--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version></dependency><!--mysql驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><!--測試--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><!--spring的ioc--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.6.RELEASE</version></dependency><!--spring和mybatis整合--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><!-- 德魯伊連接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.9</version></dependency><!--spring的jdbc--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.0.6.RELEASE</version></dependency><!-- springMVC的依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.6.RELEASE</version></dependency><!--spring的web相關的依賴--><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.6.RELEASE</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--spring的事務--><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.0.6.RELEASE</version></dependency><!--spring的aop--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.0.6.RELEASE</version></dependency></dependencies><build><plugins><!-- java編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project> <?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>ssm_par</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ssm_domain</artifactId><build><plugins><!-- java編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project> <?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>ssm_par</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ssm_mapper</artifactId><dependencies><!-- 引入domain工程的坐標--><dependency><groupId>com.itheima</groupId><artifactId>ssm_domain</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><plugins><!-- java編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project> <?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>ssm_par</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>ssm_service</artifactId><dependencies><dependency><groupId>com.itheima</groupId><artifactId>ssm_mapper</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><plugins><!-- java編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project> <?xml version="1.0" encoding="UTF-8"?> <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"><parent><artifactId>ssm_par</artifactId><groupId>com.itheima</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><packaging>war</packaging><artifactId>ssm_web</artifactId><dependencies><dependency><groupId>com.itheima</groupId><artifactId>ssm_service</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><plugins><!-- java編譯插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project>

如下dao層換成mybatis開發,web層換成controller開發。

3.RPC

RPC:遠程過程調用,我這臺電腦調用一個函數,這個函數在另一個電腦上執行。這種方式就像是本地正常的一次函數一樣,不需要關注遠程(網絡連接,斷開,傳數據)細節。 如下a和b兩個參數打包發給服務端。

1.為什么遠程remote?本機運行的程序主要干的事是A,執行A的過程需要借助B功能,但是B功能不屬于A功能范圍內事情。所以我們會調實現B功能的集群,獲取它們計算出來的結果。請求和響應和HTTP協議很像,grpc谷歌框架就是使用了http協議。

3 .數據包該如果封?程序內存中數據結構封裝到傳輸時數據結構(文本或字節數組),傳來后還要將數據(即字節或文本)恢復到內存中,這傳一次就叫做序列化和反序列化。HTTP中json等就是序列化方式,一個數據在內存中的表達可能沒法直接給人看的,或者沒法直接兩個進程間無法互相閱讀的,除了共享內存。json和xml都是文本化的序列化方式,高效的grpc中一般不會用json這種序列化方式,比如用jdk自己的序列化方式,protobuf等。

4 .如果需要提高rpc性能的話除了序列化和反序列化,數據傳輸過程中需要提高網絡IO性能。網絡IO中涉及很多的系統調用,有對這些系統調用較好的封裝如java中有名的rpc框架dubbo。dubbo中使用netty作為網絡傳輸的一個框架實現,netty又是基于java的NIO,NIO最終調用一些系統調用,這些系統調用能夠實現網絡傳輸過程中零拷貝,多路復用等等充分提高了網絡性能。

4.軟件演進

單體架構:一個tomcat正常200并發量。

垂直架構:每個系統獨立,通過復制通訊。

SOA架構:rpc調用,如果商品服務用的最多,撐不起那么大流量了,把商品服務再部署一份,怎么能保證其它系統調用把請求均勻分散到多個服務上去呢?也要實現負載均衡。

微服務架構:更多服務器。

5.Dubbo和注冊中心zookeeper

Container相當于tomcat容器或spring ioc容器。
Provider就是服務提供者,業務邏輯的實現。
Registry相當于菜單(掛了沒事,本地會緩存一份)。
Monitor監控應用程序調用了多少次,響應的速度是不是夠快,都能進行監控。

Dubbo官方推薦使用Zookeeper(像數據庫一樣,目錄樹狀結構)作為服務注冊中心。其它的dubbo支持的注冊中心有:redis、nacos。

zookeeper軟件使用:第一步:下載地址:http://archive.apache.org/dist/zookeeper/
第二步:把 zookeeper 的壓縮包(zookeeper-3.4.6.tar.gz)上傳到 linux 系統。
第三步:解壓縮壓縮包:tar -zxvf zookeeper-3.4.6.tar.gz。
第四步:進入zookeeper-3.4.6目錄,創建data目錄。
第五步:進入conf目錄 ,把zoo_sample.cfg 改名為zoo.cfg:mv zoo_sample.cfg zoo.cfg。
第六步:打開zoo.cfg文件, 修改data屬性:dataDir=/usr/zookeeper-3.4.6/data。

進入Zookeeper的bin目錄,啟動服務命令 : ./zkServer.sh start。
停止服務命令:./zkServer.sh stop 。
查看服務狀態命令:./zkServer.sh status 。ps -ef|grep zookeeper。

6.Dubbo創建接口工程

serviceImpl = Provider 。Controller = Consumer 。

File-New-Project-Maven

//pom.xml <?xml version="1.0" encoding="UTF-8"?> <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>com.itheima.dubbo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <!--java工程打包方式為jar--><description>定義業務接口</description> </project>

package com.itheima.dubbo;public interface HelloService { //定義接口String say(String name); }

如下點擊后就不用管了,把整個項目打成jar包后即安裝到了maven倉庫。

7.Dubbo創建服務提供者(Provider)

用tomcat運行業務實現,所以創建web工程。


//pom.xml <?xml version="1.0" encoding="UTF-8"?> <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>com.itheima.dubbo</groupId><artifactId>dubbo-provider</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging> <!--web工程打war包--><name>dubbo-provider Maven Webapp</name><properties><spring.version>5.0.5.RELEASE</spring.version> <!--版本--></properties><dependencies><dependency> <!--導入dubbo-interface的jar包--><groupId>com.itheima.dubbo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency> <!--導入dubbo和spring相關的jar包--><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><!-- dubbo相關 --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.0</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.7</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><!--數據庫驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!--數據庫連接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.6</version></dependency><!--mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.2</version></dependency></dependencies> </project> package com.itheima.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.itheima.dubbo.HelloService; import org.springframework.transaction.annotation.Transactional;@Service(interfaceClass = HelloService.class) @Transactional public class HelloServiceImpl implements HelloService { //業務邏輯,把它發布成服務才行,發布服務依賴dubbo,因為dubbo實現了遠程調用,實現了comsumer調用provide。 //所以dubbo配置也需要,也要下載一些dubbo相關的jar包。public String say(String s) {System.out.println("=====我被調用了");return "say hi " + s;} }

如上普通工程變成了dubbo工程,接下來怎么通過dubbo將服務發布出去,需要將spring和dubbo進行集成,集成文件如下。

//dubbo-provider.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubdo="http://code.alibabatech.com/schema/dubbo"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 當前應用名稱。用于注冊中心計算應用間依賴關系,注意:消費者和提供者應用名不要一樣 --><dubbo:application name="dubbo-provider" /><!-- 連接服務注冊中心zookeeper ip為zookeeper所在服務器的ip地址,需要先開啟zookeeper,默認2181端口,可在.cfg文件里修改端口。注冊信息保存到zookeeper里了--><dubbo:registry address="zookeeper://127.0.0.1:2181"/><!-- 消費者和提供者通訊:協議(數據結構的約定)和port:端口默認是20880,協議有rmi,rest,http --><dubbo:protocol name="dubbo" port="20881"></dubbo:protocol><!-- 掃描指定包,加入@Service注解的類會被發布為服務1.路徑匹配package2.類上使用了@Service注解--><dubbo:annotation package="com.itheima.service.impl" /> <!--哪個目錄下的類被發布為服務--> </beans>

怎么啟動這個服務呢?我們使用了spring,通過加載spring容器讀進來才能啟動服務。如下配置context指定加載spring路徑,通過listener創建ioc容器,啟動還要指定tomcat。

//web.xml <!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app><display-name>Archetype Created Web Application</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-provider.xml,classpath:applicationContext-dao.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> </web-app>


點擊綠色+號Tomcat Server - local。

8.Dubbo創建服務消費者(Consumer)

創建服務消費者(springmvc)工程,服務消費者調用服務提供者。先創建web工程步驟如上。

//pom.xml <?xml version="1.0" encoding="UTF-8"?> <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>com.itheima.dubbo</groupId><artifactId>dubbo-comsumer</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>dubbo-comsumer Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target><spring.version>5.0.5.RELEASE</spring.version></properties><dependencies><!--注意: 在消費者引入,需要調用業務邏輯的接口--><dependency><groupId>com.itheima.dubbo</groupId><artifactId>dubbo-interface</artifactId><version>1.0-SNAPSHOT</version></dependency><!--spring相關--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><!-- dubbo相關 --><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.6.0</version></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.7</version></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency></dependencies> </project> package com.itheima.dubbo.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.itheima.dubbo.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RequestMapping @RestController public class HelloController {@Reference(check = false,timeout = 10000) //com.alibaba.dubbo. //現在是遠程調用:本地通過HelloService接口生成的遠程接口的代理,所以不能使用@AutowiredHelloService helloService; @RequestMapping("/hello") public String hello(String name){ return helloService.say(name); //調用業務邏輯service} }

接下來還要將客戶端即消費者工程和dubbo進行集成。

//dubbo-consumer.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:dubdo="http://code.alibabatech.com/schema/dubbo"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 當前應用名稱,用于注冊中心計算應用間依賴關系,注意:消費者和提供者應用名不要一樣 --><dubbo:application name="dubbo-comsumer" /><!-- 連接服務注冊中心zookeeper ip為zookeeper所在服務器的ip地址--><dubbo:registry address="zookeeper://127.0.0.1:2181"/><!-- 掃描指定包,掃描將指定路徑下的 @References注解的對象進行注入 --><dubbo:annotation package="com.itheima.dubbo.controller" /> </beans>

如下還要配置springmvc。

//web.xml <!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app><display-name>Archetype Created Web Application</display-name><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <!--啟動參數--><param-name>contextConfigLocation</param-name><param-value>classpath:dubbo-comsumer.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>

同理配置一個tomcat,端口改為81。

總結

以上是生活随笔為你收集整理的【Java23】maven加强,分布式RPC框架Dubbo的全部內容,希望文章能夠幫你解決所遇到的問題。

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