spring cloud:eureka服务发现

1.eureka服务端

服务端代码
@SpringBootApplication
@EnableEurekaServer
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}


1.1高可用和域
eureka服务器没有后端存储,但注册的服务实例都有发送心跳保持他们的登记日期(这可以在内存中完成),客户也有一个内存中的缓存(所以他们不需要为每一个服务的情况去注册一次)。默认情况下eureka服务器也是一个eureka客户端,还需要一个url来定位节点

1.2单服务模式
application.yml配置如下:
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


1.3对等意识
两个对等的eureka服务配置,application.yml配置如下:
---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2/eureka/

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1/eureka/


我们使用上面例子yaml文件的配置运行在两个相同的服务器上,使用不同的spring profile。你可以使用这个配置在单个服务器测试对等性 。

1.4 使用IP
实例名称显示IP配置如下:
引用
eureka.instance.preferIpAddress=true


2.erueka客户端
eureka是netflix的服务发现服务器和客户端。eureka是提供服务注册,为每个注册服务提供知晓其他服务状态的服务。
2.1 eureka客户端代码和配置
当一个客户端注册到eureka,它会提供关于它自己的端口、地址、健康监控url和home页面等等的元数据,erueka会从每个实例接受心跳信息。如果心跳在配置的时间内失败,实例通常会从注册表中移除。
下面是eureka客户端代码:
@SpringBootApplication
@EnableEurekaClient
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
}


在这里使用的是@EnableEurekaClient,这个标注只能用于eureka服务,还可以使用@EnableDiscoveryClient。
此外还需要配置eureka服务器的位置,在application.yml文件配置,配置如下:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/


2.2 状态页面和健康监控
eureka实例的状态页面和健康监控默认为/info和/health,这是spring boot actuator提供的默认终端点。当应用不使用默认的上下文(context path或servlet path,比如配置server.servletPath=/test),或者管理zhongduan路径(比如配置management.contextPath=/admin)。
application.yml配置文件如下:
eureka:
  instance:
    statusPageUrlPath: ${management.contextPath}/info
    healthCheckUrlPath: ${management.contextPath}/health


2.3eureka的元数据
可以通过eureka.instance.metadataMap修改元数据,这些元数据不会改变客户端的行为。

默认情况下一个eureka服务使用主机名称注册,那么只能一个主机一个服务。通过eureka.instance.metadataMap.instanceId你可以修改这个实例ID。application.yml配置如下:
eureka:
  instance:
    metadataMap:
      instanceId: ${spring.application.name}:${random.value}

使用这个配置,多个服务实例可以配置在一个主机上,random.value确保了实例ID的唯一性。当然需要实例的端口不一样

2.4 使用DiscoveryClient类
如果你的应用使用@EnableEurekaClient注解,那么只能使用eureka来发现服务实例。
一个方法是使用com.netflix.discovery.DiscoveryClient
@Autowired
private DiscoveryClient discoveryClient;

public String serviceUrl() {
    InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
    return instance.getHomePageUrl();
}

请不要在@PostConstruct或者@Scheduled方法中使用DiscoveryClient,因为ApplicationContext也许没有启动。

2.5 DiscoveryClient类的替代品
你不必使用原始Netflix DiscoveryClient,通常更方便是使用一个包装器。spring cloud提供了Feign(一个rest客户端构建器),spring RestTemplate使用一个eureka服务标示代替物理的URL。用一个组固定的物理服务器配置Ribbon,使用.ribbon.listOfServers配置,用逗号分隔服务器的物理地址或主机名,其中是客户端的ID。
你也可以使用org.springframework.cloud.client.discovery.DiscoveryClient,不特定于netflix,代码如下:
@Autowired
private DiscoveryClient discoveryClient;

public String serviceUrl() {
    List<ServiceInstance> list = client.getInstances("STORES");
    if (list != null && list.size() > 0 ) {
        return list.get(0).getUri();
    }
    return null;
}


2.6注册服务心跳
注册服务默认心跳时间为30秒,当一个服务器不可用,需要3个心跳才能让服务器和客户端的元数据相同。可以使用eureka.instance.leaseRenewalIntervalInSeconds加快这个过程。在生产环境最好使用默认配置

http://blog.csdn.net/zhuchuangang/article/details/51202307

猜你喜欢

转载自rd-030.iteye.com/blog/2384533