14

मैं नेटफ्लिक्स स्टैक और वसंत बूट के साथ माइक्रोस्कोप का उपयोग कर एक एप्लिकेशन बना रहा हूं। एक चीज जो मुझे बग करती है कि मेरे पास अभी तक कोई एकीकरण परीक्षण नहीं है, जहां मैं आसपास की सेवाओं का नकल कर सकता हूं।परीक्षण सेवा जो यूरेका और रिबन का उपयोग करती है

तो, मेरे पास सेवा ए है जो कॉल के दौरान एक पंजीकृत सेवा बी के यूआरएल में यूरेका नाम को हल करने के लिए रिबन के साथ एक यूरेका क्लाइंट है।

तो आदर्श रूप से मैं वसंत बूट की एकीकरण की टिप्पणियों के साथ एप्लिकेशन शुरू करना चाहता हूं, सेवा बी को अनुकरण करने के लिए वायरमॉक का उपयोग करना चाहता हूं और फिर सेवा ए की विधि को कॉल करना चाहता हूं, इसे सेवा के प्रतीकात्मक नाम का उपयोग करके मेरी मॉक सेवा बी को कॉल करना चाहिए ।

क्या कोई इसे पहले ही हल कर चुका है? मैंने पहले से ही ऐसा करने वाले लोगों की ब्लॉग प्रविष्टियों आदि की खोज की है, लेकिन कोई भी नहीं मिला ...

मुझे SO लेख Mock an Eureka Feign Client for Unittesting के बारे में पता है, लेकिन जहां तक ​​मैं इसे देख सकता हूं, वैसे ही खोज क्लाइंट शिकायत से रोकता है।

+0

इस पोस्ट में कुछ अलग दृष्टिकोण की चर्चा: https://opencredo.com/working-locally-with-microservices/ – MarkOfHall

+0

लेख की तरह बुनियादी बातों के बारे में बात करती है केवल लग रहा है और एक उच्च स्तर में लिखा है। दृष्टिकोण बहुत स्पष्ट हैं, अगर मुझे वास्तव में ऐसा करने का अच्छा तरीका मिल गया तो मुझे अधिक दिलचस्पी है। दृष्टिकोण निंजा कोड बंदर का एक तरीका है, लेकिन यह अभी भी मुझे इंजीनियर को रिवर्स करने और "नकली यूरेका" बनाए रखने की आवश्यकता है ... –

उत्तर

5

निम्नलिखित कोड (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<String, Application> applicationMap; 
private final Map<String, Application> applicationDeltaMap; 
private final Server server; 
private boolean sentDelta; 
private int port; 
private volatile boolean simulateNotReady; 

public MockRemoteEurekaServer(int port, Map<String, Application> applicationMap, 
           Map<String, Application> 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<String, Application> applicationMap) { 
    StringBuilder builder = new StringBuilder(); 
    for (Map.Entry<String, Application> 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); 
} 

} 
+1

भविष्य में, स्पष्ट विशेषता के बिना कहीं और सामग्री की प्रतिलिपि न लें। इसे साहित्यिकता के रूप में देखा जाता है। Http://stackoverflow.com/help/referencing देखें – Matt

5

एक विकल्प यूरेका एंडपॉइंट्स को नकली/प्रतिस्थापित करने के लिए ऊंट का उपयोग करना होगा। आपके ऐप को यूरेका को देखने के लिए कॉन्फ़िगर होना चाहिए, इसलिए अपने टेस्ट कॉन्फ़िगरेशन में नए एंडपॉइंट को इंगित करने के लिए ओवरराइड करें।

फिर इस नए एंडपॉइंट का प्रतिनिधित्व करने के लिए जेटी या http का उपयोग करके परीक्षण/src में एक ऊंट मार्ग बनाएं, जो लोडबैंसर क्लाइंट की प्रतिक्रिया को वापस लौटाएगा। उस प्रतिक्रिया में यूआरआई परीक्षा के तहत होगा (यानी आपका आवेदन)।

संबंधित मुद्दे

 संबंधित मुद्दे