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();
}
}
GCF调用示例
最新推荐文章于 2021-12-02 16:42:05 发布