Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zk #3

Open
wants to merge 2 commits into
base: redis
Choose a base branch
from
Open

Zk #3

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions code/demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ dependencies {

compile "com.alibaba:druid:$druidVersion"

compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.12'
compile 'org.apache.curator:curator-recipes:4.0.0'

// https://mvnrepository.com/artifact/com.google.guava/guava
compile group: 'com.google.guava', name: 'guava', version: '28.2-jre'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis
Expand Down
21 changes: 21 additions & 0 deletions code/demo/src/main/java/com/example/demo/control/StockControl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.demo.control;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.slf4j.Logger;
Expand Down Expand Up @@ -103,4 +105,23 @@ public String deductStockRedisson() throws Exception {
return retVal;
}

@Autowired
private CuratorFramework curatorFramework;

@PostMapping(value = "/deduct_stock_zk")
public String deductStockZk() throws Exception {
String path = "/stock";
InterProcessMutex interProcessMutex = new InterProcessMutex(curatorFramework, path);
String retVal;
try {
interProcessMutex.acquire();
retVal = stockReduce();
} catch (Exception e) {
throw new Exception("lock error");
} finally {
interProcessMutex.release();
}
return retVal;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.demo.zk;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* @Author mubi
* @Date 2020/7/23 19:28
*/
@Component
public class CuratorConfiguration {

@Bean(initMethod = "start")
public CuratorFramework curatorFramework() {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient(
"127.0.0.1:2181", retryPolicy);
return client;
}

}
2 changes: 1 addition & 1 deletion code/demo/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mybatis.mapperLocations= classpath:sqlmap/*.xml

logging.level.com.example.demo.repository=info

server.port=8089
server.port=8088

# Redis数据库索引(默认为0)
spring.redis.database=0
Expand Down
16 changes: 12 additions & 4 deletions code/demo2/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ mybatis.mapperLocations=classpath:sqlmap/*.xml
#server
server.port=8088

server.tomcat.uri-encoding=UTF-8
server.tomcat.max-threads=100
server.tomcat.max-connections=200
#logging.config=classpath:logback-spring.xml

#logging.config=classpath:logback-spring.xml
#kafka相关配置
spring.kafka.bootstrap-servers=localhost:9092
#设置一个默认组
spring.kafka.consumer.group-id=console-consumer-98672
#key-value序列化反序列化
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.batch-size=65536
spring.kafka.producer.buffer-memory=524288