TestController.java
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.zhongweixian.controller;
import com.zhongweixian.entity.MessagesReqBody;
import com.zhongweixian.service.MessagesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import java.util.HashMap;
/**
* @author caoliang1918@aliyun.com
* @Date 2017/10/24:23:42
*/
@RestController
@RequestMapping("test")
public class TestController {
private Logger logger = LoggerFactory.getLogger(TestController.class);
//spring cloud 是通过http进行远程调用,这是和dobble最大的区别
@Autowired
private RestTemplate restTemplate;
//通过FeignClient注解进行远程调用
@Autowired
private MessagesService messagesService;
@PostMapping("/sendMessage")
public String index(@RequestParam String message , @RequestParam Integer to) {
MessagesReqBody messagesReqBody = new MessagesReqBody();
messagesReqBody.setMessage(message);
messagesReqBody.setTo(to);
messagesReqBody.setFrom(0);
messagesReqBody.setCts(new Date());
//FeignClient调用
messagesService.sendMessage(messagesReqBody);
HttpEntity<MessagesReqBody> httpEntity = new HttpEntity<MessagesReqBody>(messagesReqBody, new HttpHeaders());
String providerMsg = restTemplate.exchange("http://CLOUD-PROVIDER/sendMessage", HttpMethod.POST ,httpEntity ,String.class,new HashMap<>()).getBody();
logger.info("message:{}" , messagesReqBody.toString());
return "message ok";
}
}