[关闭]
@liyuj 2017-09-24T12:33:38.000000Z 字数 9253 阅读 5367

Apache-Ignite-2.2.0-中文开发手册

8.服务网格

8.1.服务网格

8.1.1.摘要

服务网格可以在集群上部署任意用户定义的服务,比如自定义计数器,ID生成器,分层映射等。
比如,服务网格可以作为基于微服务架构的解决方案或者应用的技术基础,了解更多的信息可以参考下面的文章:

Ignite可以控制每个集群节点应该部署多少个服务的实例,可以自动地确保所有的服务正确地部署和容错。

特性一览

可以访问8.2.服务示例章节来获得有关服务部署和访问服务的API信息。

注意,默认情况下,所有的集群节点的类路径中都包含服务类是必须的,服务网格是不支持对等类加载的,下面的服务部署章节会描述如何克服这个约束。

8.1.2.IgniteServices

所有的服务网格功能都是通过IgniteServices接口实现的。

  1. Ignite ignite = Ignition.ignite();
  2. // Get services instance spanning all nodes in the cluster.
  3. IgniteServices svcs = ignite.services();

可以将服务部署的范围限制在一个集群组内,这时,服务只会局限在集群组所属的节点内部。

  1. Ignite ignite = Ignitition.ignite();
  2. ClusterGroup remoteGroup = ignite.cluster().forRemotes();
  3. // Limit service deployment only to remote nodes (exclude the local node).
  4. IgniteServices svcs = ignite.services(remoteGroup);

8.1.3.负载平衡

在所有的情况下,除非单例服务部署,Ignite会自动地确保集群内的每个节点部署相同数量的服务。当网络发生变化时,为了更好地进行负载平衡,Ignite会对服务的部署进行重新评估然后可能将已经部署的服务重新部署到其他的节点。

8.1.4.容错

Ignite会一直保证服务的持续有效,以及不管拓扑发生变化或者节点故障都会按照指定的配置进行部署。

8.1.5.部署管理

默认情况下,就像上面负载平衡部分描述的那样,会根据集群的负载情况,Ignite服务会被部署到一个随机的节点(多个节点)。
除了这个默认的方式,Ignite还提供了一个API以将服务部署到特定的节点集合上,下面会详细描述各个方式。
基于节点过滤器的部署
这个方式是基于过滤谓词的,Ignite服务引擎在决定将服务部署到哪些候选节点上时每个节点都会调用,如果谓词返回true,那么节点就会包含进集合,否则就会被排除。
节点过滤器需要实现IgnitePredicate<ClusterNode>接口,比如下面这个典型示例,它会通知服务引擎将一个Ignite服务部署到本地属性中包含west.coast.attribute的非客户端节点上:

  1. // The filter implementation.
  2. public class ServiceFilter implements IgnitePredicate<ClusterNode> {
  3. @Override public boolean apply(ClusterNode node) {
  4. // The service will be deployed on non client nodes
  5. // that have the attribute 'west.coast.node'.
  6. return !node.isClient() &&
  7. node.attributes().containsKey("west.coast.node");
  8. }
  9. }

之后可以将其传给ServiceConfiguration.setNodeFilter(...)方法,然后使用这个配置启动服务。

  1. / Initiating cache configuration.
  2. ServiceConfiguration cfg = new ServiceConfiguration();
  3. // Setting service instance to deploy.
  4. cfg.setService(service);
  5. // Setting service name.
  6. cfg.setName("serviceName");
  7. // Providing the nodes filter.
  8. cfg.setNodeFilter(new ServiceFilter());
  9. // Getting instance of Ignite Service Grid.
  10. IgniteServices services = ignite.services();
  11. // Deploying the service.
  12. services.deploy(cfg);

确保节点过滤器的类位于每个节点的类路径中,不管服务是否要部署到那个节点上,否则会得到一个ClassNotFoundException
另一方面,原则上在集群的整个生命周期中,是可以只将服务的实现类添加到可能待部署的节点的类路径中的。

基于集群组的部署
另一个方式是基于定义的特定ClusterGroup,一旦通过特定的集群组获得了Ignite服务网格的引用,新的服务的部署只会发生在这个组的一个或者几个节点上。

  1. / A service will be deployed on the server nodes only.
  2. IgniteServices services = ignite.services(ignite.cluster().forServers());
  3. // Deploying the service.
  4. services.deploy(serviceCfg);

基于关系键的部署
最后一个可以影响服务部署的方式是基于关系键的定义,服务的配置需要包含关系键的值及其所属的缓存名,服务启动时,服务网格会将服务部署在该关系键所在的主节点上。在整个周期中,如果主节点发生变化,那么服务也会自动进行再部署。

  1. // Initiating cache configuration.
  2. ServiceConfiguration cfg = new ServiceConfiguration();
  3. // Setting service instance to deploy.
  4. cfg.setService(service);
  5. // Setting service name.
  6. cfg.setName("serviceName");
  7. // Setting the cache name and key's value for the affinity based deployment.
  8. cfg.setCacheName("orgCache");
  9. cfg.setAffinityKey(123);
  10. // Getting instance of Ignite Service Grid.
  11. IgniteServices services = ignite.services();
  12. // Deploying the service.
  13. services.deploy(cfg);

使用上述示例的配置部署服务后,Ignite会确保将服务部署到名为orgCache的缓存中犍为123所在的主节点上。

8.2.服务示例

8.2.1.定义服务接口

作为一个示例,可以定义一个简单的计数器服务:MyCounterService接口,注意这是一个简单的Java接口,没有任何特别的注解或者方法。

  1. public interface MyCounterService {
  2. /**
  3. * Increment counter value and return the new value.
  4. */
  5. int increment() throws CacheException;
  6. /**
  7. * Get current counter value.
  8. */
  9. int get() throws CacheException;
  10. }

8.2.2.服务实现

一个分布式服务的实现必须实现ServiceMyCounterService两个接口。
这个计数器服务的实现将计数值存储在缓存中。这个计数值的键是服务的名字,这样可以使多个计数器服务实例复用同一个缓存。

  1. public class MyCounterServiceImpl implements Service, MyCounterService {
  2. /** Auto-injected instance of Ignite. */
  3. @IgniteInstanceResource
  4. private Ignite ignite;
  5. /** Distributed cache used to store counters. */
  6. private IgniteCache<String, Integer> cache;
  7. /** Service name. */
  8. private String svcName;
  9. /**
  10. * Service initialization.
  11. */
  12. @Override public void init(ServiceContext ctx) {
  13. // Pre-configured cache to store counters.
  14. cache = ignite.cache("myCounterCache");
  15. svcName = ctx.name();
  16. System.out.println("Service was initialized: " + svcName);
  17. }
  18. /**
  19. * Cancel this service.
  20. */
  21. @Override public void cancel(ServiceContext ctx) {
  22. // Remove counter from cache.
  23. cache.remove(svcName);
  24. System.out.println("Service was cancelled: " + svcName);
  25. }
  26. /**
  27. * Start service execution.
  28. */
  29. @Override public void execute(ServiceContext ctx) {
  30. // Since our service is simply represented by a counter
  31. // value stored in cache, there is nothing we need
  32. // to do in order to start it up.
  33. System.out.println("Executing distributed service: " + svcName);
  34. }
  35. @Override public int get() throws CacheException {
  36. Integer i = cache.get(svcName);
  37. return i == null ? 0 : i;
  38. }
  39. @Override public int increment() throws CacheException {
  40. return cache.invoke(svcName, new CounterEntryProcessor());
  41. }
  42. /**
  43. * Entry processor which atomically increments value currently stored in cache.
  44. */
  45. private static class CounterEntryProcessor implements EntryProcessor<String, Integer, Integer> {
  46. @Override public Integer process(MutableEntry<String, Integer> e, Object... args) {
  47. int newVal = e.exists() ? e.getValue() + 1 : 1;
  48. // Update cache.
  49. e.setValue(newVal);
  50. return newVal;
  51. }
  52. }
  53. }

8.2.3.服务部署

可以将上述的计数器服务作为节点级单例部署在建立了myCounterCache缓存的集群组中。

  1. // Cluster group which includes all caching nodes.
  2. ClusterGroup cacheGrp = ignite.cluster().forCache("myCounterService");
  3. // Get an instance of IgniteServices for the cluster group.
  4. IgniteServices svcs = ignite.services(cacheGrp);
  5. // Deploy per-node singleton. An instance of the service
  6. // will be deployed on every node within the cluster group.
  7. svcs.deployNodeSingleton("myCounterService", new MyCounterServiceImpl());

8.2.4.服务代理

可以从集群内的任意节点访问已部署的服务实例。如果一个服务部署在某个节点,那么本地部署的实例会被返回,否则,如果服务不是本地的,那么会创建服务的一个远程代理。
粘性和非粘性代理
代理既可以是粘性的也可以是非粘性的。如果代理是粘性的,Ignite会总是访问同一个集群节点的服务,如果代理是非粘性的,那么Ignite会在服务部署的所有集群节点内对远程服务代理的调用进行负载平衡。

  1. // Get service proxy for the deployed service.
  2. MyCounterService cntrSvc = ignite.services().
  3. serviceProxy("myCounterService", MyCounterService.class, /*not-sticky*/false);
  4. // Ivoke a method on 'MyCounterService' interface.
  5. cntrSvc.increment();
  6. // Print latest counter value from our counter service.
  7. System.out.println("Incremented value : " + cntrSvc.get());

8.2.5.从计算访问服务

为了方便,可以通过@ServiceResource注解在计算中注入一个服务代理的实例。

  1. IgniteCompute compute = ignite.compute();
  2. compute.run(new IgniteRunnable() {
  3. @ServiceResource(serviceName = "myCounterService");
  4. private MyCounterService counterSvc;
  5. public void run() {
  6. // Ivoke a method on 'MyCounterService' interface.
  7. int newValue = cntrSvc.increment();
  8. // Print latest counter value from our counter service.
  9. System.out.println("Incremented value : " + newValue);
  10. }
  11. });

8.3.集群单例

8.3.1.摘要

IgniteServices可以在任意的集群节点上部署任意数量的的服务,然而,最常用的特性是在集群中部署一个服务的单例,Ignite会管理这个单例除非拓扑发生变化或者节点发生故障。

注意如果拓扑发生了变化,因为网络的延迟,可能存在一个临时的情况,就是几个单例服务的实例在不止一个节点上都处于活跃状态(比如故障检测延迟)。

8.3.2.集群单例

可以部署一个集群范围的单例服务,Ignite会保证集群内会一直有一个该服务的实例。当部署该服务的节点故障或者停止时,Ignite会自动在另一个节点上重新部署该服务。然而,如果部署该服务的节点仍然在网络中,那么服务会一直部署在该节点上,除非拓扑发生了变化。

  1. IgniteServices svcs = ignite.services();
  2. svcs.deployClusterSingleton("myClusterSingleton", new MyService());

上面的代码类似于下面的调用:

  1. svcs.deployMultiple("myClusterSingleton", new MyService(), 1, 1)

8.3.3.节点单例

也可以部署一个节点范围的单例服务,Ignite会保证每个节点都会有一个服务的实例在运行。当在集群组中启动了新的节点时,Ignite会自动地在每个新节点上部署一个新的服务实例。

  1. IgniteServices svcs = ignite.services();
  2. svcs.deployNodeSingleton("myNodeSingleton", new MyService());

上面的代码类似于下面的调用:

  1. svcs.deployMultiple("myNodeSingleton", new MyService(), 0, 1);

8.3.4.缓存键关系单例

可以将一个服务的单例通过一个给定的关系键部署在一个主节点上。当拓扑或者主键节点发生变化时,Ignite会一直确保服务在之前的主节点上卸载然后部署在一个新的主节点上。

  1. IgniteServices svcs = ignite.services();
  2. svcs.deployKeyAffinitySingleton("myKeySingleton", new MyService(), "myCache", new MyCacheKey());

上面的代码类似于下面的调用:

  1. IgniteServices svcs = ignite.services();
  2. ServiceConfiguration cfg = new ServiceConfiguration();
  3. cfg.setName("myKeySingleton");
  4. cfg.setService(new MyService());
  5. cfg.setCacheName("myCache");
  6. cfg.setAffinityKey(new MyCacheKey());
  7. cfg.setTotalCount(1);
  8. cfg.setMaxPerNodeCount(1);
  9. svcs.deploy(cfg);

8.4.服务配置

8.4.1.配置

除了通过调用Ignite提供的IgniteServices.deploy(...)方法部署服务之外,还可以通过IgniteConfiguration的serviceConfiguration属性在启动时自动地部署服务
XML:

  1. <bean class="org.apache.ignite.IgniteConfiguration">
  2. ...
  3. <!-- Distributed Service configuration. -->
  4. <property name="serviceConfiguration">
  5. <list>
  6. <bean class="org.apache.ignite.services.ServiceConfiguration">
  7. <property name="name" value="MyClusterSingletonSvc"/>
  8. <property name="maxPerNodeCount" value="1"/>
  9. <property name="totalCount" value="1"/>
  10. <property name="service">
  11. <ref bean="myServiceImpl"/>
  12. </property>
  13. </bean>
  14. </list>
  15. </property>
  16. </bean>
  17. <bean id="myServiceImpl" class="foo.bar.MyServiceImpl">
  18. ...
  19. </bean>

Java:

  1. ServiceConfiguration svcCfg1 = new ServiceConfiguration();
  2. // Cluster-wide singleton configuration.
  3. svcCfg1.setName("MyClusterSingletonSvc");
  4. svcCfg1.setMaxPerNodeCount(1);
  5. svcCfg1.setTotalCount(1);
  6. svcCfg1.setService(new MyClusterSingletonImpl());
  7. ServiceConfiguration svcCfg2 = new ServiceConfiguration();
  8. // Per-node singleton configuration.
  9. svcCfg2.setName("MyNodeSingletonSvc");
  10. svcCfg2.setMaxPerNodeCount(1);
  11. svcCfg2.setService(new MyNodeSingletonImpl());
  12. IgniteConfiguration igniteCfg = new IgniteConfiguration();
  13. igniteCfg.setServiceConfiguration(svcCfg1, svcCfg2);
  14. ...
  15. // Start Ignite node.
  16. Ignition.start(gridCfg);

8.4.2.启动后部署

可以通过配置然后在节点启动之后部署服务,除了可以部署各种集群单例的一些方便的方法外,还可以通过定制的配置来创建和部署服务。

  1. ServiceConfiguration cfg = new ServiceConfiguration();
  2. cfg.setName("myService");
  3. cfg.setService(new MyService());
  4. // Maximum of 4 service instances within cluster.
  5. cfg.setTotalCount(4);
  6. // Maximum of 2 service instances per each Ignite node.
  7. cfg.setMaxPerNodeCount(2);
  8. ignite.services().deploy(cfg);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注