zoukankan      html  css  js  c++  java
  • Spring 控制台运行及RestTemplate实现Eurka负载均衡

    spring使用控制台运行方式

    spring.main.web-application-type=none
    新老版本的配置有点差异


    Maven的modules只是实现了一个顺序编译,一次多个项目一起生成而己

    通过parent才能真正实现继承,目录关系不顶事

    @SpringBootApplication
    @EnableDiscoveryClient
    @Configuration
    public class DemoConsoleApplication implements CommandLineRunner {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoConsoleApplication.class, args);
        }
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    
        @Autowired
        RestTemplate restTemplate;
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("command line start runing now....");
            HashMap<String,String> pms=new HashMap<>();
            pms.put("a","b");
            pms.put("c","d");
    
            String ret = restTemplate.execute("http://xx/welcome", HttpMethod.GET, null, new ResponseExtractor<String>() {
                @Override
                public String extractData(ClientHttpResponse clientHttpResponse) throws IOException {
                    InputStream aa = clientHttpResponse.getBody();
                    String str="";
                    BufferedReader reader=new BufferedReader(new InputStreamReader(aa));
                    String line="";
                    do{
                        line=reader.readLine();
                        str+=line+"
    ";
                    }while(line!=null);
                    return str;
                }
            },pms);
    
            System.out.println(ret.toString());
    
        }
    
    }

    rest动态请求微服务

    下面这种更牛逼,可以使用header/object 传值方式调用

                    HashMap<String, String> headerMap = JSON.parseObject(headers, HashMap.class);
                    HashMap<String, Object> paramObj = JSON.parseObject(params, HashMap.class);
                    MultiValueMap<String, String> mtHeader=new LinkedMultiValueMap<>();
                    headerMap.forEach((String k,String v)->{
                            mtHeader.set(k,v);
                    });
                    HttpEntity<?> entiry = new HttpEntity<HashMap<String, Object>>(paramObj,mtHeader);
                    ResponseEntity<String> ret = restTemplate.exchange(url, HttpMethod.POST, entiry, String.class);
                    log.info("restTemplate,status={},statuscode={},body={}", ret.getStatusCode(), ret.getStatusCodeValue(), ret.getBody());
                    HashMap<String, Object> resultVo = JSON.parseObject(ret.getBody(), HashMap.class);
                    //判断是否消费成功
                    if (!hasSuccess(resultVo)) {
                        flag = false;
                    }
  • 相关阅读:
    Android App Bundle 使用指南
    Homebrew国内源
    Mac下配置环境变量不生效问题
    CocosCreator1.x配置打包Android App Bundle
    Android读取Json文件的工具类
    Cocos Creator 坐标转换
    XCode真机调试不了,提示"Please reconnect the device"
    Canvas: trying to draw too large(134374464bytes) bitmap.
    Modbus主从关系几点记录
    当前时间加上几天
  • 原文地址:https://www.cnblogs.com/a-xu/p/9416832.html
Copyright © 2011-2022 走看看