resttemplate

springcloud的两种负载均衡策略

放肆的年华 提交于 2019-12-04 04:56:34
前言:   之前写了通过Ribbon+RestTemplate实现调用服务,此处我再系统的说一下两者的区别 一、springcloud的负载均衡策略   1、Ribbon  是基于Netflix Ribbon实现的一套客户端 负载均衡的工具,类似Nginx 主要功能时提供客户端的软件负载均衡算法 LB就是负载均衡,集中式(F5),进程内(Nginx),消费者可以自动看 从Eureka中拿到对应的服务列表,默认进行轮询RoundRobinRule 下图是RestTemplate的自带的7中均衡策略 我们在之前通过Ribbon+RestTemplate实现调用服务的时候,在获取RestTemplate的方法上加了@LoadBalanced ,实现默认轮询,如果需要更改其均衡策略,则在配置类中声明想要的均衡策略 具体实现参考: https://www.cnblogs.com/guanyuehao0107/p/11819375.html @Configuration public class RestConfig { @Bean //通过RestTemplate来实现调用接口 @LoadBalanced //表示RestTemplate开启了负载均衡 public RestTemplate getRestTemplate(){ return new RestTemplate(); } /

Best way to map json to a Java object

我怕爱的太早我们不能终老 提交于 2019-12-04 03:48:54
问题 I'm using restTemplate to make a rquest to a servlet that returns a very simple representation of an object in json. { "id":"SomeID" "name":"SomeName" } And I have a DTO with those 2 fields and the corresponding setters and getters. What I would like to know is how to create the object using that json response without having to "parse" the response. 回答1: Personally I would recommend Jackson. Its fairly lightweight, very fast and requires very little configuration. Here's an example of

Spring RestTemplate follow redirect with cookie

99封情书 提交于 2019-12-04 03:31:16
Recently I ran into a problem where I needed to do a GET request to a remote service (Using a simple servlet i presume), and RestTemplate returned Too many redirects! . After some investigation, it seems like the first request made to the specified remote service, is actually just a 302-redirect (to itself) with some Set-Cookie headers. If I were using a "normal" browser, it would acknowledge the header, set the cookies correctly, and follow the redirect where it should meet a normal 200 response. What I've found is that RestTemplate doesn't accept the Set-Cookie header, so the redirect gets

RestTemplate usage

半世苍凉 提交于 2019-12-04 01:42:47
问题 I'm trying to use RestTemplate in order to make a PUT . For some reason I can't reproduce the PUT I created using curl that goes through without any problems. Here is my curl call that succeeds and returns 200: curl https://www.exampe.com \ -X PUT \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <bearer-token>" \ -v \ -d '{"json":"object"}' And here is Java code that tried to replicate this curl call. This code in my case will throw HttpClientErrorException with status = 406:

REST POST works correctly with POSTMAN but exception when using Spring RestTemplate

守給你的承諾、 提交于 2019-12-04 01:25:09
I am writing a Rest client to post JSON data using Spring RestTemplate. Using POSTMAN and following JSON data in body get the response correctly- { "InCode":"test", "Name":"This is test", "Email":"test@gmail.com", "Id":18, } However when trying to hit the REST API using Spring RestTemplate as follows ResponseEntity<String> response = restTemplate.exchange(baseUrl, HttpMethod.POST, getHeaders(), String.class); private HttpEntity<?> getHeaders() throws JSONException { JSONObject request = new JSONObject(); request.put("Email", "test@gmail.com"); request.put("Id", "18"); request.put("Name", "This

Ribbon with Spring Cloud and Eureka: java.lang.IllegalStateException: No instances available for Samarths-MacBook-Pro.local

馋奶兔 提交于 2019-12-04 01:18:29
I am working on Spring Boot Eureka Client Application with Ribbon Load Balancer. I have two instances of the server registered with Eureka with the name "TEST". On the client side, I have the following code to fetch the server from Eureka. @Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaClient @RestController public class EurekaConsumerApplication { @Autowired DiscoveryClient discoveryClient; @Autowired RestTemplate restTemplate; @RequestMapping(value = "/",method = RequestMethod.GET) String consumer(){ InstanceInfo instance = discoveryClient.getNextServerFromEureka("TEST",

实现负载均衡的小demo

夙愿已清 提交于 2019-12-04 00:08:16
首先我们先来了解负载均衡: 负载均衡是为了缓解网络压力的,服务器端进行扩容的重要手段  实现有两种方式:硬件F5 、 软件nginx、Dubbo 为了实现负载均衡的原理,我们基于以下两篇随笔继续学习 Euraka适合初学者的简单小demo 作为消费者访问提供者提供的功能(eureka的铺垫案例) 创建多个提供者的角色存在。 在消费者的实现中: (1) 修改pom文件,加入web、客户端的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> (2) application.yml文件中,配置eureka的相关配置 server: port: 80 #EurekaServer配置 eureka: client: register-with-eureka: false #不注册到其他的注册中心 fetch-registry:

How to use AsyncRestTemplate to make multiple calls simultaneously?

怎甘沉沦 提交于 2019-12-03 23:17:49
I don't understand how to use AsyncRestTemplate effectively for making external service calls. For the code below: class Foo { public void doStuff() { Future<ResponseEntity<String>> future1 = asyncRestTemplate.getForEntity( url1, String.class); String response1 = future1.get(); Future<ResponseEntity<String>> future2 = asyncRestTemplate.getForEntity( url2, String.class); String response2 = future2.get(); Future<ResponseEntity<String>> future3 = asyncRestTemplate.getForEntity( url3, String.class); String response3 = future3.get(); } } Ideally I want to execute all 3 calls simultaneously and

Resttemplate getForEntity - Pass headers

夙愿已清 提交于 2019-12-03 22:49:23
Is it possible to set header as part of getForEntity method or should I use exchange? I am trying to set oauth header as part of getForEntity calls. You can use .exchange : ResponseEntity<YourResponseObj> entity = new TestRestTemplate().exchange( "http://localhost:" + port + "/youruri", HttpMethod.GET, new HttpEntity<Object>(headers), YourResponseObj.class); Full Junit sample: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ReferenceTablesControllerTests { @LocalServerPort private int port; @Test public void getXxxx() throws Exception {

RestTemplate uriVariables not expanded

白昼怎懂夜的黑 提交于 2019-12-03 22:25:24
I try to access a rest endpoint by using springs RestTemplate.getForObject() but my uri variables are not expanded, and attached as parameters to the url. This is what I got so far: Map<String, String> uriParams = new HashMap<String, String>(); uriParams.put("method", "login"); uriParams.put("input_type", DATA_TYPE); uriParams.put("response_type", DATA_TYPE); uriParams.put("rest_data", rest_data.toString()); String responseString = template.getForObject(endpointUrl, String.class, uriParams); the value of the endpointUrl Variable is http://127.0.0.1/service/v4_1/rest.php and it's exactely what