- On service management and micro-service http://blog.csdn.net/suifeng3051/article/details/53992560
- The RPC framework can be divided from the different perspectives of language compatibility and service governance:
The RPC framework on language compatibility has thrift Zeroc-ice protbuf from the service governance perspective RPC Architecture has Dubbo RMI, Hessian 1.dubbo:
The main features are two functions: Service Register 2. Failover. Using the Hessian serialization protocol, the transmission is the TCP protocol, using the high-performance NIO framework Netty
Service Interface
1 public interface demoservice { 2 string SayHello (string name); /c6>3 }
Service implementation
1 Public class Implements Demoservice {2 Public string SayHello (string name) {3 return "Hello" + name; 4 }5 }
Configure Service Provider
The code snippet below shows how a Dubbo service provider are configured with spring framework, which is recommended, Howev Er you could also use API configuration if it ' s preferred.
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo"5xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd " >6<dubbo:application name= "Demo-provider"/>7<dubbo:registry address= "multicast://224.5.6.7:1234"/>8<dubbo:protocol name= "Dubbo" port= "20880"/>9<dubbo:serviceInterface= "Com.alibaba.dubbo.demo.DemoService" ref= "Demoservice"/>Ten<bean id= "Demoservice"class= "Com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> One</beans>
Start Service Provider
1 Public classProvider {2 Public Static voidMain (string[] args)throwsException {3Classpathxmlapplicationcontext context =NewClasspathxmlapplicationcontext (4 NewString[] {"Meta-inf/spring/dubbo-demo-provider.xml"});5 Context.start ();6System.in.read ();//Press any key to exit7 }8}
Configure Service Consumer
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo"5xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd " >6<dubbo:application name= "Demo-consumer"/>7<dubbo:registry address= "multicast://224.5.6.7:1234"/>8<dubbo:reference id= "Demoservice"Interface= "Com.alibaba.dubbo.demo.DemoService"/>9</beans>
Run Service Consumer
1 Public classConsumer {2 Public Static voidMain (string[] args)throwsException {3Classpathxmlapplicationcontext context =NewClasspathxmlapplicationcontext (4 Newstring[]{"Meta-inf/spring/dubbo-demo-consumer.xml"});5 Context.start ();6Demoservice Demoservice = (demoservice) context.getbean ("Demoservice");//obtain proxy object for remote invocation7String Hello = Demoservice.sayhello ("World");//Execute remote Invocation8System.out.println (hello);//Show the result9 }Ten}
2.RMI
- Implementing a structure diagram
External interface:
1 Public Interface extends Remote { 2 3public throws remoteexception; 4 5 }
Interface implementation
1 Public classServiceimplextendsUnicastRemoteObjectImplementsIService {2 - @Override - PublicString QueryName (String No)throwsRemoteException { + //the concrete implementation of the method -System.out.println ("Hello" +no); + returnstring.valueof (System.currenttimemillis ()); A }
1 //RMI Client2 Public classClient {3 4 Public Static voidMain (string[] args) {5 //Registration Manager6Registry Registry =NULL;7 Try {8 //Get Service Registry Manager9Registry = Locateregistry.getregistry ("127.0.0.1", 8088);Ten //List all registered services Onestring[] List =registry.list (); A for(String s:list) { - System.out.println (s); - } the}Catch(RemoteException e) { - - } - Try { + //Get service by name -IService Server = (iservice) registry.lookup ("Vince"); + //calling a remote method AString result = Server.queryname ("Ha ha Ha"); at //Output Call Result -SYSTEM.OUT.PRINTLN ("Result from remote:" +result); -} + } -}
1 //RMI Service Side2 Public classServer {3 4 Public Static voidMain (string[] args) {5 //Registration Manager6Registry Registry =NULL;8 //Create a service registry manager9Registry = Locateregistry.createregistry (8088); the //Create a service -Serviceimpl Server =NewServiceimpl (); - //Name the service binding -Registry.rebind ("Vince", server); at } - } -}
3.Hessian
HTTP-based remote method invocation, which is not perfect in performance, load balancing and failover relies on the application's load balancer, and Hessian is used similar to RMI, except that it fades the role of registry, called by the address shown, Use Hessianproxyfactory to create a proxy object based on the configured address, and also to introduce the Hessian jar package.
Introduction to several common RPC frameworks in Java