Spring boot 2.1.9 + Dubbo 2.7.3 + Nacos 1.1.4 構建微服務系統

Dong滴個Dong發表於2020-03-04

Spring boot 2.1.9 + Dubbo 2.7.3 + Nacos 1.1.4 構建微服務系統

  • Spring boot 2.1.9 + Dubbo 2.7.3 + Nacos 1.1.4 構建微服務系統 , mydemo

    • 下載最新版本的 Nacos Server , 具體參見下面 註冊中心部分

      #啟動 Nacos 服務
      sh startup.sh -m standalone
      
    • parent pom

          <modelVersion>4.0.0</modelVersion>
          <packaging>pom</packaging>
          <modules>
              <module>dubbo-api</module>
              <module>dubbo-provider</module>
              <module>dubbo-consumer</module>
          </modules>
          <groupId>com.example</groupId>
          <artifactId>dubbo-demo</artifactId>
          <version>1.0-SNAPSHOT</version>
      
          <properties>
              <spring-boot.version>2.1.9.RELEASE</spring-boot.version>
              <dubbo.version>2.7.3</dubbo.version>
              <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
              <java.version>1.8</java.version>
              <maven.compiler.source>1.8</maven.compiler.source>
              <maven.compiler.target>1.8</maven.compiler.target>
          </properties>
      
          <dependencyManagement>
              <dependencies>
                  <!--SpringBoot-->
                  <dependency>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-dependencies</artifactId>
                      <version>${spring-boot.version}</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
                  <!-- Apache Dubbo  -->
                  <dependency>
                      <groupId>org.apache.dubbo</groupId>
                      <artifactId>dubbo-dependencies-bom</artifactId>
                      <version>${dubbo.version}</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
                  <!-- Dubbo Spring Boot Starter -->
                  <dependency>
                      <groupId>org.apache.dubbo</groupId>
                      <artifactId>dubbo-spring-boot-starter</artifactId>
                      <version>${dubbo.version}</version>
                  </dependency>
                  <!-- 下面這個依賴可以不需要,預設是 dubbo 是 2.7.3 -->
                  <dependency>
                      <groupId>org.apache.dubbo</groupId>
                      <artifactId>dubbo</artifactId>
                      <version>${dubbo.version}</version>
                  </dependency>
                  <!-- Dubbo Registry Nacos -->
                  <dependency>
                      <groupId>org.apache.dubbo</groupId>
                      <artifactId>dubbo-registry-nacos</artifactId>
                      <version>${dubbo.version}</version>
                  </dependency>
                  <dependency>
                      <groupId>com.alibaba.boot</groupId>
                      <artifactId>nacos-config-spring-boot-starter</artifactId>
                      <version>${nacos-config-spring-boot.version}</version>
                  </dependency>
                  <!-- 如果沒有使用nacos配置中心,沒有新增 nacos-config-spring-boot-starter 依賴,則需要新增下面這個依賴 -->
                  <dependency>
                      <groupId>com.alibaba.nacos</groupId>
                      <artifactId>nacos-client</artifactId>
                      <version>1.1.4</version>
                  </dependency>
      
                  <!-- Interface -->
                  <dependency>
                      <groupId>com.example</groupId>
                      <artifactId>dubbo-api</artifactId>
                      <version>1.0-SNAPSHOT</version>
                  </dependency>
              </dependencies>
          </dependencyManagement>
      
          <build>
              <!-- 必須要,否則通過 mvn clean install -Dmaven.test.skip=true 無法執行,提示錯誤:jar中沒有主清單屬性 -->
              <pluginManagement>
                  <plugins>
                      <plugin>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-maven-plugin</artifactId>
                          <version>${spring-boot.version}</version>
                          <executions>
                              <execution>
                                  <goals>
                                      <goal>repackage</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
                  </plugins>
              </pluginManagement>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.8.0</version>
                      <configuration>
                          <source>${maven.compiler.source}</source>
                          <target>${maven.compiler.target}</target>
                      </configuration>
                  </plugin>
              </plugins>
        </build>
      
    • 擴充套件:在 parent pom 檔案中為什麼需要定義 <build> 標籤?

      • 原因是:parent pom 檔案中沒有繼承 spring-boot-starter-parent pom 檔案,即沒有下面的配置

          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.1.9.RELEASE</version>
          </parent>
        
      • 如果沒有自定義上面的 <build> 標籤,則通過 mvn 打包的程式無法執行,提升錯誤:jar中沒有主清單屬性,根本原因是jar包中缺少main-class。檢視mvn倉庫關於spring-boot-starter-parent pom 檔案的內容,發現已經定於了 <build> 標籤,把相應的定義copy 過來,就解決問題了。

      • 為什麼不能繼承 spring-boot-starter-parent pom 檔案呢? 原因是:interface / util 專案不需要繼承,只有真正需要執行的程式(無論是web 還是 console 程式,即有main函式的專案),才需要繼承 spring-boot-starter-parent pom 檔案

    • dubbo common , provider 和 consumer 共享模組

      <parent>
          <artifactId>dubbo-demo</artifactId>
          <groupId>com.example</groupId>
          <version>1.0-SNAPSHOT</version>
      </parent>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.jianzh5</groupId>
      <artifactId>dubbo-common</artifactId>
      <packaging>jar</packaging>
      
      // 定義介面
      public interface HelloService {
          String sayHello();
      }
      
    • dubbo provider : 注意,不需要 spring-boot-starter-web 依賴,它不是 web api專案,不對外提供服務

        <parent>
              <artifactId>dubbo-demo</artifactId>
              <groupId>com.example</groupId>
              <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>dubbo-provider</artifactId>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
              <!-- Dubbo -->
              <dependency>
                  <groupId>org.apache.dubbo</groupId>
                  <artifactId>dubbo-spring-boot-starter</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.apache.dubbo</groupId>
                  <artifactId>dubbo</artifactId>
              </dependency>
              <!-- Dubbo Registry Nacos -->
              <dependency>
                  <groupId>org.apache.dubbo</groupId>
                  <artifactId>dubbo-registry-nacos</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.alibaba.nacos</groupId>
                  <artifactId>nacos-client</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.example</groupId>
                  <artifactId>dubbo-api</artifactId>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      
      #配置檔案
      dubbo.scan.base-packages=com.example.demo.user.service
      dubbo.application.name = dubbo-provider
      dubbo.registry.address = nacos://192.168.136.129:8848
      dubbo.protocol.port=20881
      dubbo.protocol.name=dubbo
      
      // 介面實現
      @Service
      public class HelloServiceImpl implements HelloService {
          public String sayHello() {
              Return "welcome to WeChat public address: JAVA RI Zhi Lu".
          }
      }
      
    • @EnableDubbo 註解的作用 :通過 @EnableDubbo 可以在指定的包名下(通過 scanBasePackages),或者指定的類中(通過 scanBasePackageClasses)掃描 Dubbo 的服務提供者(以 @Service 標註)以及 Dubbo 的服務消費者(以 Reference 標註),它是 @EnableDubboConfig@DubboComponentScan兩者組合的便捷表達方式。

      @SpringBootApplication
      @EnableDubbo
      public class PrivoderApplication {
          public static void main(String[] args) {
              SpringApplication.run(PrivoderBootstrap.class, args);
          }
      }
      
    • dubbo consumer : 注意,需要 spring-boot-starter-web 依賴,因為它是web api 專案,讓外部訪問

        <parent>
              <artifactId>dubbo-demo</artifactId>
              <groupId>com.example</groupId>
              <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>dubbo-consumer</artifactId>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <!-- Dubbo -->
              <dependency>
                  <groupId>org.apache.dubbo</groupId>
                  <artifactId>dubbo-spring-boot-starter</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.apache.dubbo</groupId>
                  <artifactId>dubbo</artifactId>
              </dependency>
              <!-- Dubbo Registry Nacos -->
              <dependency>
                  <groupId>org.apache.dubbo</groupId>
                  <artifactId>dubbo-registry-nacos</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.alibaba.nacos</groupId>
                  <artifactId>nacos-client</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.example</groupId>
                  <artifactId>dubbo-api</artifactId>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      
      #配置檔案
      dubbo.scan.base-packages=com.example.demo.consumer
      spring.application.name=dubbo-consumer
      dubbo.registry.address = nacos://192.168.136.129:8848
      server.port=9090
      
      @RestController
      public class HelloController {
          @Reference
          private HelloService helloService;
      
          @GetMapping("/sayHello")
          public String sayHello(){
              return helloService.sayHello();
          }
      }
      
      // 測試結果:如果沒有 @EnableDubbo 註解,也是可以 run 的,但是如果provider沒有此註解,就無法run,不知道為什麼?
      // 為了和官方文件一致,還是加上吧
      @EnableDubbo
      @SpringBootApplication
      public class ConsumerApplication {
          public static void main(String[] args) {
              SpringApplication.run(ConsumerBootstrap.class, args);
          }
      }
      

相關文章