Dubbo提供了三大核心能力:面向接口的远程方法调用(客户端-服务消费者 服务-服务提供者 接口一定要一样),智能容错和负载均衡,以及服务自动注册(服务提供者启动的时候将自己的地址信息写入注册中心-zookeeper)和发现(消费者启动的时候订阅服务提供者-获取服务提供者地址信息)。Dubbo官网地址:https://dubbo.apache.org/zh/
dubbo快速开发:
采用的:idea2020版本、maven依赖、jdk1.8版本。
注意:我用的是mybatis-plus实现的。
对于dubbo没注册上或者其他的错误需要注意几点:
0.首先打开zookee,否则启动就出错
1.注解导包是dubbo的:service层、并且controller层还使用的是@Reference注解
1.注解导包是dubbo的:service层、并且controller层还使用的是@Reference注解
2.配置文件yml:中的内容是否正确格式。配置连接zookeeper是否正确 。导致服务提供者没注册上(当初就没注意一直没注册上QAQ,查看了代码也没错)
3.实体类对象需要实现序列化否则访问地址会报错:
报错idea报错:
界面报错:
目录
Dubbo简介:
dubbo快速开发:
对于dubbo没注册上或者其他的错误需要注意几点:
0.首先打开zookee,否则启动就出错
1.注解导包是dubbo的:service层、并且controller层还使用的是@Reference注解
2.配置文件yml:中的内容是否正确格式。配置连接zookeeper是否正确 。导致服务提供者没注册上(当初就没注意一直没注册上QAQ,查看了代码也没错)
3.实体类对象需要实现序列化否则访问地址会报错:
0.准备数据库:
1. 1.创建父工程(dubbo_parent),
1.1.1.父工程(pom.xml内容)
2.创建实体对象子模块(dubbo_domain)
2.1.包结构
2.2.User类对象
2.3.实体类对象:pom.xml内容
3.创建服务接口子模块(dubbo_interface)
3.1.包结构:
3.2.包下内容
3.2.1服务接口的service接口:
3.3.服务接口:pom.xml内容
4.创建服务提供者模块(dubbo_provider)
4.1.包结构
4.2.包下内容
4.2.1.dao层的:UserDao接口
4.2.2.service的实现类:UserServiceImpl类
4.2.3.服务提供者模块的启动类:DubboProviderApplication
4.3.服务提供者配置文件:application.yml
4.4.服务提供者的依赖(pom.xml)
5.创建服务消费者模块(dubbo_consumer)
5.1.包结构
5.2.包下内容
5.2.1.controller层:UserController
5.2.2消费者启动类:DubboConsumerApplication
5.2.3.消费者的配置文件:application.yml
5.2.4.消费者的:pom.xml
启动服务提供者->启动服务消费者->输入网页地址:http://localhost:520/find/3得到效果。">最后:打开启动zookeeper服务端->启动服务提供者->启动服务消费者->输入网页地址:http://localhost:520/find/3得到效果。
6.Dubbo相关配置:
6.1.超时配置:对于dubbo的调用一般为1秒左右,超时会导致出错。这个一般建议有服务提供者设置:(启动不会报错,访问地址会出错)
6.2.服务接口访问协议:
6.3启动时检查
6.4.负载均衡
0.准备数据库:
create database t_dubbo;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO t_user(username,age) VALUES("张三",18);
INSERT INTO t_user(username,age) VALUES("李四",22);
INSERT INTO t_user(username,age) VALUES("王五",80);
1. 1.创建父工程(dubbo_parent),
1.1.1.父工程(pom.xml内容)
4.0.0
com.demon
dubbo_parent
pom
1.0-SNAPSHOT
dubbo_domain
dubbo_interface
dubbo_provider
dubbo_consumer
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
5.1.47
1.0.9
1.18.4
3.1.1
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus}
org.projectlombok
lombok
true
${lombok.version}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.projectlombok
lombok
2.创建实体对象子模块(dubbo_domain)
2.1.包结构
1.1.1.父工程(pom.xml内容)
4.0.0
com.demon
dubbo_parent
pom
1.0-SNAPSHOT
dubbo_domain
dubbo_interface
dubbo_provider
dubbo_consumer
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
5.1.47
1.0.9
1.18.4
3.1.1
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus}
org.projectlombok
lombok
true
${lombok.version}
mysql
mysql-connector-java
${mysql.version}
com.alibaba
druid
${druid.version}
org.projectlombok
lombok
2.创建实体对象子模块(dubbo_domain)
2.1.包结构
2.1.包结构
2.2.User类对象
package com.demon.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_user")
public class User implements Serializable {
private Integer id;
private String username;
private Integer age;
}
2.3.实体类对象:pom.xml内容
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_domain
com.baomidou
mybatis-plus
3.1.1
3.创建服务接口子模块(dubbo_interface)
3.1.包结构:
dubbo_parent com.demon 1.0-SNAPSHOT 4.0.0 dubbo_domaincom.baomidou mybatis-plus3.1.1
3.创建服务接口子模块(dubbo_interface)
3.1.包结构:
3.2.包下内容
3.2.1服务接口的service接口:
package com.demon.service;
import com.demon.pojo.User;
public interface UserService {
public User findById(Integer id);
}
3.3.服务接口:pom.xml内容
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_interface
com.demon
dubbo_domain
1.0-SNAPSHOT
4.创建服务提供者模块(dubbo_provider)
4.1.包结构
package com.demon.service;
import com.demon.pojo.User;
public interface UserService {
public User findById(Integer id);
}
3.3.服务接口:pom.xml内容
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_interface
com.demon
dubbo_domain
1.0-SNAPSHOT
4.创建服务提供者模块(dubbo_provider)
4.1.包结构
4.1.包结构
4.2.包下内容
4.2.1.dao层的:UserDao接口
package com.demon.dao;
import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.demon.pojo.User;
public interface UserDao extends baseMapper {
}
4.2.2.service的实现类:UserServiceImpl类
package com.demon.service.imp;
import com.demon.dao.UserDao;
import com.demon.pojo.User;
import com.demon.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public User findById(Integer id) {
System.out.println("调用了provider1,8888端口.......");
return userDao.selectById(id);
}
}
4.2.3.服务提供者模块的启动类:DubboProviderApplication
package com.demon;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.demon.dao")
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class,args);
}
}
4.3.服务提供者配置文件:application.yml
server:
port: 1314
#数据库来源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/java_dubbo?useUnicode=true&characterEncoding=utf8
username: root
password: root
dubbo:
application:
name: dubbo-provider
#zookeeper的地址
registry:
address: zookeeper://127.0.0.1:2181
#扫描service
scan:
base-packages: com.demon.service
#超时调用:dubbo调用默认1秒钟超时。
#建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚。
provider:
timeout: 3000
#配置服务提供者的协议和端口
protocol:
name: dubbo
port: 8888
#mybatis-plus的配置
mybatis-plus:
global-config:
db-config:
#跟随数据库主键自增
id-type: auto
4.4.服务提供者的依赖(pom.xml)
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_provider
com.demon
dubbo_interface
1.0-SNAPSHOT
com.baomidou
mybatis-plus-boot-starter
org.springframework.boot
spring-boot-starter
mysql
mysql-connector-java
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
3.4.12
5.创建服务消费者模块(dubbo_consumer)
5.1.包结构
package com.demon.dao; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.demon.pojo.User; public interface UserDao extends baseMapper{ }
4.2.2.service的实现类:UserServiceImpl类
package com.demon.service.imp;
import com.demon.dao.UserDao;
import com.demon.pojo.User;
import com.demon.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public User findById(Integer id) {
System.out.println("调用了provider1,8888端口.......");
return userDao.selectById(id);
}
}
4.2.3.服务提供者模块的启动类:DubboProviderApplication
package com.demon;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.demon.dao")
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class,args);
}
}
4.3.服务提供者配置文件:application.yml
server:
port: 1314
#数据库来源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/java_dubbo?useUnicode=true&characterEncoding=utf8
username: root
password: root
dubbo:
application:
name: dubbo-provider
#zookeeper的地址
registry:
address: zookeeper://127.0.0.1:2181
#扫描service
scan:
base-packages: com.demon.service
#超时调用:dubbo调用默认1秒钟超时。
#建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚。
provider:
timeout: 3000
#配置服务提供者的协议和端口
protocol:
name: dubbo
port: 8888
#mybatis-plus的配置
mybatis-plus:
global-config:
db-config:
#跟随数据库主键自增
id-type: auto
4.4.服务提供者的依赖(pom.xml)
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_provider
com.demon
dubbo_interface
1.0-SNAPSHOT
com.baomidou
mybatis-plus-boot-starter
org.springframework.boot
spring-boot-starter
mysql
mysql-connector-java
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
3.4.12
5.创建服务消费者模块(dubbo_consumer)
5.1.包结构
package com.demon;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.demon.dao")
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class,args);
}
}
4.3.服务提供者配置文件:application.yml
server:
port: 1314
#数据库来源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/java_dubbo?useUnicode=true&characterEncoding=utf8
username: root
password: root
dubbo:
application:
name: dubbo-provider
#zookeeper的地址
registry:
address: zookeeper://127.0.0.1:2181
#扫描service
scan:
base-packages: com.demon.service
#超时调用:dubbo调用默认1秒钟超时。
#建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚。
provider:
timeout: 3000
#配置服务提供者的协议和端口
protocol:
name: dubbo
port: 8888
#mybatis-plus的配置
mybatis-plus:
global-config:
db-config:
#跟随数据库主键自增
id-type: auto
4.4.服务提供者的依赖(pom.xml)
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_provider
com.demon
dubbo_interface
1.0-SNAPSHOT
com.baomidou
mybatis-plus-boot-starter
org.springframework.boot
spring-boot-starter
mysql
mysql-connector-java
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
3.4.12
5.创建服务消费者模块(dubbo_consumer)
5.1.包结构
dubbo_parent com.demon 1.0-SNAPSHOT 4.0.0 dubbo_providercom.demon dubbo_interface1.0-SNAPSHOT com.baomidou mybatis-plus-boot-starterorg.springframework.boot spring-boot-startermysql mysql-connector-javaorg.apache.dubbo dubbo-spring-boot-starter2.7.5 org.apache.curator curator-recipes4.2.0 org.apache.zookeeper zookeeper3.4.12
5.创建服务消费者模块(dubbo_consumer)
5.1.包结构
5.2.包下内容
5.2.1.controller层:UserController
package com.demon.cotroller;
import com.demon.pojo.User;
import com.demon.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference
private UserService userService;
@GetMapping("/find/{id}")
public User findById(@PathVariable("id") Integer id){
User user = userService.findById(id);
return user;
}
}
5.2.2消费者启动类:DubboConsumerApplication
package com.demon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class,args);
}
}
5.2.3.消费者的配置文件:application.yml
server:
port: 520
dubbo:
application:
name: dubbo_consumer
#zookeeper的地址
registry:
address: zookeeper://127.0.0.1:2181
#启动时检查:不设置需要先启动服务提供者才能启动消费者不然会报错
consumer:
check: false
5.2.4.消费者的:pom.xml
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_consumer
com.demon
dubbo_interface
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
3.4.12
com.alibaba
fastjson
1.2.8
最后:打开启动zookeeper服务端->启动服务提供者->启动服务消费者->输入网页地址:http://localhost:520/find/3得到效果。
package com.demon.cotroller;
import com.demon.pojo.User;
import com.demon.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Reference
private UserService userService;
@GetMapping("/find/{id}")
public User findById(@PathVariable("id") Integer id){
User user = userService.findById(id);
return user;
}
}
5.2.2消费者启动类:DubboConsumerApplication
package com.demon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class,args);
}
}
5.2.3.消费者的配置文件:application.yml
server:
port: 520
dubbo:
application:
name: dubbo_consumer
#zookeeper的地址
registry:
address: zookeeper://127.0.0.1:2181
#启动时检查:不设置需要先启动服务提供者才能启动消费者不然会报错
consumer:
check: false
5.2.4.消费者的:pom.xml
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_consumer
com.demon
dubbo_interface
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
3.4.12
com.alibaba
fastjson
1.2.8
最后:打开启动zookeeper服务端->启动服务提供者->启动服务消费者->输入网页地址:http://localhost:520/find/3得到效果。
server:
port: 520
dubbo:
application:
name: dubbo_consumer
#zookeeper的地址
registry:
address: zookeeper://127.0.0.1:2181
#启动时检查:不设置需要先启动服务提供者才能启动消费者不然会报错
consumer:
check: false
5.2.4.消费者的:pom.xml
dubbo_parent
com.demon
1.0-SNAPSHOT
4.0.0
dubbo_consumer
com.demon
dubbo_interface
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.curator
curator-recipes
4.2.0
org.apache.zookeeper
zookeeper
3.4.12
com.alibaba
fastjson
1.2.8
最后:打开启动zookeeper服务端->启动服务提供者->启动服务消费者->输入网页地址:http://localhost:520/find/3得到效果。
注意:启动服务消费者和服务提供者有启动先后顺序,如果没设置启动时检查为false(这里设置了false)先启动服务消费者会报错。
6.Dubbo相关配置:
6.1.超时配置:对于dubbo的调用一般为1秒左右,超时会导致出错。这个一般建议有服务提供者设置:(启动不会报错,访问地址会出错)
例如设置服务提供者睡眠1.5秒:
出错访问的页面:
修改服务提供者或者服务消费者yml的配置:
dubbo:
#修改的是服务提供者
provider: #如果是服务消费者为consumer:
timeout: 3000
6.2.服务接口访问协议:
服务提供方可以配置许多种不同的协议:Dubbo支持的协议有:dubbo、rmi、hessian、http、webservice、rest、redis等。
可以去官方文档查看更多:Dubbo官网地址:https://dubbo.apache.org/zh/
服务提供者的配置文件:
#配置服务提供者的协议和端口
protocol:
name: dubbo
port: 8888
6.3启动时检查
如果想不管服务提供者是否启动都可以先启动服务消费者可以配置下面的。
开发阶段check值设置为false,生产环境改为true。
如果设置为true,启动服务消费者,会抛出异常,表示没有服务提供者
消费者的:yml配置
#启动时检查:不设置需要先启动服务提供者才能启动消费者不然会报错
consumer:
check: false
6.4.负载均衡
负载均衡(Load Balance):其实就是将请求分摊到多个操作单元上进行执行,从而共同完成工作任务。 在集群负载均衡时,Dubbo 提供了多种均衡策略(包括随机random、轮询roundrobin、最少活跃调用数leastactive),缺省【默认】为random随机调用。
服务消费者yml配置
dubbo:
consumer:
loadbalance: roundrobin
正式生产环境中,服务提供者部署到多台机器上,不需要修改任何代码,只需要部署到不同机器即可测试。
增加一个服务提供者,提供相同的服务;
如果是单机测试,必须通过修该提供者的dubbo协议端口和web服务端口来进行部署。
然后可以通过service接口的实现类里面的输出语句不同。在访问地址查看不同。



