Spring Boot Kafka Reactive Configuration - apache-kafka

Is it possible to configure Spring Boot Reactive Kafka solely by java configuration by switching of this KafkaAutoConfiguration.class class on the appĀ“s main method:
Like so:
#SpringBootApplication(exclude =
{LdapAutoConfiguration.class,
MailSenderAutoConfiguration.class,
KafkaAutoConfiguration.class,
DataSourceAutoConfiguration.class})
#EnableR2dbcRepositories
public class L0grApplication {
public static void main(String[] args) {
SpringApplication.run(L0grApplication.class, args);
}
}

Related

Replace Spring Java config with XML for Apache Kafka

Ho can be replaced a Java configuration like this
#Configuration
#EnableKafka
public class KafkaConfig {
}
With a full XML configuration?
Especially how can be methods (or classes) annotated with
#KafkaListener
public void poll(String msg){}
injected into Spring Kafka context as listeners?
#EnableKafka adds 2 beans to the application context:
#Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(
KafkaListenerConfigUtils.KAFKA_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)) {
registry.registerBeanDefinition(KafkaListenerConfigUtils.KAFKA_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME,
new RootBeanDefinition(KafkaListenerAnnotationBeanPostProcessor.class));
}
if (!registry.containsBeanDefinition(KafkaListenerConfigUtils.KAFKA_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME)) {
registry.registerBeanDefinition(KafkaListenerConfigUtils.KAFKA_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME,
new RootBeanDefinition(KafkaListenerEndpointRegistry.class));
}
}
So just add those as <bean/> s with those names.
"org.springframework.kafka.config.internalKafkaListenerAnnotationProcessor"
"org.springframework.kafka.config.internalKafkaListenerEndpointRegistry"

Spring Cloud Eureka + FeignClient + Ribbon : Load balancer does not have available server for client

I have Below Simple Setup.
Spring Cloud Eureka Server- and Two Services PricingService and DiscountService. Pricing Service calls-> DiscountService.
Server Setup
#SpringBootApplication
#EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
properties
spring.application.name=eureka-server
server.port=8761
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
DiscountService
#SpringBootApplication
#EnableDiscoveryClient
#Slf4j
public class DiscountServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DiscountServiceApplication.class, args);
}
}
#RestController
#RequestMapping("/discount/{product}")
#Slf4j
public class DiscountController{
#GetMapping
public int getDiscountPercentage(#PathVariable("product") String product){
log.info("Getting Discount for Product {}",product);
return 50;
}
}
spring.application.name=discount-service
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
server.port=8081
Pricing Service
#SpringBootApplication
#EnableDiscoveryClient
#EnableFeignClients
public class PricingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(PricingServiceApplication.class, args);
}
}
#RestController
#Slf4j
class ServiceInstanceRestController {
#Autowired
private DiscountServiceClient discountServiceClient;
#GetMapping("/test/")
public String getPriceForProduct() {
log.info("getPriceForProduct");
int dscount = discountServiceClient.getDiscountPercentage("Test");
log.info("Discount is {}",dscount);
return "Price";
}
}
#FeignClient("discount-service")
interface DiscountServiceClient {
#RequestMapping(method = RequestMethod.GET, value = "/discount/{product}")
int getDiscountPercentage(#PathVariable("product") String product);
}
spring.application.name=pricing-service
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
server.port=8080
eureka.client.fetchRegistry=true
While Calling Discount Service i am getting exception
com.netflix.client.ClientException: Load balancer does not have available server for client: discount-service
at com.netflix.loadbalancer.LoadBalancerContext.getServerFromLoadBalancer(LoadBalancerContext.java:483) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
I am using Spring Boot 2.1.4.RELEASE
dependency used
spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-openfeign
spring-cloud-starter-netflix-eureka-server-server (Fo Server)
I have already checked the answers for Load balancer does not have available server for client
But did not work for me...
What I am missing here ?
It also requires actuator dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
For more details, refer the following links
https://github.com/M-Thirumal/eureka-server
https://github.com/M-Thirumal/eureka-client-1
https://github.com/M-Thirumal/eureka-client-2

Feign client manually. Load balancer does not have available server for client

I have two services registered with eureka. Service C calls service A. Service C is feign client. I want implement feign client manually. But I catch an exception:
com.netflix.client.ClientException: Load balancer does not have
available server for client: service-test-a
Application class:
#EnableEurekaClient
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Feign interface:
#Component
public interface FeignService {
#RequestLine("GET /")
public String getServiceA();
}
Feign config:
#Configuration
#Import(FeignClientsConfiguration.class)
public class MyConfig {
}
Controller:
#RestController
public class Controller {
private FeignService feignService;
#Autowired
public void Controller() {
feignService = Feign.builder()
.client(RibbonClient.create())
.target(FeignService.class, "http://service-test-a");
}
#RequestMapping(value = "/build", method = RequestMethod.GET)
public String getServiceC() {
return feignService.getServiceA();
}
}
What am I doing wrong?
AFAIK, there is no easy way of using OpenFeign with eureka. There is no guide or example for that. Also I guess that it may require some additional implementations and configuration.
Instead, please try to use Spring Cloud Feign. It provides full integration with eureka and ribbon without any additional implementation. You can use Spring Cloud Feign with just a few changes in your above code.
Please refer to Spring Cloud Feign

Automaticle replicate microservices with Spring Cloud Netflix

I have multiple Spring Cloud microservices. I want the automatic replication of microservices to be done when microservice cannot manage to do its work.
I tried to find any solutions but I found only this: here. This solution cannot be applied to my problem as it describes only the case when we have a certain number of microservices.
I will be very grateful if you give me some examples to help me with this problem.
UPDATE: I've several microservices
Eureka:
#SpringBootApplication
#EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args){
SpringApplication.run(EurekaApplication.class, args);
}
}
GatewayApplication:
#EnableZuulProxy
#EnableEurekaClient
#SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
FirstSybsystemApplication:
#SpringBootApplication
#EnableEurekaClient
#EnableFeignClients
public class FirstSubsystemApplication implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(FirstSubsystemApplication.class);
}
#Override
public void run(String... args) throws Exception {
}
}
I want to make a high load on FirstSubsystemApplication and to launch its copy automatically.

Execute MongoDB script on Spring Boot start

I need to initialize a Mongo DB with some script file like Spring do with JPA and import.sql file.. but how?
Can someone help me?
You could use something similar that's done by mongeez. This is basically a starter for spring-boot that runs scripts before spring-data-mongodb beans are initialized.
You could tag along and leverage spring-boot's initialization lifecycle, where after wiring the beans, it executes all CommandLineRunner beans.
#SpringBootApplication
public class YourApplication {
final Logger logger = LoggerFactory.getLogger(getClass());
#Autowired
private MongoRepository repo;
#Bean
CommandLineRunner preLoadMongo() throws Exception {
return args -> {
//repo.doSOmethingInMongoDB
}
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}