SpringBoot实现apollo的简单使用与整合

26 篇文章 0 订阅
13 篇文章 0 订阅

SpringBoot实现apollo的简单使用与整合

服务信息

SpringBoot[2.0.2.RELEASE]
windows 7
apollo 1.4

安装部署概要:

注意:此次部署是使用打包好的jar包直接进行部署

分布式部署指南官方文档
源码构建git地址
安装包构建地址
- apollo-adminservice-1.4.0-github.zip
- apollo-configservice-1.4.0-github.zip
- apollo-portal-1.4.0-github.zip
- 也可以直接百度网盘下载 密码-> aych

安装步骤

  1. 执行【数据库脚本】的两个数据库脚本【apolloconfigdb.sql】【apolloportaldb.sql】
  2. 修改项目配置
    2.1: 修改【apollo-configservice-1.4.0-github】的apollo-configservice-1.4.0-github\scripts\startup.sh文件的内容的端口号,由SERVER_PORT=8080改成SERVER_PORT=18080
    修改【apollo-configservice-1.4.0-github】的apollo-configservice-1.4.0-github\config\application-github.properties文件的数据库配置信息
    2.2: 修改【apollo-adminservice-1.4.0-github】的apollo-adminservice-1.4.0-github\scripts\startup.sh文件的内容的端口号,由SERVER_PORT=8090改成SERVER_PORT=18090
    修改【apollo-adminservice-1.4.0-github】的apollo-adminservice-1.4.0-github\config\application-github.properties文件的数据库配置信息
    2.3: 修改【apollo-portal-1.4.0-github】的apollo-portal-1.4.0-github\scripts\startup.sh文件的内容的端口号,由SERVER_PORT=8070改成SERVER_PORT=18070
    修改【apollo-portal-1.4.0-github】的apollo-portal-1.4.0-github\config\application-github.properties文件的数据库配置信息
    修改【apollo-portal-1.4.0-github】的apollo-portal-1.4.0-github\config\apollo-env.properties文件的meta信息修改为【dev.meta=http://localhost:18080#apollo-configservice服务的地址,非eureka的地址】
  3. 修改数据库【apolloconfigdb】的【serverconfig】的信息,key为【eureka.service.url】的值为eureka的服务值【http://localhost:18080/eureka/】,因为【apollo-adminservice】是自己带eureka服务的
  4. 一定要先启动 [apollo-configservice] 然后是 [apollo-adminservice] 然后是 [apollo-portal]
    - admin一定要在 cfg启动成功 之后;
    - portal 一定要在 admin启动成功 之后;
  5. 访问页面localhost:18070账号:apollo,密码:admin
  6. 补充环境,补充namespace,创建自己项目,环境等信息
  7. 在本地使用时的配置
    7.1 在C盘创建【C:\opt\settings】文件夹和apollo-env.properties文件和server.properties文件和server
    apollo-env.properties
    dev.meta=http://127.0.0.1:18080
    #fat.meta=http://127.0.0.1:18080
    fat.meta=http://127.0.0.1:18080
    #uat.meta=http://127.0.0.1:18080
    #pro.meta=http://127.0.0.1:18080
    
    server.properties
    env=dev
    idc=pkk
    

7.2 在C盘【C:\opt\data】文件夹中是存放的项目的配置缓存

使用apollo

  1. 修改application.yml的配置
spring:
  application:
    name: peak-apollo

app:
  id: ${spring.application.name}
#apollo.bootstrap.enabled:在应用启动阶段,向Spring容器注入被托管的application.properties文件的配置信息
#这里和porto里面都是使用的是18080不是eureka的地址
apollo:
  meta: http://localhost:18080
  bootstrap:
    enable: true
  eagerLoad:
    enabled: true




#apollo.bootstrap.eagerLoad.enabled:将Apollo配置加载提到初始化日志系统之前。
logging:
  level:
    com:
      pkk: debug

在resources下创建文件夹META-INF,创建文件app.properties

app.name = peak-apollo
  1. 项目启动类
项目启动类上加@EnableApolloConfig

负载方式配置

前提说明:
	config		->		端口[18080]
	admin		->		端口[18090]
	portal		->		端口[18070]


/opt/cloud/apollo/admin/config/application-github.properties
	# DataSource
	spring.datasource.url = jdbc:mysql://127.0.0.1:3306/apolloconfigdb?characterEncoding=utf8
	spring.datasource.username = ******
	spring.datasource.password = ******

	# 指定此实例的ip(内网ip信息)
	eureka.instance.ip-address=127.0.0.1
	# 注册时使用ip而不是主机名
	eureka.instance.prefer-ip-address=true

/opt/cloud/apollo/config/config/application-github.properties
	spring.datasource.url = jdbc:mysql://127.0.0.1:3306/apolloconfigdb?characterEncoding=utf8
	spring.datasource.username = ******
	spring.datasource.password = ******

	# 指定此实例的ip(此处为负载ip地址信息,或者设置为域名(域名待核验))
	eureka.instance.ip-address= 139.129.223.28
	# 注册时使用ip而不是主机名
	eureka.instance.prefer-ip-address=true

/opt/cloud/apollo/portal/config/application-github.properties
	# DataSource
	spring.datasource.url = jdbc:mysql://127.0.0.1:3306/apolloportaldb?characterEncoding=utf8
	spring.datasource.username = ******
	spring.datasource.password = ******

	# 指定此实例的ip(为内网ip信息)
	eureka.instance.ip-address=127.0.0.1
	# 注册时使用ip而不是主机名
	eureka.instance.prefer-ip-address=true
	
	
	
/opt/cloud/apollo/portal/config/apollo-env.properties
	local.meta=http://localhost:18080
	dev.meta=http://localhost:18080
	fat.meta=http://localhost:18080
	uat.meta=http://localhost:18080
	lpt.meta=http://localhost:18080
	pro.meta=http://localhost:18080
	
	
	
	
	
数据库的配置
	apolloconfigdb.serverconfig
		配置想eureka.service.url的值为http://localhost:18080/eureka/
	

但服务器集群启动

我们只需要在启动之前把要使用的集群名称放到环境变量中接可以了
下面是实现方式
我们在bootstrap.yml中增加配置[apollo.ide=namespace]就可以了

public static void main(String[] args) {
    //加载apollo配置
    ApolloUtil.handleApolloCenterData();
    //启动SpringBoot容器
    SpringApplication.run(****.class, args);
  }
/**
   * 处理apollo的集群配置
   *
   * @return void
   * @Param
   * @since
   */
  public static void handleApolloCenterData() {
    String configPath = "bootstrap.yml";
    InputStream resourceAsStream = null;
    try {
      resourceAsStream = new FileInputStream(configPath);
    } catch (FileNotFoundException e) {
      resourceAsStream = ApolloUtil.class.getClassLoader().getResourceAsStream(configPath);
    }

    Yaml yaml = new Yaml();
    final Map bootstrap = yaml.loadAs(resourceAsStream, Map.class);
    if (MapUtil.isEmpty(bootstrap)) {
      return;
    }
    Map<String, String> apollo = (Map<String, String>) bootstrap.get("apollo");
    if (MapUtil.isEmpty(apollo)) {
      return;
    }
    final String idc = apollo.get("idc");
    if (StringUtil.isBlank(idc)) {
      return;
    }
    System.setProperty("idc", idc);
    log.info("Apollo Data Center is [{}] from JVM system property 'apollo.idc'", idc);
  }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值