rpc框架使用教程,超级稳定好用,大厂都在使用
rpc是什么
远程调用协议
如何使用
导入依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.3.0</version>
</dependency>
配置接口
DubboBootstrap.getInstance()
.protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50052))
.service(ServiceBuilder.newBuilder().interfaceClass //注册接口
(DoubleService.class).ref(new DoubleServiceImpl()).build()) // 实现的接口
.service(ServiceBuilder.newBuilder().interfaceClass(SendService.class).ref(new SendServiceImpl()).build())
.start()
.await();
实现接口
package com.example.springcamundastudy.service;
/**
* @author
*/
public interface DoubleService {
/**
* double测试访问
* @param name
* @return
*/
String sayHello(String name);
}
开始访问
http://localhost:50052/com.example.springcamundastudy.service.DoubleService //这个
必须使用类访问
uiy/sayHello?name=ccccc
成功
注意
必须是接口
出现404
检查是否使用全类路径
使用rpc中的过滤器
rpc是可以插拔
每次请求可以使用过滤器封装请求
具体实现方式
配置spi接口
spi 具体
@SPI(scope = ExtensionScope.MODULE)
public interface DoubleFilter extends BaseFilter {
}
启动接口
@Activate(group = PROVIDER)
public class DoubleFilterImpl implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
System.out.println(invoker.toString());
System.out.println(invocation.getInvoker());
return null;
}
}
@Activate(group = PROVIDER)
启动使用过滤器的方式
过滤器的原理
是通过代理的方式创建一个过滤器
开始使用过滤器的方式
public class SendServiceImpl implements SendService {
@DubboReference(filter="doubleFilter,tps")
private DoubleService doubleService;
@Override
public String send(String message) {
String qq = doubleService.sayHello("qq");
return "ccc"+message+"我的";
}
}
但是出现问题
这个表示服务没有注册成功
没有找到服务