• 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;
                    }
  • 相关阅读:
    js小知识
    elasticsearch查询与sql对应关系
    svnkit 异常:Exception in thread "main" org.tmatesoft.svn.core.SVNException: svn: E200030: SQLite error
    spring中引入多个quertz 注意事项
    ajax跨域
    MetaException(message:For direct MetaStore DB connections, we don't support retries at the client level.)
    center os7 安装mysql
    Center OS 7
    解决js ajax跨越请求 Access control allow origin 异常
    gps位置坐标转百度坐标
  • 原文地址:https://www.cnblogs.com/a-xu/p/9416832.html
Copyright © 2020-2023  润新知