在我们平常使用的RPC中,例如webservice,使用的习惯类似于下图
但是netty的实现过于底层,我们不能够像以前一样只关心方法的调用,而是要关心数据的传输,对于不熟悉netty的开发者,需要了解很多netty的概念和逻辑,才能实现RPC的调用。因此采用如下实现方案:
代码示例:
1.定义业务接口(普通开发者根据业务自行扩展类)
public interface HelloService {
String hello(String name);
String hello(Person person);
}
public class Person {
private String firstName;
private String lastName;
public Person() {
}
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
2.服务端实现业务接口实现及spring服务启动(普通开发者根据业务自行扩展类)
注意:在spring.xml中配置架构服务端的类RpcServer的bean,并在业务接口实现类上标注在架构上自定义的类注解,采用spring方式启动服务端,加载各个bean。
@RpcService(HelloService.class)
public class HelloServiceImpl implements HelloService {
public String hello(String name) {
System.out.println("已经调用服务端接口实现,业务处理结果为:");
System.out.println("Hello! " + name);
return "Hello! " + name;
}
public String hello(Person person) {
System.out.println("已经调用服务端接口实现,业务处理为:");
System.out.println("Hello! " + person.getFirstName() + " " + person.getLastName());
return "Hello! " + person.getFirstName() + " " + person.getLastName();
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 用户系统服务端的启动入口
* 其意义是启动springcontext,从而构造框架中的RpcServer
* 亦即:将用户系统中所有标注了RpcService注解的业务发布到RpcServer中
*/
public class RpcBootstrap {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("spring.xml");
}
}
服务端配置文件spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.zekun.rpc.sample.server"/>
<context:property-placeholder location="classpath:rpc.properties"/>
<bean id="serviceRegistry" class="cn.zekun.rpc.registry.ServiceRegistry">
<constructor-arg name="registryAddress" value="${registry.address}"/>
</bean>
<bean id="rpcServer" class="cn.zekun.rpc.server.RpcServer">
<constructor-arg name="serverAddress" value="${server.address}"/>
<constructor-arg name="serviceRegistry" ref="serviceRegistry"/>
</bean>
</beans>
rpc.properties文件
# zookeeper server
registry.address=localhost:2181
# rpc server
server.address=localhost:8000
log4j.properties文件
log4j.rootLogger=ERROR,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%m%n
log4j.logger.com.xxx.rpc=DEBUG
3.框架服务器
①自定义RPC请求注解 主要用于在服务接口的具体实现类上标注
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
/**
* RPC 请求注解(标注在服务实现类上)
*/
@Target({ ElementType.TYPE })//注解用在接口上
@Retention(RetentionPolicy.RUNTIME)//VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
@Component
public @interface RpcService {
Class<?> value();
}
②处理请求的Handler,通过构造时传入的“业务接口及实现”handlerMap,来调用客户端所请求的业务方法,并将业务方法返回值封装成response对象写入下一个handler
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.lang.reflect.Method;
import java.util.Map;
import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 处理具体的业务调用
* 通过构造时传入的“业务接口及实现”handlerMap,来调用客户端所请求的业务方法
* 并将业务方法返回值封装成response对象写入下一个handler(即编码handler——RpcEncoder)
*/
public class RpcHandler extends SimpleChannelInboundHandler<RpcRequest> {
private static final Logger LOGGER = LoggerFactory
.getLogger(RpcHandler.class);
private final Map<String, Object> handlerMap;
public RpcHandler(Map<String, Object> handlerMap) {
this.handlerMap = handlerMap;
}
/**
* 接收消息,处理消息,返回结果
*/
@Override
public void channelRead0(final ChannelHandlerContext ctx, RpcRequest request)
throws Exception {
RpcResponse response = new RpcResponse();
response.setRequestId(request.getRequestId());
try {
//根据request来处理具体的业务调用
Object result = handle(request);
response.setResult(result);
} catch (Throwable t) {
response.setError(t);
}
//写入 outbundle(即RpcEncoder)进行下一步处理(即编码)后发送到channel中给客户端
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
/**
* 根据request来处理具体的业务调用
* 调用是通过反射的方式来完成
*
* @param request
* @return
* @throws Throwable
*/
private Object handle(RpcRequest request) throws Throwable {
String className = request.getClassName();
//拿到实现类对象
Object serviceBean = handlerMap.get(className);
//拿到要调用的方法名、参数类型、参数值
String methodName = request.getMethodName();
Class<?>[] parameterTypes = request.getParameterTypes();
Object[] parameters = request.getParameters();
//拿到接口类
Class<?> forName = Class.forName(className);
//调用实现类对象的指定方法并返回结果
Method method = forName.getMethod(methodName, parameterTypes);
return method.invoke(serviceBean, parameters);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
LOGGER.error("server caught exception", cause);
ctx.close();
}
}
③框架的RPC 服务器(用于将用户系统的业务类发布为 RPC 服务)
由于本类实现了ApplicationContextAware InitializingBean
spring构造本对象时会调用setApplicationContext()方法,从而可以在方法中通过自定义注解获得用户的业务接口和实现
还会调用afterPropertiesSet()方法,在方法中启动netty服务器
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* 框架的RPC 服务器(用于将用户系统的业务类发布为 RPC 服务)
* 使用时可由用户通过spring-bean的方式注入到用户的业务系统中
* 由于本类实现了ApplicationContextAware InitializingBean
* spring构造本对象时会调用setApplicationContext()方法,从而可以在方法中通过自定义注解获得用户的业务接口和实现
* 还会调用afterPropertiesSet()方法,在方法中启动netty服务器
*/
public class RpcServer implements ApplicationContextAware, InitializingBean {
private static final Logger LOGGER = LoggerFactory
.getLogger(RpcServer.class);
private String serverAddress;
private ServiceRegistry serviceRegistry;
//用于存储业务接口和实现类的实例对象(由spring所构造)
private Map<String, Object> handlerMap = new HashMap<String, Object>();
public RpcServer(String serverAddress) {
this.serverAddress = serverAddress;
}
//服务器绑定的地址和端口由spring在构造本类时从配置文件中传入
public RpcServer(String serverAddress, ServiceRegistry serviceRegistry) {
this.serverAddress = serverAddress;
//用于向zookeeper注册名称服务的工具类
this.serviceRegistry = serviceRegistry;
}
/**
* 通过注解,获取标注了rpc服务注解的业务类的----接口及impl对象,将它放到handlerMap中
*/
public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
Map<String, Object> serviceBeanMap = ctx
.getBeansWithAnnotation(RpcService.class);
if (MapUtils.isNotEmpty(serviceBeanMap)) {
for (Object serviceBean : serviceBeanMap.values()) {
//从业务实现类上的自定义注解中获取到value,从来获取到业务接口的全名
String interfaceName = serviceBean.getClass()
.getAnnotation(RpcService.class).value().getName();
handlerMap.put(interfaceName, serviceBean);
}
}
}
/**
* 在此启动netty服务,绑定handle流水线:
* 1、接收请求数据进行反序列化得到request对象
* 2、根据request中的参数,让RpcHandler从handlerMap中找到对应的业务imple,调用指定方法,获取返回结果
* 3、将业务调用结果封装到response并序列化后发往客户端
*
*/
public void afterPropertiesSet() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel)
throws Exception {
channel.pipeline()
.addLast(new RpcDecoder(RpcRequest.class))// 注册解码 IN-1
.addLast(new RpcEncoder(RpcResponse.class))// 注册编码 OUT
.addLast(new RpcHandler(handlerMap));//注册RpcHandler IN-2
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
String[] array = serverAddress.split(":");
String host = array[0];
int port = Integer.parseInt(array[1]);
ChannelFuture future = bootstrap.bind(host, port).sync();
LOGGER.debug("server started on port {}", port);
if (serviceRegistry != null) {
serviceRegistry.register(serverAddress);
}
future.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
④公共jar
1)响应object——RpcResponse
/**
* 封装 RPC 响应
* 封装相应object
*/
public class RpcResponse {
private String requestId;
private Throwable error;
private Object result;
public boolean isError() {
return error != null;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public Throwable getError() {
return error;
}
public void setError(Throwable error) {
this.error = error;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
2)请求object ——RpcRequest
/**
* 封装 RPC 请求
* 封装发送的object的反射属性
*/
public class RpcRequest {
private String requestId;
private String className;
private String methodName;
private Class<?>[] parameterTypes;
private Object[] parameters;
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Class<?>[] getParameterTypes() {
return parameterTypes;
}
public void setParameterTypes(Class<?>[] parameterTypes) {
this.parameterTypes = parameterTypes;
}
public Object[] getParameters() {
return parameters;
}
public void setParameters(Object[] parameters) {
this.parameters = parameters;
}
}
3) 序列化工具类
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
/**
* 序列化工具类(基于 Protostuff 实现)
*
*/
public class SerializationUtil {
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
private static Objenesis objenesis = new ObjenesisStd(true);
private SerializationUtil() {
}
/**
* 获取类的schema
* @param cls
* @return
*/
@SuppressWarnings("unchecked")
private static <T> Schema<T> getSchema(Class<T> cls) {
Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
if (schema == null) {
schema = RuntimeSchema.createFrom(cls);
if (schema != null) {
cachedSchema.put(cls, schema);
}
}
return schema;
}
/**
* 序列化(对象 -> 字节数组)
*/
@SuppressWarnings("unchecked")
public static <T> byte[] serialize(T obj) {
Class<T> cls = (Class<T>) obj.getClass();
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
try {
Schema<T> schema = getSchema(cls);
return ProtostuffIOUtil.toByteArray(obj, schema, buffer);//序列化
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
} finally {
buffer.clear();
}
}
/**
* 反序列化(字节数组 -> 对象)
*/
public static <T> T deserialize(byte[] data, Class<T> cls) {
try {
/*
* 如果一个类没有参数为空的构造方法时候,那么你直接调用newInstance方法试图得到一个实例对象的时候是会抛出异常的
* 通过ObjenesisStd可以完美的避开这个问题
* */
T message = (T) objenesis.newInstance(cls);//实例化
Schema<T> schema = getSchema(cls);//获取类的schema
ProtostuffIOUtil.mergeFrom(data, message, schema);
return message;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
4) 解码器
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
/**
* RPC 解码器
*
*/
public class RpcDecoder extends ByteToMessageDecoder {
private Class<?> genericClass;
// 构造函数传入向反序列化的class
public RpcDecoder(Class<?> genericClass) {
this.genericClass = genericClass;
}
@Override
public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
int dataLength = in.readInt();
if (dataLength < 0) {
ctx.close();
}
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
}
//将ByteBuf转换为byte[]
byte[] data = new byte[dataLength];
in.readBytes(data);
//将data转换成object
Object obj = SerializationUtil.deserialize(data, genericClass);
out.add(obj);
}
}
5)编码器
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
/**
* RPC 编码器
*
*/
public class RpcEncoder extends MessageToByteEncoder {
private Class<?> genericClass;
// 构造函数传入向反序列化的class
public RpcEncoder(Class<?> genericClass) {
this.genericClass = genericClass;
}
@Override
public void encode(ChannelHandlerContext ctx, Object inob, ByteBuf out)
throws Exception {
//序列化
if (genericClass.isInstance(inob)) {
byte[] data = SerializationUtil.serialize(inob);
out.writeInt(data.length);
out.writeBytes(data);
}
}
}
pom依赖
<!-- SLF4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<!-- Netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.24.Final</version>
</dependency>
<!-- Protostuff -->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>
<!-- Objenesis -->
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>2.1</version>
</dependency>
⑤服务注册 ,ZK 在该架构中扮演了“服务注册表”的角色,用于注册所有服务器的地址与端口,并对客户端提供服务发现的功能
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 服务注册 ,ZK 在该架构中扮演了“服务注册表”的角色,用于注册所有服务器的地址与端口,并对客户端提供服务发现的功能
*
*/
public class ServiceRegistry {
private static final Logger LOGGER = LoggerFactory
.getLogger(ServiceRegistry.class);
private CountDownLatch latch = new CountDownLatch(1);
private String registryAddress;
public ServiceRegistry(String registryAddress) {
//zookeeper的地址
this.registryAddress = registryAddress;
}
/**
* 创建zookeeper链接
*
* @param data
*/
public void register(String data) {
if (data != null) {
ZooKeeper zk = connectServer();
if (zk != null) {
createNode(zk, data);
}
}
}
/**
* 创建zookeeper链接,监听
*
* @return
*/
private ZooKeeper connectServer() {
ZooKeeper zk = null;
try {
zk = new ZooKeeper(registryAddress, Constant.ZK_SESSION_TIMEOUT,
new Watcher() {
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
latch.countDown();
}
}
});
latch.await();
} catch (Exception e) {
LOGGER.error("", e);
}
return zk;
}
/**
* 创建节点
*
* @param zk
* @param data
*/
private void createNode(ZooKeeper zk, String data) {
try {
byte[] bytes = data.getBytes();
if (zk.exists(Constant.ZK_REGISTRY_PATH, null) == null) {
zk.create(Constant.ZK_REGISTRY_PATH, null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}
String path = zk.create(Constant.ZK_DATA_PATH, bytes,
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
LOGGER.debug("create zookeeper node ({} => {})", path, data);
} catch (Exception e) {
LOGGER.error("", e);
}
}
}
/**
* 常量
*/
public class Constant {
public static final int ZK_SESSION_TIMEOUT = 5000;//zk超时时间
public static final String ZK_REGISTRY_PATH = "/registry";//注册节点
public static final String ZK_DATA_PATH = ZK_REGISTRY_PATH + "/data";//节点
}
4.客户端启动类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class HelloServiceTest {
@Autowired
private RpcProxy rpcProxy;
@Test
public void helloTest1() {
// 调用代理的create方法,代理HelloService接口
HelloService helloService = rpcProxy.create(HelloService.class);
// 调用代理的方法,执行invoke
String result = helloService.hello("World");
System.out.println("服务端返回结果:");
System.out.println(result);
}
@Test
public void helloTest2() {
HelloService helloService = rpcProxy.create(HelloService.class);
String result = helloService.hello(new Person("Yong", "Huang"));
System.out.println("服务端返回结果:");
System.out.println(result);
}
}
配置文件spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:rpc.properties"/>
<bean id="serviceDiscovery" class="cn.zekun.rpc.registry.ServiceDiscovery">
<constructor-arg name="registryAddress" value="${registry.address}"/>
</bean>
<bean id="rpcProxy" class="cn.zekun.rpc.client.RpcProxy">
<constructor-arg name="serviceDiscovery" ref="serviceDiscovery"/>
</bean>
</beans>
rpc.properties文件
# zookeeper server
registry.address=127.0.0.1:2181
5.RPC 代理(用于创建 RPC 服务代理)
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.UUID;
/**
* RPC 代理(用于创建 RPC 服务代理)
*
*/
public class RpcProxy {
private String serverAddress;
private ServiceDiscovery serviceDiscovery;
public RpcProxy(String serverAddress) {
this.serverAddress = serverAddress;
}
public RpcProxy(ServiceDiscovery serviceDiscovery) {
this.serviceDiscovery = serviceDiscovery;
}
/**
* 创建代理
*
* @param interfaceClass
* @return
*/
@SuppressWarnings("unchecked")
public <T> T create(Class<?> interfaceClass) {
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
new Class<?>[] { interfaceClass }, new InvocationHandler() {
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
//创建RpcRequest,封装被代理类的属性
RpcRequest request = new RpcRequest();
request.setRequestId(UUID.randomUUID().toString());
//拿到声明这个方法的业务接口名称
request.setClassName(method.getDeclaringClass()
.getName());
request.setMethodName(method.getName());
request.setParameterTypes(method.getParameterTypes());
request.setParameters(args);
//查找服务
if (serviceDiscovery != null) {
serverAddress = serviceDiscovery.discover();
}
//随机获取服务的地址
String[] array = serverAddress.split(":");
String host = array[0];
int port = Integer.parseInt(array[1]);
//创建Netty实现的RpcClient,链接服务端
RpcClient client = new RpcClient(host, port);
//通过netty向服务端发送请求
RpcResponse response = client.send(request);
//返回信息
if (response.isError()) {
throw response.getError();
} else {
return response.getResult();
}
}
});
}
}
6.服务发现 本类用于client发现server节点的变化 ,实现负载均衡
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 本类用于client发现server节点的变化 ,实现负载均衡
*
*/
public class ServiceDiscovery {
private static final Logger LOGGER = LoggerFactory
.getLogger(ServiceDiscovery.class);
private CountDownLatch latch = new CountDownLatch(1);
private volatile List<String> dataList = new ArrayList<String>();
private String registryAddress;
/**
* zk链接
*
* @param registryAddress
*/
public ServiceDiscovery(String registryAddress) {
this.registryAddress = registryAddress;
ZooKeeper zk = connectServer();
if (zk != null) {
watchNode(zk);
}
}
/**
* 发现新节点
*
* @return
*/
public String discover() {
String data = null;
int size = dataList.size();
// 存在新节点,使用即可
if (size > 0) {
if (size == 1) {
data = dataList.get(0);
LOGGER.debug("using only data: {}", data);
} else {
data = dataList.get(ThreadLocalRandom.current().nextInt(size));
LOGGER.debug("using random data: {}", data);
}
}
return data;
}
/**
* 链接
*
* @return
*/
private ZooKeeper connectServer() {
ZooKeeper zk = null;
try {
zk = new ZooKeeper(registryAddress, Constant.ZK_SESSION_TIMEOUT,
new Watcher() {
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
latch.countDown();
}
}
});
latch.await();
} catch (Exception e) {
LOGGER.error("", e);
}
return zk;
}
/**
* 监听
*
* @param zk
*/
private void watchNode(final ZooKeeper zk) {
try {
// 获取所有子节点
List<String> nodeList = zk.getChildren(Constant.ZK_REGISTRY_PATH,
new Watcher() {
public void process(WatchedEvent event) {
// 节点改变
if (event.getType() == Event.EventType.NodeChildrenChanged) {
watchNode(zk);
}
}
});
List<String> dataList = new ArrayList<String>();
// 循环子节点
for (String node : nodeList) {
// 获取节点中的服务器地址
byte[] bytes = zk.getData(Constant.ZK_REGISTRY_PATH + "/"
+ node, false, null);
// 存储到list中
dataList.add(new String(bytes));
}
LOGGER.debug("node data: {}", dataList);
// 将节点信息记录在成员变量
this.dataList = dataList;
} catch (Exception e) {
LOGGER.error("", e);
}
}
}
7.框架的RPC 客户端(用于发送 RPC 请求)
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 框架的RPC 客户端(用于发送 RPC 请求)
*
*/
public class RpcClient extends SimpleChannelInboundHandler<RpcResponse> {
private static final Logger LOGGER = LoggerFactory
.getLogger(RpcClient.class);
private String host;
private int port;
private RpcResponse response;
private final Object obj = new Object();
public RpcClient(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 链接服务端,发送消息
*
* @param request
* @return
* @throws Exception
*/
public RpcResponse send(RpcRequest request) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel)
throws Exception {
// 向pipeline中添加编码、解码、业务处理的handler
channel.pipeline()
.addLast(new RpcEncoder(RpcRequest.class)) //OUT - 1
.addLast(new RpcDecoder(RpcResponse.class)) //IN - 1
.addLast(RpcClient.this); //IN - 2
}
}).option(ChannelOption.SO_KEEPALIVE, true);
// 链接服务器
ChannelFuture future = bootstrap.connect(host, port).sync();
//将request对象写入outbundle处理后发出(即RpcEncoder编码器)
future.channel().writeAndFlush(request).sync();
// 用线程等待的方式决定是否关闭连接
// 其意义是:先在此阻塞,等待获取到服务端的返回后,被唤醒,从而关闭网络连接
synchronized (obj) {
obj.wait();
}
if (response != null) {
future.channel().closeFuture().sync();
}
return response;
} finally {
group.shutdownGracefully();
}
}
/**
* 读取服务端的返回结果
*/
@Override
public void channelRead0(ChannelHandlerContext ctx, RpcResponse response)
throws Exception {
this.response = response;
synchronized (obj) {
obj.notifyAll();
}
}
/**
* 异常处理
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
LOGGER.error("client caught exception", cause);
ctx.close();
}
}