-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5.3 v1.5 1、提取负载均衡策略 2、实现nacos注册中心 3、提取注册中心的选择
- Loading branch information
1 parent
0c7b51c
commit 2ffc0bc
Showing
36 changed files
with
479 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
zyt-rpc-common/src/main/java/annotation/RegistryChosen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...mer/bootstrap/NettyConsumerBootStrap.java → ...otstrap/netty/NettyConsumerBootStrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package consumer.bootstrap; | ||
package consumer.bootstrap.netty; | ||
|
||
/* | ||
以netty为网络编程框架的消费者端启动类 | ||
|
2 changes: 1 addition & 1 deletion
2
...mer/bootstrap/NIOConsumerBootStrap10.java → ...bootstrap/nio/NIOConsumerBootStrap10.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...mer/bootstrap/NIOConsumerBootStrap11.java → ...bootstrap/nio/NIOConsumerBootStrap11.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...mer/bootstrap/NIOConsumerBootStrap12.java → ...bootstrap/nio/NIOConsumerBootStrap12.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
2 changes: 1 addition & 1 deletion
2
...mer/bootstrap/NIOConsumerBootStrap14.java → ...bootstrap/nio/NIOConsumerBootStrap14.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
23 changes: 23 additions & 0 deletions
23
zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootStrap15.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
zyt-rpc-consumer/src/main/java/consumer/bootstrap/nio/NIOConsumerBootstrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
zyt-rpc-consumer/src/main/java/consumer/servicediscovery/NacosServiceDiscovery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...onsumer/zkService/ZkServiceDiscovery.java → .../servicediscovery/ZkServiceDiscovery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
zyt-rpc-consumer/src/main/java/consumer/test/NacosTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ava/consumer/zkService/ZKServiceTest.java → ...ain/java/consumer/test/ZKServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package consumer.zkService; | ||
package consumer.test; | ||
|
||
|
||
import annotation.LoadBalanceMethodImpl; | ||
|
2 changes: 1 addition & 1 deletion
2
...der/bootstrap/NettyProviderBootStrap.java → ...otstrap/netty/NettyProviderBootStrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package provider.bootstrap; | ||
package provider.bootstrap.netty; | ||
|
||
/* | ||
以netty为网络编程框架的服务提供端启动类 | ||
|
8 changes: 8 additions & 0 deletions
8
zyt-rpc-provider/src/main/java/provider/bootstrap/nio/NIOProviderBootStrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
2 changes: 1 addition & 1 deletion
2
...der/bootstrap/NIOProviderBootStrap10.java → ...bootstrap/nio/NIOProviderBootStrap10.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...der/bootstrap/NIOProviderBootStrap11.java → ...bootstrap/nio/NIOProviderBootStrap11.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
3 changes: 1 addition & 2 deletions
3
...der/bootstrap/NIOProviderBootStrap12.java → ...bootstrap/nio/NIOProviderBootStrap12.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.