mirror of
https://github.com/youthlql/JavaYouth.git
synced 2026-04-18 17:03:40 +00:00
小更新,可忽略
This commit is contained in:
@@ -13,18 +13,24 @@ abbrlink: d3c530c4
|
||||
date: 2021-09-11 15:21:58
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Dubbo源码
|
||||
|
||||
## 第一节: Dubbo框架介绍与手写模拟Dubbo
|
||||
|
||||
### 笔记更新地址:
|
||||
|
||||
[https://www.yuque.com/books/share/f2394ae6-381b-4f44-819e-c231b39c1497](https://www.yuque.com/books/share/f2394ae6-381b-4f44-819e-c231b39c1497?#)(密码:kyys) 《Dubbo笔记》
|
||||
|
||||
|
||||
|
||||
|
||||
## 第一节: Dubbo框架介绍
|
||||
### 手写代码地址
|
||||
|
||||
手写模拟Dubbo代码地址:[https://gitee.com/archguide/rpc](https://gitee.com/archguide/rpc)
|
||||
|
||||
git clone地址:[https://gitee.com/archguide/rpc.git](https://gitee.com/archguide/rpc.git)
|
||||
|
||||
|
||||
|
||||
### 什么是RPC
|
||||
|
||||
@@ -119,10 +125,16 @@ Dubbo网关参考:[https://github.com/apache/dubbo-proxy](https://github.com/a
|
||||
|
||||
## 第二节: Dubbo的基本应用与高级应用
|
||||
|
||||
### 笔记更新地址:
|
||||
|
||||
[https://www.yuque.com/books/share/f2394ae6-381b-4f44-819e-c231b39c1497](https://www.yuque.com/books/share/f2394ae6-381b-4f44-819e-c231b39c1497?#)(密码:kyys) 《Dubbo笔记》
|
||||
|
||||
|
||||
|
||||
官网:[http://dubbo.apache.org/zh/docs/v2.7/user/](http://dubbo.apache.org/zh/docs/v2.7/user/)
|
||||
|
||||
demo项目地址:[https://gitee.com/archguide/dubbo-tuling-demo](https://gitee.com/archguide/dubbo-tuling-demo)
|
||||
|
||||
管理台github地址:[https://github.com/apache/dubbo-admin](https://github.com/apache/dubbo-admin)
|
||||
|
||||
|
||||
@@ -134,7 +146,7 @@ Dubbo提供了很多功能,这里我们只介绍几种比较重要的,其他
|
||||
### 项目目录
|
||||
|
||||
```java
|
||||
dubbo-youthlql-demo
|
||||
dubbo-tuling-demo
|
||||
├── consumer/
|
||||
| ├── consumer.iml
|
||||
| ├── pom.xml
|
||||
@@ -142,7 +154,7 @@ dubbo-youthlql-demo
|
||||
| ├── main/
|
||||
| | ├── java/
|
||||
| | | └── com/
|
||||
| | | └── youthlql/
|
||||
| | | └── tuling/
|
||||
| | | ├── consumer/
|
||||
| | | | ├── AsyncDubboConsumerDemo.java
|
||||
| | | | ├── CallbackDubboConsumerDemo.java
|
||||
@@ -161,7 +173,7 @@ dubbo-youthlql-demo
|
||||
| | └── application.yml
|
||||
| └── test/
|
||||
| └── java/
|
||||
├── dubbo-youthlql-demo.iml
|
||||
├── dubbo-tuling-demo.iml
|
||||
├── interface/
|
||||
| ├── interface.iml
|
||||
| ├── pom.xml
|
||||
@@ -169,7 +181,7 @@ dubbo-youthlql-demo
|
||||
| ├── main/
|
||||
| | ├── java/
|
||||
| | | └── com/
|
||||
| | | └── youthlql/
|
||||
| | | └── tuling/
|
||||
| | | ├── DemoService.java
|
||||
| | | ├── DemoServiceListener.java
|
||||
| | | ├── DemoServiceMock.java
|
||||
@@ -185,7 +197,7 @@ dubbo-youthlql-demo
|
||||
├── main/
|
||||
| ├── java/
|
||||
| | └── com/
|
||||
| | └── youthlql/
|
||||
| | └── tuling/
|
||||
| | ├── DubboProviderDemo.java
|
||||
| | ├── provider/
|
||||
| | | └── service/
|
||||
@@ -342,13 +354,13 @@ private DemoService demoService;
|
||||
> consumer项目
|
||||
|
||||
```java
|
||||
package com.youthlql.consumer;
|
||||
package com.tuling.consumer;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
public class StubDubboConsumerDemo {
|
||||
|
||||
|
||||
// @Reference(version = "timeout", timeout = 1000, stub = "com.youthlql.DemoServiceStub")
|
||||
// @Reference(version = "timeout", timeout = 1000, stub = "com.tuling.DemoServiceStub")
|
||||
@Reference(version = "timeout", timeout = 1000, stub = "true")
|
||||
private DemoService demoService;
|
||||
|
||||
@@ -364,14 +376,14 @@ public class StubDubboConsumerDemo {
|
||||
```
|
||||
|
||||
1. stub=true的是个默认配置
|
||||
2. 默认用接口的全限定类名+Stub去调用,也就是`com.youthlql.DemoServiceStub`
|
||||
2. 默认用接口的全限定类名+Stub去调用,也就是`com.tuling.DemoServiceStub`
|
||||
3. 比如这个例子,当消费者去调用`demoService.sayHello("周瑜")`时,会首先调用`interface项目`下的`DemoServiceStub`的`sayHello`方法。如果调用失败就返回`"容错数据"`
|
||||
4. 如果找不到`com.youthlql.DemoServiceStub`就抛异常
|
||||
5. 注意`DemoServiceStub`这个类不一定需要写在interface项目里的,写在哪里都行,只要能通过pom.xml的maven依赖找到`com.youthlql.DemoServiceStub`你这个路径就行
|
||||
4. 如果找不到`com.tuling.DemoServiceStub`就抛异常
|
||||
5. 注意`DemoServiceStub`这个类不一定需要写在interface项目里的,写在哪里都行,只要能通过pom.xml的maven依赖找到`com.tuling.DemoServiceStub`你这个路径就行
|
||||
6. 也可以stub直接指定全类名,这样就可以每一个消费者都提供一个本地存根
|
||||
|
||||
```java
|
||||
@Reference(version = "timeout", timeout = 1000, stub = "com.youthlql.DemoServiceStub")
|
||||
@Reference(version = "timeout", timeout = 1000, stub = "com.tuling.DemoServiceStub")
|
||||
private DemoService demoService;
|
||||
```
|
||||
|
||||
@@ -380,7 +392,7 @@ private DemoService demoService;
|
||||
> interface项目
|
||||
|
||||
```java
|
||||
package com.youthlql;
|
||||
package com.tuling;
|
||||
|
||||
public class DemoServiceStub implements DemoService {
|
||||
|
||||
@@ -438,7 +450,7 @@ public class DemoServiceStub implements DemoService {
|
||||
> interface项目
|
||||
|
||||
```java
|
||||
package com.youthlql;
|
||||
package com.tuling;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -461,7 +473,7 @@ public interface DemoService {
|
||||
> consumer项目
|
||||
|
||||
```java
|
||||
package com.youthlql.consumer;
|
||||
package com.tuling.consumer;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
public class CallbackDubboConsumerDemo {
|
||||
@@ -487,7 +499,7 @@ public class CallbackDubboConsumerDemo {
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql.consumer;
|
||||
package com.tuling.consumer;
|
||||
|
||||
public class DemoServiceListenerImpl implements DemoServiceListener {
|
||||
|
||||
@@ -502,7 +514,7 @@ public class DemoServiceListenerImpl implements DemoServiceListener {
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql;
|
||||
package com.tuling;
|
||||
|
||||
public interface DemoServiceListener {
|
||||
void changed(String msg);
|
||||
@@ -512,7 +524,7 @@ public interface DemoServiceListener {
|
||||
> provider项目
|
||||
|
||||
```java
|
||||
package com.youthlql.provider.service;
|
||||
package com.tuling.provider.service;
|
||||
|
||||
|
||||
/**
|
||||
@@ -604,6 +616,8 @@ System.out.println(demoService.sayHello("周瑜", "d3", new DemoServiceListenerI
|
||||
|
||||
|
||||
|
||||
<img src="https://npm.elemecdn.com/youthlql@1.0.2/rpc/dubbo/v1/0002.png"/>
|
||||
|
||||
|
||||
|
||||
### 异步调用
|
||||
@@ -620,7 +634,7 @@ System.out.println(demoService.sayHello("周瑜", "d3", new DemoServiceListenerI
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql;
|
||||
package com.tuling;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -643,7 +657,7 @@ public interface DemoService {
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql.provider.service;
|
||||
package com.tuling.provider.service;
|
||||
|
||||
|
||||
@Service(version = "async")
|
||||
@@ -670,7 +684,7 @@ public class AsyncDemoService implements DemoService {
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql.consumer;
|
||||
package com.tuling.consumer;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
public class AsyncDubboConsumerDemo {
|
||||
@@ -715,14 +729,14 @@ public class AsyncDubboConsumerDemo {
|
||||
2. GenericService是dubbo提供的
|
||||
|
||||
```java
|
||||
package com.youthlql.consumer;
|
||||
package com.tuling.consumer;
|
||||
|
||||
|
||||
@EnableAutoConfiguration
|
||||
public class GenericDubboConsumerDemo {
|
||||
|
||||
|
||||
@Reference(id = "demoService", version = "default", interfaceName = "com.youthlql.DemoService", generic = true)
|
||||
@Reference(id = "demoService", version = "default", interfaceName = "com.tuling.DemoService", generic = true)
|
||||
private GenericService genericService;
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
@@ -751,14 +765,14 @@ public class GenericDubboConsumerDemo {
|
||||
实现了GenericService接口的就是泛化服务
|
||||
|
||||
```java
|
||||
package com.youthlql.provider.service;
|
||||
package com.tuling.provider.service;
|
||||
|
||||
import com.youthlql.DemoService;
|
||||
import com.tuling.DemoService;
|
||||
import org.apache.dubbo.config.annotation.Service;
|
||||
import org.apache.dubbo.rpc.service.GenericException;
|
||||
import org.apache.dubbo.rpc.service.GenericService;
|
||||
|
||||
@Service(interfaceName = "com.youthlql.DemoService", version = "generic")
|
||||
@Service(interfaceName = "com.tuling.DemoService", version = "generic")
|
||||
public class GenericDemoService implements GenericService {
|
||||
|
||||
/**
|
||||
@@ -779,7 +793,7 @@ public class GenericDemoService implements GenericService {
|
||||
|
||||
```
|
||||
|
||||
意思就是实际暴露出去的服务依然是`com.youthlql.DemoService`,并且版本是generic。只是消费者调用的时候,最终执行的逻辑是这个`$invoke`方法。就是服务消费者你该怎么用还是怎么用,只是服务提供者后面真正执行逻辑不再是实现demoservice接口,实现sayhello方法了。而是GenericService的$invoke方法
|
||||
意思就是实际暴露出去的服务依然是`com.tuling.DemoService`,并且版本是generic。只是消费者调用的时候,最终执行的逻辑是这个`$invoke`方法。就是服务消费者你该怎么用还是怎么用,只是服务提供者后面真正执行逻辑不再是实现demoservice接口,实现sayhello方法了。而是GenericService的$invoke方法
|
||||
|
||||
### Dubbo中的REST
|
||||
|
||||
@@ -807,7 +821,7 @@ spring.application.name=dubbo-provider-demo
|
||||
server.port=8081
|
||||
|
||||
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
|
||||
dubbo.scan.base-packages=com.youthlql.provider.service
|
||||
dubbo.scan.base-packages=com.tuling.provider.service
|
||||
dubbo.application.name=${spring.application.name}
|
||||
|
||||
|
||||
@@ -843,7 +857,7 @@ dubbo.protocols.p2.host=0.0.0.0
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql.provider.service;
|
||||
package com.tuling.provider.service;
|
||||
|
||||
|
||||
@Service(version = "rest", protocol = "p2")
|
||||
@@ -867,9 +881,9 @@ public class RestDemoService implements DemoService {
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql.provider.service;
|
||||
package com.tuling.provider.service;
|
||||
|
||||
import com.youthlql.DemoService;
|
||||
import com.tuling.DemoService;
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.apache.dubbo.config.annotation.Service;
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
@@ -901,9 +915,9 @@ public class AsyncDemoService implements DemoService {
|
||||
|
||||
github地址:[https://github.com/apache/dubbo-admin](https://github.com/apache/dubbo-admin)
|
||||
|
||||
用户名和密码默认都是root
|
||||
|
||||
用户名和密码默认都是root,登录进去之后就这样的
|
||||
|
||||
<img src="https://npm.elemecdn.com/youthlql@1.0.2/rpc/dubbo/v1/0001.png"/>
|
||||
|
||||
1. Dubbo分为注册中心和配置中心,如果spring文件里没有明确写配置中心,配置中心默认就用注册中心。
|
||||
|
||||
@@ -979,7 +993,7 @@ conditions:
|
||||
|
||||
|
||||
```java
|
||||
package com.youthlql.controller;
|
||||
package com.tuling.controller;
|
||||
|
||||
import org.apache.dubbo.rpc.Constants;
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
@@ -1028,6 +1042,8 @@ public class ConsumerInterceptor implements HandlerInterceptor {
|
||||
|
||||
Zookeeper可视化客户端:ZooInspector
|
||||
|
||||
|
||||
|
||||
## Dubbo与其他微服务组件整合
|
||||
|
||||
sentinel: https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-dubbo
|
||||
|
||||
Reference in New Issue
Block a user