この記事では、Springboot Integrated Dubboのコード統合とリリースを紹介し、次のように共有しています。
1。Boot-Dubbo-API関連
Boot-Dubbo-APIプロジェクトを開き、SRC/Main/Javaの下でパッケージを作成し、Dubboを公開する必要があるInterface TestService.javaを作成し、ユーザーをテストするためにエンティティクラスを作成します。下の図に示すように:
ファイルとパッケージの構造を作成します
user.java
パッケージcom.boot.domain; import lombok.data; import java.io.serializable; @datapublic classユーザーはserializable {private integer id;プライベート文字列ユーザー名;プライベート文字列パスワード。民間整数年齢;プライベート整数性別;}testservice.java
パッケージcom.boot.service;インポートcom.boot.domain.user; public interface testservice {string sayshello(string str); user finduser();}2。Boot-Dubbo-Provider関連
まず、書き込みが必要なコンテンツ全体とファイルの階層図を見てみましょう。
Boot-Dubbo-Providerプロジェクト構造図
ステップ1:最初にブートデューボAPIで定義したインターフェイスを実装し、testServiceImplクラスを作成し、testServiceを実装します
パッケージcom.boot.service.impl; Import com.alibaba.dubbo.config.annotation.service; Import com.boot.domain.user; Import com.boot.service.testservice; Import java.text.text.simpledateformat; Import Java.util.date; @Service(= "1.Service" 1.0.0.0 "") testService {@Override public string sayshello(string str){simpledateformat dateformat = new simpledateFormat( "yyyy-mm-dd hh:mm:ss"); return dateformat.format(new date()) + ":" + str; } @Override public user finduser(){user user = new user(); user.setId(1001); user.setusername( "scott"); user.setPassWord( "Tiger"); user.setage(20); user.setgender(0);ユーザーを返します。 }}注:コードの@Serviceアノテーションは、com.alibaba.dubbo.config.annotation.serviceです。
ステップ2:リソースの下に構成フォルダーを作成し、構成の下にspring-dubbo.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:dubbo = "http://code.alibabatech.com/schema/dubbo" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/bean/beans. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <dubbo:アプリケーション名=" Provider = "Provider"/> <! - 登録センターのIPアドレス - > <dubbo:レジストリaddress = "zookeeper://127.0.0.1:2181:2181"/> <! - アノテーションパッケージパスをスキャンすると、複数のパッケージがコンマで分離されており、現在のアプリケーションのすべてのクラスを示すためにPacakgeを記入しないでください - > <dubbo:annotation package = "com.service.sservice.smpl"/>
ステップ3:com.bootパッケージの下に新しいスプリングブートエントリクラスを作成し、providerApplication.javaファイルを作成します。
パッケージcom.boot; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; Import org.springframework.context.annotation.importresource; Import java.io.ioexception;@springbootapplication@incortresource({"classpath:config/spring-dubbo.xml"})public class providerapplication {public static void main(string [] args){springApplication.run(voviderApplication.class、args); try {system.in.read(); } catch(ioexception e){e.printstacktrace(); }}}ステップ4:最後に、Resourcesフォルダーの下にApplication.yml、Springboot構成ファイルを作成します。
#スプリングブート構成情報をここに記述します
3。Boot-Dubbo-Consumer関連
まず、書き込みが必要なコンテンツ全体とファイルの階層図を見てみましょう。
Boot-Dubbo-Consumerプロジェクト構造図
ステップ1:コントローラーコントロールクラスを作成し、com.boot.controllerパッケージの下に新しいTestControllerクラスを作成し、アクセスアドレスを書き込みます。
パッケージcom.boot.controller; Import com.alibaba.dubbo.config.annotation.Reference; import com.boot.domain.user; import com.boot.service.testservice; Import org.springframework.web.bind.annotation.notation.getMapping; Import org.springframework.springframework.springframework.springframework.springframework.springframework.springFramework. org.springframework.web.bind.annotation.restcontroller;@restcontroller@requestmapping( "/")public class testcontroller {@reference(version = "1.0.0")private testservice testservice; @getMapping( "hello")public string hello(){return testservice.sayhello( "hello springboot and dubbo!"); } @getMapping( "user")public user user(){return testservice.finduser(); }}ステップ2:リソースの下に構成フォルダーを作成し、構成の下にspring-dubbo.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:dubbo = "http://code.alibabatech.com/schema/dubbo" xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/bean/beans. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <dubbo:dubbo:" Consumer "/> <dubbo:dubbo:registry address =" zookeeper://127.0.0.0.0.1:2181 " package = "com.boot.controller"/> </beans>
ステップ3:com.bootパッケージの下に新しいスプリングブートエントリクラスを作成し、ConsumerApplication.javaファイルを作成します。
パッケージcom.boot; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.context.annotation.importresource;@springbootapplication@incortresource({"classpath:config/spring-dubbo.xml"})public class condulerapplication {public static void main(string [] args){springerapplication.run(consumerapplication.ragss、class、class、 }}ステップ4:最後に、Resourcesフォルダーの下にApplication.yml、Springboot構成ファイルを作成します。
#スプリングブート構成情報を書くここでサーバー:ポート:8080コンテキストパス: /
この時点で、コード部分が書かれています! ! !
4. Zookeeper登録センターをコンピューターにインストールします
アドレスのダウンロード:Zookeeperダウンロードアドレス
写真に示されているように、自分に合ったバージョンをダウンロードするにはクリックしてください
Zookeeperをダウンロードしてください
ダウンロードした後、ファイルを解凍し、confフォルダーを入力し、zoo_sample.cfgのコピーをコピーし、このディレクトリでzoo.cfgファイルを生成します。
Zoo.cfgファイルをコピーして生成します
binディレクトリを入力し、zkserver.cmdファイルを実行します。
zkserver.cmdを実行します
クリックして実行して、次の図に示されているように表示されます
正常に実行します
5。これで、最終的にプロジェクトを実行できます
最初にProviderApplication.javaファイルの主な関数を実行し、次にConsumerApplication.javaファイルの主な関数を実行します。
アクセスするブラウザを開きます
http:// localhost:8080/hello
http:// localhost:8080/user
さて、エキサイティングな時が来て、ついにスプリングブートとダボの統合を完了しました。
プロジェクトアドレス:Springboot-DubboプロジェクトGitHubアドレス
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。