JavaEE Apollo安装 使用
意华 人气:1说明:
Apollo是配置管理中心,和SpringCloud-Config实现的功能有点相似。
一、安装MySQL 5.7.37(主机:192.168.233.128,用户/密码:root):
1.下载mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar,并上传到centos7:
https://dev.mysql.com/downloads/mysql/
解压mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar(cd到/root目录):
[root@localhost ~]# tar -xvf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
2.安装依赖:
[root@localhost ~]# yum -y install libaio [root@localhost ~]# yum -y install perl [root@localhost ~]# yum -y install net-tools
3.安装mysql:
[root@localhost ~]# rpm -ivh mysql-community-common-5.7.37-1.el7.x86_64.rpm [root@localhost ~]# rpm -ivh mysql-community-libs-5.7.37-1.el7.x86_64.rpm [root@localhost ~]# rpm -ivh mysql-community-client-5.7.37-1.el7.x86_64.rpm [root@localhost ~]# rpm -ivh mysql-community-server-5.7.37-1.el7.x86_64.rpm [root@localhost ~]# rpm -ivh mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm
4.启动:
[root@localhost ~]# service mysqld start
5.查看临时密码:
[root@localhost ~]# grep password /var/log/mysqld.log
6.登录(在Enter password输入上方查看的密码):
[root@localhost ~]# mysql -u root -p Enter password:
7.修改密码(密码:root):
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY 'Root123456='; mysql> SET GLOBAL validate_password_length=2; mysql> SET GLOBAL validate_password_policy=0; mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY 'root';
8.允许远程访问:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
二、Apollo安装/启动(centos7)(主机:192.168.233.128):
1.下载apollo-quick-start-1.9.2.zip:
https://www.apolloconfig.com/#/zh/deployment/quick-start?id=%e4%ba%8c%e3%80%81%e5%ae%89%e8%a3%85%e6%ad%a5%e9%aa%a4
2.创建ApolloPortalDB、ApolloConfigDB数据库:
(1)使用Navicat连接mysql5,创建ApolloPortalDB、ApolloConfigDB数据库:
注:ApolloPortalDB在生产环境部署,而ApolloConfigDB需要每个环境部署一套。
(2)导入apolloportaldb.sql到ApolloPortalDB数据库(sql文件在apollo-quick-start-1.9.2/sql目录中):
使用Navicat右击ApolloPortalDB数据库,运行 SQL 文件,选择apolloportaldb.sql,点开始按钮。
(3)导入apolloconfigdb.sql到ApolloConfigDB数据库(sql文件在apollo-quick-start-1.9.2/sql目录中):
使用Navicat右击ApolloConfigDB数据库,运行 SQL 文件,选择apolloconfigdb.sql,点开始按钮。
3.安装Apollo:
(1)将apollo-quick-start-1.9.2.zip上传到/usr/local/apollo目录(mkdir /usr/local/apollo)。
(2)解压apollo-quick-start-1.9.2.zip(cd /usr/local/apollo/):
[root@localhost apollo]# unzip apollo-quick-start-1.9.2.zip
(3)配置ApolloPortalDB、ApolloConfigDB数据库连接信息:
[root@localhost apollo]# vi demo.sh
修改如下内容:
#ApolloConfigDB连接信息 apollo_config_db_url="jdbc:mysql://192.168.233.128:3306/ApolloConfigDB?characterEncoding=utf8&serverTimezone=Asia/Shanghai" apollo_config_db_username=root apollo_config_db_password=root #ApolloPortalDB连接信息 apollo_portal_db_url="jdbc:mysql://192.168.233.128:3306/ApolloPortalDB?characterEncoding=utf8&serverTimezone=Asia/Shanghai" apollo_portal_db_username=root apollo_portal_db_password=root ... # meta server url config_server_url=http://192.168.233.128:8080 admin_server_url=http://192.168.233.128:8090 ... portal_url=http://192.168.233.128:8070
4.启动/停止Apollo:
[root@localhost apollo]# ./demo.sh start[root@localhost apollo]# ./demo.sh stop
5.打开Apollo首页(用户apollo,密码admin):
http://192.168.233.128:8070
三、Web应用接入Apollo(SpringBoot):
1.在Apollo中创建应用:
(1)在首页,点左上角"创建应用",输入AppId值等(此处AppId与待接入应用application.yml的app.id对应):
(2)进入创建的应用,点击Key/Value列表的右上角"新增配置",添加Key和Value:
(3)创建Key+Value后,回到Key/Value列表中,点右上角靠左的"发布"按钮生效:
2.Web工程中配置Apollo信息:
(1)添加apollo依赖:
<dependencies> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-openapi</artifactId> <version>1.9.2</version> </dependency> </dependencies>
(2)配置app.id与app.meta,在application.yml中:
... app: id: apollo #apollo应用ID(必须与Apollo网页上创建项目时定义的应用Id一致) apollo: meta: http://192.168.233.128:8080 #apollo服务器地址 autoUpdateInjectedSpringProperties: true #false时@Value("${key1}")值不会自动更新,默认true自动更新 #cache-dir: /opt/data/some-cache-dir #自定义本地缓存路径,默认为/opt/data/{appId}/config-cache access-key: secret: 838fe62c502a43e2b70a31d7c7156f10 #配置访问密钥,在Apollo首页-管理密钥中创建密钥 property: names: cache: enable: true #开启propertyNames缓存 bootstrap: enabled: true #开启 Apollo namespaces: application #可以注入多个命名空间,以逗号分隔 #eagerLoad: # enabled: true #将apollo初始化放在日志系统初始化之前
(3)Application启动类添加@EnableApolloConfig注解:
@SpringBootApplication @EnableApolloConfig //开启Apollo public class ApolloApplication { public static void main(String[] args) { SpringApplication.run(ApolloApplication.class); } }
(4)Config方式获取apollo配置中心的配置:
@Configuration public class ApolloConfig { @Value("${key1}")//从apollo配置中心获取添加的key1 public String key1; }
(5)获取key1并打印的测试Conrtoller:
@RestController public class UserController { @Autowired private ApolloConfig apolloConfig; @RequestMapping("/user") @ResponseBody public String user(){ return apolloConfig.key1; } }
3.启动Web应用,需要添加Apollo环境+地址参数:
(1)IntelliJ IDEA方式,Edit Configurations -> 左侧Application中选中ApolloApplication,右侧Configuration选项卡中,在VM options中填入:
-Dapollo.meta=http://192.168.233.128:8080 -Denv=dev
(2)运行jar包方式,展开IntelliJ IDEA右侧Maven -> 找到apollo模块Lifecycle菜单,分别双击clean、install,将target目录下生成的apollo.jar上传到centos7,并使用java命令启动:
[root@localhost ~]# java -jar -Dapollo.meta=http://192.168.233.128:8080 -Denv=dev apollo.jar &
(3)访问Web应用"/user"接口显示获取到的key1值可以看到效果(不停止Web应用,修改Apollo配置中心的key1值,重新发布,再刷新以下接口,可以看到值已经变成新的了):
http://192.168.233.128:10006/user
加载全部内容