master
WuXianChaoPin 7 years ago
parent f6d2781056
commit bd22c80270
  1. 2
      README.md
  2. 7
      core/pom.xml
  3. 40
      core/src/main/java/org/pqh/core/annotation/ConfigurationSelector.java
  4. 3
      core/src/main/java/org/pqh/core/annotation/EnableWebCrawlerConfiguration.java
  5. 64
      core/src/main/java/org/pqh/core/util/SpringUtil.java
  6. 8
      pom.xml
  7. 10
      web/pom.xml
  8. 9
      web/src/main/java/org/pqh/config/SpringConfig.java
  9. 7
      web/src/main/java/org/pqh/config/WebCrawlerConfig.java
  10. 18
      web/src/main/java/org/pqh/thrift/QueryImp.java
  11. 39
      web/src/main/java/org/pqh/thrift/ThriftClientDemo.java
  12. 27
      web/src/main/java/org/pqh/thrift/ThriftServerDemo.java
  13. 23
      web/src/main/resources/TestQry.thrift
  14. 11
      web/src/main/resources/config.properties

@ -1,2 +0,0 @@
![image](https://www.baidu.com/img/bd_logo1.png)

@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>webcrawler</artifactId>
<artifactId>Webcrawler</artifactId>
<groupId>org.pqh</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
@ -74,11 +74,6 @@
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>

@ -1,40 +0,0 @@
package org.pqh.core.annotation;
import org.pqh.core.util.LogManger;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
/**
* Created by reborn on 2017/9/20.
* 按条件导入配置类
*/
public class ConfigurationSelector implements ImportSelector,LogManger{
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
String appPath=this.getClass().getResource("").getPath();
log.info("appPath="+appPath);
//根据配置类判断
if(annotationMetadata.getClassName().equals("org.pqh.config.SpringConfig")){
return new String[]{"org.pqh.config.WebCrawlerConfig"};
}//根据配置类已有注解判断
else if(annotationMetadata.getAnnotationTypes().contains(Configuration.class.getName())){
return new String[]{"org.pqh.aaaaaaaaaaaa"};
}
//根据配置类路径判断
else if(appPath!=null&&appPath.contains("/web/WEB-INF")){
return new String[]{"org.pqh.bbbbbbbbbb"};
}
//根据系统信息
else if("XXXX".equals(System.getProperty("XXXX"))){
return new String[]{"org.pqh.cccccccccc"};
}
//根据环境变量
else if("XXX".equals(System.getenv("XXXX"))){
return new String[]{"org.pqh.ddddddddddd"};
}
else{
return new String[0];
}
}
}

@ -1,6 +1,8 @@
package org.pqh.core.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import java.lang.annotation.*;
@ -10,6 +12,7 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@PropertySource("classpath:config.properties")
@ComponentScan("org.pqh.core")
public @interface EnableWebCrawlerConfiguration {
}

@ -1,64 +0,0 @@
package org.pqh.core.util;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* Created by reborn on 2017/9/19.
*/
@Component
@Lazy(false)
public class SpringUtil implements ApplicationContextAware,LogManger {
private static ApplicationContext applicationContext;
public static <T>T getBean(Class<T> c){
return applicationContext.getBean(c);
}
public static <T>T getBean(String beanName){
return (T) applicationContext.getBean(beanName);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
log.info("init applicationContext");
SpringUtil.applicationContext=applicationContext;
}
/**
*
* @param clazz 注册class
* @param serviceName 注册别名
* @param propertyMap 注入属性
*/
public static void addBean(Class clazz,String serviceName,Map<String,Object> propertyMap){
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
if(propertyMap!=null){
for(String key:propertyMap.keySet()){
beanDefinitionBuilder.addPropertyValue(key, propertyMap.get(key));
}
}
registerBean(serviceName, beanDefinitionBuilder.getRawBeanDefinition());
}
/**
* @desc 向spring容器注册bean
* @param beanName
* @param beanDefinition
*/
private static void registerBean(String beanName, BeanDefinition beanDefinition) {
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) configurableApplicationContext
.getBeanFactory();
beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
}
}

@ -2,15 +2,15 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.pqh</groupId>
<artifactId>webcrawler</artifactId>
<artifactId>Webcrawler</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>core</module>
<module>web</module>
</modules>
<name>webcrawler Maven Webapp</name>
<url>https://gitee.com/WuXianChaoPin/webCrawler</url>
<name>Webcrawler Maven Webapp</name>
<url>https://gitee.com/WuXianChaoPin/WebCrawler</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -112,7 +112,7 @@
</dependencyManagement>
<build>
<finalName>webcrawler</finalName>
<finalName>Webcrawler</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>

@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>webcrawler</artifactId>
<artifactId>Webcrawler</artifactId>
<groupId>org.pqh</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
@ -17,6 +17,7 @@
<servlet-api.version>RELEASE</servlet-api.version>
<jstl.version>1.2</jstl.version>
<standard.version>1.1.2</standard.version>
<thrift.version>RELEASE</thrift.version>
</properties>
<dependencies>
@ -122,9 +123,14 @@
<version>${standard.version}</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>${thrift.version}</version>
</dependency>
</dependencies>
<build>
<finalName>web</finalName>
<finalName>Webcrawler</finalName>
</build>
</project>

@ -1,6 +1,6 @@
package org.pqh.config;
import org.pqh.core.annotation.ConfigurationSelector;
import org.pqh.core.annotation.EnableWebCrawlerConfiguration;
import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@ -10,13 +10,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Created by reborn on 2017/7/28.
*/
@Configuration
@EnableWebMvc
@ComponentScan({"${spring.scan:org.pqh.controller}"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
@PropertySource("classpath:config.properties")
@ComponentScan({"${spring.scan:org.pqh.controller}","org.pqh.util"})
@Import({ConfigurationSelector.class})
//@Import(WebCrawlerConfig.class)
@EnableWebCrawlerConfiguration
public class SpringConfig implements WebMvcConfigurer {
@Override

@ -1,7 +0,0 @@
package org.pqh.config;
import org.pqh.core.annotation.EnableWebCrawlerConfiguration;
@EnableWebCrawlerConfiguration
public class WebCrawlerConfig {
}

@ -0,0 +1,18 @@
package org.pqh.thrift;
import org.apache.thrift.TException;
public class QueryImp implements TestQry.Iface {
@Override
public QryResult qryTest(int qryCode) throws TException {
QryResult result = new QryResult();
if(qryCode==1){
result.code = 1;
result.msg = "success";
}else{
result.code = 0;
result.msg = "fail";
}
return result;
}
}

@ -0,0 +1,39 @@
package org.pqh.thrift;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class ThriftClientDemo {
private final static int DEFAULT_QRY_CODE = 1;
public static void main(String[] args){
try {
TTransport tTransport = getTTransport();
TProtocol protocol = new TBinaryProtocol(tTransport);
TestQry.Client client = new TestQry.Client(protocol);
QryResult result = client.qryTest(DEFAULT_QRY_CODE);
System.out.println("code="+result.code+" msg="+result.msg);
}catch (Exception e) {
e.printStackTrace();
}
}
private static TTransport getTTransport() throws Exception{
try{
TTransport tTransport = getTTransport("127.0.0.1", 2233, 5000);
if(!tTransport.isOpen()){
tTransport.open();
}
return tTransport;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
private static TTransport getTTransport(String host, int port, int timeout) {
final TSocket tSocket = new TSocket(host, port, timeout);
final TTransport transport = new TFramedTransport(tSocket);
return transport;
}
}

@ -0,0 +1,27 @@
package org.pqh.thrift;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
public class ThriftServerDemo {
public static void main(String[] args) {
try {
TNonblockingServerSocket socket = new TNonblockingServerSocket(2233);
TestQry.Processor processor = new TestQry.Processor(new QueryImp());
TNonblockingServer.Args arg = new TNonblockingServer.Args(socket);
arg.protocolFactory(new TBinaryProtocol.Factory());
arg.transportFactory(new TFramedTransport.Factory());
arg.processorFactory(new TProcessorFactory(processor));
TServer server = new TNonblockingServer (arg);
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,23 @@
/**
* TestQry.thrift
* struct和一个服务接口service
* thrift-0.9.2
**/
namespace java com.thrift
struct QryResult {
/**
*, 10
*/
1:i32 code;
/**
*
*/
2:string msg;
}
service TestQry{
/**
* qryCode值为1时返回"成功"qryCode值为其他值时返回"失败"
* @param qryCode测试参数
*/
QryResult qryTest(1:i32 qryCode)
}

@ -1,11 +1,8 @@
#-----------------------hibernateÅäÖÃ-----------------------------
#-----------------------hibernate\u914D\u7F6E-----------------------------
hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
hibernate.connection.driver_class=com.mysql.cj.jdbc.Driver
hibernate.connection.url=jdbc:mysql://mikuhime.xyz:3306/webcrawler?serverTimezone=UTC
hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/webcrawler?serverTimezone=UTC
hibernate.connection.username=bilibili
hibernate.connection.password=2233
#-----------------------DruidDataSourceÅäÖÃ-----------------------
druid.maxActive=20
#-----------------------DruidDataSource\u914D\u7F6E-----------------------
druid.maxActive=20
Loading…
Cancel
Save