1 知识点
1.1 微服务基础
什么是微服务?
- 每个微服务按照单一职责原则,实现一组功能
- 每个都是一个的应用程序,可以独立部署、独立运行、独立对外提供服务
什么是微服务架构?
把应用程序的功能分解成一组服务的架构风格。
微服务架构需要解决服务之间的调用和服务治理功能。
什么是Dubbo框架?
Dubbo是一款高性能、轻量级的微服务框架,它提供了RPC通信和服务治理两大功能。
什么叫服务治理?
1.2 RPC远程过程调用
RPC,Remote Procedure Call,远程过程调用。
远程过程调用的详细过程是什么?
为什么参数对象要实现Serizlizable接口?
1.3 服务注册中心
暴露服务的提供方,将服务注册到服务注册中心
import org.apache.dubbo.config.annotation.Service;// 这个Service是dubbo包下的,将服务提供方的接口暴露出来@Servicepublic class HelloServiceImpl implements HelloService {@Overridepublic String hello(Person person) {return "provider-1: name=" + person.getName();}}
dubbo:application:name: provider-1 # 服务提供方名字protocol:name: dubbo # 远程过程调用协议port: 20881 # 远程过程调用接口registry:address: zookeeper://localhost:2181 # 服务提供方注册服务注册中心
调用远程服务的消费方,向服务注册中心订阅
dubbo:
application:
name: consumer # 服务消费方名字
registry:
address: zookeeper://localhost:2181 # 服务消费方订阅服务注册中心
服务注册中心通知消费方,提供服务提供者列表
服务消费方维护了一个服务提供者列表,这个列表由服务注册中心提供,一旦服务提供方的状态发生改变,如新增服务或者服务宕机,服务注册中心会立刻知道,并且将服务提供者列表更新给消费方。
消费方通过RPC远程过程调用,使用提供方的接口
@Service // @Component
public class ConsumerService {
// 指示服务提供方的接口和负载均衡策略
@Reference(interfaceClass = HelloService.class,loadbalance = "roundrobin")
HelloService helloService;
public String hello(Person person) {
// 实际远程过程调用的执行
return helloService.hello(person);
}
}
1.4 注册的负载均衡
| 策略 | 名称策略描述 | 配置 |
|---|---|---|
| Random | 随机,按权重设置随机概率 | random |
| RoundRobin | 轮询,按照公约后的权重设置轮训比率 | roundrobin |
| LeastActive | 最少活跃调用数,相同活跃数的随机 | leastactive |
| ConsistentHash | 一致性Hash,相同参数的请求总是发到同一提供者 | consistenthash |
在consumer中配置
@Service // @Component
public class ConsumerService {
@Reference(interfaceClass = HelloService.class, loadbalance = "roundrobin")
HelloService helloService;
public String hello(Person person) {
return helloService.hello(person);
}
}
2 ZooKeeper的安装
3 SpringBoot整合Dubbo
common-api
// 公共接口
public interface HelloService {
String hello(Person person);
}
// 实现Serializable的参数类
@Data
public class Person implements Serializable {
private String name;
}
consumer
<!--引入公共接口依赖-->
<dependency>
<groupId>com.wei</groupId>
<artifactId>common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<!--zookeeper 底层依赖curator-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.10.0</version>
</dependency>
dubbo:
application:
name: consumer
registry:
address: zookeeper://localhost:2181
@Service // @Component
public class ConsumerService {
@Reference(interfaceClass = HelloService.class, loadbalance = "roundrobin")
HelloService helloService;
public String hello(Person person) {
return helloService.hello(person);
}
}
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(ConsumerApplication.class, args);
ConsumerService consumerService = context.getBean("consumerService", ConsumerService.class);
Person person = new Person();
person.setName("张三");
for (int i = 0; i <3 ; i++) {
String result = consumerService.hello(person);
System.out.println(result);
}
}
}
provider
<dependency>
<groupId>com.wei</groupId>
<artifactId>common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<!--zookeeper 底层依赖curator-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.10.0</version>
</dependency>
dubbo:
application:
name: provider-1
protocol:
name: dubbo
port: 20881
registry:
address: zookeeper://localhost:2181
scan:
base-packages: com.wei.provider.service
@Service // 注册为dubbo的service
public class HelloServiceImpl implements HelloService {
@Override
public String hello(Person person) {
return "provider-1: name=" + person.getName();
}
}
