GCF调用示例

package test;


interface TestService {
    void test(String context);
}
class TestServiceImpl implements TestService {

    @Override
    public void test(String context) {
        System.out.println("hello " + context);
    }

}
interface HsfAcceptor {

    void registerService(Class<? extends TestService> c,TestService testService);

    void bind(Address address);

}
class HsfAcceptorImpl implements HsfAcceptor {

    @Override
    public void registerService(Class<? extends TestService> c,
            TestService testService) {
        System.out.println("注册了服务器的服务 " + testService.getClass().getSimpleName());
    }

    @Override
    public void bind(Address address) {
        System.out.println("服务器端打开服务ip " + address.getServierIp() + "  打开了端口号 " + address.getPort());
    }

}
interface HsfConnector {
    void connect(Address address);
}
class HsfConnectorImpl implements HsfConnector{

    @Override
    public void connect(Address address) {
        System.out.println("客户端打开了连接 ip " + address.getServierIp() + "  打开了端口号 " + address.getPort());
    }

}
class Address {
    String servierIp;
    long port;
    public String getServierIp() {
        return servierIp;
    }
    public void setServierIp(String servierIp) {
        this.servierIp = servierIp;
    }
    public long getPort() {
        return port;
    }
    public void setPort(long port) {
        this.port = port;
    }

}
public class Server {
    HsfAcceptor acceptor;

    /*
     * 同步控制
     */
    public static void main(String[] args) {
        HsfAcceptor acceptor;
        HsfConnector connector;
        Server server = new Server(TestService.class,new TestServiceImpl());
        acceptor = server.getHsfAcceptor();
        Address serverAddress = new Address();
        serverAddress.servierIp = "192.168.1.1";
        serverAddress.port = 8080;
        acceptor.bind(serverAddress);

        Address address = new Address();
        address.servierIp = "locahost";
        address.setPort(8080);
        Client client = new Client(address);
        connector = client.getHsfConnector();
        System.out.println("=========以下RPC调用服务=======");
        System.out.println("获取动态代理");
        TestService testService = ServiceProxyFactory.getRoundFactoryInstance(connector, TestService.class);
        testService.test("GCF");
    }
    Server(Class<? extends TestService> c,TestService testService){
        acceptor = new HsfAcceptorImpl();
        acceptor.registerService(c, testService);
    }
    HsfAcceptor getHsfAcceptor() {
        return acceptor;
    }
}
class Client {
    HsfConnector connector;
    Client(Address address){
        connector = new HsfConnectorImpl();
        connector.connect(address);
    }
    HsfConnector getHsfConnector(){
        System.out.println("客户端获取了TCP连接");
        return connector;
    }
}
class ServiceProxyFactory {
    static TestService getRoundFactoryInstance(HsfConnector connector,Class<? extends TestService> testService){
        System.out.println("获取了服务器的RPC服务  ");
        return new TestServiceImpl();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值