spring cloud:eureka service discovery

1.eureka server

server code
@SpringBootApplication
@EnableEurekaServer
public class Application {

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

}


1.1 High availability and domain
eureka servers have no backend storage, but registered service instances all have to send heartbeats to keep their registration date (this can be done in memory), and clients also have an in-memory cache (so they don't need to do this for each service to register once). By default, the eureka server is also an eureka client, and a url is required to locate the node

1.2 single-service mode
application.yml is configured as follows:
server:
  port: 8761

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


1.3 Peer Awareness
Two peer eureka service configurations, application.yml configuration is as follows:
---
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/


We use the configuration of the example yaml file above to run on two identical servers, using different spring profiles. You can use this configuration to test peering on a single server.

1.4 Use the IP
instance name to display the IP configuration as follows:
quote
eureka.instance.preferIpAddress=true


2.erueka client
Eureka is the service discovery server and client of netflix. eureka is to provide service registration, providing services for each registered service to know the status of other services.
2.1 eureka client code and configuration
When a client registers with eureka, it will provide metadata about its own port, address, health monitoring url, home page, etc., erueka will receive heartbeat information from each instance. If the heartbeat fails within the configured time, the instance is usually removed from the registry.
Here is the eureka client code:
@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);
    }
}


Here @EnableEurekaClient is used, this annotation can only be used for eureka services, and @EnableDiscoveryClient can also be used.
In addition, you need to configure the location of the eureka server, which is configured in the application.yml file. The configuration is as follows:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/


2.2 Status page and health monitoring
The status page and health monitoring of eureka instances are /info and /health by default, which are the default endpoints provided by spring boot actuator. When the application does not use the default context (context path or servlet path, such as configuration server.servletPath=/test), or management zhongduan path (such as configuration management.contextPath=/admin).
The application.yml configuration file is as follows:
eureka:
  instance:
    statusPageUrlPath: ${management.contextPath}/info
    healthCheckUrlPath: ${management.contextPath}/health


2.3 The metadata of
eureka can modify the metadata through eureka.instance.metadataMap, which will not change the behavior of the client.

By default, a eureka service is registered with the host name, so there can only be one service per host. With eureka.instance.metadataMap.instanceId you can modify this instance ID. application.yml is configured as follows:
eureka:
  instance:
    metadataMap:
      instanceId: ${spring.application.name}:${random.value}

Using this configuration, multiple service instances can be configured on a single host, and random.value ensures the uniqueness of the instance ID. Of course, the port of the instance needs to be different.

2.4 Using the DiscoveryClient class
If your application is annotated with @EnableEurekaClient, you can only use eureka to discover service instances.
One way is to use com.netflix.discovery.DiscoveryClient
@Autowired
private DiscoveryClient discoveryClient;

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

Please do not use DiscoveryClient in @PostConstruct or @Scheduled methods, because ApplicationContext may not be started.

2.5 Alternatives to the DiscoveryClient class
You don't have to use the original Netflix DiscoveryClient, it is often more convenient to use a wrapper. Spring cloud provides Feign (a rest client builder), and spring RestTemplate uses an eureka service identifier instead of a physical URL. To configure Ribbon with a fixed set of physical servers, use the .ribbon.listOfServers configuration, separating the physical addresses or hostnames of the servers with a comma, where is the client ID.
You can also use org.springframework.cloud.client.discovery.DiscoveryClient, not specific to netflix, the code is as follows:
@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 Registration service heartbeat
The default heartbeat time of the registration service is 30 seconds. When a server is unavailable, it takes 3 heartbeats to make the metadata of the server and the client the same. This process can be accelerated using eureka.instance.leaseRenewalIntervalInSeconds. It is best to use the default configuration in the production environment

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326358918&siteId=291194637