Commit 441e20bc by 冯靖

add Docker Support

parent d010aa59
...@@ -77,6 +77,22 @@ ...@@ -77,6 +77,22 @@
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>*.zip</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<executions> <executions>
<execution> <execution>
......
# Dockerfile for apollo-adminservice
# Build with:
# docker build -t apollo-adminservice .
# Run with:
# docker run -p 8090:8090 -d --name apollo-adminservice apollo-adminservice
FROM openjdk:8-jre-alpine
MAINTAINER ameizi <sxyx2008@163.com>
ENV VERSION 0.8.0
RUN echo "http://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories \
&& echo "http://mirrors.aliyun.com/alpine/v3.6/community" >> /etc/apk/repositories \
&& apk update upgrade \
&& apk add --no-cache unzip curl bash tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
ADD apollo-adminservice-${VERSION}-github.zip /apollo-adminservice/apollo-adminservice-${VERSION}-github.zip
RUN unzip /apollo-adminservice/apollo-adminservice-${VERSION}-github.zip -d /apollo-adminservice \
&& rm -rf /apollo-adminservice/apollo-adminservice-${VERSION}-github.zip \
&& sed -i '$d' /apollo-adminservice/scripts/startup.sh \
&& echo "tail -f /dev/null" >> /apollo-adminservice/scripts/startup.sh
EXPOSE 8090
CMD ["/apollo-adminservice/scripts/startup.sh"]
\ No newline at end of file
...@@ -83,6 +83,22 @@ ...@@ -83,6 +83,22 @@
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>*.zip</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<executions> <executions>
<execution> <execution>
......
# Dockerfile for apollo-configservice
# Build with:
# docker build -t apollo-configservice .
# Run with:
# docker run -p 8080:8080 -d --name apollo-configservice apollo-configservice
FROM openjdk:8-jre-alpine
MAINTAINER ameizi <sxyx2008@163.com>
ENV VERSION 0.8.0
RUN echo "http://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories \
&& echo "http://mirrors.aliyun.com/alpine/v3.6/community" >> /etc/apk/repositories \
&& apk update upgrade \
&& apk add --no-cache unzip curl bash tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
ADD apollo-configservice-${VERSION}-github.zip /apollo-configservice/apollo-configservice-${VERSION}-github.zip
RUN unzip /apollo-configservice/apollo-configservice-${VERSION}-github.zip -d /apollo-configservice \
&& rm -rf /apollo-configservice/apollo-configservice-${VERSION}-github.zip \
&& sed -i '$d' /apollo-configservice/scripts/startup.sh \
&& echo "tail -f /dev/null" >> /apollo-configservice/scripts/startup.sh
EXPOSE 8080
CMD ["/apollo-configservice/scripts/startup.sh"]
\ No newline at end of file
...@@ -34,6 +34,22 @@ ...@@ -34,6 +34,22 @@
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>*.zip</include>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId> <artifactId>maven-assembly-plugin</artifactId>
<executions> <executions>
<execution> <execution>
......
# Dockerfile for apollo-portal
# Build with:
# docker build -t apollo-portal .
# Run with:
# docker run -p 9090:8080 -d --name apollo-portal apollo-portal
FROM openjdk:8-jre-alpine
MAINTAINER ameizi <sxyx2008@163.com>
ENV VERSION 0.8.0
RUN echo "http://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories \
&& echo "http://mirrors.aliyun.com/alpine/v3.6/community" >> /etc/apk/repositories \
&& apk update upgrade \
&& apk add --no-cache unzip curl bash tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
ADD apollo-portal-${VERSION}-github.zip /apollo-portal/apollo-portal-${VERSION}-github.zip
RUN unzip /apollo-portal/apollo-portal-${VERSION}-github.zip -d /apollo-portal \
&& rm -rf /apollo-portal/apollo-portal-${VERSION}-github.zip \
&& sed -i '$d' /apollo-portal/scripts/startup.sh \
&& echo "tail -f /dev/null" >> /apollo-portal/scripts/startup.sh
EXPOSE 8080
CMD ["/apollo-portal/scripts/startup.sh"]
\ No newline at end of file
target/
.mvn
mvnw
mvnw.cmd
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
\ No newline at end of file
# apollo-spring-boot-sample
Demo project for Spring Boot Apollo
Spring Boot整合携程Apollo配置中心
## 客户端maven配置
* apollo-client(必须)
```xml
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>0.8.0</version>
</dependency>
```
* spring-boot-starter-actuator 和 spring-cloud-context
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
```
注意:
1、`apollo-client`为必须配置
2、`spring-boot-starter-actuator``spring-cloud-context`为可选配置。仅在使用`Spring Cloud``@RefreshScope`时所需
# 参考文档
https://github.com/ctripcorp/apollo/wiki/
\ No newline at end of file
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>net.ameizi</groupId>
<artifactId>apollo-spring-boot-sample</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Apollo Spring Boot Sample</name>
<description>Demo project for Spring Boot Apollo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
FROM openjdk:8-jre-alpine
MAINTAINER ameizi <sxyx2008@163.com>
VOLUME /tmp
RUN echo "http://mirrors.aliyun.com/alpine/v3.6/main" > /etc/apk/repositories \
&& echo "http://mirrors.aliyun.com/alpine/v3.6/community" >> /etc/apk/repositories \
&& apk update upgrade \
&& apk add --no-cache tzdata \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
ADD *.jar app.jar
RUN sh -c 'touch /app.jar' \
&& mkdir -p /opt/settings \
&& echo "env=DEV" > /opt/settings/server.properties
ENV JAVA_OPTS=""
EXPOSE 9090
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
\ No newline at end of file
package net.ameizi;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @EnableApolloConfig要和@Configuration一起使用,不然不会生效。
*/
@Configuration
@EnableApolloConfig
public class ApolloConfigSample {
@Bean
public JavaConfigSample javaConfigSample(){
return new JavaConfigSample();
}
@Bean
public ConfigurationPropertiesSample configurationPropertiesSample() {
return new ConfigurationPropertiesSample();
}
}
package net.ameizi;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Application {
@Autowired
private JavaConfigSample javaConfigSample;
@Autowired
private ConfigurationPropertiesSample configurationPropertiesSample;
private Config config = ConfigService.getAppConfig();
// 监听属性值变化
{
config.addChangeListener(changeEvent -> {
System.out.println("Changes for namespace " + changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}
});
}
/**
* 使用apollo client api获取配置文件,apollo管控台修改配置后,实时更新
* @return
*/
@GetMapping("/clientapi")
public Sample apolloApiClient(){
int timeout = config.getIntProperty("sample.timeout",0);
int size = config.getIntProperty("sample.size",0);
return Sample.builder().timeout(timeout).size(size).build();
}
/**
* Java Config方式,JavaConfigSample内部使用了Config API,属性值的修改会立即生效
* @return
*/
@GetMapping("/javaconfig")
public JavaConfigSample commonProperties(){
return javaConfigSample;
}
/**
* Spring Boot ConfigurationProperties方式,
* ConfigurationPropertiesSample内部没有使用Config API,但因为使用了@RefreshScope注解,执行/refresh端点时会刷新属性的值
* @return
*/
@RefreshScope
@GetMapping("/configuration")
public ConfigurationPropertiesSample configurationPropertiesSample(){
return configurationPropertiesSample;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package net.ameizi;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 使用Spring Boot ConfigurationProperties方式
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "redis.cache")
public class ConfigurationPropertiesSample {
private int expireSeconds;
private int commandTimeout;
}
package net.ameizi;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.springframework.beans.factory.annotation.Value;
/**
* 使用Java Config方式
* 使用@ApolloConfig自动注入Config对象
* 使用@ApolloConfigChangeListener自动注入ConfigChangeListener对象
* 当监听到属性值发生变化后使用Config API修改属性值
*/
public class JavaConfigSample {
/**
* @ApolloConfig用来自动注入Config对象
*/
@ApolloConfig
private Config config;
@Value("${timeout:100}")
private int timeout;
private int batch;
@Value("${batch:200}")
public void setBatch(int batch) {
this.batch = batch;
}
/**
* @ApolloConfigChangeListener用来自动注册ConfigChangeListener
*/
@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
changeEvent.changedKeys().forEach(key ->{
ConfigChange change = changeEvent.getChange(key);
System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
});
if (changeEvent.isChanged("batch")) {
batch = config.getIntProperty("batch", 200);
}
}
public int getTimeout() {
return config.getIntProperty("timeout",100);
}
public int getBatch() {
return this.batch;
}
}
package net.ameizi;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "sample")
public class Sample {
private int timeout;
private int size;
}
\ No newline at end of file
app.id=apollo-spring-boot-sample
\ No newline at end of file
server.port=9090
management.security.enabled=false
\ No newline at end of file
version: '2'
services:
apollo-configservice:
image: apollo-configservice
container_name: apollo-configservice
restart: always
depends_on:
- db
ports:
- "8080:8080"
volumes:
- /tmp/logs:/opt/logs
links:
- db
apollo-adminservice:
image: apollo-adminservice
container_name: apollo-adminservice
restart: always
depends_on:
- db
ports:
- "8090:8090"
volumes:
- /tmp/logs:/opt/logs
links:
- db
apollo-portal:
image: apollo-portal
container_name: apollo-portal
restart: always
depends_on:
- db
ports:
- "9090:8080"
volumes:
- /tmp/logs:/opt/logs
links:
- db
apollo-spring-boot-sample:
image: apollo-spring-boot-sample
container_name: apollo-spring-boot-sample
restart: always
depends_on:
- apollo-configservice
- apollo-adminservice
- apollo-portal
ports:
- "9190:9090"
volumes:
- /tmp:/tmp
db:
image: mariadb:latest
container_name: db
environment:
MYSQL_ROOT_PASSWORD: root
ports:
- "3306:3306"
volumes:
- /var/data/mysql:/var/lib/mysql
\ No newline at end of file
# Apollo for Docker
# 修改明细
1、在apollo-configservice、apollo-adminservice、apollo-portal模块中分别添加了`src/main/docker/Dockerfile`以及在`pom.xml`中添加了`docker-maven-plugin`插件
2、docker环境上的sql脚本`scripts/sql-docker`
修改apolloconfigdb.sql数据库脚本中的`eureka.service.url``http://apollo-configservice:8080/eureka/`
3、添加了`Docker`环境示例代码`apollo-spring-boot-sample`
4、添加`docker-compose.yml`文件
5、添加`run.sh`脚本
# 如何使用
```bash
./run.sh
```
待数据库启动完毕,连接数据库,执行`scripts/sql-docker`目录下的数据库脚本。数据库已经映射到宿主机的`3306`端口,用户名密码为`root/root`
http://localhost:9090 Apollo配置中心
http://localhost:8080 Eureka注册中心
访问 apollo-spring-boot-sample 示例应用
在配置中心中创建应用`apollo-spring-boot-sample`,其应用id与`/apollo-spring-boot-sample/src/main/resources/META-INF/app.properties`中的内容一致
localhost:9190/clientapi 使用Config API方式实时同步属性值
localhost:9190/javaconfig 使用Config API方式实时同步属性值,注入@ApolloConfig和@ApolloConfigChangeListener
localhost:9190/configuration 使用Spring Boot ConfigurationProperties方式,没有使用Config API,但使用了Spring Cloud的@RefreshScope注解,执行/refresh端点时会刷新属性的值。Apollo配置中心的值修改后,需要执行如下请求,才能使值生效
```
curl -X POST http://localhost:9190/refresh
```
\ No newline at end of file
...@@ -101,6 +101,7 @@ ...@@ -101,6 +101,7 @@
<module>apollo-portal</module> <module>apollo-portal</module>
<module>apollo-assembly</module> <module>apollo-assembly</module>
<module>apollo-demo</module> <module>apollo-demo</module>
<module>apollo-spring-boot-sample</module>
</modules> </modules>
<dependencyManagement> <dependencyManagement>
......
#!/bin/sh
# apollo config db info
apollo_config_db_url=jdbc:mysql://db:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=root
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://db:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=root
# meta server url
dev_meta=http://apollo-configservice:8080
fat_meta=http://apollo-configservice:8080
uat_meta=http://apollo-configservice:8080
pro_meta=http://apollo-configservice:8080
META_SERVERS_OPTS="-Ddev_meta=$dev_meta -Dfat_meta=$fat_meta -Duat_meta=$uat_meta -Dpro_meta=$pro_meta"
# =============== Please do not modify the following content =============== #
# package config-service and admin-service
echo "==== starting to build config-service and admin-service ===="
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=github -Dspring_datasource_url=$apollo_config_db_url -Dspring_datasource_username=$apollo_config_db_username -Dspring_datasource_password=$apollo_config_db_password
echo "==== building config-service and admin-service finished ===="
echo "==== starting to build portal ===="
mvn clean package -DskipTests -pl apollo-portal -am -Dapollo_profile=github -Dspring_datasource_url=$apollo_portal_db_url -Dspring_datasource_username=$apollo_portal_db_username -Dspring_datasource_password=$apollo_portal_db_password $META_SERVERS_OPTS
echo "==== building portal finished ===="
echo "==== starting to build client ===="
mvn clean install -DskipTests -pl apollo-client -am $META_SERVERS_OPTS
echo "==== building client finished ===="
echo "==== starting to build apollo-spring-boot-sample ===="
mvn clean package -DskipTests -pl apollo-spring-boot-sample
echo "==== building apollo-spring-boot-sample finished ===="
echo "==== starting to build docker images ===="
cd apollo-configservice
mvn docker:build
cd ../apollo-adminservice
mvn docker:build
cd ../apollo-portal
mvn docker:build
cd ../apollo-spring-boot-sample
mvn docker:build
echo "==== building docker images finished ===="
cd ..
echo "==== starting to run docker images ===="
docker-compose up
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment