Spring Boot基础教程23-调用REST服务-如何使用代理

  • 添加依赖

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

</dependency>

  • 代码实现

/**

 * @author wujing

 */

@RestController

@RequestMapping(value = "/rest", method = RequestMethod.POST)

public class RestRoncooController {

 

@Autowired

private RoncooUserLogCache RoncooUserLogCache;

 

@RequestMapping(value = "/update")

public RoncooUserLog update(@RequestBody JsonNode jsonNode) {

System.out.println("jsonNode=" + jsonNode);

RoncooUserLog bean = RoncooUserLogCache.selectById(jsonNode.get("id").asInt(1));

if(bean == null){

bean = new RoncooUserLog();

}

bean.setUserName("测试");

bean.setCreateTime(new Date());

bean.setUserIp("192.168.1.1");

RoncooUserLogCache.updateById(bean);

return bean;

}

 

@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)

public RoncooUserLog update2(@PathVariable(value = "id") Integer id) {

RoncooUserLog bean = RoncooUserLogCache.selectById(id);

if(bean == null){

bean = new RoncooUserLog();

}

bean.setUserName("测试");

bean.setCreateTime(new Date());

bean.setUserIp("192.168.1.1");

RoncooUserLogCache.updateById(bean);

return bean;

}

 

}

  • 测试

@Autowired

private RestTemplateBuilder restTemplateBuilder;

 

/**

 * get请求

 */

@Test

public void getForObject() {

RoncooUserLog bean = restTemplateBuilder.build().getForObject("http://localhost:8080/rest/update/{id}", RoncooUserLog.class, 6);

System.out.println(bean);

Map<String,Object> map = new HashMap<String,Object>();

map.put("id", 7);

bean = restTemplateBuilder.build().postForObject("http://localhost:8080/rest/update", map, RoncooUserLog.class);

System.out.println(bean);

}

 

代理实现

static class ProxyCustomizer implements RestTemplateCustomizer {

@Override

public void customize(RestTemplate restTemplate) {

String proxyHost = "59.33.46.187";

int proxyPort = 6969;

 

HttpHost proxy = new HttpHost(proxyHost, proxyPort);

HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {

@Override

public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {

 

return super.determineProxy(target, request, context);

}

}).build();

HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

httpComponentsClientHttpRequestFactory.setConnectTimeout(10000);

httpComponentsClientHttpRequestFactory.setReadTimeout(60000);

restTemplate.setRequestFactory(httpComponentsClientHttpRequestFactory);

}

}

 

代理测试

 

String result = restTemplateBuilder.additionalCustomizers(new ProxyCustomizer()).build().getForObject("http://www.roncoo.com", String.class);

System.out.println(result);

在线代理

http://ip.zdaye.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中实现事件驱动的远程调用可以使用Spring Cloud Stream和Spring Cloud Function来实现。以下是一个示例代码,演示如何在Spring Boot应用程序中实现事件驱动的远程调用: 首先,确保你的项目中引入了Spring Cloud Stream和Spring Cloud Function的依赖。 1. 创建一个发送事件的服务: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.messaging.Source; import org.springframework.messaging.support.MessageBuilder; @SpringBootApplication @EnableBinding(Source.class) public class EventSenderApplication { @Autowired private Source source; public static void main(String[] args) { SpringApplication.run(EventSenderApplication.class, args); } public void sendEvent(String message) { source.output().send(MessageBuilder.withPayload(message).build()); } } ``` 在上述代码中,我们创建了一个发送事件的服务。通过使用 `@EnableBinding` 注解绑定到 `Source` 接口,我们可以使用它来发送消息。`sendEvent` 方法接受一个消息作为参数,并使用 `MessageBuilder` 构建一个消息对象,然后通过 `source.output().send()` 方法发送消息。 2. 创建一个接收事件的服务: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.annotation.StreamListener; import org.springframework.cloud.stream.messaging.Sink; @SpringBootApplication @EnableBinding(Sink.class) public class EventReceiverApplication { public static void main(String[] args) { SpringApplication.run(EventReceiverApplication.class, args); } @StreamListener(Sink.INPUT) public void processEvent(String message) { System.out.println("Received event: " + message); // 在这里处理收到的事件 } } ``` 在上述代码中,我们创建了一个接收事件的服务。通过使用 `@EnableBinding` 注解绑定到 `Sink` 接口,我们可以使用 `@StreamListener` 注解来监听消息。`processEvent` 方法将收到的消息作为参数,并在控制台打印出来,你可以在这里处理收到的事件。 3. 使用远程调用发送事件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class EventController { @Autowired private EventSenderApplication eventSender; @GetMapping("/send/{message}") public String sendEvent(@PathVariable String message) { eventSender.sendEvent(message); return "Event sent: " + message; } } ``` 在上述代码中,我们创建了一个REST控制器,其中`sendEvent` 方法使用 `EventSenderApplication` 来发送事件。通过调用 `eventSender.sendEvent(message)` 方法来发送消息。 在你的应用程序中,你可以通过访问 `http://localhost:8080/send/{message}` 的REST端点来发送事件。事件将被发送到绑定的消息通道,并由接收事件的服务进行处理。 请注意,以上代码只是一个示例,你可以根据实际需求进行扩展和修改。确保在应用程序中正确配置和引入了Spring Cloud Stream和Spring Cloud Function的依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值