java eureka_java – 使用eureka和功能区的测试服务

以下代码(从

https://github.com/Netflix/eureka/blob/a7a8d278e6399bbff5faa49b9fcbcd7ea9e854f4/eureka-core/src/test/java/com/netflix/eureka/mock/MockRemoteEurekaServer.java开始)可能对您有所帮助;

public class MockRemoteEurekaServer extends ExternalResource {

public static final String EUREKA_API_BASE_PATH = "/eureka/v2/";

private final Map applicationMap;

private final Map applicationDeltaMap;

private final Server server;

private boolean sentDelta;

private int port;

private volatile boolean simulateNotReady;

public MockRemoteEurekaServer(int port, Map applicationMap,

Map applicationDeltaMap) {

this.applicationMap = applicationMap;

this.applicationDeltaMap = applicationDeltaMap;

ServletHandler handler = new AppsResourceHandler();

EurekaServerConfig serverConfig = new DefaultEurekaServerConfig();

EurekaServerContext serverContext = mock(EurekaServerContext.class);

when(serverContext.getServerConfig()).thenReturn(serverConfig);

handler.addFilterWithMapping(ServerRequestAuthFilter.class, "/*", 1).setFilter(new ServerRequestAuthFilter(serverContext));

handler.addFilterWithMapping(RateLimitingFilter.class, "/*", 1).setFilter(new RateLimitingFilter(serverContext));

server = new Server(port);

server.addHandler(handler);

System.out.println(String.format(

"Created eureka server mock with applications map %s and applications delta map %s",

stringifyAppMap(applicationMap), stringifyAppMap(applicationDeltaMap)));

}

@Override

protected void before() throws Throwable {

start();

}

@Override

protected void after() {

try {

stop();

} catch (Exception e) {

Assert.fail(e.getMessage());

}

}

public void start() throws Exception {

server.start();

port = server.getConnectors()[0].getLocalPort();

}

public void stop() throws Exception {

server.stop();

}

public boolean isSentDelta() {

return sentDelta;

}

public int getPort() {

return port;

}

public void simulateNotReady(boolean simulateNotReady) {

this.simulateNotReady = simulateNotReady;

}

private static String stringifyAppMap(Map applicationMap) {

StringBuilder builder = new StringBuilder();

for (Map.Entry entry : applicationMap.entrySet()) {

String entryAsString = String.format("{ name : %s , instance count: %d }", entry.getKey(),

entry.getValue().getInstances().size());

builder.append(entryAsString);

}

return builder.toString();

}

private class AppsResourceHandler extends ServletHandler {

@Override

public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)

throws IOException, ServletException {

if (simulateNotReady) {

response.setStatus(HttpServletResponse.SC_FORBIDDEN);

return;

}

String authName = request.getHeader(AbstractEurekaIdentity.AUTH_NAME_HEADER_KEY);

String authVersion = request.getHeader(AbstractEurekaIdentity.AUTH_VERSION_HEADER_KEY);

String authId = request.getHeader(AbstractEurekaIdentity.AUTH_ID_HEADER_KEY);

Assert.assertNotNull(authName);

Assert.assertNotNull(authVersion);

Assert.assertNotNull(authId);

Assert.assertTrue(!authName.equals(ServerRequestAuthFilter.UNKNOWN));

Assert.assertTrue(!authVersion.equals(ServerRequestAuthFilter.UNKNOWN));

Assert.assertTrue(!authId.equals(ServerRequestAuthFilter.UNKNOWN));

for (FilterHolder filterHolder : this.getFilters()) {

filterHolder.getFilter().doFilter(request, response, new FilterChain() {

@Override

public void doFilter(ServletRequest request, ServletResponse response)

throws IOException, ServletException {

// do nothing;

}

});

}

String pathInfo = request.getPathInfo();

System.out.println(

"Eureka resource mock, received request on path: " + pathInfo + ". HTTP method: |" + request

.getMethod() + '|');

boolean handled = false;

if (null != pathInfo && pathInfo.startsWith("")) {

pathInfo = pathInfo.substring(EUREKA_API_BASE_PATH.length());

if (pathInfo.startsWith("apps/delta")) {

Applications apps = new Applications();

for (Application application : applicationDeltaMap.values()) {

apps.addApplication(application);

}

apps.setAppsHashCode(apps.getReconcileHashCode());

sendOkResponseWithContent((Request) request, response, toJson(apps));

handled = true;

sentDelta = true;

} else if (pathInfo.startsWith("apps")) {

Applications apps = new Applications();

for (Application application : applicationMap.values()) {

apps.addApplication(application);

}

apps.setAppsHashCode(apps.getReconcileHashCode());

sendOkResponseWithContent((Request) request, response, toJson(apps));

handled = true;

}

}

if (!handled) {

response.sendError(HttpServletResponse.SC_NOT_FOUND,

"Request path: " + pathInfo + " not supported by eureka resource mock.");

}

}

private void sendOkResponseWithContent(Request request, HttpServletResponse response, String content)

throws IOException {

response.setContentType("application/json; charset=UTF-8");

response.setStatus(HttpServletResponse.SC_OK);

response.getOutputStream().write(content.getBytes("UTF-8"));

response.getOutputStream().flush();

request.setHandled(true);

System.out.println("Eureka resource mock, sent response for request path: " + request.getPathInfo() +

" with content" + content);

}

}

private String toJson(Applications apps) throws IOException {

return new EurekaJsonJacksonCodec().getObjectMapper(Applications.class).writeValueAsString(apps);

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值