/**
* @program RestTemplate
* @description: 服务消费者
* @author: zhoubiao
* @create: 2019/07/29 08:33
*/
package cn.com.scitc.consumer;
import cn.com.scitc.domain.UserDTO;
import org.apache.commons.lang.text.StrBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.*;
@RestController
public class SayUseHelloController {
@Autowired
RestTemplate restTemplate;
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/hello")
public String hello(String name) {
//拿到服务提供商
List<ServiceInstance> list = discoveryClient.getInstances("provider");
//拿到第一个实例
ServiceInstance instance = list.get(0);
//得到主机号
String host = instance.getHost();
//得到端口号
int port = instance.getPort();
//拼接完整的请求url
String url = "http://" + host + ":" + port + "/hello?name={1}";
//restTemple 实际返回的是一个ResponseEntity 的实例
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, name);
StrBuilder sb = new StrBuilder();
//拿到状态码
HttpStatus statusCode = responseEntity.getStatusCode();
//拿到响应体
String body= responseEntity.getBody();
sb.append("statusCode:")
.append(statusCode)
.append("<br/>")
.append("body:")
.append(body)
.append("<br/>");
//拿到响应头header
HttpHeaders headers = responseEntity.getHeaders();
Set<String> keySet = headers.keySet();
for (String s : keySet) {
sb.append(s)
.append(":")
.append(headers.get(s))
.append("</br>");
}
return sb.toString();
}
@GetMapping("/hello2")
public String hello2(String name) {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
String url = "http://" + host + ":" + port + "/hello?name={name}";
Map<String,Object> map = new HashMap<>();
map.put("name",name);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, map);
StringBuffer sb = new StringBuffer();
HttpStatus statusCode = responseEntity.getStatusCode();
String body = responseEntity.getBody();
sb.append("statusCode:")
.append(statusCode)
.append("</br>")
.append("body:")
.append(body)
.append("</br>");
HttpHeaders headers = responseEntity.getHeaders();
Set<String> keyset = headers.keySet();
for (String s : keyset) {
sb.append(s)
.append(":")
.append(headers.get(s))
.append("</br>");
}
return sb.toString();
}
@GetMapping("/hello3")
public String hello3(String name) throws UnsupportedEncodingException {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
String url = "http://" + host + ":" + port + "/hello?name=" + URLEncoder.encode(name, "UTF-8");
URI uri = URI.create(url);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(uri,String.class);
StringBuffer sb = new StringBuffer();
String body = responseEntity.getBody();
HttpStatus statusCode = responseEntity.getStatusCode();
HttpHeaders headers = responseEntity.getHeaders();
sb.append("statusCode:")
.append(statusCode)
.append("</br>")
.append("body:")
.append(body)
.append("</br>");
Set<String> keySet = headers.keySet();
for (String s : keySet) {
sb.append(s)
.append(":")
.append(headers.get(s))
.append("</br>");
}
return sb.toString();
}
@GetMapping("/hello4")
public String hello4(String name) throws UnsupportedEncodingException {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
String url = "http://" + host + ":" + port + "/hello?name=" + URLEncoder.encode(name, "UTF-8");
URI uri = URI.create(url);
String s = restTemplate.getForObject(uri, String.class);
return s;
}
/**
* 可以使用 MultiValueMap来传递name
* @param MultiValueMap
* @return
*/
@GetMapping("/hello5")
public String hello5(String name) {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
String url = "http://" + host + ":" + port + "/hello2";
MultiValueMap map = new LinkedMultiValueMap();
map.add("name", name);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class);
return responseEntity.getBody();
}
/**
* postForEntity的第二个参数可以为空
* @param name
* @return
*/
@GetMapping("/hello6")
public String hello6(String name) {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
String url = "http://" + host + ":" + port + "/hello2?name={1}";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, null,String.class, name);
return responseEntity.getBody();
}
@PostMapping("/hello7")
public ResponseEntity<UserDTO> hello7(@RequestBody UserDTO userDTO) {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
String url = "http://" + host + ":" + port + "/user";
UserDTO u = new UserDTO();
u.setNickname(userDTO.getNickname());
u.setAddress(userDTO.getAddress());
ResponseEntity<UserDTO> responseEntity = restTemplate.postForEntity(url, u, UserDTO.class);
return new ResponseEntity<UserDTO>(responseEntity.getBody(),HttpStatus.OK);
}
@GetMapping("/hello8")
public String hello8() {
List<ServiceInstance> list = discoveryClient.getInstances("provider");
ServiceInstance instance = list.get(0);
String host = instance.getHost();
int port = instance.getPort();
没有合适的资源?快使用搜索试试~ 我知道了~
《springcloud&学习资料》--springboot 2.x 整合springcloud的学习.zip
共610个文件
java:182个
xml:130个
yml:49个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 176 浏览量
2024-04-19
08:58:53
上传
评论
收藏 2.31MB ZIP 举报
温馨提示
个人花大量时间整理出的实战资料,内容丰富,文档也很详细。无论做毕业设计还是用于学习技能,或工作中当做参考资料,都能发挥重要作用 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源哦~ 个人花大量时间整理出的实战资料,内容丰富,文档也很详细。无论做毕业设计还是用于学习技能,或工作中当做参考资料,都能发挥重要作用 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源哦~ 个人花大量时间整理出的实战资料,内容丰富,文档也很详细。无论做毕业设计还是用于学习技能,或工作中当做参考资料,都能发挥重要作用 亲们下载我任何一个付费资源后,即可私信联系我免费下载其他相关资源哦~
资源推荐
资源详情
资源评论
收起资源包目录
《springcloud&学习资料》--springboot 2.x 整合springcloud的学习.zip (610个子文件)
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
mvnw.cmd 6KB
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 333B
.gitignore 278B
login.html 350B
index.html 171B
index.html 171B
gateway-filter-AddRequestParameter.iml 336B
cloudConfig.iml 80B
cloud-Config-fwh.iml 80B
gateway-gaoji.iml 80B
common.iml 80B
Feign.iml 80B
commons.iml 80B
Loadbalancer.iml 80B
共 610 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
季风泯灭的季节
- 粉丝: 2029
- 资源: 3370
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 毕设和企业适用springboot社交互动平台类及健康数据分析系统源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及共享经济平台源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及大数据实时处理系统源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及城市智能管理系统源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及城市智能运营平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及企业IT解决方案平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及企业供应链平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及健康数据分析系统源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及交通信息平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及金融交易平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及健身管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及健康管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及企业数字资产管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及视觉识别平台源码+论文+视频.zip
- 毕设和企业适用springboot人力资源管理类及企业云管理平台源码+论文+视频.zip
- 毕设和企业适用springboot社交互动平台类及客户服务平台源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功