如何進(jìn)行springboot2.2.2集成dubbo的實(shí)現(xiàn)

本篇文章為大家展示了如何進(jìn)行springboot2.2.2集成dubbo的實(shí)現(xiàn),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

我們提供的服務(wù)有:網(wǎng)站建設(shè)、網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、海淀ssl等。為成百上千企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的海淀網(wǎng)站制作公司

最近在學(xué)習(xí)dubbo,想著作一些筆記,從來(lái)沒(méi)有在csdn上面寫過(guò)博客,今天獻(xiàn)出第一次,哈哈,直接上代碼

一、創(chuàng)建父工程

<?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>  <!--統(tǒng)一管理依賴版本-->  <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,感覺(jué)這個(gè)jar版本和zookeeper是大概保持一致的,但是引入3.4.14會(huì)報(bào)錯(cuò),我試了下,從3.4.13開(kāi)始就不行了 -->    <zookeeper.version>3.4.12</zookeeper.version>  </properties>  <!--依賴定義-->  <!--dependencyManagement 定義依賴版本號(hào)。子工程直接加依賴即可,不需要再次加版本號(hào),便于統(tǒng)一維護(hù)版本號(hào)-->  <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需要安裝插件,沒(méi)有用過(guò)的自行百度一下吧 -->    <dependency>      <groupId>org.projectlombok</groupId>      <artifactId>lombok</artifactId>      <optional>true</optional>    </dependency>  </dependencies></project>

二、創(chuàng)建提供者與消費(fèi)者共用的api

該模塊沒(méi)有什么好說(shuō)的,提供者和消費(fèi)者都需要使用的接口api,提供者和消費(fèi)者都需要引入該模塊

<?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);}

三、創(chuàng)建提供者

<?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>    <!-- 導(dǎo)入公共接口依賴 -->    <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:  # 應(yīng)用名稱  name: user-provider protocol:  # 協(xié)議名稱  name: dubbo  # 協(xié)議端口  port: 20880 registry:  # 注冊(cè)中心地址  address: zookeeper://192.168.104.231:2181

@SpringBootApplication// 提供服務(wù)的應(yīng)用必須配置此項(xiàng)@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@Servicepublic class UserServiceImpl implements UserService {  @Override  public User getUserById(Integer id) {    User user = User.builder()        .id(id)        .name("張三")        .age(20 + id)        .build();    return user;  }}

四、創(chuàng)建消費(fèi)者

<?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>    <!-- 導(dǎo)入公共接口依賴 -->    <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: 8081dubbo: application:  name: user-consumer protocol:  name: dubbo  port: 20880 registry:  address: zookeeper://192.168.104.231:2181

@SpringBootApplicationpublic 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);  }}

五、啟動(dòng)并訪問(wèn)

啟動(dòng)provider

啟動(dòng)consumer

瀏覽器訪問(wèn):http://localhost:8081/user/4

上述內(nèi)容就是如何進(jìn)行springboot2.2.2集成dubbo的實(shí)現(xiàn),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:如何進(jìn)行springboot2.2.2集成dubbo的實(shí)現(xiàn)
本文來(lái)源:http://bm7419.com/article6/igcjog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)App設(shè)計(jì)、微信公眾號(hào)做網(wǎng)站、用戶體驗(yàn)外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化