okhttp 通过网关请求服务端返回数据

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、 运行后结果为:

 

原文地址:https://www.cnblogs.com/northeastTycoon/p/10573029.html