Skip to content

Commit

Permalink
redis监听
Browse files Browse the repository at this point in the history
  • Loading branch information
hupengfei123 committed Apr 25, 2019
1 parent ea6c4df commit 88d6c17
Show file tree
Hide file tree
Showing 36 changed files with 586 additions and 74 deletions.
20 changes: 20 additions & 0 deletions HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Getting Started

### Reference Documentation
For further reference, please consider the following sections:

* [Official Gradle documentation](https://docs.gradle.org)

### Guides
The following guides illustrate how to use some features concretely:

* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
* [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/)
* [Messaging with Redis](https://spring.io/guides/gs/messaging-redis/)

### Additional Links
These additional references should also help you:

* [Gradle Build Scans – insights for your project's build](https://scans.gradle.com#gradle)

2 changes: 1 addition & 1 deletion common/src/main/java/Domain/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-17 20:46
**/
@Data
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/Domain/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-17 20:57
**/
@Data
Expand Down
28 changes: 14 additions & 14 deletions ioc/src/main/java/com/example/ioc/IocApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
@SpringBootApplication
public class IocApplication {

public static void main(String[] args) throws Exception {
SpringApplication.run(IocApplication.class, args);
MyBeanFactoryImpl beanFactory = new MyBeanFactoryImpl();
User user1 = (User)beanFactory.getBeanByName("com.example.ioc.domain.User");
User user2 = (User)beanFactory.getBeanByName("com.example.ioc.domain.User");
Student student1 = user1.getStudent();
Student student2 = user1.getStudent();
Student student3 = (Student)beanFactory.getBeanByName("com.example.ioc.domain.Student");
System.out.println(user1);
System.out.println(user2);
System.out.println(student1);
System.out.println(student2);
System.out.println(student3);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(IocApplication.class, args);
MyBeanFactoryImpl beanFactory = new MyBeanFactoryImpl();
User user1 = (User) beanFactory.getBeanByName("com.example.ioc.domain.User");
User user2 = (User) beanFactory.getBeanByName("com.example.ioc.domain.User");
Student student1 = user1.getStudent();
Student student2 = user1.getStudent();
Student student3 = (Student) beanFactory.getBeanByName("com.example.ioc.domain.Student");
System.out.println(user1);
System.out.println(user2);
System.out.println(student1);
System.out.println(student2);
System.out.println(student3);
}
}

12 changes: 6 additions & 6 deletions ioc/src/main/java/com/example/ioc/config/IoCInitConifg.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-23 16:44
**/
@Component
@Order(value = 1)
public class IoCInitConifg implements CommandLineRunner{
public class IoCInitConifg implements CommandLineRunner {

@Override
public void run(String... args){
ConcurrentHashMap<String,BeanDefinition> concurrentHashMap = new ConcurrentHashMap<>();
public void run(String... args) {
ConcurrentHashMap<String, BeanDefinition> concurrentHashMap = new ConcurrentHashMap<>();
Set<String> beanNameSet = Collections.synchronizedSet(new HashSet<>());
Reflections reflections = new Reflections();
//获得项目中所有被MyIoc标记得类
Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(MyIoc.class);
//将其信息初始进自定义容器MyBeanFactory中
for (Class clazz : typesAnnotatedWith){
for (Class clazz : typesAnnotatedWith) {
BeanDefinition beanDefinition = new BeanDefinition();
String className = clazz.getName();
String superclassName = clazz.getSuperclass().getName();
beanDefinition.setClassName(className);
beanDefinition.setSuperNames(superclassName);
beanDefinition.setAlias(getClassName(className));
concurrentHashMap.put(className,beanDefinition);
concurrentHashMap.put(className, beanDefinition);
beanNameSet.add(className);
}
MyBeanFactoryImpl.setBeanDineMap(concurrentHashMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
///**
// * @program: springBootPractice
// * @description:
// * @author: hu_pf@suixingpay.com
// * @author: hu_pf
// * @create: 2019-01-23 20:07
// **/
//@Component
Expand Down
28 changes: 14 additions & 14 deletions ioc/src/main/java/com/example/ioc/core/MyBeanFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-23 17:53
**/
@Log4j
public class MyBeanFactoryImpl implements MyBeanFactory{
public class MyBeanFactoryImpl implements MyBeanFactory {
//存储对象名称和已经实例化的对象映射
private static ConcurrentHashMap<String,Object> beanMap = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, Object> beanMap = new ConcurrentHashMap<>();
//存储对象名称和对应对象信息的映射
private static ConcurrentHashMap<String,BeanDefinition> beanDefineMap= new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, BeanDefinition> beanDefineMap = new ConcurrentHashMap<>();
//存储存储在容器中对象的名称
private static Set<String> beanNameSet = Collections.synchronizedSet(new HashSet<>());

@Override
public Object getBeanByName(String name) throws Exception {
//看有没有已经实例化的对象,有的话就直接返回
Object object = beanMap.get(name);
if (object != null){
if (object != null) {
return object;
}
//没有的话就实例化一个对象
object = getObject(beanDefineMap.get(name));
if (object != null){
if (object != null) {
//对实例化中对象的注入需要的参数
setFild(object);
//将实例化的对象放入Map中,便于下次使用
beanMap.put(name,object);
beanMap.put(name, object);
}
return object;
}

public void setFild(Object bean) throws Exception {
Field[] declaredFields = bean.getClass().getDeclaredFields();
for (Field field: declaredFields){
for (Field field : declaredFields) {
String filedAllName = field.getType().getName();
if (beanNameSet.contains(filedAllName)){
if (beanNameSet.contains(filedAllName)) {
Object findBean = getBeanByName(filedAllName);
//为对象中的属性赋值
field.setAccessible(true);
field.set(bean,findBean);
field.set(bean, findBean);
}
}
}
Expand All @@ -61,17 +61,17 @@ private Object getObject(BeanDefinition beanDefinition) throws Exception {
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
log.info("can not find bean by beanName: "+className);
throw new Exception("can not find bean by beanName: "+className);
log.info("can not find bean by beanName: " + className);
throw new Exception("can not find bean by beanName: " + className);
}
return clazz.newInstance();
}

public static void setBeanDineMap(ConcurrentHashMap<String,BeanDefinition> beanDefineMap){
public static void setBeanDineMap(ConcurrentHashMap<String, BeanDefinition> beanDefineMap) {
MyBeanFactoryImpl.beanDefineMap = beanDefineMap;
}

public static void setBeanNameSet(Set<String> beanNameSet){
public static void setBeanNameSet(Set<String> beanNameSet) {
MyBeanFactoryImpl.beanNameSet = beanNameSet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-22 20:46
**/
@Data
Expand Down
2 changes: 1 addition & 1 deletion ioc/src/main/java/com/example/ioc/domain/MyIocUseDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-24 20:43
**/
@MyIoc
Expand Down
6 changes: 3 additions & 3 deletions ioc/src/main/java/com/example/ioc/domain/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-23 20:25
**/
@MyIoc
public class Student {
public String play(){
return "student"+ this.toString();
public String play() {
return "student" + this.toString();
}
}
2 changes: 1 addition & 1 deletion ioc/src/main/java/com/example/ioc/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-23 20:25
**/
@MyIoc
Expand Down
12 changes: 6 additions & 6 deletions ioc/src/test/java/com/example/ioc/IocApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
@SpringBootTest()
public class IocApplicationTests {

// @MyIocUse
// private TestService2 testService2;
// @MyIocUse
// private TestService2 testService2;

@Test
public void contextLoads() {
@Test
public void contextLoads() {

System.out.println("1");
}
System.out.println("1");
}

}

2 changes: 1 addition & 1 deletion rpc-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
// compileOnly 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-18 13:46
**/
@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-18 14:20
**/
@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-18 11:13
**/
public class RpcInitConfig implements ImportBeanDefinitionRegistrar {
Expand Down
4 changes: 3 additions & 1 deletion rpc-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ dependencies {
compile project(":common")
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-web'
// implementation 'org.springframework.boot:spring-boot-starter-redis'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2'
runtimeOnly 'mysql:mysql-connector-java'
compileOnly 'org.projectlombok:lombok'
// compileOnly 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
package com.example.rpcserver;

import Domain.ClassTypeAdapterFactory;
import Domain.Request;
import Domain.Response;
import com.example.rpcserver.configuration.InitRpcConfig;
import com.example.rpcserver.rpcHandle.ServerThread;
import com.example.rpcserver.service.SendMessageImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public class RpcServerApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-20 15:47
**/
@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @program: springBootPractice
* @description:
* @author: hu_pf@suixingpay.com
* @author: hu_pf
* @create: 2019-01-20 15:45
**/
@Service
Expand Down
1 change: 0 additions & 1 deletion rpc-server/src/main/resources/application.properties

This file was deleted.

2 changes: 2 additions & 0 deletions rpc-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8081
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ include 'rpc-client'
include 'rpc-server'
include 'common'
include 'ioc'
include 'spring-redis'

30 changes: 30 additions & 0 deletions spring-redis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
HELP.md
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
Loading

0 comments on commit 88d6c17

Please sign in to comment.