Dynamically load dubbo spring

Dynamically load dubbo spring

 

1. First delete the dubboservice in the configuration file and the reference service in the project, then delete the user in the entity and the userservice in the service

<!-- Generate remote service proxy, you can use demoService like local bean -->
<!-- <dubbo:reference id="userService"  interface="com.curiousby.cn.service.UserService" /> -->

 



 

 

 

2. Modify the code in 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";//Change the ip address & port exposed by different Dubbo services  
    	 //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. Upload the class file to the specified location



 



 

 

 

In this way, you can automatically upload the class and parse the consumer service according to the configuration.



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Donate to developers 

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (support Alipay, WeChat, and the buckle group), if you have no money to support a personal field, thank you.

 

Personal homepage : http://knight-black-bob.iteye.com/



 
 
 Thank you for your sponsorship, I will do better!

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326267402&siteId=291194637
Recommended