配置方式介绍:
1、在阿里云服务器安装Quick-Start Apollo服务
2、在本地使用springboot结合Apollo配置功能调试
一、前置条件
1、在阿里云服务器/opt/apollo目录下下载解压Quick-Start apollo压缩包
2、根据quick start使用指南安装对应的软件环境,目前对外只依赖JDK和MYSQL
3、保证8080、8070、8090端口未被占用
4、阿里云服务器安全组开放对应端口
二、初始化软件环境
1、创建数据库 - MYSQL初始化
2、配置数据库信息 - apollo配置初始化
在apollo解压目录下,修改demo.sh文件中的数据库配置:
# meta server url
config_server_url=http://你的ip:8080
admin_server_url=http://你的ip:8090
eureka_service_url=$config_server_url/eureka/
portal_url=http://1你的ip:8070
三、运行apollo
在解压目录下执行./demo.sh start,等待一会儿即可
范围portal之后出现以下内容:
默认用户名/密码: apollp / admin
四、配置项目以及item配置
五、本地springboot配置信息(mac)
1、在opt/settings/server.properties
中设置应用对应的环境
env=DEV
2、在idea
启动参数中设置应用指定eureka地址
VM options: -Dapollo.configService=http://你的ip:8080
注意:这个不设置的话,阿里云中部署的服务端会根据规则默认使用内网ip,quick start模式修改db和demo.sh中的eureka配置都不好使
3、application.yml
内容
server:
port: 8080
#apollo配置可以参考
#https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97#122-environment
app:
id: springboot-apollo
apollo:
meta: http://139.129.91.148:8080
bootstrap:
enabled: true
namespaces: application
4、启动类ApolloTestApplication内容
package com.toxic.anepoch.apollotest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
/**
* @author py
* @date 2020/06/29 10:17 PM.
*/
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
public class ApolloTestApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ApolloTestApplication.class, args);
}
}
5、获取apollo配置的实体AppConfig
package com.toxic.anepoch.apollotest.controller.model;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author ying.pan
* @date 2020/6/29 9:01 PM.
*/
@Configuration
@EnableApolloConfig
public class AppConfig {
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
public class TestJavaConfigBean {
@Value("${timeout:100}")
private int timeout;
@Value("${batch:100}")
private int batch;
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public void setBatch(int batch) {
this.batch = batch;
}
public int getTimeout() {
return timeout;
}
public int getBatch() {
return batch;
}
}
}
6、测试TestController
类
package com.toxic.anepoch.apollotest.controller.web;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.toxic.anepoch.apollotest.controller.model.AppConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author ying.pan
* @date 2020/6/29 9:35 PM.
*/
@RestController
@RequestMapping("test/apollo")
public class TestController {
@Resource
private AppConfig appConfig;
@RequestMapping(value = "/getKey")
public Object getKey() {
System.out.println(appConfig.javaConfigBean().getBatch());
System.out.println(appConfig.javaConfigBean().getTimeout());
return "success";
}
@RequestMapping(value = "/getConfig")
public Object getConfig() {
//直接调用api的方式
Config config = ConfigService.getAppConfig();
String someKey = "batch";
String someDefaultValue = "none";
String value = config.getProperty(someKey, someDefaultValue);
System.out.println(value);
return "success";
}
}
六、本地验证
启动springboot,访问http://localhost:8080/test/apollo/getKey
控制台输出:
2020-06-30 02:34:47.694 INFO 41312 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2020-06-30 02:34:47.694 INFO 41312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2020-06-30 02:34:47.717 INFO 41312 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 23 ms
22211
111
七、常用信息
密码相关:
默认用户名/密码: apollo / admin
url相关:
1、portal路径: http://你的ip + 8070
2、eureka路径: http://你的ip + 8080
3、adminservice路径: http://你的ip + 8090
常用命令
1、./demo.sh stop - 停止服务
2、./demo.sh start - 启动服务