Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。
1. RPC
远程过程调用协议RPC(Remote Procedure Call Protocol)-----允许像调用本地服务一样调用远程服务。
决定 RPC 的两个重要因素:服务之间的通信,消息的序列化与反序列化(二进制流>Json>xml)。
2. Dubbo概念
-
Container : 容器 -
Provider : 服务提供者 -
Registry :注册中心 -
Consumer :消费者 -
Monitor :监控
0.容器启动初始化 —> 1.服务提供者在注册中心中注册
2.消费者订阅注册中心 —> 3.注册中心返回给消费者,已经注册的服务,或者服务下线
4.消费者调用服务接口获取服务提供者返回的消息
5.消费者,服务提供者每次请求都会被监视
3. Dubbo 注册中心配置
Dubbo 的5种注册中心的配置手册
Dubbo 推荐 Zookeeper 作为注册中心
下载地址:https://archive.apache.org/dist/zookeeper/
mac@basil bin % zkServer.sh start
mac@basil bin % zkCli.sh
mac@basil bin % zkServer.sh status
create -e -s /basil "hello"
get /basil0000000001
set /basil0000000001 123456
delete /basil0000000001
4. Dubbo 监控中心配置
dubbo-admin
用户名密码均为 root
mvn clean -Dmaven.test.skip=true package
java -jar dubbo-admin-server-0.4.0.jar
npm run dev
注意:zk3.5以后的版本, 会自动占用8080端口,启动 dubbo-admin-server 前,需修改zk配置文件(zoo.cfg)如下:
admin.serverPort=8887
5. Dubbo 与 Spring 整合
使用 maven 构建项目
gmall-interface:
? JavaBean 、 服务接口 、异常 层
pojo 类:
@Data
@Builder
public class UserAddress implements Serializable {
private Integer id;
private String userId;
private String userAddress;
private String consignee;
private String phoneNum;
private String isDefault;
}
service 类:
public interface OrderService {
void initOrder(String userId);
}
public interface UserService {
List<UserAddress> getUserAddressList(String userId);
}
user-service-provider :
? 服务提供者
@Service
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
System.out.println("用户id:" + userId);
UserAddress userAddress1 = UserAddress.builder()
.id(1)
.consignee("林先生")
.userAddress("广东省深圳市南山区")
.phoneNum("12345568")
.userId("1")
.isDefault("Y")
.build();
UserAddress userAddress2 = UserAddress.builder()
.id(2)
.consignee("李先生")
.userAddress("广东省深圳市龙岗")
.phoneNum("12345568")
.userId("1")
.isDefault("N")
.build();
return Arrays.asList(userAddress1, userAddress2);
}
}
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
System.in.read();
}
}
order-service-consumer:
? 消费者,接口调用者
@Service
public class OrderServiceImpl implements OrderService {
UserService userService;
public OrderServiceImpl(UserService userService) {
this.userService = userService;
}
@Override
public void initOrder(String userId) {
List<UserAddress> addressList = userService.getUserAddressList("1");
for (UserAddress userAddress : addressList) {
System.out.println(userAddress.getUserAddress());
}
}
}
public class MainApplication {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = ioc.getBean(OrderService.class);
orderService.initOrder("1");
System.out.println("调用完成。。。。");
System.in.read();
}
}
为了能使 消费者 远程调用 服务提供者 的服务,我们还需要创建一下文件:
在服务提供者的资源目录下创建配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="user-service-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:service interface="com.cn.service.UserService" ref="userServiceImpl"/>
<bean id="userServiceImpl" class="com.cn.service.impl.UserServiceImpl"/>
</beans>
在消费者的资源目录下创建配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.cn.service.impl"/>
<dubbo:application name="order-service-consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference interface="com.cn.service.UserService" id="userService"/>
</beans>
先后启动服务提供者及消费者可在监控中心发现:
6. Dubbo 与 SpringBoot 整合
创建 boot-user-service-provider SpringBoot工程与 boot-order-service-consumer web工程
dubbo 坐标
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
修改 gmall-interface 接口
public interface OrderService {
List<UserAddress> initOrder(String userId);
}
修改 user-service-provider 配置文件:
dubbo:
application:
name: boot-user-service-provider
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20881
启动类:
@EnableDubbo 启动dubbo的注解
@DubboService 注册服务注解
@EnableDubbo
@SpringBootApplication
public class BootUserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootUserServiceProviderApplication.class, args);
}
}
@DubboService
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress userAddress1 = UserAddress.builder()
.id(1)
.consignee("林先生")
.userAddress("广东省深圳市南山区")
.phoneNum("12345568")
.userId("1")
.isDefault("Y")
.build();
UserAddress userAddress2 = UserAddress.builder()
.id(2)
.consignee("李先生")
.userAddress("广东省深圳市龙岗")
.phoneNum("12345568")
.userId("1")
.isDefault("N")
.build();
return Arrays.asList(userAddress1, userAddress2);
}
}
修改 boot-order-service-consumer 配置文件
server:
port: 8567
dubbo:
application:
name: boot-order-service-consumer
registry:
address: zookeeper://127.0.0.1:2181
monitor:
protocol: registry
@DubboReference 远程引用服务注解
@Service
public class OrderServiceImpl implements OrderService {
@DubboReference
UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户id: " + userId);
List<UserAddress> addressList = userService.getUserAddressList("1");
return addressList;
}
}
controller
@RestController
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
@GetMapping("/order/{userId}")
public List<UserAddress> getList(@PathVariable String userId) {
return orderService.initOrder(userId);
}
}
启动服务,访问 http://localhost:8567/order/1
|