First, the way to expose the port

1. Introduction

Based on the following points, this paper is written:

  1. Apollo official website docker deployment introduction is vague
  2. Deployment of others on the net is mostly plagiarism, not actuarial
  3. I have been working on this for two weeks now, although ureaka still has some problems, but there is no problem with multi-environment use.

2. Deployment description

  1. Version: Apollo – 1.2.0

  2. Docker-compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose compose

  3. Environment introduction:

Physical server centos7 the IP address of server centos7 is 192.168.64.201

Four environments dev,fat,uat, and pro are deployed on centos7 physical servers

3. Deployment procedure

3.1 Download the source code and create a database

  1. Source code address:
https://github.com/ctripcorp/apollo/tree/v1.2.0
Copy the code
  1. Creating a database

Apollo deployedthree modules: Apollo-configService, Apollo-AdminService, and Apollo-portal

Apollo-configservice and Apollo-AdminService are deployed in different environments. In each environment, apollo-configService and Apollo-AdminService share the same database. Apollo-portal uses a single database

Apollo-configservice, Apollo-AdminService, dev, FAT, UAT,pro, apollo-portal, as shown in the figure below:

Database modifies records in the ServerConfig table:

huayun_ApolloConfigDB_DEV

http://192.168.64.201:8080/eureka/
Copy the code

huayun_ApolloConfigDB_FAT

http://192.168.64.201:8081/eureka/
Copy the code

huayun_ApolloConfigDB_UAT

http://192.168.64.201:8082/eureka/
Copy the code

huayun_ApolloConfigDB_PRO

http://192.168.64.201:8083/eureka/
Copy the code

huayun_ApolloPortalDB

The apollo-Portal modification is different, again the ServerConfig table, but instead of configuring the URL, configure the enabled environment, as follows

dev,fat,uat,pro
Copy the code

3.2 Compiling Source Code

Download the source code of the corresponding version from the official website and import IDEA, as shown below. I have marked several important modules and directories here

Zip package for config, admin, portal, and Dockerfile. Env, docker-comemess. yml, Apollo-env.properties, etc.

3.3 write a docker – compose. Yml

The env file

This file is used to define the variables used in the docker-comemage. yml file, mainly exposed port definitions, database connection string information

VERSION=1.2.0 LOG_BASE_DIR=/data/ Apollo BUILD_BASE_DIR=/data/docker-image IP_ADDRESS=192.168.64.201 CONFIG_PORT_DEV=8080  CONFIG_PORT_FAT=8081 CONFIG_PORT_UAT=8082 CONFIG_PORT_PRO=8083 ADMIN_PORT_DEV=8090 ADMIN_PORT_FAT=8091 ADMIN_PORT_UAT = 8092 8093 DATASOURCE_URL_DEV ADMIN_PORT_PRO = = JDBC: mysql: / / 192.168.64.201:13306 / huayun_ApolloConfigDB_DEV? CharacterEncoding = utf8 DATASOURCE_URL_FAT = JDBC: mysql: / / 192.168.64.201:13306 / huayun_ApolloConfigDB_FAT? CharacterEncoding = utf8 DATASOURCE_URL_UAT = JDBC: mysql: / / 192.168.64.201:13306 / huayun_ApolloConfigDB_UAT? CharacterEncoding = utf8 DATASOURCE_URL_PRO = JDBC: mysql: / / 192.168.64.201:13306 / huayun_ApolloConfigDB_PRO? CharacterEncoding = utf8 PORTAL_DATASOURCE_URL = JDBC: mysql: / / 192.168.64.201:13306 / huayun_ApolloPortalDB? characterEncoding=utf8 DATASOURCE_USERNAME=root DATASOURCE_PASSWORD=rootCopy the code

apollo-env.properties

Dev. Meta = http://192.168.64.201:8080 fat. Meta = http://192.168.64.201:8081 uat. Meta = http://192.168.64.201:8082 Pro. Meta = http://192.168.64.201:8083Copy the code

docker-compose.yml

version: '3'
services:
  # 开发环境  configservice
  apollo-configservice-dev:
    container_name: apollo-configservice-dev
    build: apollo-configservice/
    image: apollo-configservice
    restart: always
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_DEV}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${CONFIG_PORT_DEV}
    volumes:
      - ${LOG_BASE_DIR}/apollo-configservice-dev/logs:/opt/logs
    ports:
      - "${CONFIG_PORT_DEV}:8080"

  # 开发环境  adminservice
  apollo-adminservice-dev:
    container_name: apollo-adminservice-dev
    build: apollo-adminservice/
    image: apollo-adminservice
    restart: always
    depends_on:
      - apollo-configservice-dev
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_DEV}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${ADMIN_PORT_DEV}
    volumes:
      - ${LOG_BASE_DIR}/apollo-adminservice-dev/logs:/opt/logs
    ports:
      - "${ADMIN_PORT_DEV}:8090"

  # fat  configservice
  apollo-configservice-fat:
    container_name: apollo-configservice-fat
    build: apollo-configservice/
    image: apollo-configservice
    restart: always
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_FAT}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${CONFIG_PORT_FAT}
    volumes:
      - ${LOG_BASE_DIR}/apollo-configservice-fat/logs:/opt/logs
    ports:
      - "${CONFIG_PORT_FAT}:8080"

  # fat  adminservice
  apollo-adminservice-fat:
    container_name: apollo-adminservice-fat
    build: apollo-adminservice/
    image: apollo-adminservice
    restart: always
    depends_on:
      - apollo-configservice-fat
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_FAT}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${ADMIN_PORT_FAT}
    volumes:
      - ${LOG_BASE_DIR}/apollo-adminservice-fat/logs:/opt/logs
    ports:
      - "${ADMIN_PORT_FAT}:8090"

  # uat  configservice
  apollo-configservice-uat:
    container_name: apollo-configservice-uat
    build: apollo-configservice/
    image: apollo-configservice
    restart: always
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_UAT}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${CONFIG_PORT_UAT}
    volumes:
      - ${LOG_BASE_DIR}/apollo-configservice-uat/logs:/opt/logs
    ports:
      - "${CONFIG_PORT_UAT}:8080"

  # uat  adminservice
  apollo-adminservice-uat:
    container_name: apollo-adminservice-uat
    build: apollo-adminservice/
    image: apollo-adminservice
    restart: always
    depends_on:
      - apollo-configservice-uat
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_UAT}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${ADMIN_PORT_UAT}
    volumes:
      - ${LOG_BASE_DIR}/apollo-adminservice-uat/logs:/opt/logs
    ports:
      - "${ADMIN_PORT_UAT}:8090"


  # pro  configservice
  apollo-configservice-pro:
    container_name: apollo-configservice-pro
    build: apollo-configservice/
    image: apollo-configservice
    restart: always
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_PRO}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${CONFIG_PORT_PRO}
    volumes:
      - ${LOG_BASE_DIR}/apollo-configservice-pro/logs:/opt/logs
    ports:
      - "${CONFIG_PORT_PRO}:8080"

  # pro  adminservice
  apollo-adminservice-pro:
    container_name: apollo-adminservice-pro
    build: apollo-adminservice/
    image: apollo-adminservice
    restart: always
    depends_on:
      - apollo-configservice-pro
    environment:
      SPRING_DATASOURCE_URL: ${DATASOURCE_URL_PRO}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      EUREKA_INSTANCE_IP_ADDRESS: ${IP_ADDRESS}
      EUREKA_INSTANCE_HOME_PAGE_URL: http://${IP_ADDRESS}:${ADMIN_PORT_PRO}
    volumes:
      - ${LOG_BASE_DIR}/apollo-adminservice-pro/logs:/opt/logs
    ports:
      - "${ADMIN_PORT_PRO}:8090"

  # portal
  apollo-portal:
    container_name: apollo-portal
    build: apollo-portal/
    image: apollo-portal
    restart: always
    depends_on:
      - apollo-adminservice-dev
      - apollo-adminservice-fat
      - apollo-adminservice-uat
      - apollo-adminservice-pro
    environment:
      SPRING_DATASOURCE_URL: ${PORTAL_DATASOURCE_URL}
      SPRING_DATASOURCE_USERNAME: ${DATASOURCE_USERNAME}
      SPRING_DATASOURCE_PASSWORD: ${DATASOURCE_PASSWORD}
      APOLLO_PROFILE: github,auth
    volumes:
      - ${LOG_BASE_DIR}/apollo-portal/logs:/opt/logs
      - ${BUILD_BASE_DIR}/docker-compose-huayun/apollo-env.properties:/apollo-portal/config/apollo-env.properties
    ports:
      - "8070:8070"
Copy the code

Apollo-configservice and Apollo-adminService have two parameters in each environment.

EUREKA_INSTANCE_IP_ADDRESS Specifies the physical machine IP address that the service uses to register with eureAKA

EUREKA_INSTANCE_HOME_PAGE_URL Specifies the registered full path of the current service. If this parameter is not specified, the Java application client cannot connect to the Apollo server

3.4 Starting a Container

Edit the docker-comement-huayun folder and upload it to the /data/docker-image directory on centos.

Go to the docker-comement-huayun directory

Start the container

docker-compose up -d
Copy the code

Looking at the logs of each service startup (mine hangs in the /data/ Apollo directory of the physical machine), the service will initially report an error because the registry takes a certain amount of time to start

3.5 Apollo Background multi-environment verification and background function verification

  1. Confirm service startup

View logs. If the following logs are displayed on the Portal, services are started

Or to view the service information registered in the registry, the address is:

http://192.168.64.201:8080/
http://192.168.64.201:8081/
http://192.168.64.201:8082/
http://192.168.64.201:8083/
Copy the code

See that the service is successfully registered.

Apollo backstage visit

http://192.168.64.201:8070/
Copy the code

User name password: Apollo /admin

Log in to verify the creation of a user, to see whether an error will be reported, can create a user successfully OK.

3.6 Apollo Client (Java Application) access test

Create a new web project for springboot2 and integrate the Apollo client. The project structure is as follows:

Pom.xml, adding Apollo client dependencies

<? The 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" > < the parent > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.1.6. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.huayun</groupId> The < artifactId > huayun - Apollo - test < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < dependencies > < the dependency > <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> < version > 1.18.8 < / version > < / dependency > < the dependency > < artifactId > Apollo - client < / artifactId > < groupId > com. Ctrip. Framework. Apollo < / groupId > < version > 1.5.0 < / version > < / dependency > < / dependencies > < project >Copy the code

Start the app.java class with @enableApolloConfig to start Apollo

package com.huayun.apollo;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author amyzhang
 * @history 2020/12/20 新建
 * @since JDK1.7
 */
@EnableApolloConfig
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

Copy the code

Application. Yml defines the service port

server:
  port: 8001
Copy the code

Apollo-env.properties Defines the meta information for the Apollo service

Dev. Meta = http://192.168.64.201:8080 fat. Meta = http://192.168.64.201:8081 uat. Meta = http://192.168.64.201:8082 Pro. Meta = http://192.168.64.201:8083Copy the code

App.properties defines the AppID, which corresponds to the AppID on the Apollo server

app.id=huayun-apollo-test
Copy the code

To test controller ApolloController.java, simply fetch the value of AAA configured in Apollo and return it

package com.huayun.apollo.controller; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * @author @history 2020/12/20 new * @since JDK1.7 */ @restController Public class ApolloController {// @ApolloConfig // private Config config; // @RequestMapping(value = "/test",method = RequestMethod.GET) // public Object test(){ // return config.getIntProperty("aaa",-1); // } @RequestMapping(value = "/test",method = RequestMethod.GET) public Object test(){ Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null String someKey = "aaa"; int someDefaultValue = -1; int value = config.getIntProperty(someKey, someDefaultValue); return value; }}Copy the code

The Apollo server configures AAA with different values for each environment

Configure the App startup parameter -denv =dev for the test project

Configure different environments for env, test the obtained aaa value, aaa value is the corresponding environment value, then successful.

2. Traefik based reverse proxy (Apollo service itself does not expose any ports)