目录
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.调用接口
zookeeper安装
在docker下安装zookeeper
1.拉取ZooKeeper镜像
# 查看本地镜像
docker images
# 拉取ZooKeeper镜像最新版本
docker pull zookeeper:latest
2.新建文件夹
mkdir zookeeper
3.挂载本地文件夹并启动服务
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
4.查看容器
docker ps
5.进入容器(zookeeper)
docker exec -it zookeeper bash //只登录容器,不登录 zkCli
./bin/zkCli.sh //执行脚本新建一个Client,即进入容器
Dubbo Admin安装
1.下载dubbo-admin
进入github,搜索dubbo-admin:https://github.com/apache/dubbo-admin
2.zip包解压
3.修改配置文件
D:\idealTestProjects\dubbo\dubbo-admin-develop\dubbo-admin-server\src\main\resources目录下,修改application.properties 配置文件
找到下面配置的注册中心的地址,修改为zookeeper注册中心的IP和端口,也可以自定义访问端口号
4.打包项目
进入到D:\idealTestProjects\dubbo\dubbo-admin-develop\dubbo-admin-distribution目录下,按住shift+右键,打开power shell,执行清除并打包命令
mvn clean package -Dmaven.test.skip=true
都success后就成功了
5.启动jar
在目录D:\idealTestProjects\dubbo\dubbo-admin-develop\dubbo-admin-distribution\target下,执行下面的命令启动dubbo-admin
java -jar .\dubbo-admin-0.5.0-SNAPSHOT.jar
6.访问
浏览器输入:43.139.136.235:6097,用户名密码都是root
构建项目
api模块
项目结构:
1.创建实体类
@Data
public class User implements Serializable {
private Long id;
private String username;
private String password;
}
注意:实体来要添加Serializable,否则会序列化报错
2.创建测试接口
public interface UserService {
User selectUserById(Long id);
}
生产者模块
创建springboot项目:dubbo-develop-provider
1.导入依赖
<dependencies>
<dependency>
<groupId>com.lgcgk</groupId>
<artifactId>dubbo-develop-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo 与 spring-boot 整合包-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--springboot 启动核心包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--springboot rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!---dubbo 与 zookeeper整合包 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<!--mysql相关的驱动-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<!-- 描述存放资源的目录,该路径相对POM路径-->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.添加配置
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超时时间
3.编写启动类
@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容器
4.添加mapper接口
public interface UserMapper {
User selectUserById(Long id);
}
xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.lgcgk.provider.mapper.UserMapper">
<select id="selectUserById" resultType="com.lgcgk.api.entity.User">
select * from user where id = #{id}
</select>
</mapper>
5.实现接口
@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可以用它来代替
6.编写controller接口
@RestController
@RequestMapping("/provider")
@AllArgsConstructor
public class UserController {
UserService userService;
@GetMapping("/selectUserById/{id}")
public User selectUserById(@PathVariable("id")Long id){
return userService.selectUserById(id);
}
}
7.调用接口
消费者模块
1.导入依赖
<dependencies>
<!--dubbo-samples-springcloud-api 项目 依赖-->
<dependency>
<groupId>com.lgcgk</groupId>
<artifactId>dubbo-develop-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
</dependencies>
2.添加配置
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
3.编写启动类
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
4.调用生产者接口
@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 一致性