org.springframework.cloud.netflix.hystrix.HystrixCommands Java Examples

The following examples show how to use org.springframework.cloud.netflix.hystrix.HystrixCommands. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReservationClientApplication.java    From bootiful-reactive-microservices with Apache License 2.0 7 votes vote down vote up
@Bean
RouterFunction<ServerResponse> routes(WebClient client) {
		return route(GET("/reservations/names"), r -> {

				Publisher<String> map = client
					.get()
					.uri("http://reservation-service/reservations")
					.retrieve()
					.bodyToFlux(Reservation.class)
					.map(Reservation::getEmail);

				Publisher<String> fallback =
					HystrixCommands
						.from(map)
						.fallback(Mono.just("EEK!"))
						.commandName("fallback")
						.eager()
						.build();

				return ServerResponse.ok().body(fallback, String.class);
		});
}
 
Example #2
Source File: ReservationClientApplication.java    From bootiful-reactive-microservices with Apache License 2.0 6 votes vote down vote up
@Bean
RouterFunction<ServerResponse> adapter(ReservationClient client) {
	return route(GET("/reservations/names"), request -> {

		Flux<String> names = client
			.getAllReservations()
			.map(Reservation::getName);

		Publisher<String> fallback = HystrixCommands
			.from(names)
			.eager()
			.commandName("names")
			.fallback(Flux.just("EEK!"))
			.build();

		return ServerResponse.ok().body(fallback, String.class);
	});
}
 
Example #3
Source File: ReservationClientApplication.java    From training with Apache License 2.0 5 votes vote down vote up
@Bean
RouterFunction<ServerResponse> routes(WebClient client,
																																						Source src) {
		return route(GET("/reservations/names"), req -> {

				Flux<String> names = client
					.get()
					.uri("http://reservation-service/reservations")
					.retrieve()
					.bodyToFlux(Reservation.class)
					.map(Reservation::getReservationName);

				Publisher<String> fallback = HystrixCommands
					.from(names)
					.commandName("reservation-names")
					.fallback(Flux.just("EEK!"))
					.eager()
					.build();

				return ServerResponse.ok().body(fallback, String.class);
		})
			.andRoute(POST("/reservations"), req -> {
						Flux<Boolean> sendResult = req.bodyToFlux(Reservation.class)
							.map(Reservation::getReservationName)
							.map(r -> MessageBuilder.withPayload(r).build())
							.map(msg -> src.output().send(msg));
						return ServerResponse.ok().body(sendResult, Boolean.class);
				}
			);
}
 
Example #4
Source File: ReservationClientApplication.java    From training with Apache License 2.0 5 votes vote down vote up
@Bean
RouterFunction<ServerResponse> routes(WebClient client,
                                      Source src) {
	return route(GET("/reservations/names"), req -> {

		Flux<String> names = client
				.get()
				.uri("http://reservation-service/reservations")
				.retrieve()
				.bodyToFlux(Reservation.class)
				.map(Reservation::getReservationName);

		Publisher<String> fallback = HystrixCommands
				.from(names)
				.commandName("reservation-names")
				.fallback(Flux.just("EEK!"))
				.eager()
				.build();

		return ServerResponse.ok().body(fallback, String.class);
	})
			.andRoute(POST("/reservations"), req -> {
						Flux<Boolean> sendResult = req.bodyToFlux(Reservation.class)
								.map(Reservation::getReservationName)
								.map(r -> MessageBuilder.withPayload(r).build())
								.map(msg -> src.output().send(msg));
						return ServerResponse.ok().body(sendResult, Boolean.class);
					}
			);
}
 
Example #5
Source File: ReservationClientApplication.java    From bootiful-reactive-microservices with Apache License 2.0 5 votes vote down vote up
@Bean
	RouterFunction<ServerResponse> routes(ReservationClient client) {
		return route(GET("/reservations/names"), serverRequest -> {

			Flux<String> names = client
				.getAllReservations()
				.map(Reservation::getName);

			Publisher<String> cb = HystrixCommands
				.from(names)
				.eager()
				.commandName("names")
				.fallback(Flux.just("EEK!"))
				.build();


		/*	// hedging
			WebClient wc = null; ///
			DiscoveryClient dc = null; ///
			List<ServiceInstance> instances = dc.getInstances("foo-service");
			if (instances.size() >= 3) {
				List<ServiceInstance> serviceInstances = instances.subList(0, 3);

				Flux<Reservation> callThreeServicesAtTheSameTime = Flux
					.fromStream(serviceInstances.stream())
					.map(si -> si.getHost() + ':' + si.getPort())
					.flatMap(uri -> wc.get().uri(uri).retrieve().bodyToFlux(Reservation.class));

				Flux<Reservation> first = Flux.first(callThreeServicesAtTheSameTime);

			}
*/
			return ServerResponse.ok().body(cb, String.class);
		});
	}
 
Example #6
Source File: ReservationClientApplication.java    From bootiful-reactive-microservices with Apache License 2.0 5 votes vote down vote up
@Bean
RouterFunction<ServerResponse> routes(ReservationClient client
	/* WebClient client */
) {
	return route(GET("/reservations/names"), request -> {

		/*
			Flux<String> stringFlux = client
				.get()
				.uri("http://reservation-service/reservations")
				.retrieve()
				.bodyToFlux(Reservation.class)
				.map(Reservation::getReservationName);
		*/

		Flux<String> allReservations = client
			.getAllReservations()
			.map(Reservation::getReservationName);

		Publisher<String> namesFallback =
			HystrixCommands
				.from(allReservations)
				.eager()
				.commandName("names")
				.fallback(Flux.just("EEK!"))
				.build();

		return ServerResponse.ok().body(namesFallback, String.class);
	});
}