├── springboot-seckill
├── src
│ └── main
│ │ ├── resources
│ │ └── application.yml
│ │ └── java
│ │ └── com
│ │ └── sock
│ │ └── seckillexample
│ │ ├── SeckillApplication.java
│ │ ├── config
│ │ └── RedisConfig.java
│ │ └── controller
│ │ └── SeckillController.java
└── pom.xml
├── springboot-alipay
├── src
│ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── sock
│ │ │ ├── utils
│ │ │ └── ResultCode.java
│ │ │ └── alipayexample
│ │ │ ├── service
│ │ │ ├── AliPayService.java
│ │ │ └── imp
│ │ │ │ └── AliPayServiceImp.java
│ │ │ ├── AlipayApplicationMain.java
│ │ │ ├── entity
│ │ │ └── PayParams.java
│ │ │ ├── controller
│ │ │ └── AlipayController.java
│ │ │ └── config
│ │ │ └── AliPayConfig.java
│ │ └── resources
│ │ └── application.yml
└── pom.xml
├── README.md
└── pom.xml
/springboot-seckill/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | redis:
3 | port: 6379
4 | host: 192.168.11.111
--------------------------------------------------------------------------------
/springboot-alipay/src/main/java/com/sock/utils/ResultCode.java:
--------------------------------------------------------------------------------
1 | package com.sock.utils;
2 |
3 | public interface ResultCode {
4 |
5 | public static Integer SUCCESS = 20000; //成功
6 |
7 | public static Integer ERROR = 20001; //失败
8 | }
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # springboot-example
2 |
3 | 个人对代码的一些理解以及一些常用的技术案例,常用解决方案等。
4 |
5 | 详细点击跳转[Wiki](https://github.com/zhang-hao-Github/springboot-example/wiki)
6 |
7 |
8 | ### 有问题反馈
9 | 在查阅时有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流
10 |
11 | * 邮件(17775151940@163.com)
12 | * 微信:Sock0745
13 | * Q Q:656771561
14 |
15 |
16 | 欢迎指点提出建议指出不足,共同进步!
17 |
18 | 希望你喜欢我的作品,同时也能支持一下 点个Star。
19 |
--------------------------------------------------------------------------------
/springboot-alipay/src/main/java/com/sock/alipayexample/service/AliPayService.java:
--------------------------------------------------------------------------------
1 | package com.sock.alipayexample.service;
2 |
3 | import com.sock.alipayexample.entity.PayParams;
4 |
5 | /**
6 | * @program: springboot-example
7 | * @author: @ZhangHao
8 | * @create: 2020-10-09 23:02
9 | **/
10 | public interface AliPayService {
11 |
12 |
13 | String tradePrecreate(PayParams payParams);
14 |
15 | String tradeQuery(String out_trade_no);
16 | }
17 |
--------------------------------------------------------------------------------
/springboot-seckill/src/main/java/com/sock/seckillexample/SeckillApplication.java:
--------------------------------------------------------------------------------
1 | package com.sock.seckillexample;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @program: springboot-example
8 | * @author: @ZhangHao
9 | * @create: 2020-10-11 20:16
10 | **/
11 | @SpringBootApplication
12 | public class SeckillApplication {
13 | public static void main(String[] args) {
14 | SpringApplication.run(SeckillApplication.class, args);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/springboot-alipay/src/main/java/com/sock/alipayexample/AlipayApplicationMain.java:
--------------------------------------------------------------------------------
1 | package com.sock.alipayexample;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @program: springboot-example
8 | * @author: @ZhangHao
9 | * @create: 2020-10-09 22:06
10 | **/
11 | @SpringBootApplication
12 | public class AlipayApplicationMain {
13 |
14 | public static void main(String[] args) {
15 | SpringApplication.run(AlipayApplicationMain.class, args);
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/springboot-alipay/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | alipay:
2 | APPID:
3 | ALIPAY_PUBLIC_KEY: TG1tiHq7t57TSTazjt6Cuqa2huam4D9d4Vm8BHBVwblGe4j3FvUcDd/5i16gISnOiBtTG1tiHq7t57TSTazjt6Cuqa2huam4D9d4Vm8BHBVwblGe4j3FvUcDdtTG1tiHq7t57TSTazjt6Cuqa2huam4D9d4Vm8BHBVwblGe4j3FvUcDdtTG1tiHq7t57TSTazjt6Cuqa2huam4D9d4Vm8BHBVwblGe4j3FvUcDd
4 | APP_PRIVATE_KEY: tTG1tiHq7t57TSTazjt6Cuqa2huam4D9d4Vm8BHBVwblGe4j3FvUcDd/5i16gISnOiB1LbLrpAQ8QF7vZ9hHf4bvxje27aDHDjSedkxvUzR3G9Syywo1oo2C0AZKGVtQchZQK28E+fx/MsVQhjeN1dYC5FRCPAoGAN+SL8mwzHcVEsnNeIL5+03Zjqe3xkOwwXo6yVlRP4sU89USSHcVvd2akkm6eDngRLnfqcdZGwOEL/ITuXlGjg5TxjvEUf2pfa4Bk+nGHYNncWkzSFApvv0Zf+vGm84fTHtWXYjqV1qE4RgCjAKS9dUtVvoSF/Pa/tVK0ARizJgQ=
5 |
--------------------------------------------------------------------------------
/springboot-alipay/src/main/java/com/sock/alipayexample/entity/PayParams.java:
--------------------------------------------------------------------------------
1 | package com.sock.alipayexample.entity;
2 |
3 | import lombok.*;
4 | import lombok.extern.slf4j.Slf4j;
5 |
6 | /**
7 | * @program: yfaka-cloud
8 | * @author: @ZhangHao
9 | * @create: 2020-09-30 22:32
10 | **/
11 | public class PayParams {
12 |
13 |
14 | private String out_trade_no; //订单号
15 |
16 | private String subject; //标题
17 |
18 | private String total_amount;//订单金额
19 |
20 | public String getOut_trade_no() {
21 | return out_trade_no;
22 | }
23 |
24 | public void setOut_trade_no(String out_trade_no) {
25 | this.out_trade_no = out_trade_no;
26 | }
27 |
28 | public String getSubject() {
29 | return subject;
30 | }
31 |
32 | public void setSubject(String subject) {
33 | this.subject = subject;
34 | }
35 |
36 | public String getTotal_amount() {
37 | return total_amount;
38 | }
39 |
40 | public void setTotal_amount(String total_amount) {
41 | this.total_amount = total_amount;
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/springboot-seckill/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dependency-parent
7 | org.example
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | springboot-seckill
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-data-redis
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 | org.redisson
28 | redisson
29 | 3.11.1
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/springboot-alipay/src/main/java/com/sock/alipayexample/controller/AlipayController.java:
--------------------------------------------------------------------------------
1 | package com.sock.alipayexample.controller;
2 |
3 | import com.sock.alipayexample.config.AliPayConfig;
4 | import com.sock.alipayexample.entity.PayParams;
5 | import com.sock.alipayexample.service.AliPayService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.web.bind.annotation.PostMapping;
8 | import org.springframework.web.bind.annotation.RequestBody;
9 | import org.springframework.web.bind.annotation.RequestParam;
10 | import org.springframework.web.bind.annotation.RestController;
11 |
12 | /**
13 | * @program: springboot-example
14 | * @author: @ZhangHao
15 | * @create: 2020-10-09 22:18
16 | **/
17 | @RestController
18 | public class AlipayController {
19 |
20 | @Autowired
21 | private AliPayService aliPayService;
22 |
23 | /**
24 | * @description:
25 | * @author: zhanghao
26 | * @date: 2020/10/9 22:29
27 | * @param: PayParams 生成交易二维码的参数
28 | */
29 | @PostMapping("/tradePay")
30 | public String tradePay(@RequestBody PayParams payParams) {
31 |
32 | String qrCode = aliPayService.tradePrecreate(payParams);
33 | return qrCode;
34 | }
35 | /**
36 | * @description:
37 | * @author: zhanghao
38 | * @date: 2020/10/9 22:29
39 | * @param: PayParams 获取交易信息 轮询方式 (建议使用异步方式)
40 | */
41 | @PostMapping("/tradeQuery")
42 | public String tradeQuery(@RequestParam("tradeNo") String tradeNo) {
43 |
44 | String result = aliPayService.tradeQuery(tradeNo);
45 |
46 | return result;
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/springboot-alipay/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | dependency-parent
7 | org.example
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | springboot-alipay
13 |
14 |
15 | com.alipay.sdk
16 | alipay-sdk-java
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 | org.projectlombok
28 | lombok
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-configuration-processor
33 | true
34 |
35 |
36 | cn.hutool
37 | hutool-all
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/springboot-alipay/src/main/java/com/sock/alipayexample/config/AliPayConfig.java:
--------------------------------------------------------------------------------
1 | package com.sock.alipayexample.config;
2 |
3 | import com.alipay.api.AlipayClient;
4 | import com.alipay.api.DefaultAlipayClient;
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.boot.context.properties.ConfigurationProperties;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 |
10 | /**
11 | * @program: springboot-example
12 | * @author: @ZhangHao
13 | * @create: 2020-10-09 22:32
14 | **/
15 | @Configuration
16 | @ConfigurationProperties(prefix = "alipay")
17 | public class AliPayConfig {
18 | //支付宝网关
19 | private static final String ALIPAYGATEWAY = "https://openapi.alipay.com/gateway.do";
20 | //签名算法
21 | private static final String SIGN_TYPE = "RSA2";
22 | private static final String CHARSET = "UTF-8";
23 | public static final String TIMEOUT_EXPRESS = "60m";
24 | //公钥 对应接口加签中的支付宝公钥(非我们填入的)
25 | public static String ALIPAY_PUBLIC_KEY;
26 | //appd
27 | public static String APPID;
28 | //私钥(自己生成的那个)
29 | public static String APP_PRIVATE_KEY;
30 |
31 | public void setALIPAY_PUBLIC_KEY(String alipay_public_key) {
32 | this.ALIPAY_PUBLIC_KEY = alipay_public_key;
33 | }
34 |
35 | public void setAPPID(String APPID) {
36 | AliPayConfig.APPID = APPID;
37 | }
38 |
39 | public void setAppPrivateKey(String appPrivateKey) {
40 | APP_PRIVATE_KEY = appPrivateKey;
41 | }
42 |
43 | @Bean
44 | public AlipayClient alipayClient() {
45 |
46 | AlipayClient alipayClient = new DefaultAlipayClient(ALIPAYGATEWAY, AliPayConfig.APPID,
47 | AliPayConfig.APP_PRIVATE_KEY, "json", CHARSET, AliPayConfig.ALIPAY_PUBLIC_KEY, SIGN_TYPE);
48 | return alipayClient;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/springboot-seckill/src/main/java/com/sock/seckillexample/config/RedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.sock.seckillexample.config;
2 |
3 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 | import com.fasterxml.jackson.annotation.PropertyAccessor;
5 | import com.fasterxml.jackson.databind.ObjectMapper;
6 | import org.redisson.Redisson;
7 | import org.redisson.api.RedissonClient;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.data.redis.connection.RedisConnectionFactory;
11 | import org.springframework.data.redis.core.RedisTemplate;
12 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
13 | import org.springframework.data.redis.serializer.RedisSerializer;
14 | import org.springframework.data.redis.serializer.StringRedisSerializer;
15 |
16 | /**
17 | * @program: springboot-example
18 | * @author: @ZhangHao
19 | * @create: 2020-10-11 20:55
20 | **/
21 | @Configuration
22 | public class RedisConfig {
23 | @Bean
24 | public RedisTemplate redistpls(RedisConnectionFactory factory) {
25 |
26 | RedisTemplate tpls = new RedisTemplate<>();
27 |
28 | RedisSerializer redisSerializer = new StringRedisSerializer();
29 |
30 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
31 | ObjectMapper om = new ObjectMapper();
32 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
33 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
34 | jackson2JsonRedisSerializer.setObjectMapper(om);
35 |
36 | tpls.setConnectionFactory(factory);
37 | //key序列化方式
38 | tpls.setKeySerializer(redisSerializer);
39 | //value序列化
40 | tpls.setValueSerializer(jackson2JsonRedisSerializer);
41 | //value hashmap序列化
42 | tpls.setHashValueSerializer(jackson2JsonRedisSerializer);
43 | tpls.setEnableTransactionSupport(true);
44 | return tpls;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/springboot-alipay/src/main/java/com/sock/alipayexample/service/imp/AliPayServiceImp.java:
--------------------------------------------------------------------------------
1 | package com.sock.alipayexample.service.imp;
2 |
3 | import cn.hutool.json.JSONObject;
4 | import cn.hutool.json.JSONUtil;
5 | import com.alipay.api.AlipayApiException;
6 | import com.alipay.api.AlipayClient;
7 | import com.alipay.api.AlipayResponse;
8 | import com.alipay.api.DefaultAlipayClient;
9 | import com.alipay.api.request.AlipayTradePrecreateRequest;
10 | import com.alipay.api.request.AlipayTradeQueryRequest;
11 | import com.alipay.api.response.AlipayTradePrecreateResponse;
12 | import com.alipay.api.response.AlipayTradeQueryResponse;
13 | import com.sock.alipayexample.config.AliPayConfig;
14 | import com.sock.alipayexample.entity.PayParams;
15 | import com.sock.alipayexample.service.AliPayService;
16 | import org.springframework.beans.factory.annotation.Autowired;
17 | import org.springframework.stereotype.Service;
18 | import org.springframework.util.StringUtils;
19 |
20 | /**
21 | * @program: springboot-example
22 | * @author: @ZhangHao
23 | * @create: 2020-10-09 23:03
24 | **/
25 | @Service
26 | public class AliPayServiceImp implements AliPayService {
27 |
28 | @Autowired
29 | private AlipayClient alipayClient;
30 |
31 | /**
32 | * 生成交易二维码
33 | *
34 | * @param payParams
35 | * @return
36 | */
37 | @Override
38 | public String tradePrecreate(PayParams payParams) {
39 | if (StringUtils.isEmpty(payParams.getOut_trade_no()) || StringUtils.isEmpty(payParams.getSubject()) || StringUtils.isEmpty(payParams.getTotal_amount()))
40 | throw new RuntimeException("异常参数");
41 |
42 | AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
43 | JSONObject params = JSONUtil.createObj();
44 | params.set("out_trade_no", payParams.getOut_trade_no());
45 | params.set("total_amount", payParams.getTotal_amount());
46 | params.set("subject", payParams.getSubject());
47 | params.set("timeout_express", AliPayConfig.TIMEOUT_EXPRESS);
48 |
49 | request.setBizContent(params.toString());
50 | try {
51 | AlipayTradePrecreateResponse response = alipayClient.execute(request);
52 | // AlipayResponse alipayResponse = alipayClient.pageExecute();
53 | //获取到支付二维码
54 | String qrCode = response.getQrCode();
55 | return qrCode;
56 | } catch (AlipayApiException e) {
57 | e.printStackTrace();
58 | }
59 | return null;
60 | }
61 |
62 | //@out_trade_no 订单id
63 | public String tradeQuery(String out_trade_no) {
64 |
65 | AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
66 |
67 | JSONObject params = JSONUtil.createObj();
68 | params.set("out_trade_no", out_trade_no);
69 |
70 | request.setBizContent(params.toString());
71 |
72 | try {
73 | AlipayTradeQueryResponse response = alipayClient.execute(request);
74 | String body = response.getBody();
75 | // * 交易状态:WAIT_BUYER_PAY(交易创建,等待买家付款)、TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、TRADE_SUCCESS(交易支付成功)、TRADE_FINISHED(交易结束,不可退款)
76 | String tradeStatus = response.getTradeStatus();
77 | // 具体业务具体分析
78 | return body;
79 | } catch (AlipayApiException e) {
80 | e.printStackTrace();
81 | }
82 |
83 | return null;
84 |
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/springboot-seckill/src/main/java/com/sock/seckillexample/controller/SeckillController.java:
--------------------------------------------------------------------------------
1 | package com.sock.seckillexample.controller;
2 |
3 | import org.redisson.api.RSemaphore;
4 | import org.redisson.api.RedissonClient;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.beans.factory.annotation.Qualifier;
7 | import org.springframework.data.redis.core.RedisTemplate;
8 | import org.springframework.web.bind.annotation.GetMapping;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | import java.util.List;
12 | import java.util.UUID;
13 | import java.util.concurrent.TimeUnit;
14 |
15 | /**
16 | * @program: springboot-example
17 | * @author: @ZhangHao
18 | * @create: 2020-10-11 20:18
19 | **/
20 |
21 | @RestController
22 | public class SeckillController {
23 |
24 | @Autowired
25 | @Qualifier("redistpls")
26 | private RedisTemplate redisTemplate;
27 |
28 |
29 | /**
30 | * 秒杀随机率较高 非先到先得
31 | *
32 | * @description: watch
33 | * @author: hr
34 | * @date: 2020/10/11 23:09
35 | * @param: null
36 | * @return:
37 | */
38 | @GetMapping("seckill")
39 | public String seckill() {
40 |
41 | redisTemplate.watch("product");
42 | Integer kc = (Integer) redisTemplate.opsForValue().get("product");
43 | if (kc > 0) {
44 | redisTemplate.multi();
45 | redisTemplate.opsForValue().decrement("product", 1);
46 | List exec = redisTemplate.exec();
47 | if (null != exec && exec.size() > 0) {
48 | System.out.println("当前用户抢购到商品--" + kc + "+,已被抢购" + (100 - kc));
49 |
50 | //send 订单信息 到mq消费
51 | } else {
52 | System.out.println("当前用户抢购失败--" + kc);
53 | }
54 | return null;
55 | }
56 | return null;
57 | }
58 |
59 | /**
60 | * 利用setnx进行 秒杀
61 | * 取前100 先到先得 不断自旋
62 | *
63 | * @description:
64 | * @author: hr
65 | * @date: 2020/10/12 0:18
66 | * @param: null
67 | * @return:
68 | */
69 | boolean flag = false;
70 |
71 | @GetMapping("redisSetNx")
72 | public String redisSetNx() {
73 | if (flag = true) {
74 | return null;
75 | }
76 | String uuid = UUID.randomUUID().toString();
77 | Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("product:seckill:lock", uuid, 2, TimeUnit.SECONDS);
78 | if (aBoolean) {
79 | Integer kc = (Integer) redisTemplate.opsForValue().get("product");
80 | if (kc == 0) {
81 | if (uuid.equals(redisTemplate.opsForValue().get("product:seckill:lock"))) {
82 | redisTemplate.delete("product:seckill:lock");
83 | }
84 | flag = true;
85 | }
86 | if (kc > 0) {
87 | redisTemplate.opsForValue().decrement("product", 1);
88 | System.out.println("当前用户" + Thread.currentThread().getName() + "抢购到商品--" + kc + "已被抢购" + (100 - kc));
89 | //send 订单信息 到mq消费
90 | if (uuid.equals(redisTemplate.opsForValue().get("product:seckill:lock"))) {
91 | redisTemplate.delete("product:seckill:lock");
92 | }
93 |
94 | }
95 | } else {
96 | System.out.println("当前用户" + Thread.currentThread().getName() + "抢购失败商品");
97 | return redisSetNx();
98 | }
99 |
100 | return null;
101 |
102 | }
103 |
104 |
105 | }
106 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.example
8 | dependency-parent
9 | pom
10 | 1.0-SNAPSHOT
11 |
12 | springboot-alipay
13 | springboot-seckill
14 |
15 |
16 |
17 | 0.9.1
18 | 5.4.3
19 | 3.0
20 | 1.18.12
21 | 5.1.47
22 | 4.10.124.ALL
23 | Hoxton.SR8
24 | 2.3.3.RELEASE
25 |
26 |
27 |
28 |
29 | org.springframework.cloud
30 | spring-cloud-dependencies
31 | ${springcloud.version}
32 | pom
33 | import
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-dependencies
38 | ${springboot.version}
39 | pom
40 | import
41 |
42 |
43 | cn.hutool
44 | hutool-all
45 | ${hutool.version}
46 |
47 |
48 | com.alipay.sdk
49 | alipay-sdk-java
50 | ${alipay.version}
51 |
52 |
53 | javax.servlet
54 | servlet-api
55 | ${servletApi.version}
56 |
57 |
58 | mysql
59 | mysql-connector-java
60 | ${mysql.verion}
61 |
62 |
63 | org.projectlombok
64 | lombok
65 | ${lombok.version}
66 |
67 |
68 |
69 |
70 |
71 | org.springframework.boot
72 | spring-boot-starter-test
73 |
74 |
75 |
76 |
77 |
78 |
79 | org.apache.maven.plugins
80 | maven-compiler-plugin
81 | 3.6.0
82 |
83 | UTF-8
84 | 1.8
85 | 1.8
86 |
87 |
88 | org.mapstruct
89 | mapstruct-processor
90 | 1.1.0.Final
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------