栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot 集成 apollo 入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot 集成 apollo 入门

基础概念

  1. 官方文档(官网介绍较为详细,不再赘述)

Apollo

SpringBoot集成

一.官网给予了基础的Quick Start,可参考学习入门 ,(win平台启动可以使用git客户端执行sh脚本)

$ sh ./demo.sh start Windows new JAVA_HOME is: /d/DevProgram/Java/jdk ==== starting service ==== Service logging file is ./service/apollo-service.log Started [1121] Waiting for config service startup...

看到Started [1121]即表示成功! 二.这里给出在springboot工程中如何使用的简单demo 1.首先参考Quick Start文档,搭建基础环境,包括 Apollo config应用和数据库等 2.新建springboot 工程,导入web依赖和Apollo client依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
    
    com.ggq
    springboot-apollo
    1.0-SNAPSHOT
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.ctrip.framework.apollo
            apollo-client
            1.3.0
        
    

2.主启动类

package com.ggq;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
​
@SpringBootApplication
@EnableApolloConfig //此处开启apollo配置
public class ApolloClientStart {
    public static void main(String[] args) {
        SpringApplication.run(ApolloClientStart.class,args);
    }
}
​

3.构建基础配置文件,指定apollo配置中心地址和应用本身id等

server:
  port: 8081
​
app:
  id: springboot-apollo
apollo:
  meta: http://127.0.0.1:8080
  bootstrap:
    enabled: true
    eagerLoad:
      enabled: true

4.测试类

package com.ggq.controller;
​
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
public class TestController {
​
    @Value( "${name}" )
    private String name;
​
    @RequestMapping("/name")
    public String ReadName() {
        return "我的名字是:"+this.name;
    }
​
}
​

访问http://localhost:8081/name,可以得到我的名字是:张三 ,重新在apollo中配置name值无需重启,再次请求 http://localhost:8081/name,可得到我的名字是:李四 ​

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号