Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

springboot #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<kryo.version>4.0.0</kryo.version>
<logback.version>1.2.3</logback.version>
<xrpc.versin>1.0-SNAPSHOT</xrpc.versin>
<spring.version>5.2.7.RELEASE</spring.version>
</properties>


Expand Down Expand Up @@ -90,13 +89,6 @@
<version>${xrpc.versin}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>


</dependencies>
</dependencyManagement>

Expand All @@ -114,4 +106,21 @@
</repository>
</repositories>



<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>

</project>
8 changes: 8 additions & 0 deletions xrpc-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.6.15</version>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
18 changes: 18 additions & 0 deletions xrpc-core/src/main/java/com/xchb/xrpc/common/AppConst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.xchb.xrpc.common;

public class AppConst {


public static final Integer RANDOM_MIN_EXPORT_PORT = 11111;

/**
* 对外暴露的端口
*/
public static Integer exportPort;

/**
* 注册中心地址
*/
public static String zkAddr = "localhost:2181";

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xchb.xrpc.common;

import com.xchb.xrpc.common.proto.RpcResponseProto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down
2 changes: 0 additions & 2 deletions xrpc-core/src/main/java/com/xchb/xrpc/common/ServerParam.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.xchb.xrpc.common;

import com.xchb.xrpc.config.XrpcConfigProperties;
import com.xchb.xrpc.util.IpUtils;
import com.xchb.xrpc.util.SingleFactory;
import lombok.Builder;
import lombok.Data;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xchb.xrpc.extension;

import lombok.extern.slf4j.Slf4j;
import com.xchb.xrpc.register.ServerRegister;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down
9 changes: 2 additions & 7 deletions xrpc-core/src/main/java/com/xchb/xrpc/proxy/ClientProxy.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.xchb.xrpc.proxy;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.xchb.xrpc.common.ServerParam;
import com.xchb.xrpc.common.proto.RpcRequestProto;
import com.xchb.xrpc.common.proto.RpcResponseProto;
import com.xchb.xrpc.config.XrpcConfigProperties;
import com.xchb.xrpc.exceptions.ServerNotFindExeception;
import com.xchb.xrpc.extension.ExtensionLoader;
import com.xchb.xrpc.loadbalance.LoadBalance;
import com.xchb.xrpc.register.ServerRegister;
import com.xchb.xrpc.transport.client.ClientBooter;
import com.xchb.xrpc.util.AppConst;
import com.xchb.xrpc.util.ProtoBufUtils;
import com.xchb.xrpc.util.SingleFactory;
import com.xchb.xrpc.util.UnProcessRequest;
Expand All @@ -22,9 +19,9 @@
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
* @author XDD
Expand All @@ -41,8 +38,6 @@ public class ClientProxy implements InvocationHandler {

private ServerParam serverParam;

private XrpcConfigProperties xrpcConfigProperties = SingleFactory.getInstance(XrpcConfigProperties.class);


public ClientProxy(ServerParam serverParam){
local = ExtensionLoader.get(ServerRegister.class, "zk");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
package com.xchb.xrpc.proxy;

import cn.hutool.core.util.IdUtil;
import com.xchb.xrpc.transport.client.ClientBooter;
import com.xchb.xrpc.util.AppConst;
import com.xchb.xrpc.util.SingleFactory;
import com.xchb.xrpc.util.UnProcessRequest;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import lombok.extern.slf4j.Slf4j;
import com.xchb.xrpc.common.ServerParam;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;

/**
* @author XDD
Expand Down
16 changes: 13 additions & 3 deletions xrpc-core/src/main/java/com/xchb/xrpc/register/ServerRegister.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xchb.xrpc.register;

import com.xchb.xrpc.extension.SPI;
import com.xchb.xrpc.common.ServerParam;
import com.xchb.xrpc.extension.SPI;

import java.util.List;

Expand All @@ -21,6 +21,18 @@ public interface ServerRegister {
*/
boolean register() ;

/**
* 添加服务
* @param serverParam
*/
void put(ServerParam serverParam);


/**
* 服务数量
* @return
*/
Integer serverCount();


/**
Expand All @@ -29,6 +41,4 @@ public interface ServerRegister {
* @return
*/
List<ServerParam> find(String serverName);

void put(ServerParam serverParam);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xchb.xrpc.register.annotation;

import org.springframework.context.annotation.Import;
import com.xchb.xrpc.spring.CustomBeanRegistrar;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

Expand All @@ -16,7 +16,7 @@
@Target(ElementType.TYPE)
@Documented
@Import(CustomBeanRegistrar.class)
public @interface RpcScan {
public @interface EnableXRpc {

String[] basePackage();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.xchb.xrpc.register.impl;

import com.xchb.xrpc.config.XrpcConfigProperties;
import com.xchb.xrpc.common.AppConst;
import com.xchb.xrpc.common.ServerParam;
import com.xchb.xrpc.register.ServerRegister;
import com.xchb.xrpc.util.SingleFactory;
import com.xchb.xrpc.common.ServerParam;
import com.xchb.xrpc.util.ZkUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -29,10 +28,9 @@ public class ZkServerRegister implements ServerRegister {

@Override
public boolean register() {
Integer exportPort = SingleFactory.getInstance(XrpcConfigProperties.class).getExportPort();
while (!taskQueue.isEmpty()){
ServerParam serverParam = taskQueue.poll();
serverParam.setPort(exportPort);
serverParam.setPort(AppConst.exportPort);
ObjectOutputStream oos = null;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
String serverName = serverParam.serverName();
Expand Down Expand Up @@ -78,5 +76,10 @@ public void put(ServerParam serverParam) {
taskQueue.offer(serverParam);
}

@Override
public Integer serverCount() {
return taskQueue.size();
}


}
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.xchb.xrpc.spring;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.xchb.xrpc.common.AppConst;
import com.xchb.xrpc.register.ServerRegister;
import com.xchb.xrpc.register.annotation.EnableXRpc;
import com.xchb.xrpc.register.annotation.RpcReference;
import com.xchb.xrpc.register.annotation.RpcScan;
import com.xchb.xrpc.register.annotation.RpcService;
import com.xchb.xrpc.spring.properties.SpringAutoConfigProperties;
import com.xchb.xrpc.transport.server.ServerBooter;
import com.xchb.xrpc.util.SingleFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.io.ResourceLoader;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.util.*;
import java.util.Arrays;
import java.util.Map;

/**
* @author XDD
Expand All @@ -23,29 +33,20 @@
* @description 让自定义注解标识的类也交由spring管理.
*/
@Slf4j
public class CustomBeanRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
public class CustomBeanRegistrar implements ImportBeanDefinitionRegistrar{


private ResourceLoader resourceLoader;

//自定义注解中包扫描的注解属性名
private String PACKAGE_SCANNER_PARAM = "basePackage";

//spring配置文件的包路径
private String springConfigBasePackage = "com.xchb.xrpc.spring";

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}


@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry registry,
BeanNameGenerator importBeanNameGenerator) {
//获取自定义注解中包含的扫描路径
Map<String, Object> rpcScanParams =
annotationMetadata.getAnnotationAttributes(RpcScan.class.getCanonicalName());
annotationMetadata.getAnnotationAttributes(EnableXRpc.class.getCanonicalName());
String[] tmp = (String[]) rpcScanParams.get(PACKAGE_SCANNER_PARAM);
String[] basePackage = new String[tmp.length+1];
ArrayUtil.copy(tmp, basePackage, tmp.length);
Expand All @@ -58,7 +59,14 @@ public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
scanner.addIncludeFilter(new AnnotationTypeFilter(RpcService.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(RpcReference.class));
scanner.scan(basePackage);


BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(SpringBeanPostProcess.class).getBeanDefinition();
registry.registerBeanDefinition("springBeanPostProcess",beanDefinition);

}




}
Loading