1、启动类代码

package com.tycoon.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /**
* Title: ServerConsume1Application
* Description: 服务启动类
*
* @author tycoon
* @version 1.0.0
* @date 2019-02-26 10:10
*/
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}

2、 application.xml文件

spring:
application:
name: client-request
server:
port: 7777 #访问项目端口
servlet:
context-path: / #访问项目名称
tomcat:
uri-encoding: UTF-8
logging:
level:
root: INFO
org.springframework.cloud.sleuth: DEBUG
main:
allow-bean-definition-overriding: true
cloud:
consul:
discovery:
instance-id: ${spring.application.name}:${server.port}
prefer-ip-address: true
health-check-interval: 10s #心跳检查时间间隔
hostname: ${spring.application.name}
service-name: ${spring.application.name}
enabled: true
health-check-path: /health #心跳检查
host: 127.0.0.1
port: 8500

3、 测试类代码

package com.tycoon.service;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; /**
* Title: ServiceDemoApplicationTests
* Description: 服务启动类
*
* @author tycoon
* @version 1.0.0
* @date 2019-02-26 10:10
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceDemoApplicationTests { @Test
public void doGetTestOne() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//设置日期格式
String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳 System.out.println(date); // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();

// 说明service-provider 为服务提供者,card为服务controller接口,cardId=123456 说的 传入参数
String strUrl = "http://localhost:9992/api/service-provider/card?cardId=123456&accessToken=1222"; // 创建Get请求
HttpGet httpGet = new HttpGet(strUrl); // 响应模型
CloseableHttpResponse response = null;
try {
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
String date1 = df.format(new Date());
System.out.println("响应状态为:" + response.getStatusLine()+" ,当前时间:"+date1); System.out.println();
if (responseEntity != null) {
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

4、pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository northeasttycoon -->
</parent>
<groupId>com.tycoon</groupId>
<artifactId>client-request</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M7</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.15</version>
</dependency>
<dependency>
<groupId>com.squareup.okttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.ning</groupId>-->
<!--<artifactId>asy-http-client</artifactId>-->
<!--<version>1.9.31</version>-->
<!--</dependency>-->
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

5、 运行后结果为:

okhttp 通过网关请求服务端返回数据的更多相关文章

  1. 基于NIO的同步非阻塞编程完整案例,客户端发送请求,服务端获取数据并返回给客户端数据,客户端获取返回数据

    这块还是挺复杂的,挺难理解,但是多练几遍,多看看研究研究其实也就那样,就是一个Selector轮询的过程,这里想要双向通信,客户端和服务端都需要一个Selector,并一直轮询, 直接贴代码: Ser ...

  2. ajax跨域POST时执行OPTIONS请求服务端返回403forbidden的解决方法

    ajax访问服务端restful api时,由于contentType类型的原因,浏览器会先发送OPTIONS请求. 本人服务端用的是spring mvc框架,web服务器用的是tomcat的,以下给 ...

  3. 使用Fiddler伪造服务端返回数据,绕过软件试用期验证

    用过一款和visual studio集成非常好的移动端模拟器,有7天的试用期,可惜不支持国内支付,试用到期了怎么办,不想重装系统. 昨天看有人破解admin page,于是尝试自己动手试试,因为这款模 ...

  4. 【教程】【FLEX】#002 请求服务端数据(UrlLoader)

    为什么Flex需要请求服务端读取数据,而不是自己读取? Flex 是一门界面语言,主要是做界面展示的,它能实现很多绚丽的效果,这个是传统Web项目部能比的. 但是它对数据库和文件的读写 没有良好的支持 ...

  5. android菜鸟学习笔记25----与服务器端交互(二)解析服务端返回的json数据及使用一个开源组件请求服务端数据

    补充:关于PHP服务端可能出现的问题: 如果你刚好也像我一样,用php实现的服务端程序,采用的是apache服务器,那么虚拟主机的配置可能会影响到android应用的调试!! 在android应用中访 ...

  6. android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据

    主要是基于HTTP协议与服务端进行交互. 涉及到的类和接口有:URL.HttpURLConnection.HttpClient等 URL: 使用一个String类型的url构造一个URL对象,如: U ...

  7. fastjson解析服务端返回的数据

    1.配置依赖 //fastjson api 'com.alibaba:fastjson:1.2.44' 2.设计服务端返回的数据 {},{},{}]} 3.编写bean类,特别注意,要和服务端返回的类 ...

  8. js插件---WebUploader 如何接收服务端返回的数据

    js插件---WebUploader 如何接收服务端返回的数据 一.总结 一句话总结: uploadSuccess有两个参数,一个是file(上传的文件信息),一个是response(服务器返回的信息 ...

  9. ionic3使用@angular/http 访问nodejs(koa2框架)服务不能返回数据

    cordova的http插件不能使用在browser上,所以当需要在browser上浏览时,需要使用@angular/http 里的方法来访问nodejs服务. 如果出现服务端能够接收请求并相应,而客 ...

随机推荐

  1. Python测试Kafka集群(pykafka)

    生产者代码: # -* coding:utf8 *- from pykafka import KafkaClient host = 'IP:9092, IP:9092, IP:9092' client ...

  2. 支付宝支付系统繁忙,请稍后再试(ALI64)错误解决

    解决方法:将商户支付參数的seller邮箱换成与partner同样的数字串,依然无法支付请检查所给參数

  3. docker集群——Mesos集群下的负载均衡marathon-lb

    前面的章节介绍了Mesos+Zookeeper+Marathon的Docker管理平台,接下来介绍如何在该平台下构建负载均衡. 默认情况下,mesos marathon会把app发布到随机节点的随机端 ...

  4. 大话项目管理工具之Jira篇

    前言 上一篇文章谈的是知识管理工具 -- Confluence,它来自澳大利亚 Atlassian 公司. 非常凑巧的是,今天要介绍的 JIRA 也是来自 Atlassian 公司的.但他不再是知识管 ...

  5. wampServer(windows、apache、mysql、php)

    wampServer(windows/apche/mysql/php)集成环境 在线状态:区域网内可以访问 离线状态:本地设备可以访问 自拟定网站根目录: Apache -- httpd.conf - ...

  6. 在Ubuntu 12.04上配置iSCSI Target服务

      今天自己按照网上搜来的教程自己在Ubuntu 12.04上配置了iSCSI Target服务,在这里简单地做个纪录.操作系统是全新安装的Ubuntu 12.04,配置一块500 GB的SATA笔记 ...

  7. MySql常用函数数学函数、加密函数等(转—收藏)

        MySql函数众多,这里只是列举了一部分常用的函数.   一.数学函数 ABS(x)                                         // 返回x的绝对值 BI ...

  8. Linux 平台如何查看某个进程的线程数?

    Linux 平台如何查看某个进程的线程数?   三种方法:1. 使用top命令,具体用法是 top -H 加上这个选项,top的每一行就不是显示一个进程,而是一个线程. 2. 使用ps命令,具体用法是 ...

  9. 牛散NO.3:MACD放之四海 假作真时真亦假

    大宗商品日线“异曲同工夺命勾魂枪” 话说有实战意义的技术在任何资本市场里都能产生出神奇的效果.不能说放之四海皆准,但至少起到触类旁通的“牵强”吧.大宗商品特别是在国际市场交易的大宗 商品由于是来自各方 ...

  10. Android 四大组件学习之BroadcastReceiver二

    上节学习了怎样创建一个广播.也尝试接受系统打电话的广播. 本节课学习怎样自己定义广播.自己定义广播实质上也就是创建一个发送广播者,创建一个接受该广播者. 那我们就開始行动吧. 先创建一个发送广播的应用 ...