目录
zookeeper安装
1.拉取ZooKeeper镜像
2.新建文件夹
3.挂载本地文件夹并启动服务
4.查看容器
5.进入容器(zookeeper)
Dubbo Admin安装
1.下载dubbo-admin
2.zip包解压
3.修改配置文件
4.打包项目
5.启动jar
6.访问
构建项目
api模块
1.创建实体类
2.创建测试接口
生产者模块
1.导入依赖
2.添加配置
3.编写启动类
4.添加mapper接口
5.实现接口
6.编写controller接口
7.调用接口
消费者模块
1.导入依赖
2.添加配置
3.编写启动类
4.调用生产者接口
5.调用接口
在docker下安装zookeeper
# 查看本地镜像
docker images
# 拉取ZooKeeper镜像最新版本
docker pull zookeeper:latest
mkdir zookeeper
docker run -d -e TZ="Asia/Shanghai" -p 2181:2181 -v /root/docker/zookeeper:/data --name zookeeper --restart always zookeeper
-d :表示在一直在后台运行容器
-e TZ="Asia/Shanghai" :指定上海时区
-p 2181:2181 :对端口进行映射,将本地2181端口映射到容器内部的2181端口
-v :将本地目录(文件)挂载到容器指定目录;
--name :设置创建的容器名称
--restart always :始终重新启动zookeeper
docker ps
docker exec -it zookeeper bash //只登录容器,不登录 zkCli
./bin/zkCli.sh //执行脚本新建一个Client,即进入容器
进入github,搜索dubbo-admin:https://github.com/apache/dubbo-admin
D:\idealTestProjects\dubbo\dubbo-admin-develop\dubbo-admin-server\src\main\resources目录下,修改application.properties 配置文件
找到下面配置的注册中心的地址,修改为zookeeper注册中心的IP和端口,也可以自定义访问端口号
进入到D:\idealTestProjects\dubbo\dubbo-admin-develop\dubbo-admin-distribution目录下,按住shift+右键,打开power shell,执行清除并打包命令
mvn clean package -Dmaven.test.skip=true
都success后就成功了
在目录D:\idealTestProjects\dubbo\dubbo-admin-develop\dubbo-admin-distribution\target下,执行下面的命令启动dubbo-admin
java -jar .\dubbo-admin-0.5.0-SNAPSHOT.jar
浏览器输入:43.139.136.235:6097,用户名密码都是root
项目结构:
@Data
public class User implements Serializable {
private Long id;
private String username;
private String password;
}
注意:实体来要添加Serializable,否则会序列化报错
public interface UserService {
User selectUserById(Long id);
}
创建springboot项目:dubbo-develop-provider
com.lgcgk
dubbo-develop-api
1.0-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-registry-zookeeper
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
org.mybatis
mybatis-spring
1.3.1
mysql
mysql-connector-java
runtime
src/main/java
**/*.xml
org.springframework.boot
spring-boot-maven-plugin
server:
port: 6050
spring:
application:
name: dubbo-samples-privider
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dubbo-sql?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
username: root
password: 123456
mybatis-plus:
type-aliases-package: com.lgcgk.provider.entity
mapper-locations: com/lgcgk/provider/mapper/*.xml
dubbo:
application:
name: ${spring.application.name}
registry:
address: zookeeper://127.0.0.1:2181
timeout: 200000
protocol:
name: dubbo
port: 20890
scan:
base-packages: com.lgcgk.provider.service.impl
dubbo配置:服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心
dubbo.protocol.name:dubbo服务名称
dubbo.protocol.port:dubbo服务端口
dubbo.registry.timeout:请求zookeeper超时时间
@EnableDubbo
@SpringBootApplication
@MapperScan("com.demo.provider.mapper")
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
System.out.println("生产者启动完毕");
}
}
dubbo必须配置注解@EnableDubbo
@EnableDubbo:整合了三个注解@EnableDubboConfig、@DubboComponentScan、@EnableDubboLifecycle
1.@EnableDubboConfig:将用于解析配置相关的类注册到spring容器
2.@DubboComponentScan:用于指定@Service扫描路径
3.@EnableDubboLifecycle:注册了两个监听器到spring容器
public interface UserMapper {
User selectUserById(Long id);
}
xml:
@DubboService
@AllArgsConstructor
public class UserServiceImpl implements UserService {
UserMapper userMapper;
public User selectUserById(Long id) {
User user = userMapper.selectUserById(id);
return user;
}
}
1.@DubboService
在类上加入@DubboService注解,表示该Bean以ServiceBean
形式注册到Spring容器中,以便后续创建Dubbo Service
2.@AllArgsConstructor
是lombok中的注解,作用在类上;使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数;@Autowired和@resource可以用它来代替
@RestController
@RequestMapping("/provider")
@AllArgsConstructor
public class UserController {
UserService userService;
@GetMapping("/selectUserById/{id}")
public User selectUserById(@PathVariable("id")Long id){
return userService.selectUserById(id);
}
}
com.lgcgk
dubbo-develop-api
1.0-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.apache.dubbo
dubbo-registry-zookeeper
server:
port: 6051
spring:
application:
name: dubbo-samples-consumer
dubbo:
application:
name: ${spring.application.name}
registry:
address: zookeeper://127.0.0.1:2181
timeout: 200000
protocol:
name: dubbo-consumer
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
@RestController
@RequestMapping("/consumer")
@Slf4j
public class ConsumerUserController {
@DubboReference( protocol = "dubbo", loadbalance = "random")
UserService userService;
@RequestMapping("/selectUserById/{id}")
public User getUser(@PathVariable("id") Long id) {
User user = userService.selectUserById(id);
log.info("response from provider: {}", user);
return user;
}
}
@DubboReference:表名该类为Dubbo Reference,在需要调用的服务接口上使用即可直接调用远程服务。
protocol:调用的服务名
loadbalance:dubbo提供了4种负载均衡策略
1.random:基于权重随机算法
2.roundrobin:基于加权轮询算法
3.leastactive:基于最少活跃调用数算法
4.consistenthash:基于 hash 一致性