1. 程式人生 > 程式設計 >springboot2.2.2整合dubbo的實現方法

springboot2.2.2整合dubbo的實現方法

最近在學習dubbo,想著作一些筆記,從來沒有在csdn上面寫過部落格,今天獻出第一次,哈哈,直接上程式碼

一、建立父工程

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath/>
  </parent>

  <groupId>com.dubbo</groupId>
  <artifactId>demo01</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <description>Spring Boot2.x 整合 dubbo</description>

  <modules>
    <module>api</module>
    <module>provider</module>
    <module>consumer</module>
  </modules>

  <!--統一管理依賴版本-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <dubbo.version>2.7.5</dubbo.version>
    <curator.version>4.2.0</curator.version>
    <!-- 連線zookeeper的依賴,我的zookeeper版本是3.4.14,感覺這個jar版本和zookeeper是大概保持一致的,但是引入3.4.14會報錯,我試了下,從3.4.13開始就不行了 -->
    <zookeeper.version>3.4.12</zookeeper.version>
  </properties>

  <!--依賴定義-->
  <!--dependencyManagement 定義依賴版本號。子工程直接加依賴即可,不需要再次加版本號,便於統一維護版本號-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>${dubbo.version}</version>
      </dependency>
  <!-- zookeeper的api管理依賴 -->
      <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>${curator.version}</version>
      </dependency>
  <!-- zookeeper依賴 -->
      <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>${zookeeper.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <!-- 使用該依賴,idea需要安裝外掛,沒有用過的自行百度一下吧 -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>
</project>

二、建立提供者與消費者共用的api

該模組沒有什麼好說的,提供者和消費者都需要使用的介面api,提供者和消費者都需要引入該模組

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>demo01</artifactId>
    <groupId>com.dubbo</groupId>
    <version>1.0.0</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>api</artifactId>


</project>

// 註解都是lombok的,真的很方便
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class User implements Serializable {
  private Integer id;
  private String name;
  private Integer age;
}
public interface UserService {
  User getUserById(Integer id);
}

三、建立提供者


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0</modelVersion>
  <parent>
    <groupId>com.dubbo</groupId>
    <version>1.0.0</version>
    <artifactId>demo01</artifactId>
  </parent>

  <groupId>com.dubbo</groupId>
  <artifactId>provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>provider</name>
  <description>Demo project for Spring Boot</description>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-recipes</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
    </dependency>
    <!-- 匯入公共介面依賴 -->
    <dependency>
      <groupId>com.dubbo</groupId>
      <artifactId>api</artifactId>
      <version>1.0.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

dubbo:
 application:
  # 應用名稱
  name: user-provider
 protocol:
  # 協議名稱
  name: dubbo
  # 協議埠
  port: 20880
 registry:
  # 註冊中心地址
  address: zookeeper://192.168.104.231:2181

@SpringBootApplication
// 提供服務的應用必須配置此項
@DubboComponentScan("com.dubbo.provider.service")
public class ProviderApplication {

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

}

@Component
// 該service是org.apache.dubbo.config.annotation.Service
@Service
public class UserServiceImpl implements UserService {
  @Override
  public User getUserById(Integer id) {
    User user = User.builder()
        .id(id)
        .name("張三")
        .age(20 + id)
        .build();

    return user;
  }
}

四、建立消費者

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0</modelVersion>
  <parent>
    <groupId>com.dubbo</groupId>
    <version>1.0.0</version>
    <artifactId>demo01</artifactId>
  </parent>

  <groupId>com.dubbo</groupId>
  <artifactId>consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>consumer</name>
  <description>Demo project for Spring Boot</description>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-recipes</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
    </dependency>

    <!-- 匯入公共介面依賴 -->
    <dependency>
      <groupId>com.dubbo</groupId>
      <artifactId>api</artifactId>
      <version>1.0.0</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

# 埠
server:
 port: 8081
dubbo:
 application:
  name: user-consumer
 protocol:
  name: dubbo
  port: 20880
 registry:
  address: zookeeper://192.168.104.231:2181

@SpringBootApplication

public class ConsumerApplication {

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

}
@RestController
@RequestMapping("/user")
public class ConsumerController {
  @Reference
  private UserService userService;

  @GetMapping("/{id}")
  public User getUserById(@PathVariable int id) {
    return userService.getUserById(id);
  }
}

五、啟動並訪問

先啟動provider,啟動結果如下

啟動consumer,啟動結果如下

瀏覽器訪問:http://localhost:8081/user/4

好了,到此就完成了最基本的springboot與dubbo的整合,更多的dubbo的api請查閱 dubbo官方文件

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。