微服務框架-dubbo整合nacos框架

石頭城程式猿發表於2020-08-09

一、前言:

       dubbo框架,註冊中心的技術選型常見有redis,zookeeper ,本篇文章介紹如何整理當前比較火熱的nacos框架。

      nacos框架安裝,可以參考:https://mp.csdn.net/console/editor/html/107902058

 

二、框架整合:

     1) 服務端改造:

      使用nacos

      <dubbo:registry  address="nacos://192.168.213.102:8848" timeout="10000"/>

<?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/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-server"/>

    <!--dubbo:registry  address="zookeeper://192.168.213.102:2181" timeout="10000"/-->
    <dubbo:registry  address="nacos://192.168.213.102:8848" timeout="10000"/>

    <dubbo:protocol name="dubbo"  port="20880"/>
    <!--dubbo:registry address="N/A"/--><!-- 如果不用註冊中心,可以這樣設定。無需註冊到服務註冊中心-->
    <dubbo:service interface="org.example.ILoginService"  ref="loginService"/>
    <bean id="loginService"  class="org.example.LoginServiceImpl"/>

</beans>

  pom.xml檔案,增加依賴的檔案:

<dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.7.8</version>
    </dependency>
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-dependencies-zookeeper</artifactId>
      <version>2.7.8</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>com.alibaba.nacos</groupId>
      <artifactId>nacos-client</artifactId>
      <version>1.2.1</version>
    </dependency>

2)客戶端改造:

<?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/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-server"/>

    <!--dubbo:registry address="N/A"/--><!-- 無需註冊到服務註冊中心-->
    <!--dubbo:registry  address="zookeeper://192.168.213.102:2181" timeout="10000"/-->
    <dubbo:registry  address="nacos://192.168.213.102:8848" timeout="10000"/>

    <!--dubbo:reference id="loginService" interface="org.example.ILoginService" url="dubbo://192.168.117.1:20880/org.example.ILoginService"/-->
    <!--使用註冊中心後,就可以不用配置URL,預設跟interface去註冊中心尋找-->
    <dubbo:reference id="loginService" interface="org.example.ILoginService"/>

</beans>

  pom.xml,增加依賴的檔案:

<dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.2.1</version>
        </dependency>

 

三、nacos平臺,檢視註冊的服務資訊:

服務列表

服務詳情:

 

相關文章