@Override public void onError(Throwable t) { if (LOG.isErrorEnabled()) { String message = getErrorMessage(discoveryService, t); LOG.error(message, t); } if (registration.isFailFast() && instance instanceof EmbeddedServerInstance) { ((EmbeddedServerInstance) instance).getEmbeddedServer().stop(); } }
@Override public void randomize(List<String> urlList) { int listSize = 0; if (urlList != null) { listSize = urlList.size(); } if ((instanceInfo == null) || (listSize == 0)) { return; } // Find the hashcode of the instance hostname and use it to find an entry // and then arrange the rest of the entries after this entry. int instanceHashcode = instanceInfo.getHost().hashCode(); if (instanceHashcode < 0) { instanceHashcode = instanceHashcode * -1; } int backupInstance = instanceHashcode % listSize; for (int i = 0; i < backupInstance; i++) { String zone = urlList.remove(0); urlList.add(zone); } } }
@Override protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) { if (LOG.isDebugEnabled()) { LOG.debug("Server {}:{} Received Request: {} {}", embeddedServer.getHost(), embeddedServer.getPort(), msg.method(), msg.uri()); } out.add(new NettyHttpRequest<>(msg, ctx, conversionService, configuration)); } }
@Test public void testRetrieveWithPOJO() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::jsonpojo[] Flowable<Message> response = client.retrieve( GET("/greet/John"), Message.class ); assertEquals( "Hello John", response.blockingFirst().getText() ); // end::jsonpojo[] embeddedServer.stop(); client.stop(); }
@BeforeClass public static void setupServer() { System.setProperty(Environment.BOOTSTRAP_CONTEXT_PROPERTY, "true"); Map<String, Object> map = new HashMap<>(); map.put(MockSpringCloudConfigServer.ENABLED, true); map.put("micronaut.server.port", -1); map.put("spring.cloud.config.enabled", false); map.put("micronaut.environments", "dev,test"); springCloudServer = ApplicationContext.run(EmbeddedServer.class, map); server = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf( "micronaut.environments","test", "micronaut.application.name", "spring-config-sample", "micronaut.config-client.enabled", true, "spring.cloud.config.enabled", true, "spring.cloud.config.uri", springCloudServer.getURI() )); client = server .getApplicationContext() .createBean(HttpClient.class, LoadBalancer.fixed(server.getURL())); }
@Test public void testSimpleGet() { ApplicationContext applicationContext = ApplicationContext.run(); EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start(); HttpClient client = new DefaultHttpClient(server.getURL()); Flowable<HttpResponse<String>> flowable = Flowable.fromPublisher(client.exchange( HttpRequest.GET("/get/simple"), String.class )); HttpResponse<String> response = flowable.blockingFirst(); Assert.assertEquals(response.getStatus(), HttpStatus.OK); Optional<String> body = response.getBody(String.class); assertTrue(body.isPresent()); assertEquals(body.get(), "success"); client.stop(); applicationContext.stop(); }
@Test public void testPostPetValidation() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class); // tag::error[] thrown.expect(HttpClientResponseException.class); thrown.expectMessage("age: must be greater than or equal to 1"); client.save("Fred", -1).blockingGet(); // end::error[] embeddedServer.stop(); } }
@Test public void testSenderAttributes() throws Exception { try(EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class)) { StoryClient client = embeddedServer.getApplicationContext().getBean(StoryClient.class); StoryClientFilter filter = embeddedServer.getApplicationContext().getBean(StoryClientFilter.class); Story story = client.getById("jan2019").blockingGet(); Assert.assertNotNull(story); Map<String, Object> attributes = filter.getLatestRequestAttributes(); Assert.assertNotNull(attributes); Assert.assertEquals("jan2019", attributes.get("story-id")); Assert.assertEquals("storyClient", attributes.get("client-name")); Assert.assertEquals("1", attributes.get("version")); } } }
@Override public Collection<FunctionDefinition> getFunctionDefinitions() { if (!embeddedServer.isRunning()) { return Collections.emptyList(); } Map<String, URI> availableFunctions = localFunctionRegistry.getAvailableFunctions(); return availableFunctions.entrySet().stream().map((Function<Map.Entry<String, URI>, FunctionDefinition>) stringURIEntry -> new FunctionDefinition() { @Override public String getName() { return stringURIEntry.getKey(); } @Override public Optional<URI> getURI() { return Optional.of(embeddedServer.getURI().resolve(stringURIEntry.getValue())); } }).collect(Collectors.toList()); } }
@Test public void testRetrieveWithHeaders() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::headers[] Flowable<String> response = client.retrieve( GET("/hello/John") .header("X-My-Header", "SomeValue") ); // end::headers[] assertEquals( "Hello John", response.blockingFirst() ); embeddedServer.stop(); client.stop(); }
@Test public void testGetPojoList() { ApplicationContext applicationContext = ApplicationContext.run(); EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start(); HttpClient client = applicationContext.createBean(HttpClient.class, server.getURL()); Flowable<HttpResponse<List>> flowable = Flowable.fromPublisher(client.exchange( HttpRequest.GET("/get/pojoList"), Argument.of(List.class, HttpGetSpec.Book.class) )); HttpResponse<List> response = flowable.blockingFirst(); assertEquals(response.getStatus(), HttpStatus.OK); Optional<List> body = response.getBody(); assertTrue(body.isPresent()); List<HttpGetSpec.Book> list = body.get(); assertEquals(list.size(), 1); assertTrue(list.get(0) instanceof HttpGetSpec.Book); client.stop(); applicationContext.stop(); } }
@Test public void testPostPet() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class); // tag::post[] Pet pet = client.save("Dino", 10).blockingGet(); assertEquals("Dino", pet.getName()); assertEquals(10, pet.getAge()); // end::post[] embeddedServer.stop(); }
@Override public void onError(Throwable t) { if (LOG.isErrorEnabled()) { LOG.error("Error registering instance with AWS:" + t.getMessage(), t); } if (route53AutoRegistrationConfiguration.isFailFast() && instance instanceof EmbeddedServerInstance) { LOG.error("Error registering instance with AWS and Failfast is set: stopping instance"); ((EmbeddedServerInstance) instance).getEmbeddedServer().stop(); } }
@Test public void testSenderHeaders() throws Exception { Map<String,Object> config =Collections.singletonMap( "pet.client.id", "11" ); try(EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, config)) { PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class); Pet pet = client.get("Fred").blockingGet(); Assert.assertNotNull(pet); Assert.assertEquals(pet.getAge(), 11); } } }
@Test public void testSimpleRetrieve() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::simple[] String uri = UriBuilder.of("/hello/{name}") .expand(Collections.singletonMap("name", "John")) .toString(); assertEquals("/hello/John", uri); String result = client.toBlocking().retrieve(uri); assertEquals( "Hello John", result ); // end::simple[] embeddedServer.stop(); client.stop(); }
LOG.error("Error registering instance shutting down instance because failfast is set."); ((EmbeddedServerInstance) embeddedServerInstance).getEmbeddedServer().stop();
@Test public void testClientAnnotationStreaming() throws Exception { try( EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class) ) { HeadlineClient headlineClient = embeddedServer .getApplicationContext() .getBean(HeadlineClient.class); Event<Headline> headline = headlineClient.streamHeadlines().blockFirst(); assertNotNull( headline ); assertTrue( headline.getData().getText().startsWith("Latest Headline") ); } } // end::streamingClient[]
@Test public void testRetrieveWithJSON() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::jsonmap[] Flowable<Map> response = client.retrieve( GET("/greet/John"), Map.class ); // end::jsonmap[] assertEquals( "Hello John", response.blockingFirst().get("text") ); // tag::jsonmaptypes[] response = client.retrieve( GET("/greet/John"), Argument.of(Map.class, String.class, String.class) // <1> ); // end::jsonmaptypes[] assertEquals( "Hello John", response.blockingFirst().get("text") ); embeddedServer.stop(); client.stop(); }
@Test public void testClientAnnotationStreaming() throws Exception { try( EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class) ) { HeadlineClient headlineClient = embeddedServer .getApplicationContext() .getBean(HeadlineClient.class); // <1> Maybe<Headline> firstHeadline = headlineClient.streamHeadlines().firstElement(); // <2> Headline headline = firstHeadline.blockingGet(); // <3> assertNotNull( headline ); assertTrue( headline.getText().startsWith("Latest Headline") ); } } // end::streamingClient[]