Browse Source

feat Dockerfile部署脚本添加

3 weeks ago
parent
commit
7d77dc113b

+ 1 - 1
edu-travel-api/edu-travel-api-web/Dockerfile

@@ -3,4 +3,4 @@ FROM openjdk:8-jdk-slim
 WORKDIR /opt
 COPY ./target/edu-travel-api-web-1.0-SNAPSHOT.jar  edu-travel-api-web-1.0-SNAPSHOT.jar
 EXPOSE 10005
-ENTRYPOINT ["nohup","java", "-jar","-server","-Xms2048m","-Xmx2048m","edu-travel-api-web-1.0-SNAPSHOT.jar",">","edu-travel-api-web-1.0-SNAPSHOT.log","&"]
+ENTRYPOINT ["nohup","java", "-jar","-server","-Dspring.profiles.active=prod","-Xms2048m","-Xmx2048m","edu-travel-api-web-1.0-SNAPSHOT.jar",">","edu-travel-api-web-1.0-SNAPSHOT.log","&"]

+ 36 - 0
edu-travel-common/edu-travel-common-guava/pom.xml

@@ -0,0 +1,36 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>edu.travel</groupId>
+        <artifactId>edu-travel-common</artifactId>
+        <version>1.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>edu-travel-common-guava</artifactId>
+    <packaging>jar</packaging>
+
+    <name>edu-travel-common-guava</name>
+    <url>http://maven.apache.org</url>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>

+ 10 - 0
edu-travel-common/edu-travel-common-guava/src/main/java/edu/travel/guava/annotation/RateLimitAble.java

@@ -0,0 +1,10 @@
+package edu.travel.guava.annotation;
+
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+@Documented
+@Inherited
+public @interface RateLimitAble {
+}

+ 38 - 0
edu-travel-common/edu-travel-common-guava/src/main/java/edu/travel/guava/aop/RateLimitAspect.java

@@ -0,0 +1,38 @@
+package edu.travel.guava.aop;
+
+import com.google.common.util.concurrent.RateLimiter;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletResponse;
+
+@Aspect
+@Component
+public class RateLimitAspect {
+    @Autowired
+    private RateLimiter rateLimiter;
+    @Pointcut("@annotation(edu.travel.guava.annotation.RateLimitAble)")
+    public void pointCut() {}
+
+    @Around("pointCut()")
+    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
+        if (rateLimiter.tryAcquire()) {
+            return joinPoint.proceed(joinPoint.getArgs());
+        }else {
+            RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
+            HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
+            response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
+            response.setContentType("application/json");
+            response.setCharacterEncoding("UTF-8");
+            response.getWriter().write("每秒请求数量达到最高,目前是熔断降级");
+            return null;
+        }
+    }
+}

+ 35 - 0
edu-travel-common/edu-travel-common-guava/src/main/java/edu/travel/guava/cache/GuavaCacheConfig.java

@@ -0,0 +1,35 @@
+package edu.travel.guava.cache;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cache.CacheManager;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.concurrent.TimeUnit;
+
+@Configuration
+public class GuavaCacheConfig {
+    Logger logger = LoggerFactory.getLogger(GuavaCacheConfig.class);
+    @Bean
+    public Cache<String,Object> cache()    {
+        return CacheBuilder.newBuilder()
+                .initialCapacity(1000)
+                .maximumSize(1000)
+                .expireAfterWrite(30,TimeUnit.DAYS)
+                .expireAfterAccess(30, TimeUnit.DAYS)
+                .concurrencyLevel(8)
+                .removalListener(notification -> {
+                    logger.debug("Key " + notification.getKey() + " 被移除,原因: " + notification.getCause());
+                })
+                .weakKeys()
+
+                .weakValues()
+                .recordStats().build();
+    }
+
+
+
+}

+ 37 - 0
edu-travel-common/edu-travel-common-guava/src/main/java/edu/travel/guava/cache/GuavaCacheUtils.java

@@ -0,0 +1,37 @@
+package edu.travel.guava.cache;
+
+import com.google.common.cache.Cache;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+
+@Component
+public class GuavaCacheUtils {
+    @Autowired
+    private Cache cache;
+
+    public boolean put(String key, Object value) {
+        try {
+            cache.put(key, value);
+            return true;
+        }catch (Exception e) {
+            return false;
+        }
+    }
+    public Object get(String key, Callable<Object> callable) throws ExecutionException {
+        Object object = cache.get(key, callable);
+        return object;
+    }
+    public void invalidateAll(){
+        cache.invalidateAll();
+    }
+    public void invalidate(String key){
+        cache.invalidate(key);
+    }
+    public void invalidateList(List<String> keys){
+        cache.invalidateAll(keys);
+    }
+}

+ 15 - 0
edu-travel-common/edu-travel-common-guava/src/main/java/edu/travel/guava/limiter/RateLimiterConfig.java

@@ -0,0 +1,15 @@
+package edu.travel.guava.limiter;
+
+import com.google.common.util.concurrent.RateLimiter;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.concurrent.TimeUnit;
+
+@Configuration
+public class RateLimiterConfig {
+    @Bean
+    public RateLimiter rateLimiter() {
+        return RateLimiter.create(1000, 1000, TimeUnit.MILLISECONDS);
+    }
+}

+ 21 - 0
edu-travel-common/edu-travel-common-guava/src/main/java/edu/travel/guava/limiter/RateLimiterUtils.java

@@ -0,0 +1,21 @@
+package edu.travel.guava.limiter;
+
+import com.google.common.util.concurrent.RateLimiter;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.Callable;
+
+@Component
+public class RateLimiterUtils {
+    @Autowired
+    private RateLimiter rateLimiter;
+
+    public void tryAcquire(Callable<Boolean> callable) {
+        if (rateLimiter.tryAcquire()) {
+
+        }else {
+
+        }
+    }
+}

+ 1 - 0
edu-travel-common/pom.xml

@@ -27,6 +27,7 @@
         <module>edu-travel-common-debezium</module>
         <module>edu-travel-common-adapter</module>
         <module>edu-travel-kafka</module>
+        <module>edu-travel-common-guava</module>
     </modules>
 
     <properties>