Commit 335be7ee authored by vikram singh's avatar vikram singh

separated the client and server

parent 23ea0734
......@@ -51,8 +51,10 @@ Run the Mongo shell, with the Mongo daemon running in one terminal, type ~/mongo
::::::::::::::::::::::::::::::::
Kafka installation::::
brew install kafka
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties
$ kafka-server-start /usr/local/etc/kafka/server.properties
kafka-server-start /usr/local/etc/kafka/server.properties
If we get any issue in running kafka server:
......
package com.nisum.webflux.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfig {
@Bean
@Qualifier("webclient")
@Scope("prototype")
public WebClient.Builder getWebClient() {
return WebClient.builder();
}
}
......@@ -89,15 +89,6 @@ public class FruitController {
return fruitService.getAll();
}
@GetMapping("/getAllByWebClient")
public Flux<Fruit> getAllByWebClient(){
return fruitService.getFruitsByWebClient();
}
@GetMapping("/saveFruitByWebClient")
public Mono<Fruit> saveFruitsByWebClient() {
return fruitService.saveFruitsByWebClient();
}
@PostMapping("/save")
public Mono<Fruit> savetoMongo(@RequestBody
......
......@@ -8,8 +8,6 @@ public interface IFruitService {
Mono<Fruit> save(Fruit fruit);
Flux<Fruit> getAll();
Flux<Fruit> getFruitsByWebClient();
Mono<Fruit> saveFruitsByWebClient();
Mono<Fruit> getFruitById(String id);
Mono<Fruit> getFruitByPriceAndName(String price, String name);
......
......@@ -21,12 +21,6 @@ import java.util.stream.Collectors;
@Service
@Slf4j
public class FruitService implements IFruitService {
private final WebClient webClient;
@Autowired
public FruitService( @Qualifier("webclient") final WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.build();
}
@Autowired
FruitRepository fruitRepository;
......@@ -41,41 +35,8 @@ public class FruitService implements IFruitService {
return fruitRepository.findAll().collect(Collectors.maxBy(Comparator.comparing(e->Integer.parseInt(e.getPrice())))).map(Optional::get);
}
@Override
public Flux<Fruit> getFruitsByWebClient() {
return this.webClient.get().uri("http://localhost:8080/getAll").retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse-> clientResponse.bodyToMono(Map.class).flatMap(e->{
log.error(" 4XX error is occurred ");
return Mono.error(new Exception());
}))
.onStatus(HttpStatus::is5xxServerError, serverResponse->serverResponse.bodyToMono(Map.class).flatMap(e->{
log.error("there is 5XX error ");
return Mono.error(new Exception());
}))
.bodyToFlux(Fruit.class);
}
@Override
public Mono<Fruit> saveFruitsByWebClient() {
Fruit fruit=new Fruit();
fruit.setId("45");
fruit.setName("PineApple");
fruit.setPrice("70");
return this.webClient.post().uri("http://localhost:8080/save")
.body(BodyInserters.fromObject(fruit))
.retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse-> clientResponse.bodyToMono(Map.class).flatMap(e->{
log.error(" 4XX error is occurred ");
return Mono.error(new Exception());
}))
.onStatus(HttpStatus::is5xxServerError, serverResponse->serverResponse.bodyToMono(Map.class).flatMap(e->{
log.error("there is 5XX error ");
return Mono.error(new Exception());
}))
.bodyToMono(Fruit.class);
}
@Override
public Flux<Fruit> getAll() {
return fruitRepository.findAll();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment