Skip to content

Commit

Permalink
add apollo-openapi client
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Sep 24, 2018
1 parent a7e52d3 commit 83d078c
Show file tree
Hide file tree
Showing 32 changed files with 1,444 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ItemDTO create(@PathVariable("appId") String appId,
ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
if (managedEntity != null) {
throw new BadRequestException("item already exist");
throw new BadRequestException("item already exists");
} else {
entity = itemService.save(entity);
builder.createItem(entity);
Expand Down
5 changes: 5 additions & 0 deletions apollo-mockserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>apollo-mockserver</artifactId>
<name>Apollo Mock Server</name>

<properties>
<java.version>1.7</java.version>
</properties>

<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import com.ctrip.framework.apollo.core.utils.ResourceUtils;
import com.ctrip.framework.apollo.internals.ConfigServiceLocator;
import com.ctrip.framework.apollo.spring.config.PropertySourcesProcessor;
import com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Method;
Expand All @@ -19,7 +18,6 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -110,8 +108,10 @@ private void mockConfigServiceUrl(String url) throws Exception {
private String loadConfigFor(String namespace) {
String filename = String.format("mockdata-%s.properties", namespace);
final Properties prop = ResourceUtils.readConfigFile(filename, new Properties());
Map<String, String> configurations = prop.stringPropertyNames().stream().collect(
Collectors.toMap(key -> key, prop::getProperty));
Map<String, String> configurations = Maps.newHashMap();
for (String propertyName : prop.stringPropertyNames()) {
configurations.put(propertyName, prop.getProperty(propertyName));
}
ApolloConfig apolloConfig = new ApolloConfig("someAppId", "someCluster", namespace, "someReleaseKey");

Map<String, String> mergedConfigurations = mergeOverriddenProperties(namespace, configurations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertTrue;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.google.common.util.concurrent.SettableFuture;
Expand Down Expand Up @@ -32,9 +33,14 @@ public void testUpdateProperties() throws Exception {

Config otherConfig = ConfigService.getConfig(otherNamespace);

SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();

otherConfig.addChangeListener(future::set);
otherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
future.set(changeEvent);
}
});

assertEquals("otherValue1", otherConfig.getProperty("key1", null));
assertEquals("otherValue2", otherConfig.getProperty("key2", null));
Expand Down
50 changes: 50 additions & 0 deletions apollo-openapi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>1.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>apollo-openapi</artifactId>
<name>Apollo Open Api</name>

<properties>
<java.version>1.7</java.version>
</properties>

<dependencies>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package com.ctrip.framework.apollo.openapi.client;

import com.ctrip.framework.apollo.openapi.client.constant.ApolloOpenApiConstants;
import com.ctrip.framework.apollo.openapi.client.service.AppOpenApiService;
import com.ctrip.framework.apollo.openapi.client.service.ItemOpenApiService;
import com.ctrip.framework.apollo.openapi.client.service.NamespaceOpenApiService;
import com.ctrip.framework.apollo.openapi.client.service.ReleaseOpenApiService;
import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenEnvClusterDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceLockDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenReleaseDTO;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;

/**
* This class contains collections of methods to access Apollo Open Api.
* <br />
* For more information, please refer <a href="https://github.com/ctripcorp/apollo/wiki/">Apollo Wiki</a>.
*
*/
public class ApolloOpenApiClient {
private final String portalUrl;
private final String token;
private final AppOpenApiService appService;
private final ItemOpenApiService itemService;
private final ReleaseOpenApiService releaseService;
private final NamespaceOpenApiService namespaceService;

private ApolloOpenApiClient(String portalUrl, String token, RequestConfig requestConfig) {
this.portalUrl = portalUrl;
this.token = token;
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setDefaultHeaders(Lists.newArrayList(new BasicHeader("Authorization", token))).build();
Gson gson = new GsonBuilder().setDateFormat(ApolloOpenApiConstants.JSON_DATE_FORMAT).create();

String baseUrl = this.portalUrl + ApolloOpenApiConstants.OPEN_API_V1_PREFIX;
appService = new AppOpenApiService(client, baseUrl, gson);
namespaceService = new NamespaceOpenApiService(client, baseUrl, gson);
itemService = new ItemOpenApiService(client, baseUrl, gson);
releaseService = new ReleaseOpenApiService(client, baseUrl, gson);
}

/**
* Get the environment and cluster information
*/
public List<OpenEnvClusterDTO> getEnvClusterInfo(String appId) {
return appService.getEnvClusterInfo(appId);
}

/**
* Get the namespaces
*/
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName) {
return namespaceService.getNamespaces(appId, env, clusterName);
}

/**
* Get the namespace
*/
public OpenNamespaceDTO getNamespace(String appId, String env, String clusterName, String namespaceName) {
return namespaceService.getNamespace(appId, env, clusterName, namespaceName);
}

/**
* Create the app namespace
*/
public OpenAppNamespaceDTO createAppNamespace(OpenAppNamespaceDTO appNamespaceDTO) {
return namespaceService.createAppNamespace(appNamespaceDTO);
}

/**
* Get the namespace lock
*/
public OpenNamespaceLockDTO getNamespaceLock(String appId, String env, String clusterName, String namespaceName) {
return namespaceService.getNamespaceLock(appId, env, clusterName, namespaceName);
}

/**
* Add config
* @return the created config
*/
public OpenItemDTO createItem(String appId, String env, String clusterName, String namespaceName,
OpenItemDTO itemDTO) {
return itemService.createItem(appId, env, clusterName, namespaceName, itemDTO);
}

/**
* Update config
*/
public void updateItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) {
itemService.updateItem(appId, env, clusterName, namespaceName, itemDTO);
}

/**
* Create config if not exists or update config if already exists
*/
public void createOrUpdateItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) {
itemService.createOrUpdateItem(appId, env, clusterName, namespaceName, itemDTO);
}

/**
* Remove config
*
* @param operator the user who removes the item
*/
public void removeItem(String appId, String env, String clusterName, String namespaceName, String key,
String operator) {
itemService.removeItem(appId, env, clusterName, namespaceName, key, operator);
}

/**
* publish namespace
* @return the released configurations
*/
public OpenReleaseDTO publishNamespace(String appId, String env, String clusterName, String namespaceName,
NamespaceReleaseDTO releaseDTO) {
return releaseService.publishNamespace(appId, env, clusterName, namespaceName, releaseDTO);
}

/**
* @return the latest active release information or <code>null</code> if not found
*/
public OpenReleaseDTO getLatestActiveRelease(String appId, String env, String clusterName, String namespaceName) {
return releaseService.getLatestActiveRelease(appId, env, clusterName, namespaceName);
}


public String getPortalUrl() {
return portalUrl;
}

public String getToken() {
return token;
}

public static ApolloOpenApiClientBuilder newBuilder() {
return new ApolloOpenApiClientBuilder();
}

public static class ApolloOpenApiClientBuilder {

private String portalUrl;
private String token;
private int connectTimeout = -1;
private int readTimeout = -1;

/**
* @param portalUrl The apollo portal url, e.g http://localhost:8070
*/
public ApolloOpenApiClientBuilder withPortalUrl(String portalUrl) {
this.portalUrl = portalUrl;
return this;
}

/**
* @param token The authorization token, e.g. e16e5cd903fd0c97a116c873b448544b9d086de8
*/
public ApolloOpenApiClientBuilder withToken(String token) {
this.token = token;
return this;
}

/**
* @param connectTimeout an int that specifies the connect timeout value in milliseconds
*/
public ApolloOpenApiClientBuilder withConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}

/**
* @param readTimeout an int that specifies the timeout value to be used in milliseconds
*/
public ApolloOpenApiClientBuilder withReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}

public ApolloOpenApiClient build() {
Preconditions.checkArgument(!Strings.isNullOrEmpty(portalUrl), "Portal url should not be null or empty!");
Preconditions.checkArgument(portalUrl.startsWith("http://") || portalUrl.startsWith("https://"), "Portal url should start with http:// or https://" );
Preconditions.checkArgument(!Strings.isNullOrEmpty(token), "Token should not be null or empty!");

if (connectTimeout < 0) {
connectTimeout = ApolloOpenApiConstants.DEFAULT_CONNECT_TIMEOUT;
}

if (readTimeout < 0) {
readTimeout = ApolloOpenApiConstants.DEFAULT_READ_TIMEOUT;
}

RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout)
.setSocketTimeout(readTimeout).build();

return new ApolloOpenApiClient(portalUrl, token, requestConfig);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ctrip.framework.apollo.openapi.client.constant;

public interface ApolloOpenApiConstants {
int DEFAULT_CONNECT_TIMEOUT = 1000; //1 second
int DEFAULT_READ_TIMEOUT = 5000; //5 seconds
String OPEN_API_V1_PREFIX = "/openapi/v1";
String JSON_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ctrip.framework.apollo.openapi.client.exception;

public class ApolloOpenApiException extends RuntimeException {

public ApolloOpenApiException(int status, String reason, String message) {
super(String.format("Request to apollo open api failed, status code: %d, reason: %s, message: %s", status, reason,
message));
}
}
Loading

0 comments on commit 83d078c

Please sign in to comment.