Skip to content

Commit

Permalink
remove unuseless defination
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlin committed Nov 1, 2018
1 parent 7f92f31 commit 1f457e5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 136 deletions.
49 changes: 24 additions & 25 deletions README.md
Expand Up @@ -28,7 +28,7 @@
calculator.thrift

```thrift
namespace java com.icekredit.rpc.thrift.example
namespace java io.ostenant.rpc.thrift.examples
service CalculatorService {
i32 add(1: i32 arg1, 2: i32 arg2)
Expand Down Expand Up @@ -133,9 +133,9 @@ public class CalculatorService {

```xml
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-server</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
```

Expand All @@ -147,9 +147,9 @@ pom.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-example</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -158,14 +158,14 @@ pom.xml

<dependencies>
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-boot-starter-thrift-server</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-example-iface</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -277,13 +277,13 @@ public class RpcCalculatorService implements CalculatorService.Iface {

```dockerfile
FROM openjdk:8-jdk-alpine
ADD target/spring-boot-thrift-server-0.0.1-SNAPSHOT.jar calculator-server.jar
ADD target/spring-boot-thrift-server-1.0.0jar calculator-server.jar
ENTRYPOINT ["java", "-jar", "calculator-server.jar"]
```

`Dockerfile``target/spring-boot-thrift-server-0.0.1-SNAPSHOT.jar`拷贝到服务器上,构建`Thrift Server`的服务镜像:
```docker
docker build . -t icekredit/calculator-server
docker build . -t ostenant/calculator-server
```

启动三个`Thrift Server``docker`容器,分别指定对应的端口号和`Consul`注册信息:
Expand All @@ -297,7 +297,7 @@ docker run -d -p 8081:8080 -p 25001:25000 --name calculator-server-01 \
-e "SERVICE_25000_CHECK_INTERVAL=30s" \
-e "SERVICE_25000_CHECK_TIMEOUT=3s" \
-e "SERVICE_25000_TAGS=thrift-rpc-calculator-25001" \
icekredit/calculator-server
ostenant/calculator-server
```

`Thrift Server`实例2(25002端口):
Expand All @@ -309,7 +309,7 @@ docker run -d -p 8081:8080 -p 25002:25000 --name calculator-server-01 \
-e "SERVICE_25000_CHECK_INTERVAL=30s" \
-e "SERVICE_25000_CHECK_TIMEOUT=3s" \
-e "SERVICE_25000_TAGS=thrift-rpc-calculator-25002" \
icekredit/calculator-server
ostenant/calculator-server
```

`Thrift Server`实例3(25003端口):
Expand All @@ -321,7 +321,7 @@ docker run -d -p 8081:8080 -p 25003:25000 --name calculator-server-01 \
-e "SERVICE_25000_CHECK_INTERVAL=30s" \
-e "SERVICE_25000_CHECK_TIMEOUT=3s" \
-e "SERVICE_25000_TAGS=thrift-rpc-calculator-25003" \
icekredit/calculator-server
ostenant/calculator-server
```

启动`Consul``Registrator`容器,`Thrift Server`的三个服务实例成功注册到Consul服务列表:
Expand All @@ -347,9 +347,9 @@ docker run -d -p 8081:8080 -p 25003:25000 --name calculator-server-01 \

```xml
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-client</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
```

Expand All @@ -361,24 +361,24 @@ pom.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-example</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-cloud-starter-thrift-example-client</artifactId>

<dependencies>
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-client</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.icekredit.rpc.thrift</groupId>
<groupId>io.ostenant.rpc.thrift</groupId>
<artifactId>spring-cloud-starter-thrift-example-iface</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -441,7 +441,7 @@ management:
spring:
cloud:
consul:
host: 192.168.91.128 ## Consul的IP地址
host: 127.0.0.1 ## Consul的IP地址
port: 8500 ## Consul的端口号
discovery:
register: false ## 是否将自身注册为服务
Expand All @@ -452,7 +452,7 @@ spring:
max-interval: 2000
thrift:
client:
package-to-scan: com.icekredit.rpc.thrift.example.rpc ## 标记由有注解@ThriftClient接口的包路径
package-to-scan: io.ostenant.rpc.thrift.example.rpc ## 标记由有注解@ThriftClient接口的包路径
service-model: hsHa ## 服务线程模型(这里必须与服务端保持一致, 默认都是hsHa)
pool: ## 客户端连接池配置
retry-times: 3
Expand Down Expand Up @@ -530,7 +530,6 @@ public class RpcCalculatorController {
public int division(@RequestParam("arg1") int arg1, @RequestParam("arg2") int arg2) throws Exception {
return calculators.thriftClient().division(arg1, arg2);
}

}

```
Expand Down
Expand Up @@ -7,6 +7,7 @@
import io.ostenant.rpc.thrift.client.properties.ThriftClientPoolProperties;
import io.ostenant.rpc.thrift.client.properties.ThriftClientProperties;
import io.ostenant.rpc.thrift.client.properties.ThriftClientPropertiesCondition;
import io.ostenant.rpc.thrift.client.scanner.ThriftClientBeanScanProcessor;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -23,8 +24,8 @@ public class ThriftClientAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public ThriftClientBeanScannerConfigurer thriftClientBeanScannerConfigurer() {
return new ThriftClientBeanScannerConfigurer();
public ThriftClientBeanScanProcessor thriftClientBeanScannerConfigurer() {
return new ThriftClientBeanScanProcessor();
}

@Bean
Expand Down
Expand Up @@ -13,5 +13,4 @@ public void setLoadBalancer(ILoadBalancer lb) {
public ILoadBalancer getLoadBalancer() {
return lb;
}

}
@@ -1,6 +1,5 @@
package io.ostenant.rpc.thrift.client;
package io.ostenant.rpc.thrift.client.scanner;

import io.ostenant.rpc.thrift.client.scanner.ThriftClientBeanScanner;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,9 +14,9 @@

import java.util.*;

public class ThriftClientBeanScannerConfigurer implements ApplicationContextAware, BeanFactoryPostProcessor {
public class ThriftClientBeanScanProcessor implements ApplicationContextAware, BeanFactoryPostProcessor {

private static final Logger LOGGER = LoggerFactory.getLogger(ThriftClientBeanScannerConfigurer.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ThriftClientBeanScanProcessor.class);

private static final String SPRING_THRIFT_CLIENT_PACKAGE_TO_SCAN = "spring.thrift.client.package-to-scan";

Expand All @@ -33,12 +32,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinitionRegistry definitionRegistry = (BeanDefinitionRegistry) beanFactory;

ThriftClientBeanScanner beanScanner = new ThriftClientBeanScanner(definitionRegistry);
beanScanner.setResourceLoader(applicationContext);
beanScanner.setBeanNameGenerator(new AnnotationBeanNameGenerator());
beanScanner.setScopedProxyMode(ScopedProxyMode.INTERFACES);

setScannedPackages(beanScanner, applicationContext.getEnvironment().getProperty(SPRING_THRIFT_CLIENT_PACKAGE_TO_SCAN));
}

Expand All @@ -52,13 +49,13 @@ private void setScannedPackages(ThriftClientBeanScanner beanScanner, String base
if (delimiterIndex > -1) {
StringTokenizer tokenizer = new StringTokenizer(basePackages, ",");
Set<String> packageToScanSet = new HashSet<>();

while (tokenizer.hasMoreTokens()) {
String subPackage = tokenizer.nextToken();
packageToScanSet.add(subPackage);
LOGGER.info("Subpackage {} is to be scanned by {}", subPackage, beanScanner);
if (tokenizer.hasMoreTokens()) {
do {
String subPackage = tokenizer.nextToken();
packageToScanSet.add(subPackage);
LOGGER.info("Subpackage {} is to be scanned by {}", subPackage, beanScanner);
} while (tokenizer.hasMoreTokens());
}

List<String> packageToScanList = new ArrayList<>(packageToScanSet);
String[] packagesToScan = packageToScanList.toArray(new String[packageToScanList.size()]);
beanScanner.scan(packagesToScan);
Expand All @@ -67,5 +64,4 @@ private void setScannedPackages(ThriftClientBeanScanner beanScanner, String base
beanScanner.scan(basePackages);
}
}

}

This file was deleted.

Expand Up @@ -17,7 +17,7 @@

public class ThriftClientFactoryBean<T> implements FactoryBean<T>, InitializingBean {

private Logger log = LoggerFactory.getLogger(getClass());
private static final Logger LOGGER = LoggerFactory.getLogger(ThriftClientFactoryBean.class);

private String beanName;

Expand All @@ -37,32 +37,30 @@ public class ThriftClientFactoryBean<T> implements FactoryBean<T>, InitializingB
@SuppressWarnings("unchecked")
public T getObject() throws Exception {
if (beanClass.isInterface()) {
log.info("Prepare to generate proxy for {} with JDK", beanClass.getName());
LOGGER.info("Prepare to generate proxy for {} with JDK", beanClass.getName());

ThriftClientInvocationHandler invocationHandler = new ThriftClientInvocationHandler(serviceSignature, clientClass, clientConstructor);
return (T) Proxy.newProxyInstance(beanClass.getClassLoader(), new Class<?>[]{beanClass}, invocationHandler);
} else {
log.info("Prepare to generate proxy for {} with Cglib", beanClass.getName());
LOGGER.info("Prepare to generate proxy for {} with Cglib", beanClass.getName());

Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanClass);
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setUseFactory(true);

MethodInterceptor callback = (target, method, args, methodProxy) -> {
return methodProxy.invokeSuper(target, args);
};
MethodInterceptor callback = (target, method, args, methodProxy)
-> methodProxy.invokeSuper(target, args);

enhancer.setCallback(callback);
return (T) enhancer.create();
}

}

@Override
public Class<?> getObjectType() {
if (Objects.isNull(beanClass) && StringUtils.isBlank(beanName)) {
log.warn("Bean class is not found");
LOGGER.warn("Bean class is not found");
return null;
}

Expand All @@ -78,7 +76,7 @@ public Class<?> getObjectType() {
e.printStackTrace();
}
} else {
log.warn("Bean class is not found");
LOGGER.warn("Bean class is not found");
}

return null;
Expand All @@ -91,7 +89,7 @@ public boolean isSingleton() {

@Override
public void afterPropertiesSet() throws Exception {
log.info("Succeed to instantiate an instance of ThriftClientFactoryBean: {}", this);
LOGGER.info("Succeed to instantiate an instance of ThriftClientFactoryBean: {}", this);
}

public String getBeanName() {
Expand Down
Expand Up @@ -12,7 +12,6 @@ public class RpcCalculatorService implements CalculatorService.Iface {
public int add(int arg1, int arg2) {
BigDecimal arg1Decimal = new BigDecimal(arg1);
BigDecimal arg2Decimal = new BigDecimal(arg2);
System.out.println(System.currentTimeMillis());
return arg1Decimal.add(arg2Decimal).intValue();
}

Expand Down

0 comments on commit 1f457e5

Please sign in to comment.