Springboot apollo原理及使用方法详解

文章背景

如果在spring boot中接入apollo官方文档:https://github.com/ctripcorp/apollo/wiki使用官方的apollo

演示环境(Demo):

106.54.227.205账号/密码:apollo/admin

添加配置

Springboot apollo原理及使用方法详解_第1张图片

spring-boot中如何使用

pom.xml中添加配置


  com.ctrip.framework.apollo
  apollo-client
  1.1.0

配置文件中添加apollo地址

app:
 id: komiles
apollo:
 meta: http://106.54.227.205:8080
 bootstrap:
  enabled: true
  namespaces: application

启动类中添加代码

添加@EnableApolloConfig注解

package com.example.apollodemo;
 
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
@EnableApolloConfig
@MapperScan("com.example.apollodemo.mapper")
public class ApolloDemoApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(ApolloDemoApplication.class, args);
    System.out.println("============ apollo demo application end =============");
  }
}

controller类新增文件

ApolloController.java

package com.example.apollodemo.controller;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @author [email protected]
 * @date 2020-05-06 17:28
 */
@RestController
@RequestMapping("/apollo")
public class ApolloController {
 
  @Value("${name}")
  private String name;
 
  @GetMapping("/name")
  public String name()
  {
    return name;
  }
}

可以读取到配置为kongming.

数据库配置如何使用?

同理,generatorConfig.xml中也可以读取数据库配置





  
    
      
      
    
    
    
    
    
      
    
    
    
      
      
      
    
    

    
      
    
    
    
      
    
    
    

项目demo地址https://github.com/KoMiles/spring-example/tree/master/apollo-demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Springboot apollo原理及使用方法详解)