diff --git a/02nio/nio02/pom.xml b/02nio/nio02/pom.xml index 005de90a..c10848f8 100644 --- a/02nio/nio02/pom.xml +++ b/02nio/nio02/pom.xml @@ -29,7 +29,7 @@ io.netty netty-all - 4.1.45.Final + 4.1.104.Final diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java index e67b7961..1adda64a 100644 --- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java @@ -2,7 +2,10 @@ import io.github.kimmking.gateway.inbound.HttpInboundServer; +import io.netty.util.internal.PlatformDependent; +import java.lang.reflect.Constructor; +import java.nio.ByteBuffer; import java.util.Arrays; public class NettyServerApplication { @@ -12,6 +15,13 @@ public class NettyServerApplication { public static void main(String[] args) { +// sun.misc.Unsafe unsafe = sun.misc.Unsafe.getUnsafe(); +// System.out.println(unsafe.addressSize()); + + + System.out.println("PlatformDependent.hasUnsafe = " + PlatformDependent.javaVersion()); + System.out.println("PlatformDependent.hasUnsafe = " + PlatformDependent.hasUnsafe()); + String proxyPort = System.getProperty("proxyPort","8888"); // 这是之前的单个后端url的例子 diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java index 53493fb4..12fe310a 100644 --- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/filter/HeaderHttpResponseFilter.java @@ -1,10 +1,24 @@ package io.github.kimmking.gateway.filter; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; public class HeaderHttpResponseFilter implements HttpResponseFilter { @Override public void filter(FullHttpResponse response) { response.headers().set("kk", "java-1-nio"); + response.setStatus(HttpResponseStatus.CREATED); +// byte[] array = response.content().array(); +// String content = new String(array); +// System.out.println(content); +// content = content + ",kimmking"; + byte[] bytes = "hello,kimm.".getBytes(); + //response.headers().setInt("Content-Length", bytes.length); + ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes); + ByteBuf content = response.content(); + content.clear(); + content.writeBytes(byteBuf); } } diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java index 69b40fde..1cd13ca2 100644 --- a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java @@ -39,9 +39,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) { // if (uri.contains("/test")) { // handlerTest(fullRequest, ctx); // } - - handler.handle(fullRequest, ctx, filter); - + + String uri = fullRequest.getUri(); + System.out.println(" uri ==>> " + uri); + if(uri.contains("/netty/info")) { + NettyInfoHandler.INSTANCE.handle(fullRequest, ctx); + } else { + handler.handle(fullRequest, ctx, filter); + } } catch(Exception e) { e.printStackTrace(); } finally { diff --git a/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/NettyInfoHandler.java b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/NettyInfoHandler.java new file mode 100644 index 00000000..f179cc82 --- /dev/null +++ b/02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/NettyInfoHandler.java @@ -0,0 +1,83 @@ +package io.github.kimmking.gateway.inbound; + +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.util.internal.PlatformDependent; +import lombok.SneakyThrows; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; + +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/5/31 下午7:13 + */ +public class NettyInfoHandler { + + public final static NettyInfoHandler INSTANCE = new NettyInfoHandler(); + + public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx) { + System.out.println("NettyInfoHandler.handle..."); + Map infos = new HashMap<>(); + infos.put("netty.usedDirectMemory", ""+getNettyUsedDirectMemory()); + infos.put("netty.directMemoryLimit", ""+getNettyDirectMemoryLimit()); + StringBuilder sb = new StringBuilder(); + sb.append("{"); + infos.forEach((k, v) -> { + sb.append("\"").append(k).append("\"") + .append(":") + .append("\"").append(v).append("\"").append(","); + }); + if(sb.length()>1) { + sb.deleteCharAt(sb.length()-1); + } + sb.append("}"); + + byte[] body = ("{\"code\":200,\"msg\":\"success\",\"data\":" + sb +"}").getBytes(); + FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body)); + + response.headers().set("Content-Type", "application/json"); + response.headers().setInt("Content-Length", body.length); + response.headers().set("kk.gw.hanlder", "netty.info"); + + if (fullRequest != null) { + if (!HttpUtil.isKeepAlive(fullRequest)) { + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } else { + //response.headers().set(CONNECTION, KEEP_ALIVE); + ctx.write(response); + } + } + ctx.flush(); + //ctx.close(); + + } + + @SneakyThrows + private static long getNettyUsedDirectMemory() { + Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_COUNTER"); + field.setAccessible(true); + AtomicLong o = (AtomicLong)field.get(null); + return o.get(); + } + + @SneakyThrows + private static long getNettyDirectMemoryLimit() { + Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_LIMIT"); + field.setAccessible(true); + return (Long)field.get(null); + } + +} diff --git a/04fx/spring01/pom.xml b/04fx/spring01/pom.xml index 505009a7..3075aaf2 100644 --- a/04fx/spring01/pom.xml +++ b/04fx/spring01/pom.xml @@ -10,7 +10,7 @@ - 4.3.29.RELEASE + 4.3.30.RELEASE @@ -19,8 +19,8 @@ org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + 11 + 11 diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java new file mode 100644 index 00000000..bcbc7e30 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java @@ -0,0 +1,49 @@ +package io.kimmking.spring02; + +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; + +import java.lang.reflect.Method; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/22 18:01 + */ +public class SpringDemo11 { + + public static void main(String[] args) { + long s = System.currentTimeMillis(); + Enhancer enhancer = new Enhancer(); + enhancer.setInterfaces(new Class[]{IAction.class}); + enhancer.setCallback(new MI()); + enhancer.setUseCache(true); + IAction demo = (IAction) enhancer.create(); + for (int i = 0; i < 5; i++) { + long ss = System.currentTimeMillis(); + System.out.println(demo.action()); + System.out.println( i + " *****====> invoke proxy " + (System.currentTimeMillis() - ss) + " ms"); + } + System.out.println(" *****====> enhancer proxy " + (System.currentTimeMillis() - s) + " ms"); + + } + + public interface IAction { + Object action(); + } + + + static class MI implements MethodInterceptor { + @Override + public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + long s = System.currentTimeMillis(); + System.out.println(" *****==MI==> " + s + " " +"Before:"+method.getName()); + Object result = "S-" + s;//methodProxy.invokeSuper(obj, objects); + System.out.println(" *****==MI==> " + (System.currentTimeMillis() - s) + " ms After:"+method.getName()); + return result; + } + } + +} diff --git a/07rpc/rpc01/client-rest.http b/07rpc/rpc01/client-rest.http index 5808372d..cfe742ec 100644 --- a/07rpc/rpc01/client-rest.http +++ b/07rpc/rpc01/client-rest.http @@ -1 +1 @@ -http://127.0.0.1:8080/api/hello \ No newline at end of file +http://127.0.0.1:8091/api/hello \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/pom.xml b/07rpc/rpc01/rpcfx-core/pom.xml index d8b62724..5a1eeac3 100644 --- a/07rpc/rpc01/rpcfx-core/pom.xml +++ b/07rpc/rpc01/rpcfx-core/pom.xml @@ -21,7 +21,7 @@ com.alibaba fastjson - 1.2.70 + 1.2.83 @@ -45,7 +45,7 @@ org.apache.curator - curator-framework + curator-recipes 5.1.0 @@ -70,11 +70,7 @@ - - org.apache.curator - curator-recipes - 5.1.0 - + diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java index 64f3b99d..29060ace 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java @@ -2,7 +2,9 @@ public interface Filter { - boolean filter(RpcfxRequest request); + RpcfxResponse prefilter(RpcfxRequest request); + + RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response); // Filter next(); diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java index eccb66f5..5ac4fab2 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java @@ -1,9 +1,11 @@ package io.kimmking.rpcfx.api; +import io.kimmking.rpcfx.meta.InstanceMeta; + import java.util.List; public interface LoadBalancer { - String select(List urls); + InstanceMeta select(List instances); } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java index 594aeff5..a4ed3225 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java @@ -1,8 +1,10 @@ package io.kimmking.rpcfx.api; +import io.kimmking.rpcfx.meta.InstanceMeta; + import java.util.List; public interface Router { - List route(List urls); + List route(List instances); } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java index e5365ab7..79a65bde 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java @@ -2,6 +2,7 @@ import io.kimmking.rpcfx.meta.ProviderMeta; import lombok.Getter; +import lombok.Setter; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -17,9 +18,24 @@ public class RpcContext { @Getter - private MultiValueMap providerHolder = new LinkedMultiValueMap<>(); + private final MultiValueMap providerHolder = new LinkedMultiValueMap<>(); @Getter - private Map consumerHolder = new HashMap<>(); + private final Map consumerHolder = new HashMap<>(); + + @Getter + private final Map parameters = new HashMap<>(); + + @Getter + @Setter + private Router router; + + @Getter + @Setter + private LoadBalancer loadBalancer; + + @Getter + @Setter + private Filter[] filters; } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java index 5ee7b9e1..1e9edfb4 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java @@ -5,6 +5,6 @@ @Data public class RpcfxRequest { private String serviceClass; - private String method; + private String methodSign; private Object[] params; } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java index 16527507..a69fe083 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java @@ -2,16 +2,24 @@ import io.kimmking.rpcfx.annotation.RpcfxReference; import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.registry.RegistryConfiguration; import io.kimmking.rpcfx.stub.StubSkeletonHelper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.PropertyValues; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.context.annotation.Import; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; import java.io.Closeable; import java.io.IOException; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -24,12 +32,39 @@ */ @Slf4j @Component +@Import({RegistryConfiguration.class}) public class ConsumerBootstrap implements Closeable, InstantiationAwareBeanPostProcessor { - private RpcContext rpcContext = new RpcContext(); + private RpcContext context = new RpcContext(); private String scanPackage = "io.kimmking"; + @Value("${app.id:app1}") + public String app; + @Value("${app.namespace:public}") + public String ns; + @Value("${app.env:dev}") + public String env; + @Value("${app.mock:false}") + public boolean mock; + @Value("${app.cache:false}") + public boolean cache; + @Value("${app.retry:1}") + public int retry; + + @Autowired + RegistryCenter rc; + + @PostConstruct + public void init() { + this.context.getParameters().put("app.id", app); + this.context.getParameters().put("app.namespace", ns); + this.context.getParameters().put("app.env", env); + this.context.getParameters().put("app.mock", String.valueOf(mock)); + this.context.getParameters().put("app.cache", String.valueOf(cache)); + this.context.getParameters().put("app.retry", String.valueOf(retry)); + } + @Override public void close() throws IOException { @@ -38,14 +73,17 @@ public void close() throws IOException { @Override public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException { if (bean.getClass().getPackage().getName().startsWith(scanPackage)) { - Field[] declaredFields = bean.getClass().getDeclaredFields(); - List consumers = Arrays.stream(declaredFields).filter(field -> field.isAnnotationPresent(RpcfxReference.class)).collect(Collectors.toList()); + Field[] declaredFields = resolveAllField(bean.getClass()); // 解决父类里的注解扫描不到的问题 - consumers.stream().forEach(consumer -> { - Object consumer1 = createConsumer(consumer.getType()); + List consumers = Arrays.stream(declaredFields) + .filter(field -> field.isAnnotationPresent(RpcfxReference.class)) + .collect(Collectors.toList()); + + consumers.stream().forEach(field -> { + Object consumer = createConsumer(field.getType()); try { - consumer.setAccessible(true); - consumer.set(bean, consumer1); + field.setAccessible(true); + field.set(bean, consumer); } catch (IllegalAccessException e) { log.error(e.getMessage(), e); } @@ -54,7 +92,19 @@ public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, Str return null; } + private Field[] resolveAllField(Class aClass) { + List res = new ArrayList<>(20); + while ( !Object.class.equals(aClass) ) { + Field[] fields = aClass.getDeclaredFields(); + res.addAll(Arrays.asList(fields)); + aClass = aClass.getSuperclass(); + } + return res.toArray(new Field[0]); + } + private T createConsumer(Class clazz) { - return StubSkeletonHelper.createConsumer(clazz, rpcContext); + ServiceMeta sm = ServiceMeta.builder().name(clazz.getCanonicalName()) + .app(app).namespace(ns).env(env).build(); + return (T) StubSkeletonHelper.createConsumer(sm, context, rc); } } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java new file mode 100644 index 00000000..66d9ea89 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java @@ -0,0 +1,70 @@ +package io.kimmking.rpcfx.consumer; + + +import com.alibaba.fastjson.parser.ParserConfig; +import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; + +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; + +public final class RpcfxConsumerInvoker { + + static { + ParserConfig.getGlobalInstance().addAccept("io.kimmking"); + } + + RpcContext ctx; + + RegistryCenter rc; + + public RpcfxConsumerInvoker(RpcContext ctx, RegistryCenter rc) { + this.ctx = ctx; + this.rc = rc; //"localhost:2181" + } + + public void start() { + this.rc.start(); + } + + public void stop() { + this.rc.stop(); + } + + public T createFromRegistry(final ServiceMeta sm, RpcContext ctx) { + + String service = sm.getName();//"io.kimking.rpcfx.demo.api.UserService"; + System.out.println("====> "+service); + List invokers = new ArrayList<>(); + Class serviceClass = null; + try { + + serviceClass = Class.forName(service); + + List insts = rc.fetchInstances(sm); + if(insts != null && insts.size()>0) invokers.addAll(insts); + rc.subscribe(sm, e -> { + invokers.clear(); + invokers.addAll((List)e.getData()); + }); + + } catch (Exception ex) { + ex.printStackTrace(); + throw new RuntimeException(ex); + } + + return (T) create(serviceClass, invokers, ctx); + + } + + private T create(Class serviceClass, List invokers, RpcContext ctx) { + RpcfxInvocationHandler invocationHandler + = new RpcfxInvocationHandler(serviceClass, invokers, ctx); + return (T) Proxy.newProxyInstance(RpcfxConsumerInvoker.class.getClassLoader(), + new Class[]{serviceClass}, invocationHandler); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java index 5c755596..0325e0c7 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java @@ -2,31 +2,32 @@ import com.alibaba.fastjson.JSON; import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.meta.InstanceMeta; import io.kimmking.rpcfx.stub.StubSkeletonHelper; +import io.kimmking.rpcfx.utils.MethodUtils; import okhttp3.*; -import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; +import java.net.SocketTimeoutException; import java.util.List; import java.util.concurrent.TimeUnit; public class RpcfxInvocationHandler implements InvocationHandler { + public final Object target = new Object(); + public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8"); private final Class serviceClass; - private final List invokers; - private final Router router; - private final LoadBalancer loadBalance; - private final Filter[] filters; + private final List invokers; + + private final RpcContext context; - public RpcfxInvocationHandler(Class serviceClass, List invokers, Router router, LoadBalancer loadBalance, Filter... filters) { + public RpcfxInvocationHandler(Class serviceClass, List invokers, RpcContext ctx) { this.serviceClass = serviceClass; this.invokers = invokers; - this.router = router; - this.loadBalance = loadBalance; - this.filters = filters; + this.context = ctx; } // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx @@ -36,66 +37,104 @@ public RpcfxInvocationHandler(Class serviceClass, List invokers, @Override public Object invoke(Object proxy, Method method, Object[] params) throws Throwable { + long start = System.currentTimeMillis(); + if (!StubSkeletonHelper.checkRpcMethod(method)){ - return null ; + return method.invoke(target, params); } - List urls = router.route(invokers); + + int retry = 2; + while (retry-- > 0) { + System.out.println("retry:" + retry); + try { + + // check mock, 挡板功能 TODO 3 + + List insts = context.getRouter().route(invokers); // System.out.println("router.route => "); // urls.forEach(System.out::println); - String url = loadBalance.select(urls); // router, loadbalance + InstanceMeta instance = context.getLoadBalancer().select(insts); // router, loadbalance // System.out.println("loadBalance.select => "); // System.out.println("final => " + url); - if (url == null) { - throw new RuntimeException("No available providers from registry center."); - } + if (instance == null) { + throw new RuntimeException("No available providers from registry center."); + } - // 加filter地方之二 - // mock == true, new Student("hubao"); - RpcfxRequest request = new RpcfxRequest(); - request.setServiceClass(this.serviceClass.getName()); - request.setMethod(method.getName()); - request.setParams(params); + RpcfxRequest request = new RpcfxRequest(); + request.setServiceClass(this.serviceClass.getName()); + request.setMethodSign(MethodUtils.methodSign(method)); + request.setParams(params); - if (null!=filters) { - for (Filter filter : filters) { - if (!filter.filter(request)) { - return null; + Filter[] filters = context.getFilters(); + + if (null != filters) { + for (Filter filter : filters) { + RpcfxResponse response = filter.prefilter(request); + if (response != null) { + return JSON.parse(response.getResult().toString()); + } + } } - } - } - RpcfxResponse response = post(request, url); + // 没有控制超时,可能会很久 TODO 2 + RpcfxResponse response = post(request, instance); + + if (null != filters) { + for (Filter filter : filters) { + RpcfxResponse postResponse = filter.postfilter(request, response); + if (postResponse!=null) { + response = postResponse; + } + } + } + + System.out.println("Invoke spend " + (System.currentTimeMillis()-start) + " ms"); + + // 加filter地方之三 + // Student.setTeacher("cuijing"); - // 加filter地方之三 - // Student.setTeacher("cuijing"); + // 这里判断response.status,处理异常 + // 考虑封装一个全局的RpcfxException - // 这里判断response.status,处理异常 - // 考虑封装一个全局的RpcfxException + return JSON.parse(response.getResult().toString()); + + } catch (RuntimeException ex) { + ex.printStackTrace(); + if(! (ex.getCause() instanceof SocketTimeoutException)) { + break; + } + } + } + return null; - return JSON.parse(response.getResult().toString()); } OkHttpClient client = new OkHttpClient.Builder() .connectionPool(new ConnectionPool(128, 60, TimeUnit.SECONDS)) // .dispatcher(dispatcher) -// .readTimeout(httpClientConfig.getReadTimeout(), TimeUnit.SECONDS) -// .writeTimeout(httpClientConfig.getWriteTimeout(), TimeUnit.SECONDS) -// .connectTimeout(httpClientConfig.getConnectTimeout(), TimeUnit.SECONDS) + .readTimeout(1, TimeUnit.SECONDS) + .writeTimeout(1, TimeUnit.SECONDS) + .connectTimeout(1, TimeUnit.SECONDS) .build(); - private RpcfxResponse post(RpcfxRequest req, String url) throws IOException { + private RpcfxResponse post(RpcfxRequest req, InstanceMeta instance) throws Exception { String reqJson = JSON.toJSONString(req); -// System.out.println("req json: "+reqJson); + System.out.println("req json: "+reqJson); final Request request = new Request.Builder() - .url(url) + .url(instance.toString()) .post(RequestBody.create(JSONTYPE, reqJson)) .build(); - String respJson = client.newCall(request).execute().body().string(); -// System.out.println("resp json: "+respJson); + String respJson; + try { + respJson = client.newCall(request).execute().body().string(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + System.out.println("resp json: "+respJson); return JSON.parseObject(respJson, RpcfxResponse.class); } } \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvoker.java deleted file mode 100644 index 356c7fe3..00000000 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvoker.java +++ /dev/null @@ -1,91 +0,0 @@ -package io.kimmking.rpcfx.consumer; - - -import com.alibaba.fastjson.parser.ParserConfig; -import io.kimmking.rpcfx.api.*; -import org.apache.curator.RetryPolicy; -import org.apache.curator.framework.CuratorFramework; -import org.apache.curator.framework.CuratorFrameworkFactory; -import org.apache.curator.framework.recipes.cache.TreeCache; -import org.apache.curator.framework.recipes.cache.TreeCacheEvent; -import org.apache.curator.framework.recipes.cache.TreeCacheListener; -import org.apache.curator.retry.ExponentialBackoffRetry; - -import java.lang.reflect.Proxy; -import java.util.ArrayList; -import java.util.List; - -public final class RpcfxInvoker { - - static { - ParserConfig.getGlobalInstance().addAccept("io.kimmking"); - } - CuratorFramework client; - String zkUrl = null; - - public RpcfxInvoker(String zkUrl) { - this.zkUrl = zkUrl; //"localhost:2181" - this.start(); - } - - public void start() { - RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); - client = CuratorFrameworkFactory.builder().connectString(this.zkUrl).namespace("rpcfx").retryPolicy(retryPolicy).build(); - client.start(); - } - - public void stop() { - client.close(); - } - - public T createFromRegistry(final Class serviceClass, Router router, LoadBalancer loadBalance, Filter filter) { - - String service = serviceClass.getCanonicalName();//"io.kimking.rpcfx.demo.api.UserService"; - System.out.println("====> "+service); - List invokers = new ArrayList<>(); - - try { - - if ( null == client.checkExists().forPath("/" + service)) { - return null; - } - - fetchInvokers(client, service, invokers); - - final TreeCache treeCache = TreeCache.newBuilder(client, "/" + service).setCacheData(true).setMaxDepth(2).build(); - treeCache.getListenable().addListener(new TreeCacheListener() { - public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception { - System.out.println("treeCacheEvent: "+treeCacheEvent); - fetchInvokers(client, service, invokers); - } - }); - treeCache.start(); - - } catch (Exception ex) { - ex.printStackTrace(); - } - - return (T) create(serviceClass, invokers, router, loadBalance, filter); - - } - - - - private void fetchInvokers(CuratorFramework client, String service, List invokers) throws Exception { - List services = client.getChildren().forPath("/" + service); - invokers.clear(); - for (String svc : services) { - System.out.println(svc); - String url = svc.replace("_", ":"); - invokers.add("http://" + url); - } - } - - private T create(Class serviceClass, List invokers, Router router, LoadBalancer loadBalance, Filter... filters) { - RpcfxInvocationHandler invocationHandler - = new RpcfxInvocationHandler(serviceClass, invokers, router, loadBalance, filters); - return (T) Proxy.newProxyInstance(RpcfxInvoker.class.getClassLoader(), - new Class[]{serviceClass}, invocationHandler); - } - -} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/discovery/DiscoveryClient.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/discovery/DiscoveryClient.java deleted file mode 100644 index 46f89433..00000000 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/discovery/DiscoveryClient.java +++ /dev/null @@ -1,10 +0,0 @@ -package io.kimmking.rpcfx.discovery; - -/** - * Description for this class. - * - * @Author : kimmking(kimmking@apache.org) - * @create 2024/1/13 20:17 - */ -public class DiscoveryClient { -} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java new file mode 100644 index 00000000..e1388143 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java @@ -0,0 +1,48 @@ +package io.kimmking.rpcfx.meta; + +import com.google.common.base.Strings; +import lombok.*; + +import java.net.URI; +import java.util.Map; +import java.util.Objects; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 19:46 + */ + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(of = {"scheme", "host", "port", "context"}) +public class InstanceMeta { + + private String scheme; + private String host; + private Integer port; + private String context; + private boolean status; + private Map metadata; + + public static InstanceMeta from(String instance) { + URI uri = URI.create(instance); + String path = uri.getPath(); + path = Strings.isNullOrEmpty(path) ? "" : path.substring(1); + return InstanceMeta.builder() + .scheme(uri.getScheme()) + .host(uri.getHost()) + .port(uri.getPort()) + .context(path) + .build(); + } + + @Override + public String toString() { + return scheme + "://" + host + ":" + port + "/" + context; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java new file mode 100644 index 00000000..239042b8 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java @@ -0,0 +1,24 @@ +package io.kimmking.rpcfx.meta; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/4/13 21:43 + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(of = {"url"}) +public class ServerMeta { + private String url; + private boolean leader; + private boolean status; + private long version; +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java new file mode 100644 index 00000000..c443a272 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java @@ -0,0 +1,25 @@ +package io.kimmking.rpcfx.meta; + +import lombok.Builder; +import lombok.Data; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 19:46 + */ +@Data +@Builder +public class ServiceMeta { + + private String app; + private String namespace; + private String env; + private String name; + + @Override + public String toString() { + return String.format("%s_%s_%s_%s", app, namespace, env, name); + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java index 5b541a34..ce714bb0 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java @@ -2,14 +2,19 @@ import io.kimmking.rpcfx.annotation.RpcfxService; import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.registry.RegistryConfiguration; import io.kimmking.rpcfx.stub.StubSkeletonHelper; import lombok.Getter; import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; @@ -29,6 +34,7 @@ */ @Component +@Import({RegistryConfiguration.class}) public class ProviderBootstrap { @Autowired @@ -37,16 +43,24 @@ public class ProviderBootstrap { @Autowired Environment environment; - private RpcContext rpcContext = new RpcContext(); + @Value("${app.id:app1}") + public String app; + @Value("${app.namespace:public}") + public String ns; + @Value("${app.env:dev}") + public String env; + + private final RpcContext context = new RpcContext(); @Getter - private RpcfxInvoker invoker; + private final RpcfxProviderInvoker invoker = new RpcfxProviderInvoker(context);; - private static String PROTO = "http"; + private static String SCHEME = "http"; private static String ip; private static int port; - RegistryCenter registry = new RegistryCenter(); + @Autowired + RegistryCenter registry;// = new KKRegistryCenter(); @SneakyThrows @PostConstruct @@ -77,8 +91,7 @@ private void buildProvider() { } private void createProvider(Class clazz, Object bean) { - StubSkeletonHelper.createProvider(clazz, bean, rpcContext); // 初始化了holder - this.invoker = new RpcfxInvoker(rpcContext); + StubSkeletonHelper.createProvider(clazz, bean, context); // 初始化了holder } @Order(Integer.MIN_VALUE) @@ -91,13 +104,18 @@ private void registerServices() { registry.start(); - System.out.println("registry all services from zk..."); - rpcContext.getProviderHolder().forEach( (x,y) -> + System.out.println("registry all services from RegistryCenter..."); + context.getProviderHolder().forEach( (x, y) -> { System.out.println(" register " + x); + ServiceMeta sm = ServiceMeta.builder().name(x) + .app(app).namespace(ns).env(env).build(); + InstanceMeta im = InstanceMeta.builder() + .scheme(SCHEME).host(ip).port(port).context("").build(); try { - registry.registerService(x, ip, port); + registry.registerService(sm, im); + registry.heartbeat(sm, im); } catch (Exception e) { throw new RuntimeException(e); } @@ -111,12 +129,16 @@ public void stop() { } private void unregisterServices() { - System.out.println("unregistry all services from zk..."); - rpcContext.getProviderHolder().forEach( (x,y) -> + System.out.println("unregistry all services from RegistryCenter..."); + context.getProviderHolder().forEach( (x, y) -> { System.out.println(" unregister " + x); + ServiceMeta sm = ServiceMeta.builder().name(x) + .app(app).namespace(ns).env(env).build(); + InstanceMeta im = InstanceMeta.builder() + .scheme(SCHEME).host(ip).port(port).context("").build(); try { - registry.unregisterService(x, ip, port); + registry.unregisterService(sm, im); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java similarity index 87% rename from 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxInvoker.java rename to 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java index 4767e046..6842dfe6 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxInvoker.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java @@ -13,11 +13,11 @@ import java.util.List; import java.util.Optional; -public class RpcfxInvoker { +public class RpcfxProviderInvoker { RpcContext context; - public RpcfxInvoker(RpcContext context) { + public RpcfxProviderInvoker(RpcContext context) { this.context = context; } @@ -25,10 +25,11 @@ public RpcfxResponse invoke(RpcfxRequest request) { RpcfxResponse response = new RpcfxResponse(); String serviceClass = request.getServiceClass(); - ProviderMeta meta = findProvider(serviceClass, request.getMethod()); + ProviderMeta meta = findProvider(serviceClass, request.getMethodSign()); try { Method method = meta.getMethod(); + // 没有控制超时,所以可能会很久 TODO 1 Object result = method.invoke(meta.getServiceImpl(), request.getParams()); // dubbo, fastjson, // 两次json序列化能否合并成一个 response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName)); @@ -50,7 +51,8 @@ public RpcfxResponse invoke(RpcfxRequest request) { protected ProviderMeta findProvider(String interfaceName, String methodSign) { List providerMetas = context.getProviderHolder().get(interfaceName); if (!CollectionUtils.isEmpty(providerMetas)) { - Optional providerMeta = providerMetas.stream().filter(provider -> methodSign.equals(provider.getMethodSign())).findFirst(); + Optional providerMeta = providerMetas.stream() + .filter(provider -> methodSign.equals(provider.getMethodSign())).findFirst(); return providerMeta.orElse(null); } return null; diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java new file mode 100644 index 00000000..971cbcda --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java @@ -0,0 +1,13 @@ +package io.kimmking.rpcfx.registry; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 20:19 + */ +public interface ChangedListener { + + void fireEvent(Event e); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java new file mode 100644 index 00000000..19a92cd4 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java @@ -0,0 +1,29 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.meta.InstanceMeta; +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 20:20 + */ +public interface Event { + + T getData(); + + static Event> withData(List list) { + return new ChangedEvent(list); + } + + @Data + @AllArgsConstructor + class ChangedEvent implements Event> { + List data; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java index f7ec83bf..871c1a7e 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java @@ -1,57 +1,35 @@ package io.kimmking.rpcfx.registry; import io.kimmking.rpcfx.api.ServiceProviderDesc; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; import org.apache.curator.RetryPolicy; -import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; +import java.util.List; + /** * Description for this class. * * @Author : kimmking(kimmking@apache.org) - * @create 2024/1/13 20:16 + * @create 2024/2/8 15:23 */ -public class RegistryCenter { - - CuratorFramework client = null; - public void start() { - RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); - client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("rpcfx").retryPolicy(retryPolicy).build(); - client.start(); - } - - public void stop(){ - client.close(); - } - - public void registerService(String service, String host, int port) throws Exception { - ServiceProviderDesc userServiceSesc = ServiceProviderDesc.builder() - .host(host) - .port(port).serviceClass(service).build(); - // String userServiceSescJson = JSON.toJSONString(userServiceSesc); - - try { - if ( null == client.checkExists().forPath("/" + service)) { - client.create().withMode(CreateMode.PERSISTENT).forPath("/" + service, "service".getBytes()); - } - } catch (Exception ex) { - ex.printStackTrace(); - } - - client.create().withMode(CreateMode.EPHEMERAL). - forPath( "/" + service + "/" + userServiceSesc.getHost() + "_" + userServiceSesc.getPort(), "provider".getBytes()); - } - - public void unregisterService(String service, String host, int port) throws Exception { - - if (null == client.checkExists().forPath("/" + service)) { - return; - } - System.out.println("delete " + "/" + service + "/" + host + "_" + port); - client.delete().quietly(). - forPath( "/" + service + "/" + host + "_" + port); - } +public interface RegistryCenter { + + void start(); + + void stop(); + + void registerService(ServiceMeta service, InstanceMeta instance) throws Exception; + + void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception; + + List fetchInstances(ServiceMeta service) throws Exception; + + void subscribe(ServiceMeta service, ChangedListener> listener); + + void heartbeat(ServiceMeta service, InstanceMeta instance); } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java new file mode 100644 index 00000000..8391c4aa --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java @@ -0,0 +1,24 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.registry.kkregistry.KKRegistryCenter; +import io.kimmking.rpcfx.registry.zookeeper.ZookeeperRegistryCenter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/9 01:05 + */ + +@Configuration +public class RegistryConfiguration { + + @Bean + RegistryCenter createRC() { + return new KKRegistryCenter(); + //return new ZookeeperRegistryCenter(); //KKRegistryCenter(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java new file mode 100644 index 00000000..bbf0e17e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java @@ -0,0 +1,42 @@ +package io.kimmking.rpcfx.registry.kkregistry; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/1 06:18 + */ +public class KKHeathChecker { + + final int interval = 5_000; + + final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"); + + public void check(Callback callback) { + executor.scheduleWithFixedDelay(() -> { + System.out.println("start to check kk health ...[" + DTF.format(LocalDateTime.now()) + "]"); + try { + callback.call(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }, interval, interval, TimeUnit.MILLISECONDS); + } + + public void stop() { + this.executor.shutdown(); + } + + public interface Callback { + void call() throws Exception; + } + + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java new file mode 100644 index 00000000..001a3854 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java @@ -0,0 +1,209 @@ +package io.kimmking.rpcfx.registry.kkregistry; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServerMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.ChangedListener; +import io.kimmking.rpcfx.registry.Event; +import io.kimmking.rpcfx.registry.RegistryCenter; +import lombok.SneakyThrows; +import okhttp3.ConnectionPool; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +import static io.kimmking.rpcfx.consumer.RpcfxInvocationHandler.JSONTYPE; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 15:25 + */ +public class KKRegistryCenter implements RegistryCenter { + + public String RC_Server = "http://localhost:8485"; + private ServerMeta leader; + private List servers; + private Map TV = new HashMap<>(); + + OkHttpClient client; + @SneakyThrows + @Override + public void start() { + client = new OkHttpClient.Builder() + .connectionPool(new ConnectionPool(128, 60, TimeUnit.SECONDS)) +// .dispatcher(dispatcher) + .readTimeout(65, TimeUnit.SECONDS) + .writeTimeout(65, TimeUnit.SECONDS) + .connectTimeout(3, TimeUnit.SECONDS) + .build(); + + String url = RC_Server + "/cluster"; + boolean init = false; + while(!init) { + System.out.println("===============>> cluster info from :" + url); + List new_servers = null; + ServerMeta new_leader = null; + try { + String respJson = get(url); + new_servers = JSON.parseObject(respJson, new TypeReference>() { + }); + new_leader = new_servers.stream().filter(ServerMeta::isStatus) + .filter(ServerMeta::isLeader).findFirst().orElse(null); + } catch (Exception exception) { + exception.printStackTrace(); + } + + if(new_leader == null) { + System.out.println("===============>> no leader, 500ms later and retry."); + Thread.sleep(500); + Random random = new Random(); + if(new_servers !=null && new_servers.size() > 1) { + url = new_servers.get(random.nextInt(new_servers.size())).getUrl() + "/cluster"; + } else if((new_servers ==null || new_servers.isEmpty()) && !servers.isEmpty()) { + url = servers.get(random.nextInt(servers.size())).getUrl() + "/cluster"; + } + } else { + this.servers = new_servers; + this.leader = new_leader; + init = true; + System.out.println("===============>> init ok, new_leader = " + new_leader); + System.out.println("===============>> init ok, new_servers = " + new_servers); + } + } + } + + @Override + public void stop() { + this.checker.stop(); + } + + @Override + public void registerService(ServiceMeta service, InstanceMeta instance) throws Exception { + instance.setStatus(true); + String reqJson = JSON.toJSONString(instance); + String url = leader.getUrl() + "/reg?service=" + service; + post(url, reqJson); +// String reqJson = "{\n" + +// " \"scheme\": \"http\",\n" + +// " \"ip\": \"" + instance.getIp() + "\",\n" + +// " \"port\": \"" + instance.getPort() + "\",\n" + +// " \"context\": \"\",\n" + +// " \"status\": \"online\",\n" + +// " \"metadata\": {\n" + +// " \"env\": \"dev\",\n" + +// " \"tag\": \"RED\"\n" + +// " }\n" + +// "}"; +// final Request request = new Request.Builder() +// .url("http://localhost:8484/reg?service=" + service) +// .post(RequestBody.create(JSONTYPE, reqJson)) +// .build(); +// String respJson = client.newCall(request).execute().body().string(); +// System.out.println(respJson); + } + + private String post(String url, String reqJson) throws IOException { + System.out.println(" ====> request: " + url); + final Request request = new Request.Builder() + .url(url) + .post(RequestBody.create(JSONTYPE, reqJson)) + .build(); + String respJson = client.newCall(request).execute().body().string(); + System.out.println(" ====> response: " + respJson); + return respJson; + } + + private String get(String url) throws IOException { + System.out.println(" ====> request: " + url); + final Request request = new Request.Builder() + .url(url) + .get() + .build(); + String respJson = client.newCall(request).execute().body().string(); + System.out.println(" ====> response: " + respJson); + return respJson; + } + + @Override + public void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception { + String reqJson = "{\n" + + " \"scheme\": \"http\",\n" + + " \"host\": \"" + instance.getHost() + "\",\n" + + " \"port\": \"" + instance.getPort() + "\",\n" + + " \"context\": \"\"\n" + + "}"; + String url = leader.getUrl() + "/unreg?service=" + service; + post(url, reqJson); + } + + public List fetchInstances(ServiceMeta service) throws Exception { + String url = RC_Server + "/findAll?service=" + service; + String respJson = get(url); + List instances = JSON.parseObject(respJson, new TypeReference>() { + }); + return instances; + } + + KKHeathChecker checker = new KKHeathChecker(); + + // for Consumer + public void subscribe(ServiceMeta service, final ChangedListener> listener) { + checker.check( () -> { + if(hb(service)) { + List instances = fetchInstances(service); + Event> e = Event.withData(instances); + listener.fireEvent(e); + } + }); + + // 定时器轮询 + // 保存上一次的TV + // 如果有差异就fire + } + + private boolean hb(ServiceMeta service) throws Exception { + String svc = service.toString(); + String url = RC_Server + "/version?service=" + svc; + String respJson = get(url); + Long v = Long.valueOf(respJson); + Long o = TV.getOrDefault(svc, -1L); + if ( v > o) { + TV.put(svc, v); + return o > -1L; + } + return false; + } + + + // for Provider + public void heartbeat(ServiceMeta service, InstanceMeta instance) { + checker.check( () -> { + heart(service, instance); + }); + } + + Long heart(ServiceMeta service, InstanceMeta instance) throws Exception { + String reqJson = "{\n" + + " \"scheme\": \"http\",\n" + + " \"host\": \"" + instance.getHost() + "\",\n" + + " \"port\": \"" + instance.getPort() + "\",\n" + + " \"context\": \"\",\n" + + " \"status\": true\n" + + "}"; + String url = leader.getUrl() + "/renew?service=" + service; + String respJson = post(url, reqJson); + return Long.valueOf(respJson); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java new file mode 100644 index 00000000..37f7b6ea --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java @@ -0,0 +1,101 @@ +package io.kimmking.rpcfx.registry.zookeeper; + +import io.kimmking.rpcfx.api.ServiceProviderDesc; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.ChangedListener; +import io.kimmking.rpcfx.registry.Event; +import io.kimmking.rpcfx.registry.RegistryCenter; +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; + +import java.util.ArrayList; +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 20:16 + */ +public class ZookeeperRegistryCenter implements RegistryCenter { + +// private final List listeners = new ArrayList<>(); +// public void addListener(ChangedListener listener) { +// this.listeners.add(listener); +// } + + CuratorFramework client = null; + public void start() { + RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); + client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("rpcfx").retryPolicy(retryPolicy).build(); + client.start(); + } + + public void stop(){ + client.close(); + } + + public void registerService(ServiceMeta service, InstanceMeta instance) throws Exception { + ServiceProviderDesc userServiceSesc = ServiceProviderDesc.builder() + .host(instance.getHost()) + .port(instance.getPort()).serviceClass(service.getName()).build(); + // String userServiceSescJson = JSON.toJSONString(userServiceSesc); + + try { + if ( null == client.checkExists().forPath("/" + service)) { + client.create().withMode(CreateMode.PERSISTENT).forPath("/" + service, "service".getBytes()); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + client.create().withMode(CreateMode.EPHEMERAL). + forPath( "/" + service + "/" + userServiceSesc.getHost() + "_" + userServiceSesc.getPort(), "provider".getBytes()); + } + + public void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception { + + if (null == client.checkExists().forPath("/" + service)) { + return; + } + System.out.println("delete " + "/" + service + "/" + instance.getHost() + "_" + instance.getPort()); + client.delete().quietly(). + forPath( "/" + service + "/" + instance.getHost() + "_" + instance.getPort()); + } + + public List fetchInstances(ServiceMeta service) throws Exception { + List services = client.getChildren().forPath("/" + service); + List instances = new ArrayList<>(); + for (String svc : services) { + System.out.println(svc); + String url = svc.replace("_", ":"); + instances.add(InstanceMeta.from("http://" + url)); + } + return instances; + } + + public void subscribe(ServiceMeta service, ChangedListener listener) { + final TreeCache treeCache = TreeCache.newBuilder(client, "/" + service).setCacheData(true).setMaxDepth(2).build(); + treeCache.getListenable().addListener((curatorFramework, treeCacheEvent) -> { + System.out.println("treeCacheEvent: "+treeCacheEvent); + List instances = fetchInstances(service); + listener.fireEvent(Event.withData(instances)); + }); + try { + treeCache.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void heartbeat(ServiceMeta service, InstanceMeta instance) { + // do nothing + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java new file mode 100644 index 00000000..9fe5269c --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java @@ -0,0 +1,30 @@ +package io.kimmking.rpcfx.stub; + +import io.kimmking.rpcfx.utils.MockUtils; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/11 02:57 + */ +public class MockHandler implements InvocationHandler { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Class type = method.getReturnType(); + System.out.println("invoke by mock handler..."); + return MockUtils.mock(type, null); + } + + public static T createMock(Class serviceClass) { + //final ServiceMeta sm, Router router, LoadBalancer loadBalance, Filter filter) { + return (T) Proxy.newProxyInstance(MockHandler.class.getClassLoader(), + new Class[]{serviceClass}, new MockHandler()); + + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java index ebe5f7ab..e77215a8 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java @@ -1,14 +1,18 @@ package io.kimmking.rpcfx.stub; import io.kimmking.rpcfx.api.*; -import io.kimmking.rpcfx.consumer.RpcfxInvoker; +import io.kimmking.rpcfx.consumer.RpcfxConsumerInvoker; +import io.kimmking.rpcfx.meta.InstanceMeta; import io.kimmking.rpcfx.meta.ProviderMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.utils.MethodUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.util.MultiValueMap; import java.lang.reflect.Method; -import java.util.List; -import java.util.Random; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; /** * @author lirui @@ -32,7 +36,7 @@ public static void createProvider(Class clazz, Object serviceImpl, RpcContext } private static ProviderMeta buildProviderMeta(Method method, Object serviceImpl) { - String methodSign = method.getName();//MethodUtils.methodSign(method); + String methodSign = MethodUtils.methodSign(method); ProviderMeta providerMeta = new ProviderMeta(); providerMeta.setMethod(method); providerMeta.setServiceImpl(serviceImpl); @@ -54,43 +58,129 @@ public static boolean checkRpcMethod(final Method method) { return true; } - public static T createConsumer(Class clazz, RpcContext rpcContext) { - String clazzName = clazz.getName(); - T proxyHandler = (T) rpcContext.getConsumerHolder().get(clazzName); + public static T createConsumer(ServiceMeta sm, RpcContext ctx, RegistryCenter rc) { + String clazzName = sm.getName(); + Class serviceClass = null; + try { + serviceClass = Class.forName(clazzName); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + T proxyHandler = (T) ctx.getConsumerHolder().get(clazzName); if (proxyHandler == null) { // TODO configuration - proxyHandler = new RpcfxInvoker("localhost:2181") - .createFromRegistry(clazz, new TagRouter(), - new RandomLoadBalancer(), new CuicuiFilter()); - rpcContext.getConsumerHolder().put(clazzName, proxyHandler); + + ctx.setRouter(new TagRouter()); + ctx.setLoadBalancer(new RoundRibbonLoadBalancer()); + ctx.setFilters(createFilters(ctx)); + + T mockHandler = createMockHandler(ctx, serviceClass); + if(mockHandler != null) { + return mockHandler; + } + + RpcfxConsumerInvoker consumerInvoker = new RpcfxConsumerInvoker(ctx, rc); + consumerInvoker.start(); + proxyHandler = consumerInvoker.createFromRegistry(sm, ctx); + ctx.getConsumerHolder().put(clazzName, proxyHandler); + } + return proxyHandler; + } + + private static Filter[] createFilters(RpcContext ctx) { + String cache = ctx.getParameters().getOrDefault("app.cache", "false"); + Filter[] filters = null; + if("true".equalsIgnoreCase(cache)) { + filters = new Filter[]{new CuicuiFilter(), new CacheFilter()}; + } else { + filters = new Filter[]{new CuicuiFilter()}; } - return (T) proxyHandler; + return filters; + } + + private static T createMockHandler(RpcContext ctx, Class serviceClass) { + String mock = ctx.getParameters().getOrDefault("app.mock", "false"); + if("true".equalsIgnoreCase(mock)) { + return (T) MockHandler.createMock(serviceClass); + } + return null; } private static class TagRouter implements Router { @Override - public List route(List urls) { - return urls; + public List route(List instances) { + return instances; + } + } + + private static class RoundRibbonLoadBalancer implements LoadBalancer { + private final AtomicInteger count = new AtomicInteger(0); + @Override + public InstanceMeta select(List instances) { + if(instances.isEmpty()) return null; + return instances.get((count.getAndIncrement() & Integer.MAX_VALUE) % instances.size()); } } private static class RandomLoadBalancer implements LoadBalancer { private final Random random = new Random(); @Override - public String select(List urls) { - if(urls.isEmpty()) return null; - return urls.get(random.nextInt(urls.size())); + public InstanceMeta select(List instances) { + if(instances.isEmpty()) return null; + return instances.get(random.nextInt(instances.size())); } } @Slf4j private static class CuicuiFilter implements Filter { @Override - public boolean filter(RpcfxRequest request) { + public RpcfxResponse prefilter(RpcfxRequest request) { + //log.info("filter {} -> {}", this.getClass().getName(), request.toString()); + //System.out.printf("filter %s -> %s%n", this.getClass().getName(), request.toString()); + return null; + } + + @Override + public RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response) { + return response; + } + + } + + private static class CacheFilter implements Filter { + + static Map CACHE = new HashMap<>(); + + @Override + public RpcfxResponse prefilter(RpcfxRequest request) { + RpcfxResponse response = CACHE.get(genKey(request)); + if(response != null) { + System.out.println("CacheFilter.prefilter hit! => request: \n" + request + "\n =>response: \n" + response); + } + return response; //log.info("filter {} -> {}", this.getClass().getName(), request.toString()); //System.out.printf("filter %s -> %s%n", this.getClass().getName(), request.toString()); - return true; } + + @Override + public RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response) { + String key = genKey(request); + if(!CACHE.containsKey(key)) { + CACHE.put(key, response); + } + return response; + } + + } + + public static String genKey(RpcfxRequest request) { + StringBuilder sb = new StringBuilder(); + sb.append(request.getServiceClass()); + sb.append("@"); + sb.append(request.getMethodSign()); + //sb.append(""); + Arrays.stream(request.getParams()).forEach(x -> sb.append("_"+x.toString())); + return sb.toString(); } } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java index 8158ccff..f17a102f 100644 --- a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java @@ -9,10 +9,10 @@ public class MethodUtils { public static String methodSign(Method method) { if (method != null) { - StringBuilder builder = new StringBuilder("method:"); + StringBuilder builder = new StringBuilder(); String name = method.getName(); builder.append(name); - builder.append("_"); + builder.append("@"); int count = method.getParameterCount(); builder.append(count); builder.append("_"); @@ -20,8 +20,9 @@ public static String methodSign(Method method) { Class[] classes = method.getParameterTypes(); Arrays.stream(classes).forEach(c -> builder.append(c.getName() + ",")); } - String string = builder.toString(); - return DigestUtils.md5DigestAsHex(string.getBytes()); + return builder.toString(); +// String string = builder.toString(); +// return DigestUtils.md5DigestAsHex(string.getBytes()); } return ""; } diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java new file mode 100644 index 00000000..8d22536a --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java @@ -0,0 +1,143 @@ +package io.kimmking.rpcfx.utils; + +import lombok.Data; +import org.springframework.util.ClassUtils; + +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/11 03:15 + */ +public class MockUtils { + + public static Object mock(Class clazz, Type[] generics) { + boolean primitiveOrWrapper = ClassUtils.isPrimitiveOrWrapper(clazz); + if(primitiveOrWrapper) return mockPrimitive(clazz); + if(String.class.equals(clazz)) return mockString(); + if (Number.class.isAssignableFrom(clazz)) { + return 10; + } + if(clazz.isArray()) { + return mockArray(clazz.getComponentType()); + } + if(List.class.isAssignableFrom(clazz)) { + return mockList(clazz, generics[0]); + } + if(Map.class.isAssignableFrom(clazz)) { + return mockMap(clazz, generics[1]); + } + return mockPojo(clazz); + } + + private static Object mockMap(Class clazz, Type generic) { + HashMap map = new HashMap<>(); + map.put("a", mock((Class)generic, null)); + map.put("b", mock((Class)generic, null)); + return map; + } + + private static Object mockList(Class clazz, Type generic) { + List list = new ArrayList<>(); + list.add(mock((Class)generic, null)); + list.add(mock((Class)generic, null)); + return list; + } + + private static Object mockArray(Class clazz) { + Object array = Array.newInstance(clazz, 2); + Array.set(array, 0, mock(clazz, null)); + Array.set(array, 1, mock(clazz,null)); + return array; + } + + private static Object mockPojo(Class clazz) { + try { + Object object = clazz.getDeclaredConstructor().newInstance(); + Field[] fields = clazz.getDeclaredFields(); + for (Field f : fields) { + f.setAccessible(true); + Type genericType = f.getGenericType(); +// System.out.println(f.getGenericType()); +// System.out.println(f.getType()); + if (genericType instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericType; + Type[] typeArguments = parameterizedType.getActualTypeArguments(); + System.out.println("genericType="+Arrays.toString(typeArguments)); + f.set(object, mock(f.getType(), typeArguments)); + } else { + f.set(object, mock(f.getType(),null)); + } + } + return object; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + private static Object mockString() { + return "this_is_a_mock_string"; + } + + private static Object mockPrimitive(Class clazz) { + + if (Boolean.class.equals(clazz)) { + return true; + } + + return 1; + } + + public static void main(String[] args) { + + System.out.println(mock(ListPojo.class,null)); + + +// System.out.println(mock(Byte.class)); +// System.out.println(mock(Character.class)); +// System.out.println(mock(Boolean.class)); +// System.out.println(mock(Integer.class)); +// System.out.println(mock(Float.class)); +// System.out.println(mock(Short.class)); +// System.out.println(mock(Long.class)); +// System.out.println(mock(Double.class)); +// System.out.println(mock(BigInteger.class)); +// System.out.println(mock(BigDecimal.class)); +// System.out.println(mock(String.class)); + +// System.out.println(mock(Pojo.class)); + +// Arrays.stream(((Pojo[]) mock(new Pojo[]{}.getClass()))).forEach(System.out::println); + + } + + + @Data + public static class Pojo { + private int id; + private String name; + private float amount; + private InnerPojo inner; + } + + @Data + public static class InnerPojo { + private int value; + private String key; + } + + @Data + public static class ListPojo { + private List list; + private Integer inner; + private Map map; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java new file mode 100644 index 00000000..6ada40ed --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java @@ -0,0 +1,219 @@ +package io.kimmking.rpcfx.utils; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/13 23:44 + */ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by caojun on 2018/2/20. + * + * 基本概念: + * weight: 配置文件中指定的该后端的权重,这个值是固定不变的。 + * effective_weight: 后端的有效权重,初始值为weight。 + * 在释放后端时,如果发现和后端的通信过程中发生了错误,就减小effective_weight。 + * 此后有新的请求过来时,在选取后端的过程中,再逐步增加effective_weight,最终又恢复到weight。 + * 之所以增加这个字段,是为了当后端发生错误时,降低其权重。 + * current_weight: + * 后端目前的权重,一开始为0,之后会动态调整。那么是怎么个动态调整呢? + * 每次选取后端时,会遍历集群中所有后端,对于每个后端,让它的current_weight增加它的effective_weight, + * 同时累加所有后端的effective_weight,保存为total。 + * 如果该后端的current_weight是最大的,就选定这个后端,然后把它的current_weight减去total。 + * 如果该后端没有被选定,那么current_weight不用减小。 + * + * 算法逻辑: + * 1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行: + *     peer->current_weight += peer->effecitve_weight。 + *     同时累加所有peer的effective_weight,保存为total。 + * 2. 从集群中选出current_weight最大的peer,作为本次选定的后端。 + * 3. 对于本次选定的后端,执行:peer->current_weight -= total。 + * + */ +public class RoundRobinByWeightLoadBalance { + + //约定的invoker和权重的键值对 + final private List nodes; + + public RoundRobinByWeightLoadBalance(Map invokersWeight){ + if (invokersWeight != null && !invokersWeight.isEmpty()) { + nodes = new ArrayList<>(invokersWeight.size()); + invokersWeight.forEach((invoker, weight)->nodes.add(new Node(invoker, weight))); + }else + nodes = null; + } + + /** + * 算法逻辑: + * 1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行: + *     peer->current_weight += peer->effecitve_weight。 + *     同时累加所有peer的effective_weight,保存为total。 + * 2. 从集群中选出current_weight最大的peer,作为本次选定的后端。 + * 3. 对于本次选定的后端,执行:peer->current_weight -= total。 + * + * @Return ivoker + */ + public Invoker select(){ + if (! checkNodes()) + return null; + else if (nodes.size() == 1) { + if (nodes.get(0).invoker.isAvalable()) + return nodes.get(0).invoker; + else + return null; + } + Integer total = 0; + Node nodeOfMaxWeight = null; + for (Node node : nodes) { + total += node.effectiveWeight; + node.currentWeight += node.effectiveWeight; + + if (nodeOfMaxWeight == null) { + nodeOfMaxWeight = node; + }else{ + nodeOfMaxWeight = nodeOfMaxWeight.compareTo(node) > 0 ? nodeOfMaxWeight : node; + } + } + + nodeOfMaxWeight.currentWeight -= total; + return nodeOfMaxWeight.invoker; + } + + public void onInvokeSuccess(Invoker invoker){ + if (checkNodes()){ + nodes.stream() + .filter((Node node)->invoker.id().equals(node.invoker.id())) + .findFirst() + .get() + .onInvokeSuccess(); + } + } + + public void onInvokeFail(Invoker invoker){ + if (checkNodes()){ + nodes.stream() + .filter((Node node)->invoker.id().equals(node.invoker.id())) + .findFirst() + .get() + .onInvokeFail(); + } + } + + private boolean checkNodes(){ + return (nodes != null && nodes.size() > 0); + } + + public void printCurrenctWeightBeforeSelect(){ + if (checkNodes()) { + final StringBuffer out = new StringBuffer("{"); + nodes.forEach(node->out.append(node.invoker.id()) + .append("=") + .append(node.currentWeight+node.effectiveWeight) + .append(",")); + out.append("}"); + System.out.print(out); + } + } + + public void printCurrenctWeight(){ + if (checkNodes()) { + final StringBuffer out = new StringBuffer("{"); + nodes.forEach(node->out.append(node.invoker.id()) + .append("=") + .append(node.currentWeight) + .append(",")); + out.append("}"); + System.out.print(out); + } + } + + public interface Invoker{ + Boolean isAvalable(); + String id(); + } + + private static class Node implements Comparable{ + final Invoker invoker; + final Integer weight; + Integer effectiveWeight; + Integer currentWeight; + + Node(Invoker invoker, Integer weight){ + this.invoker = invoker; + this.weight = weight; + this.effectiveWeight = weight; + this.currentWeight = 0; + } + + @Override + public int compareTo(Node o) { + return currentWeight > o.currentWeight ? 1 : (currentWeight.equals(o.currentWeight) ? 0 : -1); + } + + public void onInvokeSuccess(){ + if (effectiveWeight < this.weight) + effectiveWeight++; + } + + public void onInvokeFail(){ + effectiveWeight--; + } + } + + public static void main(String[] args){ + Map invokersWeight = new HashMap<>(3); + Integer aWeight = 4; + Integer bWeight = 2; + Integer cWeight = 1; + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "a"; + } + }, aWeight); + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "b"; + } + }, bWeight); + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "c"; + } + }, cWeight); + + Integer times = 7; + RoundRobinByWeightLoadBalance roundRobin = new RoundRobinByWeightLoadBalance(invokersWeight); + for(int i=1; i<=times; i++){ + System.out.print(new StringBuffer(i+"").append(" ")); + roundRobin.printCurrenctWeightBeforeSelect(); + Invoker invoker = roundRobin.select(); + System.out.print(new StringBuffer(" ").append(invoker.id()).append(" ")); + roundRobin.printCurrenctWeight(); + System.out.println(); + } + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java b/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java new file mode 100644 index 00000000..7375135e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java @@ -0,0 +1,47 @@ +import java.util.HashMap; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/3/14 17:52 + */ +public class FourSumCount { + public static void main(String[] args) { + int[] numsA = {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2}; + int[] numsB = {-2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1,-2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1}; + int[] numsC = {-1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2}; + int[] numsD = {0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2}; + + long start = System.nanoTime(); + int count = fourSumCount(numsA, numsB, numsC, numsD); + System.out.println(" take " + (System.nanoTime()-start)/1000000.0 + " ms"); + System.out.println(count); + } + + public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Map map = new HashMap<>(); + int temp; + int res = 0; + for (int i : nums1) { + for (int j : nums2) { + temp = i + j; + if (map.containsKey(temp)) { + map.put(temp, map.get(temp) + 1); + } else { + map.put(temp, 1); + } + } + } + for (int i : nums3) { + for (int j : nums4) { + temp = i + j; + if (map.containsKey(0 - temp)) { + res += map.get(0 - temp); + } + } + } + return res; + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java index 8940d291..c7678f10 100644 --- a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java @@ -4,6 +4,8 @@ public interface UserService { User findById(int id); + User find(int timeout); + //User findById(int id, String name); } diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java index dc058edd..88e23382 100644 --- a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java @@ -2,26 +2,16 @@ import com.alibaba.fastjson.JSON; import io.kimmking.rpcfx.annotation.RpcfxReference; -import io.kimmking.rpcfx.api.Filter; -import io.kimmking.rpcfx.api.LoadBalancer; -import io.kimmking.rpcfx.api.Router; -import io.kimmking.rpcfx.api.RpcfxRequest; -import io.kimmking.rpcfx.consumer.RpcfxInvoker; -import io.kimmking.rpcfx.demo.api.User; +import io.kimmking.rpcfx.demo.api.OrderService; import io.kimmking.rpcfx.demo.api.UserService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; -import java.util.Random; @SpringBootApplication -@ComponentScan("io.kimmking.rpcfx") +@ComponentScan({"io.kimmking.rpcfx.consumer","io.kimmking.rpcfx.demo.consumer"}) public class RpcfxClientApplication { public static void main(String[] args) { @@ -44,11 +34,21 @@ public static void main(String[] args) { SpringApplication.run(RpcfxClientApplication.class, args); } -// @Override -// public void run(String... args) throws Exception { -// // userService2 = RpcfxInvoker.createFromRegistry(UserService.class, new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter()); -// User user = userService2.findById(1); -// System.out.println(JSON.toJSONString(user)); + @RpcfxReference + UserService userService; + + @RpcfxReference + OrderService orderService; + + @Bean + public ApplicationRunner runUserService() { + System.out.println(JSON.toJSONString(userService.hashCode())); + return x -> System.out.println(JSON.toJSONString(userService.find(500))); + } + +// @Bean +// public ApplicationRunner runOrderService() { +// return x -> System.out.println(JSON.toJSONString(orderService.findOrderById(11))); // } } diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml index 8728f144..8b216a03 100644 --- a/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml @@ -11,4 +11,12 @@ spring: execution: pool: core-size: 32 - max-size: 128 \ No newline at end of file + max-size: 128 + +app: + id: app2 + namespace: ns1 + env: sit + mock: false + cache: false + retry: 2 diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java index aacab597..ca236d16 100644 --- a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java @@ -17,13 +17,13 @@ @SpringBootApplication @RestController -@ComponentScan("io.kimmking.rpcfx") +@ComponentScan({"io.kimmking.rpcfx.provider", "io.kimmking.rpcfx.demo.provider"}) public class RpcfxServerApplication implements CommandLineRunner { @Autowired ProviderBootstrap bootstrap; - public static void main(String[] args) throws Exception { + public static void main(String[] args) { SpringApplication.run(RpcfxServerApplication.class, args); } @@ -38,12 +38,12 @@ public RpcfxResponse invoke() { RpcfxRequest request = new RpcfxRequest(); request.setServiceClass("io.kimmking.rpcfx.demo.api.UserService"); request.setParams(new Object[]{1}); - request.setMethod("findById"); + request.setMethodSign("findById@1_int,"); return bootstrap.getInvoker().invoke(request); } @Override - public void run(String... args) throws Exception { + public void run(String... args) { RpcfxResponse response = invoke(); System.out.println(JSON.toJSONString(response)); } diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java index ed410cc7..425fe315 100644 --- a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java @@ -19,4 +19,14 @@ public User findById(int id) { return new User(id, "KK-" + environment.getProperty("server.port") + "_" + System.currentTimeMillis()); } + + public User find(int timeout) { + try { + String p = environment.getProperty("server.port"); + if("8081".equals(p)) Thread.sleep(timeout); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return findById(100); + } } diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml index bec5b5f8..9abe8100 100644 --- a/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml @@ -7,3 +7,8 @@ spring: timeout-per-shutdown-phase: 20s main: allow-circular-references: true + +app: + id: app2 + namespace: ns1 + env: sit \ No newline at end of file diff --git a/09mq/kmq-core/.gitignore b/09mq/kmq-core/.gitignore index e4e0bd7f..14fb3721 100644 --- a/09mq/kmq-core/.gitignore +++ b/09mq/kmq-core/.gitignore @@ -31,3 +31,5 @@ build/ ### VS Code ### .vscode/ + +*.dat diff --git a/09mq/kmq-core/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/kmq-core/.mvn/wrapper/MavenWrapperDownloader.java deleted file mode 100644 index a45eb6ba..00000000 --- a/09mq/kmq-core/.mvn/wrapper/MavenWrapperDownloader.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2007-present the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import java.net.*; -import java.io.*; -import java.nio.channels.*; -import java.util.Properties; - -public class MavenWrapperDownloader { - - private static final String WRAPPER_VERSION = "0.5.6"; - /** - * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. - */ - private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" - + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; - - /** - * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to - * use instead of the default one. - */ - private static final String MAVEN_WRAPPER_PROPERTIES_PATH = - ".mvn/wrapper/maven-wrapper.properties"; - - /** - * Path where the maven-wrapper.jar will be saved to. - */ - private static final String MAVEN_WRAPPER_JAR_PATH = - ".mvn/wrapper/maven-wrapper.jar"; - - /** - * Name of the property which should be used to override the default download url for the wrapper. - */ - private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; - - public static void main(String args[]) { - System.out.println("- Downloader started"); - File baseDirectory = new File(args[0]); - System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); - - // If the maven-wrapper.properties exists, read it and check if it contains a custom - // wrapperUrl parameter. - File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); - String url = DEFAULT_DOWNLOAD_URL; - if (mavenWrapperPropertyFile.exists()) { - FileInputStream mavenWrapperPropertyFileInputStream = null; - try { - mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); - Properties mavenWrapperProperties = new Properties(); - mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); - url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); - } catch (IOException e) { - System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); - } finally { - try { - if (mavenWrapperPropertyFileInputStream != null) { - mavenWrapperPropertyFileInputStream.close(); - } - } catch (IOException e) { - // Ignore ... - } - } - } - System.out.println("- Downloading from: " + url); - - File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); - if (!outputFile.getParentFile().exists()) { - if (!outputFile.getParentFile().mkdirs()) { - System.out.println( - "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); - } - } - System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); - try { - downloadFileFromURL(url, outputFile); - System.out.println("Done"); - System.exit(0); - } catch (Throwable e) { - System.out.println("- Error downloading"); - e.printStackTrace(); - System.exit(1); - } - } - - private static void downloadFileFromURL(String urlString, File destination) throws Exception { - if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { - String username = System.getenv("MVNW_USERNAME"); - char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); - Authenticator.setDefault(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(username, password); - } - }); - } - URL website = new URL(urlString); - ReadableByteChannel rbc; - rbc = Channels.newChannel(website.openStream()); - FileOutputStream fos = new FileOutputStream(destination); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); - fos.close(); - rbc.close(); - } - -} diff --git a/09mq/kmq-core/.mvn/wrapper/maven-wrapper.jar b/09mq/kmq-core/.mvn/wrapper/maven-wrapper.jar deleted file mode 100644 index 2cc7d4a5..00000000 Binary files a/09mq/kmq-core/.mvn/wrapper/maven-wrapper.jar and /dev/null differ diff --git a/09mq/kmq-core/.mvn/wrapper/maven-wrapper.properties b/09mq/kmq-core/.mvn/wrapper/maven-wrapper.properties deleted file mode 100644 index 642d572c..00000000 --- a/09mq/kmq-core/.mvn/wrapper/maven-wrapper.properties +++ /dev/null @@ -1,2 +0,0 @@ -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/kmq-core/mvnw b/09mq/kmq-core/mvnw deleted file mode 100755 index a16b5431..00000000 --- a/09mq/kmq-core/mvnw +++ /dev/null @@ -1,310 +0,0 @@ -#!/bin/sh -# ---------------------------------------------------------------------------- -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# ---------------------------------------------------------------------------- - -# ---------------------------------------------------------------------------- -# Maven Start Up Batch script -# -# Required ENV vars: -# ------------------ -# JAVA_HOME - location of a JDK home dir -# -# Optional ENV vars -# ----------------- -# M2_HOME - location of maven2's installed home dir -# MAVEN_OPTS - parameters passed to the Java VM when running Maven -# e.g. to debug Maven itself, use -# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -# MAVEN_SKIP_RC - flag to disable loading of mavenrc files -# ---------------------------------------------------------------------------- - -if [ -z "$MAVEN_SKIP_RC" ] ; then - - if [ -f /etc/mavenrc ] ; then - . /etc/mavenrc - fi - - if [ -f "$HOME/.mavenrc" ] ; then - . "$HOME/.mavenrc" - fi - -fi - -# OS specific support. $var _must_ be set to either true or false. -cygwin=false; -darwin=false; -mingw=false -case "`uname`" in - CYGWIN*) cygwin=true ;; - MINGW*) mingw=true;; - Darwin*) darwin=true - # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home - # See https://developer.apple.com/library/mac/qa/qa1170/_index.html - if [ -z "$JAVA_HOME" ]; then - if [ -x "/usr/libexec/java_home" ]; then - export JAVA_HOME="`/usr/libexec/java_home`" - else - export JAVA_HOME="/Library/Java/Home" - fi - fi - ;; -esac - -if [ -z "$JAVA_HOME" ] ; then - if [ -r /etc/gentoo-release ] ; then - JAVA_HOME=`java-config --jre-home` - fi -fi - -if [ -z "$M2_HOME" ] ; then - ## resolve links - $0 may be a link to maven's home - PRG="$0" - - # need this for relative symlinks - while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG="`dirname "$PRG"`/$link" - fi - done - - saveddir=`pwd` - - M2_HOME=`dirname "$PRG"`/.. - - # make it fully qualified - M2_HOME=`cd "$M2_HOME" && pwd` - - cd "$saveddir" - # echo Using m2 at $M2_HOME -fi - -# For Cygwin, ensure paths are in UNIX format before anything is touched -if $cygwin ; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --unix "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --unix "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --unix "$CLASSPATH"` -fi - -# For Mingw, ensure paths are in UNIX format before anything is touched -if $mingw ; then - [ -n "$M2_HOME" ] && - M2_HOME="`(cd "$M2_HOME"; pwd)`" - [ -n "$JAVA_HOME" ] && - JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" -fi - -if [ -z "$JAVA_HOME" ]; then - javaExecutable="`which javac`" - if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then - # readlink(1) is not available as standard on Solaris 10. - readLink=`which readlink` - if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then - if $darwin ; then - javaHome="`dirname \"$javaExecutable\"`" - javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" - else - javaExecutable="`readlink -f \"$javaExecutable\"`" - fi - javaHome="`dirname \"$javaExecutable\"`" - javaHome=`expr "$javaHome" : '\(.*\)/bin'` - JAVA_HOME="$javaHome" - export JAVA_HOME - fi - fi -fi - -if [ -z "$JAVACMD" ] ; then - if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - else - JAVACMD="`which java`" - fi -fi - -if [ ! -x "$JAVACMD" ] ; then - echo "Error: JAVA_HOME is not defined correctly." >&2 - echo " We cannot execute $JAVACMD" >&2 - exit 1 -fi - -if [ -z "$JAVA_HOME" ] ; then - echo "Warning: JAVA_HOME environment variable is not set." -fi - -CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher - -# traverses directory structure from process work directory to filesystem root -# first directory with .mvn subdirectory is considered project base directory -find_maven_basedir() { - - if [ -z "$1" ] - then - echo "Path not specified to find_maven_basedir" - return 1 - fi - - basedir="$1" - wdir="$1" - while [ "$wdir" != '/' ] ; do - if [ -d "$wdir"/.mvn ] ; then - basedir=$wdir - break - fi - # workaround for JBEAP-8937 (on Solaris 10/Sparc) - if [ -d "${wdir}" ]; then - wdir=`cd "$wdir/.."; pwd` - fi - # end of workaround - done - echo "${basedir}" -} - -# concatenates all lines of a file -concat_lines() { - if [ -f "$1" ]; then - echo "$(tr -s '\n' ' ' < "$1")" - fi -} - -BASE_DIR=`find_maven_basedir "$(pwd)"` -if [ -z "$BASE_DIR" ]; then - exit 1; -fi - -########################################################################################## -# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central -# This allows using the maven wrapper in projects that prohibit checking in binary data. -########################################################################################## -if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found .mvn/wrapper/maven-wrapper.jar" - fi -else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." - fi - if [ -n "$MVNW_REPOURL" ]; then - jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - else - jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - fi - while IFS="=" read key value; do - case "$key" in (wrapperUrl) jarUrl="$value"; break ;; - esac - done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" - if [ "$MVNW_VERBOSE" = true ]; then - echo "Downloading from: $jarUrl" - fi - wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" - if $cygwin; then - wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` - fi - - if command -v wget > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found wget ... using wget" - fi - if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - wget "$jarUrl" -O "$wrapperJarPath" - else - wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" - fi - elif command -v curl > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found curl ... using curl" - fi - if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - curl -o "$wrapperJarPath" "$jarUrl" -f - else - curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f - fi - - else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Falling back to using Java to download" - fi - javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" - # For Cygwin, switch paths to Windows format before running javac - if $cygwin; then - javaClass=`cygpath --path --windows "$javaClass"` - fi - if [ -e "$javaClass" ]; then - if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Compiling MavenWrapperDownloader.java ..." - fi - # Compiling the Java class - ("$JAVA_HOME/bin/javac" "$javaClass") - fi - if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - # Running the downloader - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Running MavenWrapperDownloader.java ..." - fi - ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") - fi - fi - fi -fi -########################################################################################## -# End of extension -########################################################################################## - -export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} -if [ "$MVNW_VERBOSE" = true ]; then - echo $MAVEN_PROJECTBASEDIR -fi -MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" - -# For Cygwin, switch paths to Windows format before running java -if $cygwin; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --path --windows "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --windows "$CLASSPATH"` - [ -n "$MAVEN_PROJECTBASEDIR" ] && - MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` -fi - -# Provide a "standardized" way to retrieve the CLI args that will -# work with both Windows and non-Windows executions. -MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" -export MAVEN_CMD_LINE_ARGS - -WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain - -exec "$JAVACMD" \ - $MAVEN_OPTS \ - -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ - "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ - ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/kmq-core/mvnw.cmd b/09mq/kmq-core/mvnw.cmd deleted file mode 100644 index c8d43372..00000000 --- a/09mq/kmq-core/mvnw.cmd +++ /dev/null @@ -1,182 +0,0 @@ -@REM ---------------------------------------------------------------------------- -@REM Licensed to the Apache Software Foundation (ASF) under one -@REM or more contributor license agreements. See the NOTICE file -@REM distributed with this work for additional information -@REM regarding copyright ownership. The ASF licenses this file -@REM to you under the Apache License, Version 2.0 (the -@REM "License"); you may not use this file except in compliance -@REM with the License. You may obtain a copy of the License at -@REM -@REM https://www.apache.org/licenses/LICENSE-2.0 -@REM -@REM Unless required by applicable law or agreed to in writing, -@REM software distributed under the License is distributed on an -@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -@REM KIND, either express or implied. See the License for the -@REM specific language governing permissions and limitations -@REM under the License. -@REM ---------------------------------------------------------------------------- - -@REM ---------------------------------------------------------------------------- -@REM Maven Start Up Batch script -@REM -@REM Required ENV vars: -@REM JAVA_HOME - location of a JDK home dir -@REM -@REM Optional ENV vars -@REM M2_HOME - location of maven2's installed home dir -@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands -@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending -@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven -@REM e.g. to debug Maven itself, use -@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files -@REM ---------------------------------------------------------------------------- - -@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' -@echo off -@REM set title of command window -title %0 -@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' -@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% - -@REM set %HOME% to equivalent of $HOME -if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") - -@REM Execute a user defined script before this one -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre -@REM check for pre script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" -if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" -:skipRcPre - -@setlocal - -set ERROR_CODE=0 - -@REM To isolate internal variables from possible post scripts, we use another setlocal -@setlocal - -@REM ==== START VALIDATION ==== -if not "%JAVA_HOME%" == "" goto OkJHome - -echo. -echo Error: JAVA_HOME not found in your environment. >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -:OkJHome -if exist "%JAVA_HOME%\bin\java.exe" goto init - -echo. -echo Error: JAVA_HOME is set to an invalid directory. >&2 -echo JAVA_HOME = "%JAVA_HOME%" >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -@REM ==== END VALIDATION ==== - -:init - -@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". -@REM Fallback to current working directory if not found. - -set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% -IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir - -set EXEC_DIR=%CD% -set WDIR=%EXEC_DIR% -:findBaseDir -IF EXIST "%WDIR%"\.mvn goto baseDirFound -cd .. -IF "%WDIR%"=="%CD%" goto baseDirNotFound -set WDIR=%CD% -goto findBaseDir - -:baseDirFound -set MAVEN_PROJECTBASEDIR=%WDIR% -cd "%EXEC_DIR%" -goto endDetectBaseDir - -:baseDirNotFound -set MAVEN_PROJECTBASEDIR=%EXEC_DIR% -cd "%EXEC_DIR%" - -:endDetectBaseDir - -IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig - -@setlocal EnableExtensions EnableDelayedExpansion -for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a -@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% - -:endReadAdditionalConfig - -SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" -set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" -set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain - -set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - -FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( - IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B -) - -@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central -@REM This allows using the maven wrapper in projects that prohibit checking in binary data. -if exist %WRAPPER_JAR% ( - if "%MVNW_VERBOSE%" == "true" ( - echo Found %WRAPPER_JAR% - ) -) else ( - if not "%MVNW_REPOURL%" == "" ( - SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - ) - if "%MVNW_VERBOSE%" == "true" ( - echo Couldn't find %WRAPPER_JAR%, downloading it ... - echo Downloading from: %DOWNLOAD_URL% - ) - - powershell -Command "&{"^ - "$webclient = new-object System.Net.WebClient;"^ - "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ - "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ - "}"^ - "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ - "}" - if "%MVNW_VERBOSE%" == "true" ( - echo Finished downloading %WRAPPER_JAR% - ) -) -@REM End of extension - -@REM Provide a "standardized" way to retrieve the CLI args that will -@REM work with both Windows and non-Windows executions. -set MAVEN_CMD_LINE_ARGS=%* - -%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* -if ERRORLEVEL 1 goto error -goto end - -:error -set ERROR_CODE=1 - -:end -@endlocal & set ERROR_CODE=%ERROR_CODE% - -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost -@REM check for post script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" -if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" -:skipRcPost - -@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' -if "%MAVEN_BATCH_PAUSE%" == "on" pause - -if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% - -exit /B %ERROR_CODE% diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java index ebf03192..8989faab 100644 --- a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java @@ -2,6 +2,8 @@ import lombok.SneakyThrows; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; @@ -13,14 +15,28 @@ public Kmq(String topic, int capacity) { this.queue = new LinkedBlockingQueue(capacity); } - private String topic; +// public List consumers = new ArrayList<>(); - private int capacity; + private List listeners = new ArrayList<>(); + + private final String topic; + + private final int capacity; private LinkedBlockingQueue queue; public boolean send(KmqMessage message) { - return queue.offer(message); + boolean offered = queue.offer(message); + if(offered) { + listeners.forEach(listener -> { + try { + listener.onMessage(message); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + return offered; } public KmqMessage poll() { @@ -32,4 +48,8 @@ public KmqMessage poll(long timeout) { return queue.poll(timeout, TimeUnit.MILLISECONDS); } + public void addListener(MessageListener listener) { + listeners.add(listener); + } + } diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java index 672557db..c0aa344f 100644 --- a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java @@ -2,6 +2,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; public final class KmqBroker { // Broker+Connection @@ -21,8 +22,9 @@ public KmqProducer createProducer() { return new KmqProducer(this); } + final AtomicInteger consumerId = new AtomicInteger(0); public KmqConsumer createConsumer() { - return new KmqConsumer(this); + return new KmqConsumer("CID" + consumerId.getAndIncrement(), this); } } diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java index a7dd83ae..83ae7245 100644 --- a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java @@ -1,12 +1,18 @@ package io.kimmking.kmq.core; +import lombok.Getter; + public class KmqConsumer { private final KmqBroker broker; + @Getter + private final String id; + private Kmq kmq; - public KmqConsumer(KmqBroker broker) { + public KmqConsumer(String id, KmqBroker broker) { + this.id = id; this.broker = broker; } @@ -15,6 +21,12 @@ public void subscribe(String topic) { if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); } + public void subscribe(String topic, MessageListener listener) { + this.kmq = this.broker.findKmq(topic); + if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); + this.kmq.addListener(listener); + } + public KmqMessage poll(long timeout) { return kmq.poll(timeout); } diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java index fb7d90de..73823c9d 100644 --- a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java @@ -2,15 +2,31 @@ import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import java.util.HashMap; +import java.util.concurrent.atomic.AtomicLong; @AllArgsConstructor +@NoArgsConstructor @Data public class KmqMessage { - private HashMap headers; + static AtomicLong MID = new AtomicLong(0); + private HashMap headers = new HashMap<>(); + private String topic; + private Long id; private T body; + public KmqMessage(String topic, T body) { + this.topic = topic; + this.body = body; + this.id = MID.getAndIncrement(); + } + + public static KmqMessage from(String topic, T body) { + return new KmqMessage<>(topic, body); + } + } diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java new file mode 100644 index 00000000..4cfdbe08 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java @@ -0,0 +1,13 @@ +package io.kimmking.kmq.core; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/13 下午3:29 + */ +public interface MessageListener { + + void onMessage(KmqMessage message) throws Exception; + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java index ad6e2d56..9921af3d 100644 --- a/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java @@ -16,6 +16,11 @@ public static void main(String[] args) { KmqBroker broker = new KmqBroker(); broker.createTopic(topic); + KmqConsumer subscriber = broker.createConsumer(); + subscriber.subscribe(topic, (message) -> { + System.out.println(subscriber.getId() + " : " + message.getBody()); + }); + KmqConsumer consumer = broker.createConsumer(); consumer.subscribe(topic); final boolean[] flag = new boolean[1]; @@ -24,14 +29,14 @@ public static void main(String[] args) { while (flag[0]) { KmqMessage message = consumer.poll(100); if(null != message) { - System.out.println(message.getBody()); + System.out.println(consumer.getId() + " : " + message.getBody()); } } System.out.println("程序退出。"); }).start(); KmqProducer producer = broker.createProducer(); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < 10; i++) { Order order = new Order(1000L + i, System.currentTimeMillis(), "USD2CNY", 6.51d); producer.send(topic, new KmqMessage(null, order)); } diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java new file mode 100644 index 00000000..baccdaff --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java @@ -0,0 +1,97 @@ +package io.kimmking.kmq.store; + +import com.alibaba.fastjson.JSON; +import io.kimmking.kmq.core.KmqMessage; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Scanner; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/13 下午9:59 + */ +public class FileDemo { + + public static void main(String[] args) throws IOException { + + String content = "this is a good file.\r\n" + + "that is a new line.\r\n"; + String topic = "topicA"; + System.out.println(content.length()); + + File file = new File("store001.dat"); + if (!file.exists()) { + file.createNewFile(); + } + Path path = Paths.get(file.toURI()); + try(FileChannel channel = (FileChannel) Files.newByteChannel(path, + StandardOpenOption.READ, + StandardOpenOption.WRITE)) { + + MappedByteBuffer mappedByteBuffer = channel + .map(FileChannel.MapMode.READ_WRITE, 0, 10240); + + if (mappedByteBuffer != null) { + + System.out.println(Charset.forName("utf-8") + .decode(mappedByteBuffer.asReadOnlyBuffer())); + + for (int i = 0; i < 100; i++) { + KmqMessage km = KmqMessage.from(topic, content); + String message = encodeMessage(km); + Indexer.addEntry(topic, km.getId(), mappedByteBuffer.position(), message.length()); + int pos = write(mappedByteBuffer, message); + System.out.println("POS = " + pos); + } + + System.out.println(" ======== indexer ========= "); + System.out.println(Indexer.getEntries(topic)); + } + + ByteBuffer readOnlyBuffer = mappedByteBuffer.asReadOnlyBuffer(); + Scanner sc = new Scanner(System.in); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + if (line.equals("exit")) { + break; + } + System.out.println("IN = "+line); + Long id = Long.valueOf(line); + Indexer.Entry entry = Indexer.getEntry(id); + System.out.println("EN = " + entry); + if(entry == null) { + System.out.println("!!!No entry for id=" + id); + } else { + readOnlyBuffer.position(entry.offset); + byte[] bytes = new byte[entry.length]; + readOnlyBuffer.get(bytes, 0, entry.length); + System.out.println("MSG = " + new String(bytes)); + } + } + + } + } + + private static String encodeMessage(KmqMessage message) { + return JSON.toJSONString(message); + } + + public static int write(MappedByteBuffer buffer, String content) throws IOException { + buffer.put( + Charset.forName("utf-8") + .encode(content)); + return buffer.position(); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java new file mode 100644 index 00000000..792ac400 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java @@ -0,0 +1,53 @@ +package io.kimmking.kmq.store; + +import lombok.Data; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/14 下午6:19 + */ + +@Data +public class Indexer { + + static Map> index = new HashMap<>(); + static Map mappings = new HashMap<>(); + + @Data + public static class Entry { + long id; + int offset; + int length; + + public Entry(long id, int offset, int length) { + this.offset = offset; + this.length = length; + } + } + + public static void addEntry(String topic, long id, int offset, int length) { + List entries = index.get(topic); + if(entries == null) { + entries = new java.util.ArrayList<>(); + index.put(topic, entries); + } + Entry e = new Entry(id, offset, length); + entries.add(e); + mappings.put(id, e); + } + + public static List getEntries(String topic) { + return index.get(topic); + } + + public static Entry getEntry(long id) { + return mappings.get(id); + } + +}