Skip to content

Commit

Permalink
add support timeout op
Browse files Browse the repository at this point in the history
optimize async op
add deployment-template for k8s
optimize rest-api build
add Dockerfile for local-distribution
  • Loading branch information
Ahoo-Wang committed May 16, 2021
1 parent 0829cf7 commit 537af2c
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 58 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ process cache refresh, with unparalleled QPS performance and real-time consisten
> Kotlin DSL
``` kotlin
val governVersion = "0.9.11";
val governVersion = "0.9.12";
implementation("me.ahoo.govern:spring-cloud-starter-config:${governVersion}")
implementation("me.ahoo.govern:spring-cloud-starter-discovery:${governVersion}")
```
Expand All @@ -32,7 +32,7 @@ process cache refresh, with unparalleled QPS performance and real-time consisten
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<govern.version>0.9.11</govern.version>
<govern.version>0.9.12</govern.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -73,31 +73,31 @@ spring:
#### Option 1:Download the executable file
> Download [rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.11/rest-api-0.9.11.tar)
> Download [rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.12/rest-api-0.9.12.tar)
> tar *rest-api-0.9.11.tar*
> tar *rest-api-0.9.12.tar*
```shell
cd rest-api-0.9.11
# Working directory: rest-api-0.9.11
cd rest-api-0.9.12
# Working directory: rest-api-0.9.12
bin/rest-api --server.port=8080 --govern.redis.uri=redis://localhost:6379
```

#### Option 2:Docker run

```shell
docker pull ahoowang/govern-service:0.9.11
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.11
docker pull ahoowang/govern-service:0.9.12
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.12
```

---
> MacBook Pro (M1)
>
> Please use *ahoowang/govern-service:0.9.11-armv7*
> Please use *ahoowang/govern-service:0.9.12-armv7*
```shell
docker pull ahoowang/govern-service:0.9.11-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.11-armv7
docker pull ahoowang/govern-service:0.9.12-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.12-armv7
```

---
Expand Down
22 changes: 11 additions & 11 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Govern Service* 提供了超高TPS&QPS。*Govern Service* 结合本地进程缓
> Kotlin DSL
``` kotlin
val governVersion = "0.9.11";
val governVersion = "0.9.12";
implementation("me.ahoo.govern:spring-cloud-starter-config:${governVersion}")
implementation("me.ahoo.govern:spring-cloud-starter-discovery:${governVersion}")
```
Expand All @@ -27,7 +27,7 @@ Govern Service* 提供了超高TPS&QPS。*Govern Service* 结合本地进程缓
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<properties>
<govern.version>0.9.11</govern.version>
<govern.version>0.9.12</govern.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -72,31 +72,31 @@ logging:

#### 方式一:下载可执行文件

> 下载 [rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.11/rest-api-0.9.11.tar)
> 下载 [rest-api-server](https://github.com/Ahoo-Wang/govern-service/releases/download/0.9.12/rest-api-0.9.12.tar)
> 解压 *rest-api-0.9.11.tar*
> 解压 *rest-api-0.9.12.tar*
```shell
cd rest-api-0.9.11
# 工作目录: rest-api-0.9.11
cd rest-api-0.9.12
# 工作目录: rest-api-0.9.12
bin/rest-api --server.port=8080 --govern.redis.uri=redis://localhost:6379
```

#### 方式二:Docker run

```shell
docker pull ahoowang/govern-service:0.9.11
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.11
docker pull ahoowang/govern-service:0.9.12
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.12
```

---
> MacBook Pro (M1)
>
> 请使用 *ahoowang/govern-service:0.9.11-armv7*
> 请使用 *ahoowang/govern-service:0.9.12-armv7*
```shell
docker pull ahoowang/govern-service:0.9.11-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.11-armv7
docker pull ahoowang/govern-service:0.9.12-armv7
docker run --name govern-service -d -p 8080:8080 --link redis -e GOVERN_REDIS_URI=redis://redis:6379 ahoowang/govern-service:0.9.12-armv7
```

---
Expand Down
32 changes: 32 additions & 0 deletions core/src/main/java/me/ahoo/govern/core/util/Futures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.ahoo.govern.core.util;

import me.ahoo.govern.core.GovernException;

import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static java.lang.Thread.currentThread;

/**
* @author ahoo wang
*/
public final class Futures {
private Futures() {
}

public static <T> T getUnChecked(Future<T> future, Duration timeout) {
try {
return future.get(timeout.toNanos(), TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
currentThread().interrupt();
throw new GovernException(e);
} catch (TimeoutException e) {
throw new GovernException(e);
} catch (ExecutionException e) {
throw new GovernException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.ahoo.govern.discovery.loadbalancer;

import me.ahoo.govern.core.util.Futures;
import me.ahoo.govern.discovery.ServiceInstance;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

/**
Expand All @@ -13,6 +15,10 @@ public interface LoadBalancer {

CompletableFuture<ServiceInstance> choose(String namespace, String serviceId);

default ServiceInstance choose(String namespace, String serviceId, Duration timeout) {
return Futures.getUnChecked(choose(namespace, serviceId), timeout);
}

interface Chooser {
ServiceInstance choose();
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=me.ahoo.govern
version=0.9.11
version=0.9.12

description=Govern Service On Redis
website=https://github.com/Ahoo-Wang/govern-service
Expand Down
37 changes: 37 additions & 0 deletions k8s/deployment/govern-servce-rest-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: govern-service-rest-api
spec:
replicas: 1
selector:
matchLabels:
app: govern-service-rest-api
template:
metadata:
labels:
app: govern-service-rest-api
spec:
containers:
- env:
- name: GOVERN_MODE
value: cluster
- name: GOVERN_REDIS_URI
value: redis://redis-url:6379
image: ahoowang/govern-service:0.9.11
name: govern-service
resources:
limits:
cpu: "1"
memory: 640Mi
requests:
cpu: 250m
memory: 512Mi
volumeMounts:
- mountPath: /etc/localtime
name: volume-localtime
volumes:
- hostPath:
path: /etc/localtime
type: ""
name: volume-localtime
34 changes: 34 additions & 0 deletions k8s/docker/rest-api-local/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# docker buildx build --push --platform linux/arm/v7 --build-arg GOVERN_VERSION=0.9.12 --build-arg JDK_VERSION=armv7l-centos-jdk-11.0.11_9-slim -t ahoowang/govern-service:0.9.12-armv7 .
# docker buildx build --push --platform linux/amd64,linux/arm64 --build-arg GOVERN_VERSION=0.9.12 --build-arg JDK_VERSION=jdk11u-centos-nightly-slim -t ahoowang/govern-service:0.9.12 .

ARG JDK_VERSION=jdk11u-centos-nightly
ARG GOVERN_VERSION=0.9.12
ARG GOVERN_SERVICE_HOME=/govern-service
FROM adoptopenjdk/openjdk11:${JDK_VERSION} AS base

ARG GOVERN_VERSION
RUN echo "Building Govern-Service ${GOVERN_VERSION}"

FROM curlimages/curl as build
ARG GOVERN_VERSION
ARG GOVERN_SERVICE_HOME
USER root

WORKDIR ${GOVERN_SERVICE_HOME}

ENV GOVERN_SERVICE_REST_API_TAR=rest-api-${GOVERN_VERSION}.tar
COPY ${GOVERN_SERVICE_REST_API_TAR} .
RUN tar -xvf ${GOVERN_SERVICE_REST_API_TAR};\
rm ${GOVERN_SERVICE_REST_API_TAR}

FROM base as run
ARG GOVERN_VERSION
ARG GOVERN_SERVICE_HOME

LABEL maintainer="ahoowang@qq.com"
COPY --from=build ${GOVERN_SERVICE_HOME} ${GOVERN_SERVICE_HOME}

WORKDIR ${GOVERN_SERVICE_HOME}/rest-api-${GOVERN_VERSION}
EXPOSE 8080

ENTRYPOINT ["bin/rest-api"]
6 changes: 3 additions & 3 deletions docker/rest-api/Dockerfile → k8s/docker/rest-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# docker buildx build --push --platform linux/arm/v7 --build-arg GOVERN_VERSION=0.9.11 --build-arg JDK_VERSION=armv7l-centos-jdk-11.0.11_9-slim -t ahoowang/govern-service:0.9.11-armv7 .
# docker buildx build --push --platform linux/amd64,linux/arm64 --build-arg GOVERN_VERSION=0.9.11 --build-arg JDK_VERSION=jdk11u-centos-nightly-slim -t ahoowang/govern-service:0.9.11 .
# docker buildx build --push --platform linux/arm/v7 --build-arg GOVERN_VERSION=0.9.12 --build-arg JDK_VERSION=armv7l-centos-jdk-11.0.11_9-slim -t ahoowang/govern-service:0.9.12-armv7 .
# docker buildx build --push --platform linux/amd64,linux/arm64 --build-arg GOVERN_VERSION=0.9.12 --build-arg JDK_VERSION=jdk11u-centos-nightly-slim -t ahoowang/govern-service:0.9.12 .

ARG JDK_VERSION=jdk11u-centos-nightly
ARG GOVERN_VERSION=0.9.11
ARG GOVERN_VERSION=0.9.12
ARG GOVERN_SERVICE_HOME=/govern-service
FROM adoptopenjdk/openjdk11:${JDK_VERSION} AS base

Expand Down
19 changes: 13 additions & 6 deletions rest-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ java {
}
}

tasks.jar.configure {
exclude("application.yml", "bootstrap.yml")
}

distributions {
main {
contents {
val dashboardDistPath = "${rootDir.absolutePath}/dashboard/dist";
from(dashboardDistPath).include("**")
}
}
}

application {
mainClass.set("me.ahoo.govern.rest.RestApiServer")

Expand All @@ -20,12 +33,6 @@ application {
"-Dspring.cloud.bootstrap.enabled=true",
"-Dspring.cloud.bootstrap.location=config/bootstrap.yml"
)

val dashboardDistPath = "${rootDir.absolutePath}/dashboard/dist";

applicationDistribution.from(dashboardDistPath) {
include("**")
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public StatController(NamespaceService namespaceService, ServiceDiscovery servic
@GetMapping
public CompletableFuture<GetStatResponse> getStat(@PathVariable String namespace) {
var statResponse = new GetStatResponse();

var setNamespacesFuture = namespaceService.getNamespaces().thenAccept(namespaces -> statResponse.setNamespaces(namespaces.size()));
var setServicesFuture = serviceDiscovery.getServices(namespace).thenAccept(services -> statResponse.setServices(services.size()));
var setInstancesFuture = serviceStatistic.getInstanceCount(namespace).thenAccept(count -> statResponse.setInstances(count.intValue()));
Expand Down
31 changes: 19 additions & 12 deletions rest-api/src/main/java/me/ahoo/govern/rest/job/StatServiceJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
* @author ahoo wang
*/
Expand All @@ -28,17 +30,22 @@ public void doStatService() {
log.info("doStatService - start.");
}
var currentNamespace = NamespacedContext.GLOBAL.getNamespace();
var namespaces = namespaceService.getNamespaces().join();
if (!namespaces.contains(currentNamespace)) {
namespaceService.setNamespace(currentNamespace).join();
}

if (!namespaces.isEmpty()) {
namespaces.forEach(serviceStatistic::statService);
}

if (log.isInfoEnabled()) {
log.info("doStatService - end.");
}
namespaceService.getNamespaces().thenCompose(namespaces -> {
if (!namespaces.contains(currentNamespace)) {
CompletableFuture future = namespaceService.setNamespace(currentNamespace);
return future;
}
if (!namespaces.isEmpty()) {
var futures = namespaces.stream()
.map(serviceStatistic::statService)
.toArray(CompletableFuture[]::new);
return CompletableFuture.allOf(futures);
}
return CompletableFuture.completedFuture(null);
}).thenAccept((nil) -> {
if (log.isInfoEnabled()) {
log.info("doStatService - end.");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import me.ahoo.govern.spring.cloud.GovernProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;

import static me.ahoo.govern.config.spring.cloud.GovernConfigProperties.PREFIX;

/**
Expand All @@ -17,6 +19,7 @@ public class GovernConfigProperties {
private String configId;

private String fileExtension = "yml";
private Duration timeout = Duration.ofSeconds(2);

public boolean isEnabled() {
return enabled;
Expand All @@ -41,4 +44,12 @@ public String getFileExtension() {
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}

public Duration getTimeout() {
return timeout;
}

public void setTimeout(Duration timeout) {
this.timeout = timeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.ahoo.govern.config.ConfigService;
import me.ahoo.govern.core.Consts;
import me.ahoo.govern.core.NamespacedContext;
import me.ahoo.govern.core.util.Futures;
import org.apache.logging.log4j.util.Strings;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.boot.env.PropertySourceLoader;
Expand Down Expand Up @@ -54,7 +55,7 @@ public PropertySource<?> locate(Environment environment) {

log.info("locate - configId:[{}] @ namespace:[{}]", configId, namespace);

var config = configService.getConfig(configId).join();
var config = Futures.getUnChecked(configService.getConfig(configId), configProperties.getTimeout());

if (Objects.isNull(config)) {
log.warn("locate - can not find configId:[{}] @ namespace:[{}]", configId, namespace);
Expand Down
Loading

0 comments on commit 537af2c

Please sign in to comment.