本帖最后由 年强_HF 于 2018-12-20 13:31 编辑
SpringBoot系列之整合Dubbo1 前言各位大佬们,IT的技术发展是非常的快,如果再不更新自己的技能,那就要被淘汰了,所以黑马程序员是你不错的选择,本次教程我计划是已连载的方式记录。交给大家基于SpringBoot 2.0.1 Release 版本整合当前IT技术界流行的技术。本教程是基于SpringBoot 2.0.1 整合Dubbo,Dubbo的版本是2.6.2。 2 快速入门
2.1 开发环境准备开发工具:IDEA 2017.3 JDK:1.8 Dubbo 2.6.2、SpringBoot 2.0.1、Zookeeper 3.4.6等 2.2 工程结构介绍
boot_dubbo : 父工程 dubbo_api :接口工程 dubbo_provide :服务的提供方 dubbo_consumer:服务消的费方
2.3 编码实现本文默认已经搭建好zookeeper的环境,本文演示的zookeeper的环境是基于CentOS 6.5 搭建。 启动zookeeper服务:
IP:192.168.25.135, 端口:2181 2.3.1 父工程创建父工程
添加依赖 对Module子模块的依赖的管理和聚合 [XML] 纯文本查看 复制代码 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<dubbo-spring-boot-starter.version>0.2.0</dubbo-spring-boot-starter.version>
<springboot.version>2.0.1.RELEASE</springboot.version>
<fastjson-version>1.2.31</fastjson-version>
<zk-client.version>0.2</zk-client.version>
<dockerfile-maven.version>1.4.3</dockerfile-maven.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zk-client.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
2.3.2 接口工程2.3.3 服务提供方工程创建module模块,dubbo_provide, 方法同上 添加依赖 [XML] 纯文本查看 复制代码 <dependencies>
<!--本工程依赖-->
<dependency>
<groupId>com.itheima.dubbo</groupId>
<artifactId>dubbo_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> 编写application.yml 配置文件 [Bash shell] 纯文本查看 复制代码 dubbo:
application:
name: dubbo_provide
registry:
address: zookeeper://192.168.25.135:2181
# port: 2181
# protocol: zookeeper
# server: 192.168.25.135
scan:
base-packages: com.itheima.dubbo.service
server:
port: 8080 编写主引导类 ProvideApplication [Java] 纯文本查看 复制代码 @SpringBootApplication
@EnableDubbo // 开启dubbo的支持
@DubboComponentScan // 扫描dubbo相关注解的包
public class ProvideApplication {
public static void main(String[] args) {
SpringApplication.run(ProvideApplication.class, args);
}
} 编写实现类UserServiceImpl, 实现了UserService接口 [Java] 纯文本查看 复制代码 @Service
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "Hello Dubbo .... " + name;
}
} 注意: a. pom.xml依赖中必须添加dubbo_api 的依赖 b. @Service的注解一定是com.alibaba.dubbo.config.annotation.Service; 包下的注解
2.3.4 服务消费方工程创建module模块,dubbo_consumer, 方法同上 添加依赖 [XML] 纯文本查看 复制代码 <dependencies>
<dependency>
<groupId>com.itheima.dubbo</groupId>
<artifactId>dubbo_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> 编写application.yml 配置文件 [Bash shell] 纯文本查看 复制代码 dubbo:
application:
name: dubbo_consumer
registry:
address: zookeeper://192.168.25.135:2181
scan:
base-packages: com.itheima.dubbo.controller
server:
port: 80 编写主引导类 ConsumerApplication [Java] 纯文本查看 复制代码 @SpringBootApplication
@EnableDubbo // 开启dubbo的支持
@DubboComponentScan // 扫描dubbo相关注解的包
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
} 编写UserController [Java] 纯文本查看 复制代码 @RestController
@RequestMapping("user")
public class UserController {
@Reference
UserService userService;
@GetMapping("sayHello")
public String sayHello() {
String result = userService.sayHello("黑马程序员");
return result;
}
} 注意:使用 @Reference 远程注入userService接口
2.3.5 工程启动测试3 总结本教程主要是带着大家学习,基于SpringBoot2.X 版本整合Dubbo完成一个简单的案例, 希望大家可以通过这个教程学习使用SpringBoot整合第三方技术的一些方式方法。 一般总结为三大步骤: SpringBoot在企业开发中用的非常的多,希望各位学员能够对SpringBoot 有充分的认知,也期待后续给大家介绍SpringBoot相关的使用等高级的知识。
|