[root@startsky ~]# git clone https://github.com/ctripcorp/apollo.git
Cloning into 'apollo'...
error: RPC failed; result=35, HTTP code = 0
fatal: The remote end hung up unexpectedly
[root@startsky ~]# git clone https://github.com/ctripcorp/apollo.git
Cloning into 'apollo'...
remote: Enumerating objects: 98806, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 98806 (delta 5), reused 7 (delta 1), pack-reused 98784
Receiving objects: 100% (98806/98806), 45.44 MiB | 2.88 MiB/s, done.
Resolving deltas: 100% (73395/73395), done.
Checking out files: 100% (1406/1406), done.
# 导入
[root@startsky ~]# mysql -h 192.168.1.15 -uroot -proot < apollo/scripts/sql/apolloportaldb.sql
[root@startsky ~]# mysql -h 192.168.1.15 -uroot -proot < apollo/scripts/sql/apolloconfigdb.sql
文件目录:
[root@startsky apollo]# ls -al
total 8
drwxr-xr-x. 3 root root 56 5月 16 09:59 .
drwxr-xr-x. 18 root root 250 5月 16 09:07 ..
-rw-r--r--. 1 root root 1385 5月 16 09:52 docker-compose.yml
-rw-r--r--. 1 root root 65 5月 16 09:44 .env
docker-compose.yml
:
version: '3.7'
services:
apollo-configservice:
container_name: apollo-configservice
image: apolloconfig/apollo-configservice
volumes:
- $PWD/logs/configservice:/opt/logs
ports:
- 8080:8080
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${mysql_host}/ApolloConfigDB?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${mysql_user}
- SPRING_DATASOURCE_PASSWORD=${mysql_password}
# - EUREKA_INSTANCE_HOME_PAGE_URL=http://192.168.1.15:8080
apollo-adminservice:
depends_on:
- apollo-configservice
container_name: apollo-adminservice
image: apolloconfig/apollo-adminservice
volumes:
- $PWD/logs/adminservice:/opt/logs
ports:
- 8090:8090
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${mysql_host}/ApolloConfigDB?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${mysql_user}
- SPRING_DATASOURCE_PASSWORD=${mysql_password}
apollo-portal:
depends_on:
- apollo-adminservice
container_name: apollo-portal
image: apolloconfig/apollo-portal
volumes:
- $PWD/logs/portal:/opt/logs
ports:
- 8070:8070
environment:
- SPRING_DATASOURCE_URL=jdbc:mysql://${mysql_host}/ApolloPortalDB?characterEncoding=utf8
- SPRING_DATASOURCE_USERNAME=${mysql_user}
- SPRING_DATASOURCE_PASSWORD=${mysql_password}
- APOLLO_PORTAL_ENVS=dev # 激活环境类型
- DEV_META=http://192.168.1.15:8080 # 配置中心地址, 即 configservice 地址
.env 配置文件
, 在当前目录下创建 .env
文件:
[root@startsky apollo]# cat .env
mysql_host=192.168.1.15:3306
mysql_user=root
mysql_password=root
注册, 在 .env 文件中定义变量 , 中间用 _ , 不要用 . , 如 mysql_host
, 不要定义成 mysql.host
可以通过命令查看 yml 文件是否有效:
docker-compose config
修改 ApolloConfigDB
中的 serverconfig 表中的 eureka.serevice.url
, 改成configservice 的地址(eureka 地址和configservice 一样), 我这里是 192.168.1.15:8080
docker-compose up -d
## 启动后目录
[root@startsky apollo]# tree .
.
├── docker-compose.yml
└── logs
├── adminservice
│ └── 100003172
│ ├── apollo-adminservice.log
│ └── gc.log.0.current
├── configservice
│ └── 100003171
│ ├── apollo-configservice.log
│ └── gc.log.0.current
└── portal
└── 100003173
├── apollo-portal.log
└── gc.log.0.current
7 directories, 7 files
http://192.169.1.15:8070
用户名/密码: apollo/admin
# 启动 apollo 配置中心
apollo.bootstrap.enabled = true
server.port=7777
spring.application.name=apollo-config-client
### apollo server地址
apollo.meta=http://192.168.1.15:8080
### apollo server的应用id
app.id=apollo-config-client
# 本地缓存配置文件路径
apollo.cacheDir=./config
@Data
@Configuration
@EnableApolloConfig //启动apollo 配置
public class ApolloConfig {
@Value("${name}")
private String name;
@Value("${user.age}")
private int age;
}
// 获取配置文件
@RestController
public class ApolloConfigController {
@Autowired
private ApolloConfig config;
@GetMapping("/apollo/config")
public Mono<ApolloConfigDto> getConfig() {
ApolloConfigDto dto = new ApolloConfigDto();
BeanUtils.copyProperties(config, dto);
return Mono.just(dto);
}
}
果不其然的报错了
Long polling failed, will retry in 1 seconds. appId: apollo-config-client,
cluster: default, namespaces: application, long polling url:
http://172.22.0.2:8080/notifications/v2?cluster=default&appId=apollo-config-client&ip=192.168.1.17
¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D,
reason: Could not complete get operation [Cause: Connect timed out]
一看是 172开头的,那一定是用容器中的ip了, 查看系统信息, 是取到这里没错了
打开 docker-compose.yml 文件中的 - EUREKA_INSTANCE_HOME_PAGE_URL=http://192.168.1.15:8080
配置, 只需要在 configservice 上修改就行!!
http://localhost:7777/apollo/config
:
{
"name": "tom",
"age": 18
}
good luck !