使用框架:
- jdk 1.8
- springboot-2.1.3
- dubbo-2.6
- spring-data-jpa-2.1.5
一, 开发 dubbo 服务接口:
按照 Dubbo 官方开发建议, 创建一个接口项目, 该项目只定义接口和 model 类;
1, 创建 springboot 工程 spring-boot-demo-dubbo-interface
坐标:
- <groupId>
- com.example
- </groupId>
- <artifactId>
- spring-boot-demo-dubbo-interface
- </artifactId>
- <version>
- 0.0.1-SNAPSHOT
- </version>
添加 spring-data-jpa 依赖:
- <dependency>
- <groupId>
- org.springframework.boot
- </groupId>
- <artifactId>
- spring-boot-starter-data-jpa
- </artifactId>
- </dependency>
2, 创建 model
- package com.example.demo.model;
- @Entity
- public class User implements Serializable{
- private static final long serialVersionUID = 1L;
- @Id @GeneratedValue
- private long id;
- private String userName;
- private String password;
- private int age;
- public long getId() {
- return id;
- }
- // 省略 set get 方法
3, 创建接口:
- package com.example.demo.service;
- import com.example.demo.model.User;
- public interface UserService {
- public void save(User user);
- public String sayHello(String Word);
- }
4, 使用命令 clean install 打包安装到 maven 仓库.
阿里巴巴提供的 dubbo 集成 springboot 开源项目;
参考文档:
https://github.com/apache/dubbo-spring-boot-project/blob/0.2.x/README_CN.md
本工程采用该项目的 jar 包进行继承:
- <dependency>
- <groupId>
- com.alibaba.boot
- </groupId>
- <artifactId>
- dubbo-spring-boot-starter
- </artifactId>
- <version>
- 0.2.0
- </version>
- </dependency>
二, 开发 dubbo 服务提供者:
1, 创建一个 Springboot 项目 spring-boot-demo-dubbo-provider 并配置好相关的依赖;
pom.xml
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- 加入 springboot 与 dubbo 集成的起步依赖 -->
- <dependency>
- <groupId>com.alibaba.boot</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>0.2.0</version>
- </dependency>
- <!-- 由于使用了 zookeeper 作为注册中心, 则需要加入 zookeeper 的客户端 jar 包: -->
- <dependency>
- <groupId>com.101tec</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.10</version>
- </dependency>
- <!-- spring-data-jpa -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!-- 添加接口服务 -->
- <dependency>
- <groupId>com.example</groupId>
- <artifactId>spring-boot-demo-dubbo-interface</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </dependency>
- </dependencies>
2, 在 Springboot 的核心配置文件 application.properties 中配置 dubbo 的信息:
- spring.datasource.driver-class-name=com.MySQL.cj.jdbc.Driver
- spring.datasource.url=jdbc:MySQL://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
- spring.datasource.username=root
- spring.datasource.password=root
- spring.jpa.properties.hibernate.hbm2ddl.auto=create
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
- spring.jpa.properties.hibernate.format_sql=true
- spring.jpa.show-sql=true
- # 访问端口
- server.port=8080
- # dubbo 配置
- dubbo.application.name=springboot-dubbo-provider
- dubbo.registry.address=zookeeper://192.168.146.128:2181
3, 开发编写 Dubbo 的接口实现类:
- package com.example.demo.service;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import com.alibaba.dubbo.config.annotation.Service;
- import com.example.demo.model.User;
- import com.example.demo.repository.UserRepository;
- @Component // 注册为 spring bean
- @Service // 这注解是 dubbo 提供的
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserRepository userRepository;
- @Override
- public void save(User user) {
- userRepository.save(user);
- }
- @Override
- public String sayHello(String Word) {
- return Word;
- }
- }
4, 入口 main 程序启动 Dubbo 服务提供者: 添加注解 @EnableDubbo
- package com.example.demo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
- @SpringBootApplication
- @EnableDubbo
- public class SpringBootDemoDubboProviderApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringBootDemoDubboProviderApplication.class, args);
- }
- }
启动 main , 服务发布到 zookeeper 注册中心.
三, 开发 dubbo 服务消费者:
1, 创建一个 Springboot 项目 spring-boot-demo-dubbo-consumer 并配置好相关的依赖;
2, 加入 springboot 与 dubbo 集成的起步依赖:(pom.xml 配置同上)
** 注意: 服务提供者 和 消费者都要配置 服务接口依赖 **
3, 在 Springboot 的核心配置文件 application.properties 中配置 dubbo 的信息:
- spring.datasource.driver-class-name=com.MySQL.cj.jdbc.Driver
- spring.datasource.url=jdbc:MySQL://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
- spring.datasource.username=root
- spring.datasource.password=root
- # Web\u670D\u52A1\u7AEF\u53E3
- server.port=8081
- # dubbo\u914D\u7F6E
- dubbo.application.name=springboot-dubbo-consumer
- dubbo.registry.address=zookeeper://192.168.146.128:2181
4, 编写一个 Controller 类, 调用远程的 Dubbo 服务:
- package com.example.demo.controller;
- import org.springframework.Web.bind.annotation.RequestMapping;
- import org.springframework.Web.bind.annotation.RestController;
- import com.alibaba.dubbo.config.annotation.Reference;
- import com.example.demo.model.User;
- import com.example.demo.service.UserService;
- @RestController
- public class UserController {
- @Reference // 该注解是 dubbo 提供的
- private UserService userService;
- @RequestMapping("/say")
- public String sayHello(String name) {
- return userService.sayHello(name);
- }
- @RequestMapping("/save")
- public void save() {
- User u = new User();
- u.setAge(20);
- u.setPassword("123");
- u.setUserName("zheng");
- userService.save(u);
- }
- }
5, 启动类添加 开启 dubbo 注解 @EnableDubbo
- package com.example.demo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
- @SpringBootApplication
- @EnableDubbo
- public class SpringBootDemoDubboConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringBootDemoDubboConsumerApplication.class, args);
- }
- }
6, 启动 main 方法.
7, 调用远程接口:
http://localhost:8081/say?name=hello
一个简单的 springboot 基于 dubbo 的服务接口开发完成.
来源: http://www.jianshu.com/p/f7e1b397ce0c