springboot整合dubbo的简单案例
使用框架:
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的服务接口开发完成。
springboot整合dubbo的简单案例的更多相关文章
- springboot搭建dubbo+zookeeper简单案例
背景:只是自己使用单机版zookeeper搭建dubbo的一个学习案例,记录成功的过程 1.搭建zookeeper坏境 使用docker来构建环境 1.1 拉取镜像:docker pull zooke ...
- spring-boot整合Dubbo分布式架构案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.项目文件目录 3.Maven Plugin管理 总项目 pom.xml配置代码: &l ...
- Springboot整合Dubbo和Zookeeper
Dubbo是一款由阿里巴巴开发的远程服务调用框架(RPC),其可以透明化的调用远程服务,就像调用本地服务一样简单.截至目前,Dubbo发布了基于Spring Boot构建的版本,版本号为0.2.0,这 ...
- springboot整合dubbo\zookeeper做注册中心
springboot整合dubbo发布服务,zookeeper做注册中心.前期的安装zookeeper以及启动zookeeper集群就不说了. dubbo-admin-2.5.4.war:dubbo服 ...
- springBoot整合MyBatise及简单应用
springBoot整合MyBatise及简单应用 我采用的是 工具IDEA 框架是springBoot+maven+Mybatise 第一步: pom.xml 引入相关jar包 <?xml v ...
- 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...
- SpringBoot整合dubbo(yml格式配置)
yml文件 如果只作为服务的消费者不用暴露端口号,扫描的包名根据自己service改 dubbo: application: name: springboot-dubbo-demo #应用名 regi ...
- 从无到有Springboot整合Spring-data-jpa实现简单应用
本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...
- dubbo学习实践(4)之Springboot整合Dubbo及Hystrix服务熔断降级
1. springboot整合dubbo 在provider端,添加maven引入,修改pom.xml文件 引入springboot,版本:2.3.2.RELEASE,dubbo(org.apache ...
随机推荐
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- PHP 操作数据库乱码 以及调试
mysql> show create database pxscj;+----------+--------------------------------------------------- ...
- request获取请求参数
/** * 方式1 */ Enumeration<?> enu=request.getParameterNames(); while(enu.hasMoreElements()){ Str ...
- HTML城市联动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- sublime 自定义快捷键
[ { "keys": ["alt+space"], "command": "auto_complete" }, // ...
- Debug技巧(1)
首先声明,以下有些是自己遇到的问题自己解决了,其它里面包括了网上看到的Debug经验和书里看到的经验,时间问题就不一一说明,如有侵权,私信我进行删除,我会道歉.我写这个的本意是记录我学习中遇到的问题, ...
- UVM基础之-------uvm factory机制override<博>
override功能是UVM中一个比较重要的功能,这个功能也是在factory里面实现的,我们会在env或者具体的case中使用override功能. class case_x extends bas ...
- [Windows Server 2012] MySQL更改数据库引擎(MyISAM改为INNODB)
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:更改MyS ...
- 【OpenCV】像素操作的数字图像处理
之前几天捣鼓matlab,用来处理数字图像,矩阵操作什么的,如果忘记线性代数就真的GG了. 在用了matlab被深深地吐槽之后,决定改用opencv,C++貌似也是处理数字图像的很好的工具 1. 在u ...
- IOS7 状态栏和 Navigation Bar重叠的问题解决
一 Status bar重叠问题: 方法一:隐藏Status bar 在plist里面增加2个变量 Status bar is initially hidden -> YES Vie ...