Dubbo中文官网:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
Github上 Apache Dubbo 最新版本(未打包): https://github.com/apache/dubbo-spring-boot-project
Github(已经打成stater): https://github.com/alibaba/dubbo-spring-boot-starter
ZooKeeper下载地址 http://www.apache.org/dyn/closer.cgi/zookeeper
运行环境:JDK8、Maven 3.3.9、MySql 5.5.15、IntelliJ IDEA 2019.1.1
技术栈:SpringBoot 2.1.1、Dubbo 2.7.1、ZooKeeper 3.4.14、MyBatis 2+
1.创建三个工程
1.1.创建普遍的Maven Java工程(comm-api),用于存放实体类,API:
1.1.1 UserService 接口
package org.my.dubbo.api;
import org.my.dubbo.entity.UserEntity;
import java.util.List;
public interface UserService {
/**
* 获取所有用户列表
* @return
*/
List findAll();
/**
* 工具登录名,密码获取用户
* @param u_login_id
* @param u_password
* @return
*/
UserEntity getUserById(String u_login_id, String u_password);
}
1.1.2 UserEntity 实体类
package org.my.dubbo.entity;
import java.io.Serializable;
public class UserEntity implements Serializable {
public String id;
public String u_name;
public String u_login_id;
public String u_password;
public String u_age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public String getU_login_id() {
return u_login_id;
}
public void setU_login_id(String u_login_id) {
this.u_login_id = u_login_id;
}
public String getU_password() {
return u_password;
}
public void setU_password(String u_password) {
this.u_password = u_password;
}
public String getU_age() {
return u_age;
}
public void setU_age(String u_age) {
this.u_age = u_age;
}
}
1.2.创建Spring Boot Web 工程(dobbo-registry-zookeeper-provider),用于提供服务:
1.2.1 dobbo-registry-zookeeper-provider工程的pom文件
4.0.0
1.0-SNAPSHOT
dubbo.registry.zookeeper.provider.sample
dubbo.registry.zookeeper.provider.sample
dubbo.registry.zookeeper.provider
UTF-8
1.7
1.7
2.1.1.RELEASE
2.7.1
5.1.39
2.0.0
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.dubbo
dubbo-dependencies-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo
${dubbo.version}
org.springframework
spring
javax.servlet
servlet-api
log4j
log4j
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
pom
org.my.dubbo.api
org.my.dubbo.api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-test
test
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.spring-boot}
mysql
mysql-connector-java
${mysql.connector}
com.alibaba
fastjson
1.2.30
1.2.2 dobbo-registry-zookeeper-provider工程的application.properties文件
server.port=8888
spring.application.name=dubbo-registry-provider
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot_project?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=org.my.dubbo.entity
mybatis.mapperLocations=classpath:mapper/*.xml
##dubbo 配置
spring.dubbo.application.name=provider
dubbo.scan.base-packages=com.my.dubbo.providerm.serviceimpl
#通信规则
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
## Dubbo Registry
#dubbo.registry.address=N/A
#注册中心 协议 和地址
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
service.api.version=1.0.0
1.2.3 dobbo-registry-zookeeper-provider工程的启动入口App.class
package com.my.dubbo.providerm;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
/**
* P丶少 2019/06/16
*/
@MapperScan(basePackages="com.my.dubbo.providerm.dao")//扫描映射接口包
@SpringBootApplication
//@EnableAutoConfiguration
public class App {
/**
* 配置FastJson
* @return HttpMessageConverters
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
public static void main( String[] args ) {
SpringApplication.run(App.class,args);
}
}
1.2.4 dobbo-registry-zookeeper-provider工程的UserController .class
package com.my.dubbo.providerm.controller;
import org.my.dubbo.api.UserService;
import org.my.dubbo.entity.UserEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("user")
public class UserController {
@Resource
private UserService userService;
/**
* 获取所有用户列表
* @return
*/
@RequestMapping(value = "findAll", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
public List findAll(){
// Map map =JSON.parseObject( JSON.toJSONString(obj),Map.class);
return userService.findAll();
}
/**
* 工具登录名,密码获取用户
* @param u_login_id
* @param u_password
* @return
*/
@RequestMapping(value = "getUser", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
public UserEntity getUser(@RequestParam String u_login_id,@RequestParam String u_password){
UserEntity userEntity = userService.getUserById(u_login_id,u_password);
return userEntity;
}
}
1.2.5 dobbo-registry-zookeeper-provider工程的UserDao .class
package com.my.dubbo.providerm.dao;
import org.my.dubbo.entity.UserEntity;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserDao {
/**
* 获取所有用户列表
* @return
*/
List findAll();
/**
* 工具登录名,密码获取用户
* @param u_login_id
* @param u_password
* @return
*/
UserEntity getUserById(String u_login_id,String u_password);
}
1.2.6 dobbo-registry-zookeeper-provider工程的UserServiceImpl .class
package com.my.dubbo.providerm.serviceimpl;
import com.my.dubbo.providerm.dao.UserDao;
import org.apache.dubbo.config.annotation.Service;
import org.my.dubbo.api.UserService;
import org.my.dubbo.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service(version = "${service.api.version}")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
/**
* 获取所有用户列表
* @return
*/
@Override
public List findAll() {
return userDao.findAll();
}
/**
* 工具登录名,密码获取用户
* @param u_login_id
* @param u_password
* @return
*/
@Override
public UserEntity getUserById(String u_login_id, String u_password) {
return userDao.getUserById(u_login_id,u_password);
}
}
1.2.7 dobbo-registry-zookeeper-provider工程的UserMapper.xml
id, u_name, u_login_id, u_password ,u_age
1.3.创建Spring Boot Web 工程(dobbo-registry-zookeeper-consumer),用于消费服务:
1.3.1 dobbo-registry-zookeeper-consumer工程的pom文件
dubbo.registry.zookeeper.consumer.sample
dubbo.registry.zookeeper.consumer.sample
1.0-SNAPSHOT
4.0.0
dubbo.registry.zookeeper.consumer
UTF-8
1.7
1.7
2.1.1.RELEASE
2.7.1
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.dubbo
dubbo-dependencies-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo
${dubbo.version}
org.springframework
spring
javax.servlet
servlet-api
log4j
log4j
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo
org.apache.dubbo
dubbo-dependencies-zookeeper
${dubbo.version}
pom
org.my.dubbo.api
org.my.dubbo.api
1.0-SNAPSHOT
com.alibaba
fastjson
1.2.30
apache.snapshots.https
Apache Development Snapshot Repository
https://repository.apache.org/content/repositories/snapshots
false
true
1.3.2 dobbo-registry-zookeeper-consumer工程的application.properties文件
server.port=8083
spring.application.name=dubbo-registry-consumer-1.0
#注册中心 协议 和地址
spring.dubbo.application.name=consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
service.api.version=1.0.0
1.3.3 dobbo-registry-zookeeper-consumer工程的启动入口App.class
package com.my.dubbo.consumer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
/**
* P丶少 2019/06/16
*/
//@EnableAutoConfiguration
@SpringBootApplication
public class App{
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* 配置FastJson
* @return HttpMessageConverters
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1.定义一个converters转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastjson的配置信息,比如: 是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3.在converter中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将converter赋值给HttpMessageConverter
HttpMessageConverter> converter = fastConverter;
// 5.返回HttpMessageConverters对象
return new HttpMessageConverters(converter);
}
public static void main( String[] args ) {
SpringApplication.run(App.class,args);
}
}
1.3.4 dobbo-registry-zookeeper-consumer工程的UserController .class
package com.my.dubbo.consumer.controller;
import org.apache.dubbo.config.annotation.Reference;
import org.my.dubbo.api.UserService;
import org.my.dubbo.entity.UserEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("user")
public class UserController {
@Reference(version = "${service.api.version}")
private UserService userService;
@RequestMapping(value = "findAll", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
public List findAll(){//Object obj
// Map map = (Map) obj;
return userService.findAll();
}
@RequestMapping(value = "getUser", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
public UserEntity getUser(@RequestParam String u_login_id, @RequestParam String u_password){
UserEntity userEntity = userService.getUserById(u_login_id,u_password);
return userEntity;
}
}
1、数据库:
/*
Navicat MySQL Data Transfer
Source Server : 本地
Source Server Version : 50515
Source Host : localhost:3306
Source Database : spring_boot_project
Target Server Type : MYSQL
Target Server Version : 50515
File Encoding : 65001
Date: 2019-06-16 00:53:34
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` varchar(70) NOT NULL COMMENT '用户id',
`u_name` varchar(100) NOT NULL COMMENT '用户名称',
`u_login_id` varchar(70) NOT NULL COMMENT '登录账户',
`u_password` varchar(100) NOT NULL DEFAULT '123456' COMMENT '登录密码',
`u_age` varchar(3) NOT NULL DEFAULT '0' COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '1', '1', '123456', '1');
INSERT INTO `user` VALUES ('2', '2', '2', '123456', '2');
启动过程:
先开启本地 zookeeper ,
再启动dobbo-registry-zookeeper-provider ,
继续启动 dobbo-registry-zookeeper-consumer
消费端使用远程调用得用@Reference(version=xxx)
提供端得用org.apache.dubbo.config.annotation.Service 的注解:格式:@Service (version=xxx)
Controller层:使用@RestController 它返回的是方法体信息
注意版本的使用。。。。