各位大佬们,IT的技术发展是非常的快,如果再不更新自己的技能,那就要被淘汰了,所以黑马程序员是你不错的选择,本次教程我计划是已连载的方式记录。交给大家基于SpringBoot 2.0.1 Release 版本整合当前IT技术界流行的技术。本教程是基于SpringBoot 2.0.1 整合Dubbo,Dubbo的版本是2.6.2。
2 快速入门开发工具: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:服务消的费方
本文默认已经搭建好zookeeper的环境,本文演示的zookeeper的环境是基于CentOS 6.5 搭建。
启动zookeeper服务:
IP:192.168.25.135, 端口:2181
2.3.1 父工程<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>
public interface UserService {
public String sayHello(String name);
}
<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>
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
@SpringBootApplication
@EnableDubbo // 开启dubbo的支持
@DubboComponentScan // 扫描dubbo相关注解的包
public class ProvideApplication {
public static void main(String[] args) {
SpringApplication.run(ProvideApplication.class, args);
}
}
@Service
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "Hello Dubbo .... " + name;
}
}
<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>
dubbo:
application:
name: dubbo_consumer
registry:
address: zookeeper://192.168.25.135:2181
scan:
base-packages: com.itheima.dubbo.controller
server:
port: 80
@SpringBootApplication
@EnableDubbo // 开启dubbo的支持
@DubboComponentScan // 扫描dubbo相关注解的包
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
@RestController
@RequestMapping("user")
public class UserController {
@Reference
UserService userService;
@GetMapping("sayHello")
public String sayHello() {
String result = userService.sayHello("黑马程序员");
return result;
}
}
本教程主要是带着大家学习,基于SpringBoot2.X 版本整合Dubbo完成一个简单的案例, 希望大家可以通过这个教程学习使用SpringBoot整合第三方技术的一些方式方法。
一般总结为三大步骤:
SpringBoot在企业开发中用的非常的多,希望各位学员能够对SpringBoot 有充分的认知,也期待后续给大家介绍SpringBoot相关的使用等高级的知识。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |