How to create Eureka Server?
Add the dependency of
spring-cloud-starter-netflix-eureka-server
.Use @EnableEurekaServer
@SpringBootApplication@EnableEurekaServerpublic class Application {public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);}}Standalone Mode
server:port: 8761eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl: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: peer1eureka:instance:hostname: peer1Second Profile:
spring:profiles: peer2eureka:instance:hostname: peer2
How to Register with Eureka?
- You need to have
spring-cloud-starter-netflix-eureka-client
in you classpath - Giving Eureka Server address in application.yml
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
- 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?
- Add
spring-boot-starter-security
to the class path - Web Security Config to your app for disabling csrf.
@EnableWebSecurityclass WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().ignoringAntMatchers("/eureka/**");super.configure(http);}}
- Provide Spring Security User name and password in application.yml
spring:security:user:name: adminpassword: pwd
- Change the default zone url accordingly.
defaultZone: http://admin:pwd@localhost:8761/eureka/
How to enable Hystrix Circuit Breaker?
@Beanpublic 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?
- Include
spring-cloud-starter-netflix-hystrix
in you classpath - Miminal Hystrix Client:
@SpringBootApplication@EnableCircuitBreakerpublic class Application {public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);}}@Componentpublic 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.