├── .gitignore
├── ActivemqDemo2
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── activemq
│ │ │ └── activemq
│ │ │ ├── ActivemqApplication.java
│ │ │ ├── Queue
│ │ │ ├── Consumer.java
│ │ │ ├── Consumer2.java
│ │ │ ├── MqConfig.java
│ │ │ └── ProducerPoint.java
│ │ │ ├── QueueAndTopic
│ │ │ ├── ActiveMQConfig.java
│ │ │ └── Consumer5.java
│ │ │ └── Topic
│ │ │ ├── Consumer3.java
│ │ │ ├── Consumer4.java
│ │ │ └── ProducerTopic.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── activemq
│ └── activemq
│ └── ActivemqApplicationTests.java
├── README.md
├── SpringBoot-RabbitMq
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ ├── one2one
│ │ │ ├── Receiving.java
│ │ │ └── Send.java
│ │ │ ├── publish
│ │ │ ├── Rece1.java
│ │ │ ├── Rece2.java
│ │ │ └── Send.java
│ │ │ ├── route
│ │ │ ├── Rece1.java
│ │ │ ├── Rece2.java
│ │ │ └── Send.java
│ │ │ ├── topic
│ │ │ ├── Rece1.java
│ │ │ ├── Rece2.java
│ │ │ ├── Rece3.java
│ │ │ └── Send.java
│ │ │ └── work
│ │ │ ├── Rece1.java
│ │ │ ├── Rece2.java
│ │ │ └── Send.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── demo
│ └── DemoApplicationTests.java
├── SpringBoot-Redis
├── SpringBoot-Redisson
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── demo
│ │ │ │ └── redisson
│ │ │ │ ├── RedissonApplication.java
│ │ │ │ ├── config
│ │ │ │ ├── DistributedLocker.java
│ │ │ │ ├── RedisDistributedLocker.java
│ │ │ │ ├── RedisLockUtil.java
│ │ │ │ └── RedissonConfig.java
│ │ │ │ └── controller
│ │ │ │ └── RedissonController.java
│ │ └── resources
│ │ │ └── application.yml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── demo
│ │ └── redisson
│ │ └── RedissonApplicationTests.java
├── Springboot-Redis-SETEX
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── demo
│ │ │ │ ├── DemoApplication.java
│ │ │ │ ├── controller
│ │ │ │ ├── RedisRedLockContoller.java
│ │ │ │ └── RedisSetexContoller.java
│ │ │ │ └── redis
│ │ │ │ ├── config
│ │ │ │ └── DistributedLockConfig.java
│ │ │ │ ├── pojo
│ │ │ │ └── Lock.java
│ │ │ │ └── redlock
│ │ │ │ ├── AquiredLockWorker.java
│ │ │ │ ├── DistributedLocker.java
│ │ │ │ ├── RedisLocker.java
│ │ │ │ ├── RedissonConnector.java
│ │ │ │ └── UnableToAquireLockException.java
│ │ └── resources
│ │ │ └── application.yml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ └── DemoApplicationTests.java
└── springBoot-redis
│ └── SpringBoot-Redis
│ ├── redis-cluster
│ ├── pom.xml
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── rediscluster
│ │ │ │ └── RedisClusterApplication.java
│ │ └── resources
│ │ │ └── application.yml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── rediscluster
│ │ └── RedisClusterApplicationTests.java
│ └── sentinel-redis
│ ├── pom.xml
│ └── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── sentinelredis
│ │ │ ├── RedisCacheConfig.java
│ │ │ └── SentinelRedisApplication.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── sentinelredis
│ └── SentinelRedisApplicationTests.java
├── SpringBoot-dubbo
└── 代码示例
├── SpringBoot-quartz
├── .gitignore
├── HELP.md
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ ├── cifg
│ │ │ ├── Job.java
│ │ │ ├── QuartzConfig.java
│ │ │ └── TestJob.java
│ │ │ └── controller
│ │ │ └── QuartzController.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── demo
│ └── DemoApplicationTests.java
├── SpringBoot-vue-crud
├── .gitignore
├── HELP.md
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── vue
│ │ │ └── demo
│ │ │ └── vuedemo
│ │ │ ├── VuedemoApplication.java
│ │ │ ├── common
│ │ │ ├── PageQuery.java
│ │ │ └── ReturnMessage.java
│ │ │ ├── controller
│ │ │ ├── LoginController.java
│ │ │ └── StudentController.java
│ │ │ ├── dao
│ │ │ └── StudentDao.java
│ │ │ ├── pojo
│ │ │ └── Student.java
│ │ │ └── service
│ │ │ ├── StudentService.java
│ │ │ └── StudentServiceImpl.java
│ └── resources
│ │ ├── application.yml
│ │ ├── mapper
│ │ └── StudentMapper.xml
│ │ └── sql
│ │ └── student.sql
│ └── test
│ └── java
│ └── com
│ └── vue
│ └── demo
│ └── vuedemo
│ └── VuedemoApplicationTests.java
├── SpringBootDuridDemo
├── README.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── demo
│ │ │ ├── MysqlConfig2Databases
│ │ │ ├── MultiDataSourceConfig.java
│ │ │ ├── OneDataSourceConfig.java
│ │ │ └── SecondDataSourceConfig.java
│ │ │ ├── SpringBootDemoApplication.java
│ │ │ ├── controller
│ │ │ └── UserController.java
│ │ │ ├── mapper
│ │ │ ├── one
│ │ │ │ └── User1Mapper.java
│ │ │ └── second
│ │ │ │ └── User2Mapper.java
│ │ │ └── pojo
│ │ │ └── User.java
│ └── resources
│ │ ├── application.properties
│ │ ├── mapper
│ │ ├── config
│ │ │ └── mybatis-config.xml
│ │ ├── one
│ │ │ └── User1Mapper.xml
│ │ └── second
│ │ │ └── User2Mapper.xml
│ │ └── sql
│ │ └── user.sql
│ └── test
│ └── java
│ └── com
│ └── example
│ └── demo
│ └── SpringBootDemoApplicationTests.java
├── Springboot-MongodbDemo
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── mongodbdemo
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ ├── m1dao
│ │ │ └── MongoDao1.java
│ │ │ ├── m2dao
│ │ │ └── MongoDao2.java
│ │ │ └── test
│ │ │ ├── MongoConfig1.java
│ │ │ ├── MongoConfig2.java
│ │ │ ├── MultipleMongoConfig.java
│ │ │ ├── MultipleMongoProperties.java
│ │ │ └── User.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── mongodbdemo
│ └── demo
│ └── DemoApplicationTests.java
├── Springboot-Redis-SETEX
├── .gitignore
└── mvnw.cmd
├── spring-session
├── .gitignore
├── .mvn
│ └── wrapper
│ │ ├── MavenWrapperDownloader.java
│ │ ├── maven-wrapper.jar
│ │ └── maven-wrapper.properties
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── springsession
│ │ │ └── demo
│ │ │ ├── Config
│ │ │ └── SessionConfig.java
│ │ │ ├── DemoApplication.java
│ │ │ └── jpa
│ │ │ └── StudentController.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── springsession
│ └── demo
│ └── DemoApplicationTests.java
├── springBoot-redis
└── SpringBoot-Redis
│ ├── README.md
│ ├── redis-cluster
│ └── .gitignore
│ └── sentinel-redis
│ └── .gitignore
├── springboot-atomikos
├── .gitignore
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── springbootatomikos
│ │ │ ├── SpringbootatomikosApplication.java
│ │ │ ├── config
│ │ │ ├── one
│ │ │ │ └── MyBatisConfig1.java
│ │ │ ├── pojo
│ │ │ │ ├── DBConfig1.java
│ │ │ │ └── DBConfig2.java
│ │ │ └── two
│ │ │ │ └── MyBatisConfig2.java
│ │ │ ├── mapper
│ │ │ ├── one
│ │ │ │ └── UserMapper1.java
│ │ │ └── two
│ │ │ │ └── UserMapper2.java
│ │ │ ├── pojo
│ │ │ └── User.java
│ │ │ └── service
│ │ │ └── UserService.java
│ └── resources
│ │ └── application.yml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── springbootatomikos
│ └── SpringbootatomikosApplicationTests.java
├── springboot-handlerinterceptor
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── springboothandlerinterceptor
│ │ │ ├── SpringbootHandlerinterceptorApplication.java
│ │ │ ├── config
│ │ │ ├── MyHandlerInterceptor.java
│ │ │ ├── MyInterceptorConfig.java
│ │ │ └── MyInterceptorConfig2.java
│ │ │ ├── controller
│ │ │ └── TestController.java
│ │ │ └── other
│ │ │ └── User.java
│ └── resources
│ │ ├── application.properties
│ │ └── static
│ │ ├── a.txt
│ │ ├── css
│ │ └── a.css
│ │ └── index.html
│ └── test
│ └── java
│ └── com
│ └── example
│ └── springboothandlerinterceptor
│ └── SpringbootHandlerinterceptorApplicationTests.java
├── springboot-listener-demo
├── .gitignore
├── .mvn
│ └── wrapper
│ │ ├── MavenWrapperDownloader.java
│ │ ├── maven-wrapper.jar
│ │ └── maven-wrapper.properties
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── springbootlistenerdemo
│ │ │ ├── SpringbootListenerDemoApplication.java
│ │ │ ├── bean
│ │ │ └── User.java
│ │ │ ├── controller
│ │ │ └── TestController.java
│ │ │ ├── listener
│ │ │ ├── MyEvent.java
│ │ │ ├── MyEventListener.java
│ │ │ ├── MyHttpSessionListener.java
│ │ │ ├── MyServletContextListener.java
│ │ │ └── MyServletRequestListener.java
│ │ │ └── services
│ │ │ └── UserService.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── springbootlistenerdemo
│ └── SpringbootListenerDemoApplicationTests.java
├── springbootCrudDemo
├── README.md
└── springBootDemo
│ ├── mvnw
│ ├── mvnw.cmd
│ ├── pom.xml
│ ├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── demo
│ │ │ │ ├── SpringBootDemoApplication.java
│ │ │ │ ├── common
│ │ │ │ └── beans.java
│ │ │ │ ├── controller
│ │ │ │ └── userController.java
│ │ │ │ ├── fiflter
│ │ │ │ └── fiflterTest.java
│ │ │ │ ├── mapper
│ │ │ │ └── userMapper.java
│ │ │ │ ├── pojo
│ │ │ │ └── User.java
│ │ │ │ ├── service
│ │ │ │ └── userService.java
│ │ │ │ └── serviceImp
│ │ │ │ └── userServiceImp.java
│ │ └── resources
│ │ │ ├── NewFile.xml
│ │ │ ├── application.properties
│ │ │ ├── mapper
│ │ │ ├── config
│ │ │ │ └── mybatis-config.xml
│ │ │ └── userMapper.xml
│ │ │ └── sql
│ │ │ └── user.sql
│ └── test
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ └── SpringBootDemoApplicationTests.java
│ └── target
│ ├── classes
│ ├── NewFile.xml
│ ├── application.properties
│ ├── mapper
│ │ ├── config
│ │ │ └── mybatis-config.xml
│ │ └── userMapper.xml
│ └── sql
│ │ └── user.sql
│ └── m2e-wtp
│ └── web-resources
│ └── META-INF
│ ├── MANIFEST.MF
│ └── maven
│ └── com.example
│ └── springBootDemo
│ ├── pom.properties
│ └── pom.xml
└── springsecuritydemo
├── .gitignore
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── example
│ │ └── springsecuritydemo
│ │ ├── SpringsecuritydemoApplication.java
│ │ ├── config
│ │ └── SecurityConfig.java
│ │ └── controller
│ │ └── SecurityController.java
└── resources
│ ├── application.properties
│ ├── static
│ ├── a.css
│ └── css
│ │ └── main.css
│ └── templates
│ ├── admin.html
│ ├── content.html
│ ├── index.html
│ └── login.html
└── test
└── java
└── com
└── example
└── springsecuritydemo
└── SpringsecuritydemoApplicationTests.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
--------------------------------------------------------------------------------
/ActivemqDemo2/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/ActivemqDemo2/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.6.RELEASE
9 |
10 |
11 | com.example.Activemq
12 | activemq
13 | 0.0.1-SNAPSHOT
14 | activemq
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-activemq
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-web
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-devtools
36 | runtime
37 | true
38 |
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | true
44 |
45 |
46 |
47 | org.springframework.boot
48 | spring-boot-starter-test
49 | test
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 | org.springframework.boot
58 | spring-boot-maven-plugin
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/ActivemqApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ActivemqApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(ActivemqApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/Queue/Consumer.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.Queue;
2 |
3 | import org.springframework.jms.annotation.JmsListener;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * 创建消费者
8 | *
9 | * 使用注解 @JmsListener(destination = "neo.queue"),
10 | * 表示此方法监控了名为 neo.queue 的队列。
11 | * 当队列 neo.queue 中有消息发送时会触发此方法的执行,text 为消息内容。
12 | **/
13 | @Component
14 | public class Consumer {
15 |
16 | @JmsListener(destination = "neo.queue")
17 | public void receiveQueue(String text) {
18 | System.out.println(" Consumer 接受到的消息是 : " + text);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/Queue/Consumer2.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.Queue;
2 |
3 | import org.springframework.jms.annotation.JmsListener;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * 创建消费者
8 | *
9 | * 使用注解 @JmsListener(destination = "neo.queue"),
10 | * 表示此方法监控了名为 neo.queue 的队列。
11 | * 当队列 neo.queue 中有消息发送时会触发此方法的执行,text 为消息内容。
12 | **/
13 | @Component
14 | public class Consumer2 {
15 |
16 | @JmsListener(destination = "neo.queue")
17 | public void receiveQueue(String text) {
18 | System.out.println(" Consumer2 接受到的消息是 : " + text);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/Queue/MqConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.Queue;
2 |
3 |
4 | import org.apache.activemq.command.ActiveMQQueue;
5 | import org.apache.activemq.command.ActiveMQTopic;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 |
9 | import javax.jms.Queue;
10 | import javax.jms.Topic;
11 |
12 |
13 | /**
14 | * 创建队列
15 | * 队列发送的消息,只能被一个消费者接收。
16 | */
17 |
18 | @Configuration
19 | public class MqConfig {
20 | @Bean
21 | public Queue queue() {
22 | return new ActiveMQQueue("neo.queue");
23 | }
24 |
25 | @Bean
26 | public Topic topic() {
27 | return new ActiveMQTopic("topic1");
28 | }
29 |
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/Queue/ProducerPoint.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.Queue;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.jms.core.JmsMessagingTemplate;
5 | import org.springframework.stereotype.Component;
6 |
7 | import javax.jms.Queue;
8 |
9 | /**
10 | * @description:
11 | * @author: 消息生产者
12 | * @create: 创建一个消息的生产者:
13 | **/
14 |
15 | @Component
16 | public class ProducerPoint {
17 |
18 | //JmsMessagingTemplate 是 Spring 提供发送消息的工具类,使用 JmsMessagingTemplate 和创建好的 queue 对消息进行发送。
19 | @Autowired
20 | private JmsMessagingTemplate jmsMessagingTemplate;
21 |
22 | @Autowired
23 | private Queue queue;
24 |
25 | public void sendQueue(String msg) {
26 | System.out.println(" 发送消息为 :" + msg);
27 | this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/QueueAndTopic/ActiveMQConfig.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.QueueAndTopic;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.jms.annotation.EnableJms;
6 | import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
7 | import org.springframework.jms.config.JmsListenerContainerFactory;
8 |
9 | import javax.jms.ConnectionFactory;
10 |
11 | /**
12 | * 在同一项目中既支持队列模式也支持广播模式,
13 | * 可以通过 DefaultJmsListenerContainerFactory 创建自定义的 JmsListenerContainerFactory 实例,
14 | * 之后在 @JmsListener 注解中通过 containerFactory 属性引用它。
15 | **/
16 |
17 | @Configuration
18 | @EnableJms
19 | public class ActiveMQConfig {
20 |
21 | @Bean("queueListenerFactory")
22 | public JmsListenerContainerFactory> queueListenerFactory(ConnectionFactory connectionFactory) {
23 | DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
24 | factory.setConnectionFactory(connectionFactory);
25 | factory.setPubSubDomain(false);
26 | return factory;
27 | }
28 |
29 | @Bean("topicListenerFactory")
30 | public JmsListenerContainerFactory> topicListenerFactory(ConnectionFactory connectionFactory) {
31 | DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
32 | factory.setConnectionFactory(connectionFactory);
33 | factory.setPubSubDomain(true);
34 | return factory;
35 | }
36 | }
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/QueueAndTopic/Consumer5.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.QueueAndTopic;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.jms.annotation.JmsListener;
5 | import org.springframework.jms.core.JmsMessagingTemplate;
6 | import org.springframework.messaging.Message;
7 | import org.springframework.messaging.MessageHeaders;
8 | import org.springframework.stereotype.Component;
9 |
10 | import javax.jms.Queue;
11 |
12 | /**
13 | *
14 | **/
15 | @Component
16 | public class Consumer5 {
17 |
18 | @JmsListener(destination = "neo.queue", containerFactory = "queueListenerFactory")
19 | public void receiveQueue(String text) {
20 | System.out.println("Consumer5 queue msg : " + text);
21 | }
22 |
23 | @JmsListener(destination = "topic1", containerFactory = "topicListenerFactory")
24 | public void receiveTopic1(String text) {
25 | System.out.println("Consumer5 receiveTopic1 msg : " + text);
26 | }
27 |
28 | @JmsListener(destination = "topic1", containerFactory = "topicListenerFactory")
29 | public void receiveTopic2(String text) {
30 | System.out.println("Consumer5 receiveTopic2 msg : " + text);
31 | }
32 | }
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/Topic/Consumer3.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.Topic;
2 |
3 | import org.springframework.jms.annotation.JmsListener;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * 创建消费者
8 | *
9 | * 使用注解 @JmsListener(destination = "neo.queue"),
10 | * 表示此方法监控了名为 neo.queue 的队列。
11 | * 当队列 neo.queue 中有消息发送时会触发此方法的执行,text 为消息内容。
12 | **/
13 | @Component
14 | public class Consumer3 {
15 |
16 | @JmsListener(destination = "topic1")
17 | public void receiveTopic(String text) {
18 | System.out.println(" Consumer3 接受到的消息是 : " + text);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/Topic/Consumer4.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.Topic;
2 |
3 | import org.springframework.jms.annotation.JmsListener;
4 | import org.springframework.stereotype.Component;
5 |
6 | /**
7 | * 创建消费者
8 | *
9 | * 使用注解 @JmsListener(destination = "neo.queue"),
10 | * 表示此方法监控了名为 neo.queue 的队列。
11 | * 当队列 neo.queue 中有消息发送时会触发此方法的执行,text 为消息内容。
12 | **/
13 | @Component
14 | public class Consumer4 {
15 |
16 |
17 | @JmsListener(destination = "topic1")
18 | public void receiveTopic(String text) {
19 | System.out.println(" Consumer4 接受到的消息是: " + text);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/java/com/example/activemq/activemq/Topic/ProducerTopic.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq.Topic;
2 |
3 |
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.jms.core.JmsMessagingTemplate;
6 | import org.springframework.stereotype.Component;
7 |
8 | import javax.jms.Topic;
9 |
10 | @Component
11 | public class ProducerTopic {
12 |
13 | @Autowired
14 | private JmsMessagingTemplate jmsMessagingTemplate;
15 | @Autowired
16 | private Topic topic;
17 |
18 | public void sendTopic(String msg) {
19 | System.out.println(" 发送的消息是 :" + msg);
20 | this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
21 | }
22 | }
--------------------------------------------------------------------------------
/ActivemqDemo2/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | # 基于内存的 ActiveMQ
2 | #spring.activemq.in-memory=true
3 | # 独立安装的 ActiveMQ
4 | spring.activemq.broker-url=tcp://127.0.0.1:61616
5 | spring.activemq.user=admin
6 | spring.activemq.password=admin
7 | # 不使用连接池
8 | #spring.activemq.pool.enabled=false
9 |
10 |
--------------------------------------------------------------------------------
/ActivemqDemo2/src/test/java/com/example/activemq/activemq/ActivemqApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.activemq.activemq;
2 |
3 | import com.example.activemq.activemq.Queue.ProducerPoint;
4 | import com.example.activemq.activemq.Topic.ProducerTopic;
5 | import org.junit.Test;
6 | import org.junit.runner.RunWith;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.boot.test.context.SpringBootTest;
9 | import org.springframework.test.context.junit4.SpringRunner;
10 |
11 | @RunWith(SpringRunner.class)
12 | @SpringBootTest
13 | public class ActivemqApplicationTests {
14 |
15 | @Test
16 | public void contextLoads() {
17 | }
18 |
19 |
20 | @Autowired
21 | private ProducerPoint producer;
22 |
23 | @Autowired
24 | private ProducerTopic topic;
25 |
26 |
27 | // 生产者- 消费者 p2p 一个消息只能被一个消费者消费
28 |
29 | //当有多个消费者监听一个队列时,消费者会自动均衡负载的接收消息,并且每个消息只能有一个消费者所接收。
30 | @Test
31 | public void test1() {
32 | for (int i = 0; i < 3; i++) {
33 | producer.sendQueue(" 你好。。。");
34 | }
35 |
36 | /*
37 |
38 | 发送消息为 : 你好。。。
39 | 发送消息为 : 你好。。。
40 | Consumer2 接受到的消息是 : 你好。。。
41 | Consumer 接受到的消息是 : 你好。。。
42 | 发送消息为 : 你好。。。
43 | Consumer2 接受到的消息是 : 你好。。。
44 |
45 |
46 |
47 | */
48 | }
49 |
50 | // 生产者- 消费者 topic
51 | @Test
52 | public void test2() {
53 | producer.sendQueue(" 你好。。。");
54 | topic.sendTopic(" 你们好。。。");
55 | }
56 |
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## SpringBoot 代码整合 (麻烦给个start,谢谢)
2 |
3 | SpringBoot coding
4 |
5 |
6 | ## 1 ActivemqDemo2 消息队列 : 同时支持 p2p 和 topic
7 | ## 2 springBootDuridDemo 多数据源 : mybatis + mysql + srpingboot + durid(连接池)
8 | ## 3 spring-session 分布式 session : spring session + springboot
9 | ## 4 springbootCrudDemo 简单的crud项目 : mysql + springboot
10 | ## 5 springboot-MongodbDemo mongodb 多数据源
11 | ## 6 springsecuritydemo springsecuritydemo security项目(不带数据库)
12 | ## 7 springBoot-quartz 整合quartz定时器 手动控制启动暂停任务
13 | ## 8 springboot-Redis-SETEX redis setex 和 redis redlock 分布式锁
14 | ## 9 springboot-atomikos atomikos 分布式事务
15 | ## 10 springBoot-redis 整合redis主从和集群模式
16 | ## 11 SpringBoot-RabbitMq 整合Rabbit五种消息队列类型
17 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.0.6.RELEASE
9 |
10 |
11 | com.example
12 | demo
13 | 0.0.1-SNAPSHOT
14 | rabbitmq
15 | SpringBoot整合RabbitMq
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 |
34 |
35 | com.github.luues
36 | spring-boot-starter-rabbitmq
37 | 1.2.9.1.RELEASE
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 | org.springframework.boot
47 | spring-boot-maven-plugin
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class DemoApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(DemoApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/one2one/Receiving.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.one2one;
2 |
3 | import com.rabbitmq.client.*;
4 |
5 | import java.io.IOException;
6 |
7 | import static com.example.demo.one2one.Send.QUEUE_NAME;
8 |
9 |
10 | /**
11 | * @description: 点对点 消息生产消费
12 | * @author: Administrator
13 | * @create: 2020-07-11 21:57
14 | **/
15 | public class Receiving {
16 |
17 | public static void main(String[] argv) throws Exception {
18 |
19 | ConnectionFactory factory = new ConnectionFactory();
20 | factory.setHost("localhost");
21 | Connection connection = factory.newConnection();
22 | Channel channel = connection.createChannel();
23 |
24 | channel.queueDeclare(QUEUE_NAME, false, false, false, null);
25 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
26 |
27 |
28 | while (true) {
29 |
30 | Consumer consumer = new DefaultConsumer(channel) {
31 | @Override
32 | public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
33 | String message = new String(body, "UTF-8");
34 | System.out.println(" [x] Received '" + message + "'");
35 | channel.basicAck(envelope.getDeliveryTag(), false);
36 | }
37 | };
38 | //true 监听队列 有消息就获取 没有就阻塞 true表示自动确认消息
39 | channel.basicConsume(QUEUE_NAME, false, consumer);
40 |
41 | }
42 |
43 |
44 | }
45 |
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/one2one/Send.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.one2one;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 |
7 | import java.io.IOException;
8 |
9 |
10 | /**
11 | * @description: 发送消息
12 | * @author: Administrator
13 | * @create: 2020-07-11 21:58
14 | **/
15 | public class Send {
16 |
17 | public static final String QUEUE_NAME = "test_queue1";
18 |
19 |
20 | public static void main(String[] args) {
21 |
22 | ConnectionFactory factory = new ConnectionFactory();
23 | Channel channel = null;
24 | Connection connection = null;
25 | factory.setHost("localhost");
26 | //factory.setPort(5671);
27 |
28 | try {
29 | connection = factory.newConnection();
30 | channel = connection.createChannel();
31 | //创建生命队列
32 | channel.queueDeclare(QUEUE_NAME, false, false, false, null);
33 | String message = "Hello World! 111 ";
34 | //发送消息
35 | channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
36 | System.out.println(" [x] Sent '" + message + "'");
37 | } catch (Exception e) {
38 | e.printStackTrace();
39 | } finally {
40 | factory.clone();
41 | if (connection != null) {
42 | try {
43 | connection.close();
44 | } catch (IOException e) {
45 | e.printStackTrace();
46 | }
47 | }
48 | }
49 |
50 |
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/publish/Rece1.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.publish;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 | import com.rabbitmq.client.DeliverCallback;
7 |
8 | import java.io.IOException;
9 | import java.util.concurrent.TimeoutException;
10 |
11 | /**
12 | * @description:
13 | * @author: Administrator
14 | * @create: 2020-07-12 17:55
15 | **/
16 | public class Rece1 {
17 |
18 | //交换机
19 | private static final String EXCHANGE_NAME = "exchange_logs";
20 | //队列
21 | public static final String QUEUE_NAME = "test_queue1_name";
22 |
23 | public static void main(String[] args) throws IOException, TimeoutException {
24 | ConnectionFactory factory = new ConnectionFactory();
25 | factory.setHost("localhost");
26 | factory.setVirtualHost("/test");
27 | factory.setUsername("test");
28 | factory.setPassword("test");
29 |
30 | Connection connection = factory.newConnection();
31 | Channel channel = connection.createChannel();
32 | //声明队列
33 | channel.queueDeclare(QUEUE_NAME,false,false,false,null);
34 |
35 | //绑定交换机
36 | channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
37 |
38 | //绑定队列到交换机
39 | channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
40 |
41 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
42 |
43 | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
44 | String message = new String(delivery.getBody(), "UTF-8");
45 | System.out.println(" [x] Received1 '" + message + "'");
46 | };
47 |
48 | //自动确认消息
49 | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
50 | });
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/publish/Rece2.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.publish;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 | import com.rabbitmq.client.DeliverCallback;
7 |
8 | import java.io.IOException;
9 | import java.util.concurrent.TimeoutException;
10 |
11 | /**
12 | * @description:
13 | * @author: Administrator
14 | * @create: 2020-07-12 17:55
15 | **/
16 | public class Rece2 {
17 |
18 | //交换机
19 | private static final String EXCHANGE_NAME = "exchange_logs";
20 | //队列
21 | public static final String QUEUE_NAME = "test_queue2_name";
22 |
23 | public static void main(String[] args) throws IOException, TimeoutException {
24 |
25 | ConnectionFactory factory = new ConnectionFactory();
26 |
27 | factory.setHost("localhost");
28 | factory.setVirtualHost("/test");
29 | factory.setUsername("test");
30 | factory.setPassword("test");
31 |
32 | Connection connection = factory.newConnection();
33 | Channel channel = connection.createChannel();
34 | //声明队列
35 | channel.queueDeclare(QUEUE_NAME,false,false,false,null);
36 |
37 | //绑定交换机
38 | channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
39 |
40 | //绑定队列到交换机
41 | channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
42 |
43 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
44 |
45 | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
46 | String message = new String(delivery.getBody(), "UTF-8");
47 | System.out.println(" [x] Received2 '" + message + "'");
48 | };
49 |
50 | //自动确认消息
51 | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
52 | });
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/publish/Send.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.publish;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 |
7 |
8 | /**
9 | * @description:
10 | * @author: Administrator
11 | * @create: 2020-07-12 17:53
12 | **/
13 | public class Send {
14 | //交换机
15 | private static final String EXCHANGE_NAME = "exchange_logs";
16 |
17 |
18 | public static void main(String[] argv) throws Exception {
19 | ConnectionFactory factory = new ConnectionFactory();
20 | factory.setHost("localhost");
21 | factory.setVirtualHost("/test");
22 | factory.setUsername("test");
23 | factory.setPassword("test");
24 |
25 |
26 | Connection connection = factory.newConnection();
27 | Channel channel = connection.createChannel();
28 |
29 | //绑定交换机
30 | channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
31 |
32 | //消息
33 | String msg = "订单更新..... ";
34 | channel.basicPublish(EXCHANGE_NAME,"",null,msg.getBytes());
35 | System.out.println("发送的消息 : " + msg);
36 |
37 | channel.close();
38 | connection.close();
39 | }
40 | }
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/route/Rece1.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.route;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 | import com.rabbitmq.client.DeliverCallback;
7 |
8 | import java.io.IOException;
9 | import java.util.concurrent.TimeoutException;
10 |
11 | /**
12 | * @description:
13 | * @author: Administrator
14 | * @create: 2020-07-12 17:55
15 | **/
16 | public class Rece1 {
17 |
18 | //交换机
19 | private static final String EXCHANGE_NAME = "exchange_router";
20 | //队列
21 | public static final String QUEUE_NAME = "router_queue_add";
22 |
23 | public static void main(String[] args) throws IOException, TimeoutException {
24 | ConnectionFactory factory = new ConnectionFactory();
25 | factory.setHost("localhost");
26 | factory.setVirtualHost("/test");
27 | factory.setUsername("test");
28 | factory.setPassword("test");
29 |
30 | Connection connection = factory.newConnection();
31 | Channel channel = connection.createChannel();
32 | //声明队列
33 | channel.queueDeclare(QUEUE_NAME,false,false,false,null);
34 |
35 | //绑定交换机
36 | channel.exchangeDeclare(EXCHANGE_NAME, "direct");
37 |
38 | //绑定队列到交换机
39 | channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "add");
40 |
41 | //同一时刻只接受一条消息
42 | channel.basicQos(1);
43 |
44 |
45 |
46 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
47 |
48 | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
49 | String message = new String(delivery.getBody(), "UTF-8");
50 | System.out.println(" [x] 新增商品 '" + message + "'");
51 | };
52 |
53 | //自动确认消息
54 | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
55 | });
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/route/Rece2.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.route;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 | import com.rabbitmq.client.DeliverCallback;
7 |
8 | import java.io.IOException;
9 | import java.util.concurrent.TimeoutException;
10 |
11 | /**
12 | * @description:
13 | * @author: Administrator
14 | * @create: 2020-07-12 17:55
15 | **/
16 | public class Rece2 {
17 |
18 | //交换机
19 | private static final String EXCHANGE_NAME = "exchange_router";
20 | //队列
21 | public static final String QUEUE_NAME = "router_queue_del";
22 |
23 | public static void main(String[] args) throws IOException, TimeoutException {
24 |
25 | ConnectionFactory factory = new ConnectionFactory();
26 |
27 | factory.setHost("localhost");
28 | factory.setVirtualHost("/test");
29 | factory.setUsername("test");
30 | factory.setPassword("test");
31 |
32 | Connection connection = factory.newConnection();
33 | Channel channel = connection.createChannel();
34 | //声明队列
35 | channel.queueDeclare(QUEUE_NAME,false,false,false,null);
36 |
37 | //绑定交换机
38 | channel.exchangeDeclare(EXCHANGE_NAME, "direct");
39 |
40 |
41 | //绑定队列到交换机 绑定del路由 key
42 | channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "del");
43 |
44 |
45 | //同一时刻只接受一条消息
46 | channel.basicQos(1);
47 |
48 |
49 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
50 |
51 | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
52 | String message = new String(delivery.getBody(), "UTF-8");
53 | System.out.println(" [x] 删除redis缓存 '" + message + "'");
54 | };
55 |
56 | //自动确认消息
57 | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
58 | });
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/route/Send.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.route;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 |
7 |
8 | /**
9 | * @description:
10 | * @author: Administrator
11 | * @create: 2020-07-12 17:53
12 | **/
13 | public class Send {
14 | //交换机
15 | private static final String EXCHANGE_NAME = "exchange_router";
16 |
17 |
18 | public static void main(String[] argv) throws Exception {
19 | ConnectionFactory factory = new ConnectionFactory();
20 | factory.setHost("localhost");
21 | factory.setVirtualHost("/test");
22 | factory.setUsername("test");
23 | factory.setPassword("test");
24 |
25 |
26 | Connection connection = factory.newConnection();
27 | Channel channel = connection.createChannel();
28 |
29 | //绑定交换机
30 | channel.exchangeDeclare(EXCHANGE_NAME, "direct");
31 |
32 | //消息
33 | /* String msg = "新增商品..... ";
34 | channel.basicPublish(EXCHANGE_NAME,"add",null,msg.getBytes());*/
35 | String msg = "删除商品..... ";
36 | channel.basicPublish(EXCHANGE_NAME,"del",null,msg.getBytes());
37 | System.out.println("发送的消息 : " + msg);
38 |
39 | channel.close();
40 | connection.close();
41 | }
42 | }
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/topic/Rece1.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.topic;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 | import com.rabbitmq.client.DeliverCallback;
7 |
8 | import java.io.IOException;
9 | import java.util.concurrent.TimeoutException;
10 |
11 | /**
12 | * @description:
13 | * @author: Administrator
14 | * @create: 2020-07-12 17:55
15 | **/
16 | public class Rece1 {
17 |
18 | //交换机
19 | private static final String EXCHANGE_NAME = "exchange_topic";
20 | //队列
21 | public static final String QUEUE_NAME = "router_queue_topic_add";
22 |
23 | public static void main(String[] args) throws IOException, TimeoutException {
24 | ConnectionFactory factory = new ConnectionFactory();
25 | factory.setHost("localhost");
26 | factory.setVirtualHost("/test");
27 | factory.setUsername("test");
28 | factory.setPassword("test");
29 |
30 | Connection connection = factory.newConnection();
31 | Channel channel = connection.createChannel();
32 | //声明队列
33 | channel.queueDeclare(QUEUE_NAME,false,false,false,null);
34 |
35 | //绑定交换机
36 | channel.exchangeDeclare(EXCHANGE_NAME, "topic");
37 |
38 | //绑定队列到交换机
39 | channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "iteme.add");
40 |
41 | //同一时刻只接受一条消息
42 | channel.basicQos(1);
43 |
44 |
45 |
46 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
47 |
48 | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
49 | String message = new String(delivery.getBody(), "UTF-8");
50 | System.out.println(" [x] 新增商品 '" + message + "'");
51 | };
52 |
53 | //自动确认消息
54 | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
55 | });
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/topic/Rece2.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.topic;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 | import com.rabbitmq.client.DeliverCallback;
7 |
8 | import java.io.IOException;
9 | import java.util.concurrent.TimeoutException;
10 |
11 | /**
12 | * @description:
13 | * @author: Administrator
14 | * @create: 2020-07-12 17:55
15 | **/
16 | public class Rece2 {
17 |
18 | //交换机
19 | private static final String EXCHANGE_NAME = "exchange_topic";
20 | //队列
21 | public static final String QUEUE_NAME = "router_queue_topic_del";
22 |
23 | public static void main(String[] args) throws IOException, TimeoutException {
24 |
25 | ConnectionFactory factory = new ConnectionFactory();
26 |
27 | factory.setHost("localhost");
28 | factory.setVirtualHost("/test");
29 | factory.setUsername("test");
30 | factory.setPassword("test");
31 |
32 | Connection connection = factory.newConnection();
33 | Channel channel = connection.createChannel();
34 | //声明队列
35 | channel.queueDeclare(QUEUE_NAME,false,false,false,null);
36 |
37 | //绑定交换机
38 | channel.exchangeDeclare(EXCHANGE_NAME, "topic");
39 |
40 |
41 | //绑定队列到交换机 绑定del路由 key
42 | channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "iteme.del");
43 |
44 |
45 | //同一时刻只接受一条消息
46 | channel.basicQos(1);
47 |
48 |
49 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
50 |
51 | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
52 | String message = new String(delivery.getBody(), "UTF-8");
53 | System.out.println(" [x] 删除redis缓存 '" + message + "'");
54 | };
55 |
56 | //自动确认消息
57 | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
58 | });
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/topic/Rece3.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.topic;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 | import com.rabbitmq.client.DeliverCallback;
7 |
8 | import java.io.IOException;
9 | import java.util.concurrent.TimeoutException;
10 |
11 | /**
12 | * @description:
13 | * @author: Administrator
14 | * @create: 2020-07-12 17:55
15 | **/
16 | public class Rece3 {
17 |
18 | //交换机
19 | private static final String EXCHANGE_NAME = "exchange_topic";
20 | //队列
21 | public static final String QUEUE_NAME = "router_queue_topic_all";
22 |
23 | public static void main(String[] args) throws IOException, TimeoutException {
24 |
25 | ConnectionFactory factory = new ConnectionFactory();
26 |
27 | factory.setHost("localhost");
28 | factory.setVirtualHost("/test");
29 | factory.setUsername("test");
30 | factory.setPassword("test");
31 |
32 | Connection connection = factory.newConnection();
33 | Channel channel = connection.createChannel();
34 | //声明队列
35 | channel.queueDeclare(QUEUE_NAME,false,false,false,null);
36 |
37 | //绑定交换机
38 | channel.exchangeDeclare(EXCHANGE_NAME, "topic");
39 |
40 |
41 | //绑定队列到交换机 绑定del路由 key
42 | channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "iteme.#");
43 |
44 |
45 | //同一时刻只接受一条消息
46 | channel.basicQos(1);
47 |
48 |
49 | System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
50 |
51 | DeliverCallback deliverCallback = (consumerTag, delivery) -> {
52 | String message = new String(delivery.getBody(), "UTF-8");
53 | System.out.println(" 获取所有消息 '" + message + "'");
54 | };
55 |
56 | //自动确认消息
57 | channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
58 | });
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/topic/Send.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.topic;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 |
7 |
8 | /**
9 | * @description:
10 | * @author: Administrator
11 | * @create: 2020-07-12 17:53
12 | **/
13 | public class Send {
14 | //交换机
15 | private static final String EXCHANGE_NAME = "exchange_topic";
16 |
17 |
18 | public static void main(String[] argv) throws Exception {
19 | ConnectionFactory factory = new ConnectionFactory();
20 | factory.setHost("localhost");
21 | factory.setVirtualHost("/test");
22 | factory.setUsername("test");
23 | factory.setPassword("test");
24 |
25 |
26 | Connection connection = factory.newConnection();
27 | Channel channel = connection.createChannel();
28 |
29 | //绑定交换机
30 | channel.exchangeDeclare(EXCHANGE_NAME, "topic");
31 |
32 | //消息
33 | String msg = "新增消息接收..... ";
34 | channel.basicPublish(EXCHANGE_NAME,"iteme.add",null,msg.getBytes());
35 |
36 |
37 | //String msg = "删除消息接收..... ";
38 | //channel.basicPublish(EXCHANGE_NAME,"iteme.del",null,msg.getBytes());
39 | System.out.println("发送的消息 : " + msg);
40 |
41 | channel.close();
42 | connection.close();
43 | }
44 | }
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/work/Rece1.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.work;
2 |
3 | import com.rabbitmq.client.*;
4 |
5 | import java.io.IOException;
6 | import java.util.concurrent.TimeoutException;
7 |
8 | /**
9 | * @description:
10 | * @author: Administrator
11 | * @create: 2020-07-12 16:19
12 | **/
13 | public class Rece1 {
14 |
15 |
16 | public static String QUEUE_NAME = "test_queue";
17 |
18 | public static void main(String[] args) throws IOException, TimeoutException {
19 |
20 | ConnectionFactory factory = new ConnectionFactory();
21 | factory.setHost("localhost");
22 | factory.setVirtualHost("/test");
23 | factory.setUsername("test");
24 | factory.setPassword("test");
25 | Connection connection = factory.newConnection();
26 | Channel channel = connection.createChannel();
27 |
28 | //同一时刻只能发送一个消息给消费者 那个消费者早消费完 早可以拿消息进行消费 能者多劳
29 | channel.basicQos(1);
30 |
31 |
32 | channel.queueDeclare(
33 | QUEUE_NAME, false, false,
34 | false, null
35 | );
36 |
37 |
38 | DefaultConsumer consumer = new DefaultConsumer(channel) {
39 | @Override
40 | public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
41 | String message = new String(body, "UTF-8");
42 | System.out.println(" [x] Received '" + message + "'");
43 | try {
44 | Thread.sleep(1 * 1000);
45 | } catch (InterruptedException e) {
46 | e.printStackTrace();
47 | }
48 | //反馈消息
49 | channel.basicAck(envelope.getDeliveryTag(), false);
50 | }
51 | };
52 |
53 | //false 表示不自动确认消息 需要手动反馈消息
54 | channel.basicConsume(QUEUE_NAME, false, consumer);
55 | }
56 | }
57 |
58 |
59 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/work/Rece2.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.work;
2 |
3 | import com.rabbitmq.client.*;
4 |
5 | import java.io.IOException;
6 | import java.util.concurrent.TimeoutException;
7 |
8 | /**
9 | * @description:
10 | * @author: Administrator
11 | * @create: 2020-07-12 16:19
12 | **/
13 | public class Rece2 {
14 |
15 |
16 | public static String QUEUE_NAME = "test_queue";
17 |
18 | public static void main(String[] args) throws IOException, TimeoutException {
19 |
20 | ConnectionFactory factory = new ConnectionFactory();
21 |
22 | factory.setHost("localhost");
23 | factory.setVirtualHost("/test");
24 | factory.setUsername("test");
25 | factory.setPassword("test");
26 | Connection connection = factory.newConnection();
27 | Channel channel = connection.createChannel();
28 |
29 | //同一时刻只能发送一个消息给消费者
30 | channel.basicQos(1);
31 |
32 | channel.queueDeclare(
33 | QUEUE_NAME, false, false,
34 | false, null
35 | );
36 |
37 |
38 | DefaultConsumer consumer = new DefaultConsumer(channel) {
39 | @Override
40 | public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
41 | String message = new String(body, "UTF-8");
42 | System.out.println(" [x] Received '" + message + "'");
43 | try {
44 | Thread.sleep(2 * 1000);
45 | } catch (InterruptedException e) {
46 | e.printStackTrace();
47 | }
48 | channel.basicAck(envelope.getDeliveryTag(), false);
49 | }
50 | };
51 |
52 |
53 |
54 | channel.basicConsume(QUEUE_NAME, false, consumer);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/java/com/example/demo/work/Send.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.work;
2 |
3 | import com.rabbitmq.client.Channel;
4 | import com.rabbitmq.client.Connection;
5 | import com.rabbitmq.client.ConnectionFactory;
6 |
7 | import java.io.IOException;
8 | import java.util.concurrent.TimeoutException;
9 |
10 | /**
11 | * @description:
12 | * @author: Administrator
13 | * @create: 2020-07-12 16:19
14 | **/
15 | public class Send {
16 |
17 |
18 | public static String QUEUE_NAME = "test_queue";
19 |
20 | public static void main(String[] args) throws IOException, TimeoutException {
21 | ConnectionFactory factory = new ConnectionFactory();
22 | factory.setHost("localhost");
23 | factory.setVirtualHost("/test");
24 | factory.setUsername("test");
25 | factory.setPassword("test");
26 | Connection connection = factory.newConnection();
27 | Channel channel = connection.createChannel();
28 | channel.queueDeclare(
29 | QUEUE_NAME, false, false,
30 | false, null
31 | );
32 |
33 | //发送消息
34 | for (int i = 0; i < 50; i++) {
35 | String message = "message: " + i;
36 | channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
37 | System.out.println(message);
38 | try {
39 | Thread.sleep(i * 10);
40 | } catch (InterruptedException e) {
41 | e.printStackTrace();
42 | }
43 | }
44 |
45 |
46 | channel.close();
47 | connection.close();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/SpringBoot-RabbitMq/src/test/java/com/example/demo/DemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class DemoApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.6.RELEASE
9 |
10 |
11 | com.demo
12 | redisson
13 | 0.0.1-SNAPSHOT
14 | redisson
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-data-redis
33 |
34 |
35 |
36 | org.redisson
37 | redisson-spring-boot-starter
38 | 3.10.6
39 |
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-test
44 | test
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.springframework.boot
53 | spring-boot-maven-plugin
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/src/main/java/com/demo/redisson/RedissonApplication.java:
--------------------------------------------------------------------------------
1 | package com.demo.redisson;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class RedissonApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(RedissonApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/src/main/java/com/demo/redisson/config/DistributedLocker.java:
--------------------------------------------------------------------------------
1 | package com.demo.redisson.config;
2 |
3 | /**
4 | * @description:
5 | * @author: Administrator
6 | * @create: 2020-06-30 22:30
7 | **/
8 |
9 | import org.redisson.api.RLock;
10 |
11 | import java.util.concurrent.TimeUnit;
12 |
13 | /**
14 | * @author gourd
15 | */
16 | public interface DistributedLocker {
17 |
18 | RLock lock(String lockKey);
19 |
20 | RLock lock(String lockKey, int timeout);
21 |
22 | RLock lock(String lockKey, TimeUnit unit, int timeout);
23 |
24 | boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime);
25 |
26 | void unlock(String lockKey);
27 |
28 | void unlock(RLock lock);
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/src/main/java/com/demo/redisson/config/RedisDistributedLocker.java:
--------------------------------------------------------------------------------
1 | package com.demo.redisson.config;
2 |
3 | /**
4 | * @description: 实现类
5 | * @author: Administrator
6 | * @create: 2020-06-30 22:30
7 | **/
8 |
9 | import org.redisson.api.RLock;
10 | import org.redisson.api.RedissonClient;
11 | import org.springframework.beans.factory.annotation.Autowired;
12 | import org.springframework.stereotype.Component;
13 |
14 | import java.util.concurrent.TimeUnit;
15 |
16 | /**
17 | * @author gourd
18 | */
19 | @Component
20 | public class RedisDistributedLocker implements DistributedLocker {
21 |
22 | @Autowired
23 | private RedissonClient redissonClient;
24 |
25 | @Override
26 | public RLock lock(String lockKey) {
27 | RLock lock = redissonClient.getLock(lockKey);
28 | lock.lock();
29 | return lock;
30 | }
31 |
32 | @Override
33 | public RLock lock(String lockKey, int leaseTime) {
34 | RLock lock = redissonClient.getLock(lockKey);
35 | lock.lock(leaseTime, TimeUnit.SECONDS);
36 | return lock;
37 | }
38 |
39 | @Override
40 | public RLock lock(String lockKey, TimeUnit unit ,int timeout) {
41 | RLock lock = redissonClient.getLock(lockKey);
42 | lock.lock(timeout, unit);
43 | return lock;
44 | }
45 |
46 | @Override
47 | public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
48 | RLock lock = redissonClient.getLock(lockKey);
49 | try {
50 | return lock.tryLock(waitTime, leaseTime, unit);
51 | } catch (InterruptedException e) {
52 | return false;
53 | }
54 | }
55 |
56 | @Override
57 | public void unlock(String lockKey) {
58 | RLock lock = redissonClient.getLock(lockKey);
59 | lock.unlock();
60 | }
61 |
62 | @Override
63 | public void unlock(RLock lock) {
64 | lock.unlock();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/src/main/java/com/demo/redisson/config/RedisLockUtil.java:
--------------------------------------------------------------------------------
1 | package com.demo.redisson.config;
2 |
3 | /**
4 | * @description: redis分布式锁帮助类
5 | * @author: Administrator
6 | * @create: 2020-06-30 22:29
7 | **/
8 |
9 | import org.redisson.api.RLock;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.stereotype.Component;
12 |
13 | import javax.annotation.PostConstruct;
14 | import java.util.concurrent.TimeUnit;
15 |
16 | /**
17 | * redis分布式锁帮助类
18 | * @author yangzhilong
19 | *
20 | */
21 | @Component
22 | public class RedisLockUtil {
23 |
24 |
25 | @Autowired
26 | private DistributedLocker locker;
27 |
28 | private static DistributedLocker distributedLocker;
29 |
30 |
31 | @PostConstruct
32 | private void init() {
33 | distributedLocker = locker;
34 | }
35 |
36 | /**
37 | * 加锁
38 | * @param lockKey
39 | * @return
40 | */
41 | public static RLock lock(String lockKey) {
42 | return distributedLocker.lock(lockKey);
43 | }
44 |
45 | /**
46 | * 释放锁
47 | * @param lockKey
48 | */
49 | public static void unlock(String lockKey) {
50 | distributedLocker.unlock(lockKey);
51 | }
52 |
53 | /**
54 | * 释放锁
55 | * @param lock
56 | */
57 | public static void unlock(RLock lock) {
58 | distributedLocker.unlock(lock);
59 | }
60 |
61 | /**
62 | * 带超时的锁
63 | * @param lockKey
64 | * @param timeout 超时时间 单位:秒
65 | */
66 | public static RLock lock(String lockKey, int timeout) {
67 | return distributedLocker.lock(lockKey, timeout);
68 | }
69 |
70 | /**
71 | * 带超时的锁
72 | * @param lockKey
73 | * @param unit 时间单位
74 | * @param timeout 超时时间
75 | */
76 | public static RLock lock(String lockKey, int timeout,TimeUnit unit ) {
77 | return distributedLocker.lock(lockKey, unit, timeout);
78 | }
79 |
80 | /**
81 | * 尝试获取锁
82 | * @param lockKey
83 | * @param waitTime 最多等待时间
84 | * @param leaseTime 上锁后自动释放锁时间
85 | * @return
86 | */
87 | public static boolean tryLock(String lockKey, int waitTime, int leaseTime) {
88 | return distributedLocker.tryLock(lockKey, TimeUnit.SECONDS, waitTime, leaseTime);
89 | }
90 |
91 | /**
92 | * 尝试获取锁
93 | * @param lockKey
94 | * @param unit 时间单位
95 | * @param waitTime 最多等待时间
96 | * @param leaseTime 上锁后自动释放锁时间
97 | * @return
98 | */
99 | public static boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
100 | return distributedLocker.tryLock(lockKey, unit, waitTime, leaseTime);
101 | }
102 | }
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/src/main/java/com/demo/redisson/config/RedissonConfig.java:
--------------------------------------------------------------------------------
1 | package com.demo.redisson.config;
2 |
3 | import org.redisson.Redisson;
4 | import org.redisson.api.RedissonClient;
5 | import org.redisson.config.Config;
6 | import org.springframework.beans.factory.annotation.Value;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 |
10 | /**
11 | * @description: 配置类
12 | * @author: Administrator
13 | * @create: 2020-06-30 22:41
14 | **/
15 | @Configuration
16 | public class RedissonConfig {
17 |
18 | @Value("${spring.redis.host}")
19 | private String host;
20 |
21 | @Value("${spring.redis.port}")
22 | private String port;
23 |
24 | @Bean
25 | public RedissonClient redissonClient(){
26 | Config config = new Config();
27 | //单机配置
28 | config.useSingleServer().setAddress("redis://"+host+":"+port);
29 |
30 |
31 |
32 | //集群配置
33 | //config.useClusterServers().addNodeAddress(".... 可变参数 .");
34 |
35 | /*
36 | //主从
37 | config.useMasterSlaveServers()
38 | .setMasterAddress("主节点配置")
39 | .addSlaveAddress("从节点配置 可变参数");
40 | //哨兵配置
41 | config.useSentinelServers().addSentinelAddress("哨兵配置地址 可变参数")
42 | .setMasterName("主库地址")
43 | .setTimeout(50000)
44 | .setMasterConnectionPoolSize(10)
45 | .setSlaveConnectionPoolSize(5);
46 |
47 | */
48 |
49 |
50 | RedissonClient redisson = Redisson.create(config);
51 | return redisson;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | # redis
3 | redis:
4 | host: 127.0.0.1
5 | port: 6379
6 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/SpringBoot-Redisson/src/test/java/com/demo/redisson/RedissonApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.demo.redisson;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class RedissonApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/Springboot-Redis-SETEX/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.1.6.RELEASE
9 |
10 |
11 | com.example
12 | redis-setnx
13 | 0.0.1-SNAPSHOT
14 | redis-setnx
15 | redis 分布式锁
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 |
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-data-redis
38 |
39 |
40 |
41 |
42 | redis.clients
43 | jedis
44 | 2.6.2
45 |
46 |
47 |
48 |
49 | org.projectlombok
50 | lombok
51 | 1.16.10
52 | provided
53 |
54 |
55 |
56 |
57 | org.redisson
58 | redisson
59 | 3.7.0
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 | org.springframework.boot
69 | spring-boot-maven-plugin
70 |
71 |
72 |
73 |
74 |
75 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/Springboot-Redis-SETEX/src/main/java/com/example/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class DemoApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(DemoApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SpringBoot-Redis/Springboot-Redis-SETEX/src/main/java/com/example/demo/controller/RedisRedLockContoller.java:
--------------------------------------------------------------------------------
1 | package com.example.demo.controller;
2 |
3 |
4 | import com.example.demo.redis.redlock.AquiredLockWorker;
5 | import com.example.demo.redis.redlock.RedisLocker;
6 |
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.web.bind.annotation.RequestMapping;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | import java.time.LocalDateTime;
12 | import java.util.Random;
13 | import java.util.concurrent.Executors;
14 | import java.util.concurrent.ScheduledExecutorService;
15 |
16 | /**
17 | * @description: Redlock 分布式锁
18 | * @author: Administrator
19 | * @create: 2020-05-01 22:28
20 | **/
21 | @RestController
22 | public class RedisRedLockContoller {
23 |
24 |
25 | @Autowired
26 | RedisLocker redisLocker;
27 |
28 |
29 | @RequestMapping("redLock")
30 | public String index() throws Exception {
31 |
32 | redisLocker.lock("test", new AquiredLockWorker