Apollo+springboot实例demo

Apollo的部署这里不作复述,网上很详细

这里就贴一个demo代码

springboot程序入口:

@SpringBootApplication

public class H_Main implements EmbeddedServletContainerCustomizer{
    public static void main(String[] args) {
          SpringApplication.run(H_Main.class, args);
          // 运行之后在浏览器中访问:http://localhost:9999/hello
    }


    @Override
    public void customize (ConfigurableEmbeddedServletContainer arg0) {
        arg0.setPort(9999);
    }

}

实现EmbeddedServletContainerCustomizer接口,你可以自定义http访问的端口,是不是很方便。。

下面是restful接口,由springboot初始化加载:


@Controller
@EnableAutoConfiguration
@RequestMapping("/apo")
public class ApolloConfigReadController {
    ApolloConfigReadController apolloConfigReadController;

      @RequestMapping("/wyx")
      @ResponseBody
      String mywyx(){
          return "hello!hello! hello!";
      }
    
      @RequestMapping("/apollo")
      @ResponseBody
    public void apollotest(){
        Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
        String someKey = "name";
        String someDefaultValue = "wbl";
        String value = config.getProperty(someKey, someDefaultValue);
        
        System.out.println("local-value"+value);
        
        System.out.println(config.getProperty("myname", null));
        
        
    }
    
    @RequestMapping("/sg")
    @ResponseBody
    public String setSg376(){
        System.out.println("前端访问正常.");
        Config config = ConfigService.getConfig("protocolconfig");//namespace
         try {
                Properties prop2 = new Properties();
                
                apolloConfigReadController = new ApolloConfigReadController();
                
                URL url = apolloConfigReadController.getClass().getResource("");
                System.out.println(url.toString());
                
                InputStream in = ApolloConfigReadController.class.getClassLoader().getResourceAsStream("./config2.properties");
                prop2.load(in);
                Iterator it=prop2.stringPropertyNames().iterator();
                System.out.println("本地配置读取完成.");
                int countListt=0;
                String vval = "";
                while(it.hasNext()){
                    countListt=0;
                    String key=it.next();
//                    config.getProperty(key,prop2.getProperty(key));
                    vval = prop2.getProperty(key);
                    
                    countListt=getCount(vval,"*");
                    if(countListt>6){
                        System.out.println("最多的*号个数是:"+countListt);
                    }
                }
                in.close();
                System.out.println("配置写入apollo服务端配置文件完成.");
            } catch (IOException e) {
                e.printStackTrace();
            }
         return "配置加载结束!";
    }
    
    public static int getCount(String str, String tag) {
        int index = 0;
        int count = 0;       
         while ((index = str.indexOf(tag)) != -1 ) {
             str = str.substring(index + tag.length());
             count++;
        }
        return count;
    }
    
}

你可能感兴趣的:(java)