有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top
全网最细面试题手册,支持艾宾浩斯记忆法。这是一份最全面、最详细、最高质量的 java面试题,不建议你死记硬背,只要每天复习一遍,有个大概印象就行了。 https://store.amazingmemo.com/chapterDetail/1685324709017001`
public User setUser(User user) {
return user;
}
@Data
public class User {
String name;
int age;
}
public class GenericInvoke {
public static void main(String[] args) {
ApplicationConfig app = new ApplicationConfig("ConsumerTest");
RegistryConfig reg = new RegistryConfig("nacos://localhost:8848");
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(app);
bootstrap.registry(reg);
bootstrap.start();
try {
// 引用远程服务
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
// 弱类型接口名
reference.setInterface("com.xxx.api.service.TestService");
reference.setGroup("dev");
reference.setVersion("1.0");
reference.setRetries(0);
// RpcContext中设置generic=gson
RpcContext.getContext().setAttachment("generic","gson");
// 声明为泛化接口
reference.setGeneric(true);
reference.setCheck(false);
GenericService genericService = ReferenceConfigCache.getCache().get(reference);
// 传递参数对象的json字符串进行一次调用
Object res = genericService.$invoke("setUser", new String[]{"com.xxx.api.service.User"}, new Object[]{"{'name':'Tom','age':24}"});
System.out.println("result[setUser]:"+res); // 响应结果:result[setUser]:{name=Tom, class=com.xxx.api.service.User, age=24}
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}