使用框架:
-
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
坐标:
com.example spring-boot-demo-dubbo-interface 0.0.1-SNAPSHOT 复制代码
添加spring-data-jpa 依赖:
复制代码 org.springframework.boot spring-boot-starter-data-jpa
2、创建model
package com.example.demo.model;@Entitypublic 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开源项目;
参考文档:
本工程采用该项目的jar包进行继承:
复制代码 com.alibaba.boot dubbo-spring-boot-starter 0.2.0
二、开发dubbo服务提供者:
1、创建一个Springboot项目spring-boot-demo-dubbo-provider并配置好相关的依赖;
pom.xml
复制代码 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.alibaba.boot dubbo-spring-boot-starter 0.2.0 com.101tec zkclient 0.10 org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java runtime com.example spring-boot-demo-dubbo-interface 0.0.1-SNAPSHOT
2、在Springboot的核心配置文件application.properties中配置dubbo的信息:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=rootspring.jpa.properties.hibernate.hbm2ddl.auto=createspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialectspring.jpa.properties.hibernate.format_sql=truespring.jpa.show-sql=true# 访问端口server.port=8080# dubbo配置dubbo.application.name=springboot-dubbo-providerdubbo.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@EnableDubbopublic 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.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=root# WEB\u670D\u52A1\u7AEF\u53E3server.port=8081# dubbo\u914D\u7F6Edubbo.application.name=springboot-dubbo-consumerdubbo.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;@RestControllerpublic 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@EnableDubbopublic class SpringBootDemoDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoDubboConsumerApplication.class, args); }}复制代码
6、启动main方法。
7、调用远程接口:
一个SpringBoot基于Dubbo的服务接口开发完成。