<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          dubbo整合springboot

          共 4565字,需瀏覽 10分鐘

           ·

          2020-09-18 14:07

          點擊上方藍色字體,選擇“標星公眾號”

          優(yōu)質文章,第一時間送達

          ? 作者?|??Mr陳二

          來源 |? urlify.cn/a2eqUj

          66套java從入門到精通實戰(zhàn)課程分享

          目前的dubbo已支持和springboot集成,還是之前的例子,這次我們通過springboot容器來實現。借此了解一下基于springboot容器啟動的dubbo的配置及使用。

          1. 準備工作

          創(chuàng)建一個Maven空項目,作為項目的父工程,此工程的子項目基于Spring Boot 2.0.5 實現

          在父工程的pom.xml引入之后要創(chuàng)建的子工程

          ????
          ????????gmall-interface
          ????????user-service-provider
          ????????order-service-consumer
          ????

          可以提前看一下工程結構

          下面分別來實現子工程:(子工程的實現方式都是在gmall工程下新建Module)

          2. 公共API

          項目中共用的接口和POJO類,代碼和之前一樣,這里不再展開

          3. 服務提供者

          工程結構如下

          引入依賴


          ????????
          ????????????com.zang
          ????????????gmall-interface
          ????????????1.0-SNAPSHOT
          ????????

          ????????
          ????????
          ????????????com.alibaba.boot
          ????????????dubbo-spring-boot-starter
          ????????????0.2.0
          ????????

          ????????
          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter
          ????????


          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-test
          ????????????test
          ????????

          ?需要注意的是,根據jdk和Spring Boot版本的不同,dubbo-spring-boot-starter的版本需要有根據的選擇

          dubbo提供了@Service注解,可將類聲明為提供方,省去了大量配置的麻煩

          import?com.alibaba.dubbo.config.annotation.Service;
          import?com.zang.gmall.bean.UserAddress;
          import?com.zang.gmall.service.UserService;
          import?org.springframework.stereotype.Component;

          import?java.util.Arrays;
          import?java.util.List;

          @Service ??//屬于Dubbo的@Service注解,非Spring ?作用:暴露服務
          @Component
          public?class?UserServiceImpl?implements?UserService?{

          ????@Override
          ????public?List?getUserAddressList(String?userId)?{
          ????//省略
          }}

          通過屬性配置的方式設置application.properties??

          #當前服務/應用的名字
          dubbo.application.name=user-service-provider

          #注冊中心的協(xié)議和地址
          dubbo.registry.protocol=zookeeper
          dubbo.registry.address=127.0.0.1:2181

          #通信規(guī)則(通信協(xié)議和接口)
          dubbo.protocol.name=dubbo
          dubbo.protocol.port=20880

          #連接監(jiān)控中心
          dubbo.monitor.protocol=registry
          #開啟包掃描,可替代?@EnableDubbo?注解
          ##dubbo.scan.base-packages=com.zang.gmall

          springboot容器根據配置啟動服務提供方,這里需要添加 @EnableDubbo 注解

          import?com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;

          //?開啟基于注解的dubbo功能(主要是包掃描@DubboComponentScan)
          //?也可以在配置文件中使用dubbo.scan.base-package來替代???@EnableDubbo
          @EnableDubbo

          @SpringBootApplication
          public?class?UserServiceProviderApplication?{

          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication.run(UserServiceProviderApplication.class,?args);
          ????}
          }

          提供方順利啟動

          4. 服務消費者

          消費者工程在初始化時設置為web項目,結構如下

          引入和服務提供方同樣的依賴,除此之外,添加springboot web模塊的依賴。

          ????
          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-web
          ????????

          dubbo提供了@Reference注解,可替換@Autowired注解,用于引入遠程服務

          import?com.alibaba.dubbo.config.annotation.Reference;
          import?com.zang.gmall.bean.UserAddress;
          import?com.zang.gmall.service.OrderService;
          import?com.zang.gmall.service.UserService;
          import?org.springframework.stereotype.Service;

          import?java.util.List;

          @Service
          public?class?OrderServiceImpl?implements?OrderService?{

          ????@Reference
          ????UserService?userService;

          ????@Override
          ????public?List?initOrder(String?userId)?{
          ??//代碼省略
          ???}
          }

          配置文件application.properties

          #避免和監(jiān)控中心端口沖突,設為8081端口訪問
          server.port=8081??

          dubbo.application.name=order-service-consumer
          dubbo.registry.address=zookeeper://127.0.0.1:2181
          dubbo.monitor.protocol=registry

          啟動類同樣加上@EnableDubbo注解

          import?com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;

          @EnableDubbo
          @SpringBootApplication
          public?class?OrderServiceConsumerApplication?{

          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication.run(OrderServiceConsumerApplication.class,?args);
          ????}
          }

          為查看調用是否成功,新增控制層用于訪問

          import?com.zang.gmall.bean.UserAddress;
          import?com.zang.gmall.service.OrderService;
          import?org.springframework.beans.factory.annotation.Autowired;
          import?org.springframework.stereotype.Controller;
          import?org.springframework.web.bind.annotation.RequestMapping;
          import?org.springframework.web.bind.annotation.RequestParam;
          import?org.springframework.web.bind.annotation.ResponseBody;

          import?java.util.List;

          @Controller
          public?class?OrderController?{

          ????@Autowired
          ????OrderService?orderService;

          ????@ResponseBody???//以json格式返回
          ????@RequestMapping("/initOrder")
          ????public?List?initOrder(@RequestParam("uid")?String?userId){

          ???????return?orderService.initOrder(userId);
          ????}

          }

          5. 測試調用

          啟動消費方,在瀏覽器訪問

          調用成功

          ?

          附:springboot也允許引用xml文件配置,方法是在啟動類中加入如下注解

          //@EnableDubbo
          //引入配置信息
          @ImportResource(locations="classpath:provider.xml")
          @SpringBootApplication
          public?class?UserServiceProviderApplication?{
          //略
          }



          粉絲福利:108本java從入門到大神精選電子書領取

          ???

          ?長按上方鋒哥微信二維碼?2 秒
          備注「1234」即可獲取資料以及
          可以進入java1234官方微信群



          感謝點贊支持下哈?

          瀏覽 66
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  激情色五月天 | 91av在线无码 | 欧美日韩高清在线观看 | 国产美女一级毛片真精品酒店 | 国产精品视频免费观看 |