SpringCloud 虚拟化服务 - Hoverfly

原理图

在这里插入图片描述

安装 hoverfly

Hoverfly的安装

Spring Initializr

在这里插入图片描述

新建真实服务

package com.example.hoverflydemo;
 
import java.util.Date;
import java.util.UUID;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@SpringBootApplication
public class HoverflyActualServiceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(HoverflyActualServiceApplication.class, args);
    }
}
 
@RestController
class MyRestController {
 
    @RequestMapping(value = "/service/hoverfly")
    public HoverflyServiceResponse getSampleResponse() {
        System.out.println("Inside HoverflyActualServiceApplication::getSampleResponse()");
        return new HoverflyServiceResponse("returned value from HoverflyActualServiceApplication", new Date().toString(), UUID.randomUUID().toString());
    }
}
 
class HoverflyServiceResponse {
    private String message;
    private String responseTime;
    private String transactionid;
 
    public HoverflyServiceResponse(String message, String responseTime, String transactionid) {
        super();
        this.message = message;
        this.responseTime = responseTime;
        this.transactionid = transactionid;
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
 
    public String getResponseTime() {
        return responseTime;
    }
 
    public void setResponseTime(String responseTime) {
        this.responseTime = responseTime;
    }
 
    public String getTransactionid() {
        return transactionid;
    }
 
    public void setTransactionid(String transactionid) {
        this.transactionid = transactionid;
    }
}

更改运行端口:

server.port=9080

浏览器访问:
在这里插入图片描述

创建 Proxy 服务

1.和创建真实服务一样的步骤,创建一个 proxy 服务。
2.新建 `HoverflyActualServiceClientApplication 类文件:

package com.example.hoverflyactualserviceclient;
 
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
@SpringBootApplication
public class HoverflyActualServiceClientApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(HoverflyActualServiceClientApplication.class, args);
    }
}
 
@RestController
class TestController {
 
    private static final int HOVERFLY_PORT = 8500;
    private static final String HOVERFLY_HOST = "localhost";
    private static final String PROXY = "proxy";
 
    @RequestMapping("/invoke")
    public String invoke() {
        System.out.println("inside TestController::invoke()");
        String url = "http://localhost:9080/service/hoverfly";
        String response = restTemplate.exchange(url, HttpMethod.GET, null,
                new ParameterizedTypeReference<String>() {
                }).getBody();
        System.out.println("Actual Response : " + response);
        return response;
    }
 
    @Bean
    public RestTemplate restTemplate() {
        String mode = System.getProperty("mode");
        if (!PROXY.equals(mode)){
            return new RestTemplate();
        }

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(HOVERFLY_HOST, HOVERFLY_PORT));
        requestFactory.setProxy(proxy);

        return new RestTemplate(requestFactory);
    }
 
    @Autowired
    RestTemplate restTemplate;
}

设置运行端口:

server.port=8080

运行参数增加 mode
在这里插入图片描述

启动 Hoverfly

在这里插入图片描述
设置:
在这里插入图片描述
访问 http://localhost:8080/invoke 多次,然后浏览器访问:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值