开发cloudify配方文件- Attributes API

The Attributes API

服务实例有时需要共享一些信息,例如一个服务实例可能给其他服务实例分享它的监控端口(运行时)

Cloudify提供通过Attributes API实现这种机制,属性可以被分配到应用、服务或服务实例的上下文中,一旦被分配,这些属性可以被这个应用下的服务实例读取和修改

下面的例子表明,myKey-myValue这对属性在服务实例启动后被分配到应用中,另外其他服务的otherKey 属性被读取

gistfile1.groovy

service{

name "myService"

lifecycle {

postStart {

context.attributes.thisInstance.myKey = "myValue"

println context.attributes.otherService.otherKey

}

}

The API 的语法

下面是对分配属性值到应用、服务和服务实例的语法:

  • context.attributes.thisInstance.myKey = "myValue"分配myValuemyKey 属性到当前服务实例的上下文中,也可以这样写: context.attributes.thisInstance["myKey"] = "myValue"
  • context.attributes.thisService.myKey = "myValue"分配myValuemyKey 属性到当前服务的上下文中,注意:只有每个服务上下文中只能有一个myKey  属性。 因此,无论在那个服务实例修改这个属性都会覆盖这个值。
  • context.attributes.thisApplication.myKey = "myValue分配myValuemyKey 属性到当前应用的上下文中,注意: 整个应用上下文中只能有一个myKey属性。因此,无论在那个服务实例修改这个属性都会覆盖这个值。

    一个服务实例可以读取或修改其他服务实例或服务的属性,需要指定具体的服务名而不能使用thisService,例如:

  • context.attributes.tomcat.myKey = "myValue"分配myValuemyKey 属性到tomcat服务的上下文中
  • context.attributes.tomcat.instances[1].myKey = "myValue"分配myValuemyKey 属性到tomcatID1的服务实例上下文中,注意:服务实例的ID是从1开始的。

    不能访问其他应用的属性,只针对当前应用的服务和服务实例。

    下面的例子是使用each 方法来遍历服务实例的所有属性:

    context.attributes.thisService.instances.each{println it.myKey}

    读写属性

    属性可以使用Service Context APIServiceContextFactory 类来获取

    在当前服务中获取myKey属性,例:

    retrieveAttributes.groovy

    import org.cloudifysource.dsl.context.ServiceContextFactory;

    def context = com.gigaspaces.cloudify.dsl.context.ServiceContextFactory.getServiceContext()

    context.attributes.thisService["myKey"];


    读写应用和服务的属性,例:

    readWriteAttr1.groovy

    //set the value of an application level attribute

    context.attributes.thisApplication["myKey"] = "myValue"


    //read an application level attribute

    def appAttribute = context.attributes.thisApplication["myKey"]


    //set the value of a service level attribute

    context.attributes.thisService["myKey"] = "myValue"


    //read a service level attribute from current service

    def serviceAttribute = context.attributes.thisService["myKey"]


    //to read an attribute of a different service use the service name in this example cassandra

    def cassandraAttribute = context.attributes.cassandra["port"]


    读写当前服务实例和其他服务实例的属性,例:

    wRInstanceAttrs.groovy

    //Writing an attribute at the service instance level

    context.attributes.thisInstance["myKey"] = "myValue"


    //Reading an attribute(in this case port) from instance #1 in the same cluster

    context.attributes.thisService.instances[1].port = 4000


    //Reading an attribute(in this case jmxPort) from instance #1 of the tomcat service

    context.attributes.tomcat.instances[1].jmxPort = 1099


    //Instances are iterable, so you can do iterable stuff of groovy on top of it.

    context.attributes.tomcat.instances.each{println it.key}

转载于:https://my.oschina.net/OQKuDOtsbYT2/blog/121779

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值