@Override public Optional<? extends LoadBalancer> resolve(String... serviceReferences) { if (ArrayUtils.isEmpty(serviceReferences) || StringUtils.isEmpty(serviceReferences[0])) { return Optional.empty(); } String reference = serviceReferences[0]; if (reference.startsWith("/")) { // current server reference if (beanContext.containsBean(EmbeddedServer.class)) { EmbeddedServer embeddedServer = beanContext.getBean(EmbeddedServer.class); URL url = embeddedServer.getURL(); return Optional.of(LoadBalancer.fixed(url)); } else { return Optional.empty(); } } else if (reference.indexOf('/') > -1) { try { URL url = new URL(reference); return Optional.of(LoadBalancer.fixed(url)); } catch (MalformedURLException e) { return Optional.empty(); } } else { reference = NameUtils.hyphenate(reference); return resolveLoadBalancerForServiceID(reference); } }
@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())); }
@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(); } }
@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)); } }
String applicationName = applicationConfiguration.getName().orElse(Environment.DEFAULT_NAME); boolean explicitInstanceId = instanceId != null; setPreferIpAddress(embeddedServer.getApplicationContext().get(PREFER_IP_ADDRESS, Boolean.class).orElse(false)); String serverHost = embeddedServer.getHost(); int serverPort = embeddedServer.getPort(); final boolean preferIpAddress = isPreferIpAddress(); if (ipAddress != null) {
@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(); }
@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(); } }
@BeforeClass public static void setupServer() { server = ApplicationContext.run(EmbeddedServer.class, Collections.singletonMap( "consul.client.enabled", false )); client = server .getApplicationContext() .createBean(HttpClient.class, server.getURL()); }
@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(); } }
NewCheck check = null; EmbeddedServerInstance embeddedServerInstance = (EmbeddedServerInstance) instance; ApplicationConfiguration applicationConfiguration = embeddedServerInstance.getEmbeddedServer().getApplicationConfiguration(); ApplicationConfiguration.InstanceConfiguration instanceConfiguration = applicationConfiguration.getInstance(); instanceConfiguration.getGroup().ifPresent(g -> { } else { URL serverURL = ((EmbeddedServerInstance) instance).getEmbeddedServer().getURL(); HTTPCheck httpCheck; try {
@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 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); } } }
@Nonnull @Override public String generateId(Environment environment, ServiceInstance serviceInstance) { Optional<String> cloudFoundryId = environment.getProperty("vcap.application.instance_id", String.class); if (cloudFoundryId.isPresent()) { return cloudFoundryId.get(); } else { StringJoiner joiner = new StringJoiner(":"); String applicationName = serviceInstance.getId(); joiner.add(applicationName); if (serviceInstance instanceof EmbeddedServerInstance) { EmbeddedServerInstance esi = (EmbeddedServerInstance) serviceInstance; Optional<String> id = esi.getEmbeddedServer().getApplicationConfiguration().getInstance().getId(); if (id.isPresent()) { joiner.add(id.get()); } else { joiner.add(String.valueOf(esi.getPort())); } } else { joiner.add(String.valueOf(serviceInstance.getPort())); } return joiner.toString(); } } }
@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(); }
@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 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(); }
@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 testPostInvalidFormData() { EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class); RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL()); // tag::postform[] Map<String, String> data = new LinkedHashMap<>(); data.put("title", "The Stand"); data.put("pages", "notnumber"); data.put("url", "noturl"); Flowable<HttpResponse<Book>> call = client.exchange( POST("/binding/book", data) .contentType(MediaType.APPLICATION_FORM_URLENCODED), Book.class ); // end::postform[] thrown.expect(HttpClientResponseException.class); thrown.expectMessage(CoreMatchers.startsWith("Failed to convert argument [pages] for value [notnumber]")); HttpResponse<Book> response = call.blockingFirst(); embeddedServer.stop(); client.stop(); } }
@Override public Optional<? extends LoadBalancer> resolve(String... serviceReferences) { if (ArrayUtils.isEmpty(serviceReferences) || StringUtils.isEmpty(serviceReferences[0])) { return Optional.empty(); } String reference = serviceReferences[0]; if (reference.startsWith("/")) { // current server reference if (beanContext.containsBean(EmbeddedServer.class)) { EmbeddedServer embeddedServer = beanContext.getBean(EmbeddedServer.class); URL url = embeddedServer.getURL(); return Optional.of(LoadBalancer.fixed(url)); } else { return Optional.empty(); } } else if (reference.indexOf('/') > -1) { try { URL url = new URL(reference); return Optional.of(LoadBalancer.fixed(url)); } catch (MalformedURLException e) { return Optional.empty(); } } else { reference = NameUtils.hyphenate(reference); return resolveLoadBalancerForServiceID(reference); } }