动态加载 dubbo spring
1.首先删除 配置文件中 dubboservice ,以及 项目中引用 service,然后 删除entity 中user,以及service 中userservice
<!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <!-- <dubbo:reference id="userService" interface="com.curiousby.cn.service.UserService" /> -->
2. usercontroller中修改代码
@RequestMapping(value="/auto/{userId}", method=RequestMethod.GET) public Object getUserById2(@PathVariable int userId) throws Exception { String url = "dubbo://localhost:20880/com.curiousby.cn.service.UserService";//更改不同的Dubbo服务暴露的ip地址&端口 //dubbo%3A%2F%2F10.133.254.143%3A20880%2Fcom.curiousby.cn.service.UserService%3Fanyhost%3Dtrue%26application%3Ddubbox-productor%26dubbo%3D2.5.3%26interface%3Dcom.curiousby.cn.service.UserService%26methods%3DfindById%26pid%3D14284%26side%3Dprovider%26timestamp%3D1517473747502 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); URL classesurl = new URL( "file:///D:/cache/eclipse4mvn-workplace/SpringBootDubboxDemo-Consumer/target/classes/com/curiousby/cn/" ); ClassLoader custom = new URLClassLoader( new URL[] { classesurl }, systemClassLoader ); //Class myClazz = Class.forName("com.curiousby.cn.service.UserService"); Class myClazz = custom.loadClass("com.curiousby.cn.service.UserService"); List classList = new ArrayList(); List valList = new ArrayList(); classList.add(Class.forName("java.lang.Integer")); valList.add(userId); ReferenceBean referenceBean = new ReferenceBean(); referenceBean.setApplicationContext(applicationContext); referenceBean.setInterface(myClazz); referenceBean.setUrl(url); try { referenceBean.afterPropertiesSet(); Object object = referenceBean.get(); Method methodService = myClazz.getMethod("findById", (Class[])classList.toArray(new Class[0])); return methodService.invoke(object, valList.toArray()); } catch (Exception e) { e.printStackTrace(); } return null; }
3.上传 class 文件到指定位置
这样就可以按照配置,自动上传 class 并解析 消费服务了
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!