Skip to content

Commit

Permalink
5.3 v1.5 1、提取负载均衡策略 2、实现nacos注册中心 3、提取注册中心的选择
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzzzzzyt committed May 3, 2022
1 parent 0c7b51c commit 2ffc0bc
Show file tree
Hide file tree
Showing 36 changed files with 479 additions and 41 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,12 @@
<version>4.3.0</version>
</dependency>

<!--添加Nacos依赖 使用服务注册 服务发现-->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>1.1.1</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import annotation.RpcClientBootStrap;

import consumer.bootstrap.NIOConsumerBootStrap10;
import consumer.bootstrap.NIOConsumerBootStrap11;
import consumer.bootstrap.NIOConsumerBootStrap12;
import consumer.bootstrap.NIOConsumerBootStrap14;
import consumer.bootstrap.nio.*;


import java.io.IOException;

//之后启动直接在这边启动根据 在注解中配置对应的版本号 将相应的操作封装到之后的操作中即可 这样很方便 就是每次咱加一个启动器还得改下switch
//比如说这里的version 1.2 就是v1.2版本的启动器
@RpcClientBootStrap(version = "1.4")
@RpcClientBootStrap(version = "1.5")
public class ClientBootStrap {
public static void start() throws IOException{
//获取当前的注解上的版本然后去调用相应的远端方法 反射的方法
Expand All @@ -35,6 +32,9 @@ public static void start() throws IOException{
case "1.4":
NIOConsumerBootStrap14.main(null);
break;
case "1.5":
NIOConsumerBootStrap15.main(null);
break;
default:
System.out.println("太着急了兄弟,这个版本还没出呢!要不你给我提个PR");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
import annotation.RpcServerBootStrap;
import init.ZK;
import org.apache.zookeeper.KeeperException;
import provider.bootstrap.NIOProviderBootStrap10;
import provider.bootstrap.NIOProviderBootStrap11;
import provider.bootstrap.NIOProviderBootStrap12;
import provider.bootstrap.NIOProviderBootStrap14;
import provider.bootstrap.nio.*;
import service.call.ServerCall;

import java.io.IOException;
import java.lang.reflect.Method;


//之后启动直接在这边启动根据 在注解中配置对应的版本号 将相应的操作封装到之后的操作中即可
//比如说这里的version 1.2 就是v1.2版本的启动器
@RpcServerBootStrap(version = "1.4")
@RpcServerBootStrap(version = "1.5")
public class ServerBootStrap {


Expand Down Expand Up @@ -62,8 +59,13 @@ public static void start() throws IOException, InterruptedException, KeeperExcep
NIOProviderBootStrap12.main(null);
break;
case "1.4":
//1.4 增加了注册中心 zk
NIOProviderBootStrap14.main(new String[]{methodBuilder.toString(), numBuilder.toString()});
break;
case "1.5":
//1.5 将注册中心换成了nacos
NIOProviderBootStrap15.main(new String[]{methodBuilder.toString(), numBuilder.toString()});
break;
default:
System.out.println("太着急了兄弟,这个版本还没出呢!要不你给我提个PR");
}
Expand Down
1 change: 1 addition & 0 deletions zyt-rpc-call/src/main/java/service/call/ClientCall.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package service.call;


import service.bootstrap.ClientBootStrap;

import java.io.IOException;
Expand Down
4 changes: 1 addition & 3 deletions zyt-rpc-call/src/main/java/service/call/ServerCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@




import annotation.RpcMethodCluster;
import org.apache.zookeeper.KeeperException;
import service.bootstrap.ServerBootStrap;

import java.io.IOException;

//通用启动类 将启动的逻辑藏在ServerBootStrap中
//注解 看你像启动多少个服务和对应的方法
//注解 看你想启动多少个服务和对应的方法
@RpcMethodCluster(method = {"Hello","Bye"},startNum = {2,3})

public class ServerCall {
public static void main(String[] args) throws IOException, InterruptedException, KeeperException, NoSuchMethodException {
ServerBootStrap.start();
Expand Down
13 changes: 13 additions & 0 deletions zyt-rpc-common/src/main/java/annotation/RegistryChosen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//注册中心选择 默认采用zookeeper
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RegistryChosen {
String registryName() default "zookeeper";
}
21 changes: 21 additions & 0 deletions zyt-rpc-common/src/main/java/constants/RpcConstants.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
package constants;


import java.util.Properties;

public class RpcConstants {
static {

}
//zookeeper服务器连接地址
public static String ZOOKEEPER_ADDRESS = "zytCentos:2181";
//超时时间
public static int ZOOKEEPER_SESSION_TIMEOUT = 2000;


public static String NACOS_DISCOVERY_ADDRESS = "http://192.168.18.128:8848/nacos/v1/ns/instance/list?";

//找到对应要注册的地方
public static Properties NACOS_PROPERTIES= new Properties() ;

//serverAddr nacos的地址
//namespace 存放的服务列表
public static Properties propertiesInit()
{
NACOS_PROPERTIES.setProperty("serverAddr","192.168.18.128:8848");
NACOS_PROPERTIES.setProperty("namespace","public");
return NACOS_PROPERTIES;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consumer.bootstrap;
package consumer.bootstrap.netty;

/*
以netty为网络编程框架的消费者端启动类
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consumer.bootstrap;
package consumer.bootstrap.nio;

import consumer.nio.NIONonBlockingClient10;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consumer.bootstrap;
package consumer.bootstrap.nio;

import consumer.nio.NIOBlockingClient11;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consumer.bootstrap;
package consumer.bootstrap.nio;


import consumer.proxy.RpcClientProxy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consumer.bootstrap;
package consumer.bootstrap.nio;


import consumer.proxy.RpcClientProxy;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package consumer.bootstrap.nio;


import consumer.proxy.RpcClientProxy;
import method.Customer;

import java.io.IOException;

/*
以nio为网络编程框架的消费者端启动类 配合15的集体启动类
*/

public class NIOConsumerBootStrap15{
public static void main(String[] args) throws IOException {

RpcClientProxy clientProxy = new RpcClientProxy();
Customer customer = (Customer) clientProxy.getBean(Customer.class);
String response = customer.Hello("success");
System.out.println(response);
System.out.println(customer.Bye("fail"));
System.out.println(customer.Hello("fail"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package consumer.bootstrap.nio;

import annotation.RegistryChosen;

//这个类就是为了放一些注解的

@RegistryChosen(registryName = "nacos")
public interface NIOConsumerBootstrap {
}
39 changes: 36 additions & 3 deletions zyt-rpc-consumer/src/main/java/consumer/proxy/RpcClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package consumer.proxy;

import consumer.zkService.ZkServiceDiscovery;
import annotation.RegistryChosen;
import com.alibaba.nacos.api.exception.NacosException;
import consumer.bootstrap.nio.NIOConsumerBootstrap;
import consumer.servicediscovery.NacosServiceDiscovery;
import consumer.servicediscovery.ZkServiceDiscovery;
import exception.RpcException;
import org.apache.zookeeper.KeeperException;


import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//代理类的实现
public class RpcClientProxy {
public class RpcClientProxy implements NIOConsumerBootstrap{

//获取代理对象 并返回 当前类别
public static Object getBean(final Class<?> serviceClass){
Expand All @@ -25,10 +33,35 @@ public static Object getBean(final Class<?> serviceClass){
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//暂时还没有设置回信这个操作
String methodName = method.getName();
String response = ZkServiceDiscovery.getStart(methodName, (String) args[0]);
//String response = ZkServiceDiscovery.getStart(methodName, (String) args[0]);
//String response = NacosServiceDiscovery.getStart(methodName, (String) args[0]);
//根据注解类进行调用
String response = getResponse(methodName,(String) args[0]);
return response;
}
}
);
}

/**
* 实际去获得对应的服务 并完成方法调用的方法
* @param methodName
* @param port
* @return
*/
private static String getResponse(String methodName, String port) throws RpcException, IOException, NacosException, InterruptedException, KeeperException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
//根据注解进行方法调用
//根据在代理类上的注解调用 看清楚底下的因为是个class数组 可以直接继续获取 注解
Class<NIOConsumerBootstrap>[] interfaces = (Class<NIOConsumerBootstrap>[]) RpcClientProxy.class.getInterfaces();
RegistryChosen annotation = interfaces[0].getAnnotation(RegistryChosen.class);
switch (annotation.registryName())
{
case "nacos":
return NacosServiceDiscovery.getStart(methodName, port);
case "zookeeper":
return ZkServiceDiscovery.getStart(methodName,port);
default:
throw new RpcException("不存在该注册中心");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package consumer.servicediscovery;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import constants.RpcConstants;
import consumer.nio.NIONonBlockingClient12;
import exception.RpcException;
import org.apache.zookeeper.KeeperException;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Properties;

public class NacosServiceDiscovery {
public static String getMethodAddress(String methodName) throws NacosException, RpcException {
Properties properties = RpcConstants.propertiesInit();
NamingService namingService = NacosFactory.createNamingService(properties);

//这个方法内部实现了负载均衡
Instance instance = namingService.selectOneHealthyInstance(methodName);
if (instance==null)
{
System.out.println("没有提供该方法");
throw new RpcException("没有对应的方法");
}
String ip = instance.getIp();
int port = instance.getPort();
String methodAddress = ip+":"+port;
return methodAddress;
}

public static String getStart(String methodName,String msg) throws IOException, RpcException,NacosException {
//获取相应的远端地址
String methodAddress = getMethodAddress(methodName);
//进行连接
String[] strings = methodAddress.split(":");
//启动
String address = strings[0];
int port = Integer.valueOf(strings[1]);
return NIONonBlockingClient12.start(address,port,msg);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consumer.zkService;
package consumer.servicediscovery;

import annotation.LoadBalanceMethodImpl;
import constants.RpcConstants;
Expand Down
24 changes: 24 additions & 0 deletions zyt-rpc-consumer/src/main/java/consumer/test/NacosTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package consumer.test;

import constants.RpcConstants;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;

//进行测试获取
public class NacosTest {
@Test
public void nacosDiscovery() throws IOException {
String serviceName = "nacos.test.1";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpUriRequest request = RequestBuilder.get(URI.create(RpcConstants.NACOS_DISCOVERY_ADDRESS +"serviceName="+serviceName)).build();
CloseableHttpResponse response = httpClient.execute(request);

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package consumer.zkService;
package consumer.test;


import annotation.LoadBalanceMethodImpl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider.bootstrap;
package provider.bootstrap.netty;

/*
以netty为网络编程框架的服务提供端启动类
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package provider.bootstrap.nio;

import annotation.RegistryChosen;

//注册中心的选择 启用的是nacos 目前
@RegistryChosen(registryName = "nacos")
public interface NIOProviderBootStrap {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider.bootstrap;
package provider.bootstrap.nio;

import provider.nio.NIONonBlockingServer10;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider.bootstrap;
package provider.bootstrap.nio;


import provider.nio.NIOBlockingServer11;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider.bootstrap;
package provider.bootstrap.nio;


import init.ZK;
import org.apache.zookeeper.KeeperException;
import provider.nio.NIONonBlockingServer12bye;
import provider.nio.NIONonBlockingServer12hello;
Expand Down
Loading

0 comments on commit 2ffc0bc

Please sign in to comment.