Java Microservice Interview Questions

How to create Eureka Server?

  1. Add the dependency of spring-cloud-starter-netflix-eureka-server.

  2. Use @EnableEurekaServer

    @SpringBootApplication
    @EnableEurekaServer
    public class Application {
    public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
    }
    • Standalone Mode

      server:
      port: 8761
      eureka:
      instance:
      hostname: localhost
      client:
      registerWithEureka: false
      fetchRegistry: false
      serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      ```
    • Peer Aware Mode

      For Peer Aware mode, you run the same application twice with different profiles.

      First Profile:

      spring:
      profiles: peer1
      eureka:
      instance:
      hostname: peer1

      Second Profile:

      spring:
      profiles: peer2
      eureka:
      instance:
      hostname: peer2

How to Register with Eureka?

  1. You need to have spring-cloud-starter-netflix-eureka-client in you classpath
  2. Giving Eureka Server address in application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
  1. If in Peer mode You can give multiple peer address:
eureka:
client:
serviceUrl:
defaultZone: https://peer1/eureka/,http://peer2/eureka/,http://peer3/eureka/

How do you secure Eureka Sever?

  1. Add spring-boot-starter-security to the class path
  2. Web Security Config to your app for disabling csrf.
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
  1. Provide Spring Security User name and password in application.yml
spring:
security:
user:
name: admin
password: pwd
  1. Change the default zone url accordingly.
defaultZone: http://admin:pwd@localhost:8761/eureka/

How to enable Hystrix Circuit Breaker?

@Bean
public Customizer<HystrixCircuitBreakerFactory> defaultConfig() {
return factory -> factory.configureDefault(id -> HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(id))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(4000)));
}

How to write hystrix client?

  1. Include spring-cloud-starter-netflix-hystrix in you classpath
  2. Miminal Hystrix Client:
@SpringBootApplication
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
@Component
public class StoreIntegration {
@HystrixCommand(fallbackMethod = "defaultStores")
public Object getStores(Map<String, Object> parameters) {
//do stuff that might fail
}
public Object defaultStores(Map<String, Object> parameters) {
return /* something useful */;
}
}

What are the common Hystrix Circuit Breaker configuration?

  • circuitBreaker.enabled
  • circuitBreaker.requestVolumeThreshold
  • circuitBreaker.sleepWindowInMilliseconds
  • circuitBreaker.errorThresholdPercentage
  • circuitBreaker.forceOpen
  • circuitBreaker.forceClosed

These above you can use with @HystrixProperty annotation.