示例#1
0
 def test_unsubscribe(self, session_mock):
     print(session_mock)
     session_mock.post('http://127.0.0.1:13330/services/unsubscribe',
                       text=json.dumps(dict(success=True)))
     mesh = MeshClient(ApplicationInfo("pybolt_test_app"))
     mesh.unsubscribe("com.alipay.test")
示例#2
0
class _BaseClient(object):
    """
    Basic class for client implementation. Provides subscribe/unsubscribe method.
    """
    mesh_service_address = ("127.0.0.1", 12220)
    sofa_default_port = 12200

    def __init__(self,
                 app_name,
                 data_center=None,
                 zone=None,
                 registry_end_point=None,
                 is_antsharecloud=False,
                 access_key=None,
                 secret_key=None):
        """
        Check ApplicationInfo's comment for params' explanations.
        """
        try:
            self._mesh_client = MeshClient(
                ApplicationInfo(app_name, data_center, zone,
                                registry_end_point, access_key, secret_key,
                                is_antsharecloud))
            self._mesh_client.startup()
        except:
            logger.error("Fail to startup mesh client")
            self._mesh_client = None

    def _get_address(self, interface):
        addr = SERVICE_MAP.get(interface, self.mesh_service_address)
        if isinstance(addr, str):
            addr = (addr, self.sofa_default_port)
        return addr

    def subscribe(self, *interface):
        """
        Subscribe interfaces from mosnd, must called one before call to relevant interface.
        :param interface: the interface name.
        :type interface: str
        """
        if not self._mesh_client:
            return
        for inf in interface:
            try:
                self._mesh_client.subscribe(inf)
                SERVICE_MAP[inf] = self.mesh_service_address
            except Exception as e:
                logger.error(e)

    def unsubscribe(self, *interface):
        """
        Unsubscribe interfaces from mosnd.

        :param interface: the interface name
        :type interface: str
        """
        if not self._mesh_client:
            return
        for inf in interface:
            try:
                self._mesh_client.unsubscribe(inf)
                SERVICE_MAP.pop(inf)
            except Exception as e:
                logger.error(e)

    def invoke_sync(self, interface, method, content, **kwargs):
        raise NotImplementedError()

    def invoke_async(self, interface, method, content, **kwargs):
        raise NotImplementedError()

    def invoke_oneway(self, interface, method, content, **kwargs):
        raise NotImplementedError()