Skip to content

Commit

Permalink
fix: retry sending the request using http
Browse files Browse the repository at this point in the history
  • Loading branch information
Similarityoung committed Nov 8, 2024
1 parent 6c0249a commit d71fe34
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<artifactId>spring-context-support</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

<dependency>
<groupId>org.apache.dubbo.extensions</groupId>
<artifactId>dubbo-rpc-rest</artifactId>
Expand Down Expand Up @@ -147,13 +152,6 @@
<version>4.1.92.Final</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>


<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,41 @@
package org.apache.dubbo.samples.spi.protocol.rest;


import org.apache.dubbo.samples.spi.protocol.rest.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class RestConsumer {

protected static final String HOST = System.getProperty("zookeeper.address", "localhost");

protected static String toUri(String path) {
return "http://" + HOST + ":20880/" + path;
}

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/http-consumer.xml");
context.start();

DemoService demoService = (DemoService) context.getBean("demoService");
System.out.println("RestConsumer result: " + demoService.sayHello("world"));
RestTemplate restTemplate = new RestTemplate();

// create form data
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("number", "1");

// set the request header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// create a request entity
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map, headers);

// send a post request
ResponseEntity<String> response = restTemplate.postForEntity(toUri("demo/sayHello"), requestEntity, String.class);

System.out.println("Response: " + response.getBody());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,45 @@
*/


import org.apache.dubbo.samples.spi.protocol.rest.api.DemoService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/http-consumer.xml"})
public class DemoServiceIT {

@Autowired
private DemoService demoService;
protected static final String HOST = System.getProperty("zookeeper.address", "localhost");

protected static String toUri(String path) {
return "http://" + HOST + ":20880/" + path;
}

@Test
public void test() {
Assert.assertTrue(demoService.sayHello("world").startsWith("Hello world"));
RestTemplate restTemplate = new RestTemplate();

// Create form data
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("number", "world");

// Set the request header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

// create a request entity
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map, headers);

// send a post request
ResponseEntity<String> response = restTemplate.postForEntity(toUri("demo/sayHello"), requestEntity, String.class);

System.out.println("Response: " + response.getBody());
Assert.assertTrue(response.getBody().startsWith("Hello world"));
}

}

0 comments on commit d71fe34

Please sign in to comment.