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.
Related
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);
}
}
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
I'm just exploring the Spring Cloud stack. I've set up an Eureka instance and then set up a service that registers itself with Eureka.
Eureka:
appliation.properties
spring.application.name=registry
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Main class:
#SpringBootApplication
#EnableEurekaServer
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryApplication.class, args);
}
}
Service:
application.properties:
spring.application.name=example-service
server.port=1111
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Main class:
#SpringBootApplication
#EnableDiscoveryClient
#RestController
public class AppServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AppServiceApplication.class, args);
}
#RequestMapping("/")
public String sayHi() {
return "Hi";
}
}
Now both eureka and the service are starting up and the service registers itself with eureka as expected. However it registers itself with a rather weird hostname and I am not able to figure out what exactly is going on or why this is happening. This is a screenshot of the eureka dashboard showing the weird hostname:
I tried the same setup on my laptop and there the hostname is just 'localhost' as expected.
Does someone have an explanation for this ? Thank you in advance for your answers!
Edit: I'm using Spring Boot version 2.0.5.RELEASE
I have an application with eureka, ribbon and feign. I have a feign RequestInterceptor, but the problem is that I need to know which is the host where I'm making the call. So far, with my current code I just can get the path with the RequestTemplate, but not the host.
Any idea?
I'm new to this as well, but I believe I've just learned something relevant to your question.
In the service you're creating, each instance can be given some unique identifier tied to a property included in its instance profile. Some .properties examples below:
application.properties (shared properties between instances)
spring.application.name=its-a-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
application-instance1.properties
server.port=5678
instance.uid=some-unique-property
application-instance2.properties
server.port=8765
instance.uid=other-unique-property
This service, as an extremely contrived example will show, can send out #Value annotated attributes to be consumed by the Ribbon app:
#SpringBootApplication
#EnableDiscoveryClient
#RestController
public class ItsAServiceApplication {
#Value("${server.port}")
private int port;
#Value("${instance.uid}")
private String instanceIdentifier;
public static void main(String[] args) {
SpringApplication.run(ItsAServiceApplication.class, args);
}
#RequestMapping
public String identifyMe() {
return "Instance: " + instanceIdentifier + ". Running on port: " + port + ".";
}
}
And just to complete the example, the Ribbon app that might consume these properties could look like this:
#SpringBootApplication
#EnableDiscoveryClient
#RestController
public class ServiceIdentifierAppApplication {
#Autowired
private RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(ServiceIdentifierAppApplication.class, args);
}
#GetMapping
public String identifyMe() {
return restTemplate.getForEntity("http://its-a-service", String.class).getBody();
}
#Bean
#LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Results:
Reloading the rest template created by the services
As I said earlier, I'm pretty new to this and have just been learning myself. Hopefully this has given you an idea for how you might send these properties! I would imagine creating a dynamic property identifier through Spring Cloud Config would be ideal here.
public class ABC{ public static void main(String[] args) { out.println("Hello"); } }
This works, though static imports are not generally considered a good thing in java.
import static java.lang.System.out;
public class ABC {
public static void main(String[] args) {
out.println("hello");
}
}