欢迎访问优讯网!
您当前的位置:首页 > 爱编程

springboot2.2.2集成dubbo的实现方法

时间:2020-01-21 08:24:07  来源:优讯网  作者:小卡司  浏览次数:
这篇文章主要介绍了springboot2.2.2集成dubbo的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

最近在学习dubbo,想着作一些笔记,从来没有在csdn上面写过博客,今天献出第一次,哈哈,直接上代码

一、创建父工程

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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,提供者和消费者都需要引入该模块

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>

1
2
3
4
5
6
7
8
9
10
// 注解都是lombok的,真的很方便
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class User implements Serializable {
  private Integer id;
  private String name;
  private Integer age;
}
 
1
2
3
public interface UserService {
  User getUserById(Integer id);
}

三、创建提供者

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?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>

1
2
3
4
5
6
7
8
9
10
11
12
dubbo:
 application:
  # 应用名称
  name: user-provider
 protocol:
  # 协议名称
  name: dubbo
  # 协议端口
  port: 20880
 registry:
  # 注册中心地址
  address: zookeeper://192.168.104.231:2181

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
// 提供服务的应用必须配置此项
@DubboComponentScan("com.dubbo.provider.service")
public class ProviderApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(ProviderApplication.class, args);
  }
 
}
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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;
  }
}

四、创建消费者

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?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>

1
2
3
4
5
6
7
8
9
10
11
# 端口
server:
 port: 8081
dubbo:
 application:
  name: user-consumer
 protocol:
  name: dubbo
  port: 20880
 registry:
  address: zookeeper://192.168.104.231:2181

1
2
3
4
5
6
7
8
9
@SpringBootApplication
 
public class ConsumerApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
  }
 
}

1
2
3
4
5
6
7
8
9
10
11
@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官方文档

以上就是本文的全部内容,希望对大家的学习有所帮助

来顶一下
返回首页
返回首页

原文链接:https://www.jb51.net/article/178946.htm


推荐资讯
如何下载旧版centos iso镜像 如何下载迷你mini版的centos镜像
如何下载旧版centos i
计算机的正确使用姿势 电脑痴如何正确的使用电脑
计算机的正确使用姿势
好用的后台管理的前端框架模版H-ui H-ui框架模版分享
好用的后台管理的前端
微信电脑多开方法 无需辅助电脑版微信双开方法分享
微信电脑多开方法 无
相关文章
栏目更新
栏目热门