spring-boot(二十二)集成apollo

分类: spring-boot
阅读:342
作者:majingjing
发布:2019-03-08 16:20:29

spring-boot(二十二)集成apollo配置中心

  1. 启动apollo服务,进入系统,创建项目 image

  2. 在222项目中添加一个参数 key2 image

  3. 快速构建一个springboot程序 image

  4. 引入 apollo 的依赖

    	<dependency>
    		<groupId>com.ctrip.framework.apollo</groupId>
    		<artifactId>apollo-client</artifactId>
    		<version>1.1.0</version>
    	</dependency>
    
  5. 修改 application.properties

    	apollo.bootstrap.enabled = true
    	apollo.meta: http://localhost:8080
    	server.port=8888
    
  6. 创建apollo的配置文件 src/main/resources/META-INF/app.properties

    	app.id=222
    
  7. 注入apollo中的参数到bean中 image

  8. 编写调用示例

    	@RestController
    	public class MyController {
    
    		@Autowired
    		MyApolloConfig myApolloConfig;
    
    		@GetMapping(value = "/get")
    		public Object get(){
    			HashMap<String,Object> map = new HashMap<>();
    			map.put("date", System.currentTimeMillis());
    			map.put("key2",myApolloConfig.getKey2());
    
    			return map;
    		}
    
    	}
    
  9. 启动程序 , 访问 http://localhost:8888/get image

  10. 在apollo中修改 key2 的值 image

  11. 再次访问 http://localhost:8888/get image

可以看到apollo的配置是实时刷新的。

源代码地址 https://github.com/majinding/apollo-client-demo.git