@EnableEurekaServer
ApplicationEvent
@Import
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(EurekaServerMarkerConfiguration.class) public @interface EnableEurekaServer { }
EurekaServerMarkerConfiguration
@Configuration public class EurekaServerMarkerConfiguration { @Bean public Marker eurekaServerMarkerBean() { return new Marker(); } class Marker { } }
@ConditionalOnBean
restTemplate.getForEntity("http://eureka-server", String.class);
LoadBalancerInterceptor
loadBalancer
RibbonLoadBalancerClient
@SpringBootApplication @EnableDiscoveryClient @RestController @Slf4j public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; /** * 使用Ribbon的负载均衡 * @return */ @GetMapping("/") public ResponseEntity getTime(){ return restTemplate.getForEntity("http://eureka-server", String.class); } /** * 模拟轮询负载的调用 * @return */ @GetMapping("/discovery") public ResponseEntity discovery(){ List instances = discoveryClient.getInstances("eureka-server"); int i = incrementAndGetModule(instances.size()); return restTemplate.getForEntity(((EurekaDiscoveryClient.EurekaServiceInstance) instances.get(i)).getInstanceInfo().getHomePageUrl(), String.class); } private AtomicInteger nextIndex = new AtomicInteger(); private int incrementAndGetModule(int module) { for (; ; ) { int current = nextIndex.get(); int next = (current + 1) % module; if (nextIndex.compareAndSet(current,next) && current
discoveryClient
@LoadBalanced