<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+Nacos實戰(zhàn)(二)

          共 8543字,需瀏覽 18分鐘

           ·

          2022-03-22 21:42

          前言

          nacos的實戰(zhàn)在前面一章已經(jīng)介紹到?Spring Cloud Alibaba+Nacos的介紹與實戰(zhàn)(一)[1]?以及幾種注冊中心的區(qū)別介紹?幾種常見的注冊中心以及區(qū)別[2]

          新建父工程cloud-alibaba-demo

          源碼已經(jīng)上傳到gitee上 地址:https://gitee.com/culzb/cloud-alibaba-democa5f33e57bb08651cc2dfa96de27fcf4.webp配置文件pom.xml 管理子工程jar版本


          <module>gtwmodule>
          <module>demo-servicemodule>
          <module>dubbo-demo-servicemodule>



          <java.version>1.8java.version>
          <spring-cloud.version>Greenwich.SR1spring-cloud.version>





          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-dependenciesartifactId>
          <version>${spring-cloud.version}version>
          <type>pomtype>
          <scope>importscope>


          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-alibaba-dependenciesartifactId>
          <version>0.9.0.RELEASEversion>
          <type>pomtype>
          <scope>importscope>


          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-testartifactId>
          <version>2.2.1.RELEASEversion>
          <scope>testscope>



          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
          <version>0.2.1.RELEASEversion>



          新建子工程dubbo-demo-service

          當前子工程相是被調(diào)用的服務,也就是說服務提供者。在dubbo中作為providera12baee0b8a0f6e836aefad0738e0111.webp然后引入相關jar包 配置pom.xml


          "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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0modelVersion>

          <groupId>com.cuizbgroupId>
          <artifactId>cloud-alibaba-demoartifactId>
          <version>1.0version>

          <artifactId>dubbo-demo-serviceartifactId>
          <version>1.0version>
          <name>dubbo-demo-servicename>
          <description>Demo project for Spring Bootdescription>

          <java.version>1.8java.version>




          <groupId>org.apache.dubbogroupId>
          <artifactId>dubbo-spring-boot-starterartifactId>
          <version>2.7.0version>


          <groupId>org.apache.dubbogroupId>
          <artifactId>dubboartifactId>
          <version>2.7.0version>



          <groupId>org.apache.dubbogroupId>
          <artifactId>dubbo-registry-nacosartifactId>
          <version>2.7.1version>


          <groupId>com.alibaba.nacosgroupId>
          <artifactId>nacos-clientartifactId>
          <version>1.0.0version>



          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-webartifactId>



          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-testartifactId>
          <scope>testscope>






          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-maven-pluginartifactId>






          然后創(chuàng)建api接口,配置dubbo掃描的包路徑,會將指定掃描的包路徑中的所有接口注冊到nacos中。

          ?注意?創(chuàng)建的api接口實現(xiàn)類上一定要加上@service,引入包路徑為:import org.apache.dubbo.config.annotation.Service;

          否則在啟動時控制臺會出現(xiàn)[DUBBO] No Spring Bean annotating Dubbo's @Service was found under package[com.cuizb.dubbo.demoservice.api]

          0837b68f9faedf8dd7e9e08a3f49f07e.webp


          我創(chuàng)建了一個api接口和一個api接口實現(xiàn)類 DubboDemoService

          package com.cuizb.dubbo.demoservice.api;
          /**
          * @Author cuizb
          * @Date 2022-03-19 18:09:31
          * @Desc *
          */
          public interface DubboDemoService {
          String hello(String name);
          }

          DubboDemoServiceImpl

          package com.cuizb.dubbo.demoservice.controller;

          import com.cuizb.dubbo.demoservice.api.DubboDemoService;
          import org.apache.dubbo.config.annotation.Service;

          /**
          * @Author cuizb
          * @Date 2022-03-20 22:07:15
          * @Desc *
          */
          @Service
          public class DubboDemoServiceImpl implements DubboDemoService {
          @Override
          public String hello(String name) {
          return "hello " + name + "i am dubbo demoService!";
          }
          }

          最后配置dubbo以及nacos相關信息 application.properties

          server.port=8702
          spring.application.name=dubbo-demo-service


          # 配置服務信息
          dubbo.application.name=dubbo-demo-service
          ## 禁用QOS同一臺機器可能會有端口沖突現(xiàn)象
          #dubbo.qos-enable=false
          #dubbo.qos-accept-foreign-ip=False
          # 配置注冊中心

          #spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
          dubbo.registry.address=nacos://127.0.0.1:8848
          # 設置協(xié)議-協(xié)議由提供方指定消費方被動接受
          dubbo.protocol.name=dubbo
          dubbo.protocol.port=7702
          dubbo.scan.base-packages=com.cuizb.dubbo.demoservice.api
          # 解決Bean重復定義問題
          spring.main.allow-bean-definition-overriding=true

          啟動類加上@EnableDubbo

          package com.cuizb.dubbo.demoservice;

          import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
          import org.springframework.boot.SpringApplication;
          import org.springframework.boot.autoconfigure.SpringBootApplication;


          @EnableDubbo //開啟Dubbo的注解支持
          //@EnableDiscoveryClient
          @SpringBootApplication
          public class DubboDemoServiceApplication {
          public static void main(String[] args) {
          SpringApplication.run(DubboDemoServiceApplication.class, args);
          }
          }

          新建子工程gtw

          我沒有按照dubbo那樣分為provider和consumer區(qū)分工程,我按照項目習慣將網(wǎng)關作為服務消費者,其他服務作為服務提供者。由于我沿用了上一章節(jié)的網(wǎng)關工程,所以我會將pom.xml中的





          注釋掉了,防止jar沖突。以及config中也不需要注入RestTemplate的bean了。b14c24b91dc5ebfac2380f6aacc7f83d.webp首先引入jar包 pom.xml


          "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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0modelVersion>

          <groupId>com.cuizbgroupId>
          <artifactId>cloud-alibaba-demoartifactId>
          <version>1.0version>

          <groupId>com.cuizb.cloud.alibabagroupId>
          <artifactId>gtwartifactId>
          <version>1.0version>
          <name>gtwname>
          <description>Demo project for Spring Bootdescription>

          <java.version>1.8java.version>



          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-webartifactId>



          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-testartifactId>
          <scope>testscope>








          <groupId>org.apache.dubbogroupId>
          <artifactId>dubbo-spring-boot-starterartifactId>
          <version>2.7.0version>


          <groupId>org.apache.dubbogroupId>
          <artifactId>dubboartifactId>
          <version>2.7.0version>



          <groupId>org.apache.dubbogroupId>
          <artifactId>dubbo-registry-nacosartifactId>
          <version>2.7.1version>


          <groupId>com.alibaba.nacosgroupId>
          <artifactId>nacos-clientartifactId>
          <version>1.0.0version>


          <groupId>com.cuizbgroupId>
          <artifactId>dubbo-demo-serviceartifactId>
          <version>1.0version>
          <scope>compilescope>






          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-maven-pluginartifactId>






          網(wǎng)關入口類 GtwServiceImpl

          package com.cuizb.cloud.alibaba.gtw.controller;

          import com.cuizb.dubbo.demoservice.api.DubboDemoService;
          import org.apache.dubbo.config.annotation.Reference;
          import org.springframework.web.bind.annotation.GetMapping;
          import org.springframework.web.bind.annotation.RequestParam;
          import org.springframework.web.bind.annotation.RestController;

          /**
          * @Author cuizb
          * @Date 2022-03-19 18:12:16
          * @Desc *
          */
          @RestController
          public class GtwServiceImpl {

          // @Autowired
          // private RestTemplate restTemplate;
          //
          // @GetMapping("/hello")
          // public String hello(@RequestParam("name") String name) {
          // System.out.println(name);
          // return restTemplate.getForObject("http://demo-service/hello?name=" + name , String.class);
          //
          // }

          // Dubbo遠程調(diào)用注解
          @Reference
          private DubboDemoService dubboDemoService;

          @GetMapping("/hello")
          public String hello(@RequestParam String name) {
          return dubboDemoService.hello(name);
          }

          }

          注意:?注入注解是dubbo中@Reference

          dubbo和nacos配置信息

          server.port=8705
          spring.application.name=dubbo-gtw


          # 配置服務信息
          dubbo.application.name=dubbo-gtw
          # 禁用QOS同一臺機器可能會有端口沖突現(xiàn)象
          dubbo.qos-enable=false
          dubbo.qos-accept-foreign-ip=false
          # 配置注冊中心
          dubbo.registry.address=nacos://127.0.0.1:8848
          # 設置協(xié)議-協(xié)議由提供方指定消費方被動接受
          dubbo.protocol.name=dubbo
          dubbo.protocol.port=7705
          dubbo.consumer.timeout=3000
          # 解決Bean重復定義問題
          spring.main.allow-bean-definition-overriding=true

          啟動類加上@EnableDubbo

          @EnableDubbo
          @SpringBootApplication
          public class GtwApplication {
          public static void main(String[] args) {
          SpringApplication.run(GtwApplication.class, args);
          }

          }

          啟動工程

          首先啟動服務提供者dubbo-demo-service,然后啟動服務消費者gtw 查看nacosda22cc17a397ca127299bb6b0ca2a103.webp

          測試

          輸入地址:http://localhost:8705/hello?name=cuizb16fe0e91f6c9003eaf8127cf9268b9ece.webp

          如果先啟動消費者會出現(xiàn)啟動報錯

          Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.cuizb.dubbo.demoservice.api.DubboDemoService. No provider available for the service com.cuizb.dubbo.demoservice.api.DubboDemoService from the url nacos://127.0.0.1:8848/org.apache.dubbo.registry.RegistryService?application=dubbo-gtw&default.generic=false&default.timeout=3000&dubbo=2.0.2&generic=false&interface=com.cuizb.dubbo.demoservice.api.DubboDemoService&methods=hello&pid=53247&qos.enable=false&register.ip=192.168.1.5&release=2.7.0&side=consumer&timestamp=1647833584728 to the consumer 192.168.1.5 use dubbo version 2.7.0
          at org.apache.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:393) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:301) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:225) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.init(ReferenceAnnotationBeanPostProcessor.java:162) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.access$100(ReferenceAnnotationBeanPostProcessor.java:146) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildInvocationHandler(ReferenceAnnotationBeanPostProcessor.java:140) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildProxy(ReferenceAnnotationBeanPostProcessor.java:122) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:116) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:49) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.getInjectedObject(AnnotationInjectedBeanPostProcessor.java:340) ~[dubbo-2.7.0.jar:2.7.0]
          at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement.inject(AnnotationInjectedBeanPostProcessor.java:520) ~[dubbo-2.7.0.jar:2.7.0]
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
          at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.postProcessPropertyValues(AnnotationInjectedBeanPostProcessor.java:128) ~[dubbo-2.7.0.jar:2.7.0]

          References

          [1]?Spring Cloud Alibaba+Nacos的介紹與實戰(zhàn)(一):?https://cuizb.top/myblog/article/1647705645
          [2]?幾種常見的注冊中心以及區(qū)別:?https://cuizb.top/myblog/article/1646575927


          本文作者:Java技術(shù)債務

          原文鏈接:https://www.cuizb.top/myblog/article/1647833843

          版權(quán)聲明: 本博客所有文章除特別聲明外,均采用 CC BY 3.0 CN協(xié)議進行許可。轉(zhuǎn)載請署名作者且注明文章出處。

          Java技術(shù)債務


          4faf602541b6d374761262c19cf76c63.webp

          JVM內(nèi)存泄漏和內(nèi)存溢出的原因
          JVM常用監(jiān)控工具解釋以及使用

          Redis 常見面試題(一)

          ClickHouse之MaterializeMySQL引擎(十)

          三種實現(xiàn)分布式鎖的實現(xiàn)與區(qū)別

          線程池的理解以及使用


          號外!號外!最近面試BAT,整理一份面試資料,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。想獲取嗎?如果你想提升自己,并且想和優(yōu)秀的人一起進步,感興趣的朋友,可以在掃碼關注下方公眾號。資料在公眾號里靜靜的躺著呢。。。



          喜歡就分享
          認同就點贊

          支持就在看

          一鍵四連,你的offer也四連4bf3e459972c0ddda8ee79c2e419f716.webp


          瀏覽 68
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  人人摸人人搞人人操 | 免费黄色成人在线观看 | 亚洲一区二区视频 | 日本三级视频在线观看历史 | 夜夜撸天天干 |