浅谈服务治理与微服务 http://blog.csdn.net/suifeng3051/article/details/53992560
- RPC框架可以从语言兼容和服务治理不同角度来划分:
从语言兼容上的rpc框架有 thrift zeroC-ICE protbuf
从服务治理角度的rpc架构有 dubbo RMI、Hessian spring Cloud
所谓服务治理,主要包括服务发现、负载均衡、容错、日志收集等功能
1.dubbo:
使用Hessian的序列化协议,传输则是TCP协议,使用了高性能的NIO框架Netty
服务接口
1
public
interface
DemoService {
2
String sayHello(String name);
3 }
服务实现
1
2
3
4
5
6 11 public class DemoServiceImpl implements DemoService {
22 public String sayHello(String name) {
33 return "Hello " + name;
44 }
55 }
6
Configure service provider
The code snippet below shows how a dubbo service provider is configured with spring framework, which is recommended, however you could also use API configuration if it’s preferred.
1
2
3
4
5
6
7
8
9
10
11
12 11 <?xml version="1.0" encoding="UTF-8"?>
2 2 <beans xmlns="http://www.springframework.org/schema/beans"
3 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
5 5 xsi: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 6 <dubbo:application name="demo-provider"/>
7 7 <dubbo:registry address="multicast://224.5.6.7:1234"/>
8 8 <dubbo:protocol name="dubbo" port="20880"/>
9 9 <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
1010 <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
1111 </beans>
12
Start service provider
1
2
3
4
5
6
7
8
9 11 public class Provider {
22 public static void main(String[] args) throws Exception {
33 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
44 new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
55 context.start();
66 System.in.read(); // press any key to exit
77 }
88 }
9
Configure service consumer
1
2
3
4
5
6
7
8
9
10 11 <?xml version="1.0" encoding="UTF-8"?>
22 <beans xmlns="http://www.springframework.org/schema/beans"
33 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
55 xsi: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">
66 <dubbo:application name="demo-consumer"/>
77 <dubbo:registry address="multicast://224.5.6.7:1234"/>
88 <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
99 </beans>
10
Run service consumer
1
2
3
4
5
6
7
8
9
10
11 11 public class Consumer {
2 2 public static void main(String[] args) throws Exception {
3 3 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
4 4 new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
5 5 context.start();
6 6 DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation
7 7 String hello = demoService.sayHello("world"); // execute remote invocation
8 8 System.out.println(hello); // show the result
9 9 }
1010 }
11
2.RMI
- 实现结构图
对外接口:
1
2
3
4
5
6 11 public interface IService extends Remote {
22
33 public String queryName(String no) throws RemoteException;
44
55 }
6
接口实现
1
2
3
4
5
6
7
8
9
10 11 public class ServiceImpl extends UnicastRemoteObject implements IService {
2 2
317 @Override
418 public String queryName(String no) throws RemoteException {
519 // 方法的具体实现
620 System.out.println("hello" + no);
721 return String.valueOf(System.currentTimeMillis());
822 }
9
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 11 // RMI客户端
2 2 public class Client {
3 3
4 4 public static void main(String[] args) {
5 5 // 注册管理器
6 6 Registry registry = null;
7 7 try {
8 8 // 获取服务注册管理器
9 9 registry = LocateRegistry.getRegistry("127.0.0.1",8088);
1010 // 列出所有注册的服务
1111 String[] list = registry.list();
1212 for(String s : list){
1313 System.out.println(s);
1414 }
1515 } catch (RemoteException e) {
1616
1717 }
1818 try {
1919 // 根据命名获取服务
2020 IService server = (IService) registry.lookup("vince");
2121 // 调用远程方法
2222 String result = server.queryName("ha ha ha ha");
2323 // 输出调用结果
2424 System.out.println("result from remote : " + result);
2525 }32 }
2633 }
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 11 // RMI服务端
2 2 public class Server {
3 3
4 4 public static void main(String[] args) {
5 5 // 注册管理器
6 6 Registry registry = null;
7 8 // 创建一个服务注册管理器
8 9 registry = LocateRegistry.createRegistry(8088);
915 // 创建一个服务
1016 ServiceImpl server = new ServiceImpl();
1117 // 将服务绑定命名
1218 registry.rebind("vince", server);
13 23 }
1424 }
1525 }
16
3.Hessian
基于HTTP的远程方法调用,在性能方面还不够完美,负载均衡和失效转移依赖于应用的负载均衡器,Hessian的使用则与RMI类似,区别在于淡化了Registry的角色,通过显示的地址调用,利用HessianProxyFactory根据配置的地址create一个代理对象,另外还要引入Hessian的Jar包。