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<String> 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;
    }
    
}

發佈了10 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章