Integrate spring Cloud cloud service architecture-Eureka Foundation

Source: Internet
Author: User

Before we build the project, let's take a look at the Eureka, which is an official explanation, and I'll look back at you for a moment:

Service Discovery: Eureka Client
Service discovery is one of the key principles of micro-service architecture. Trying to configure each client or some form of convention can be very difficult and very fragile. The Netflix service discovers that servers and clients are Eureka. Servers can be configured and deployed as high availability, and each server replicates the state of the enrollment service to other servers.

How to include Eureka clients
To include the Eureka client in your project, use the initiator of group Org.springframework.cloud and artifact ID spring-cloud-starter-eureka.

Register Eureka
When a client registers Eureka, it provides metadata about itself, such as host and port, health indicator URL, home page, and so on. Eureka receives heartbeat messages from each instance belonging to the service. If a heartbeat failure exceeds a configurable timesheet, the instance is typically removed from the registry.

Example Eureka Client:

@Configuration@ComponentScan@EnableAutoConfiguration@EnableEurekaClient@RestControllerpublic class Application {    @RequestMapping("/")    public String home() {        return "Hello world";    }    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    }}

(That is, the fully normal spring boot application). In this example, we explicitly use @enableeurekaclient, but only Eureka is available, and you can use @enablediscoveryclient. Configuration is required to locate the Eureka server. Cases:

Application.yml

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/

where "Defaultzone" is a magic string fallback value that provides a service URL for any client that does not represent a preference (that is, it is a useful default value).

The default application name (service ID) obtained from environment, the virtual host and the unsecured port are ${spring.application.name},${spring.application.name} and ${server.port respectively }。

@EnableEurekaClient the application into a Eureka "instance" (that is, registering itself) and a "client" (that is, it can query the registry to find other services). The instance behavior is driven by the eureka.instance.* configuration key, but if you make sure that your application has spring.application.name (which is the default value for the Eureka Service ID or VIP), the default value will be normal.

Authenticating using the Eureka server
If one of the Eureka.client.serviceUrl.defaultZone URLs contains a credential (such as Http://user:[email Protected]:8761/eureka), HTTP Basic authentication is automatically added to your Eureka client. For more complex requirements, you can create Discoveryclientoptionalargs types of @bean and inject clientfilter instances into them, all of which will be applied to calls from the client to the server.

Attention
Because of the limitations in Eureka, it is not possible to support basic authentication credentials for each server, so only the first found collection can be used.
Status page and health metrics
The status page and health indicators for the Eureka instance default to "/info" and "/Health" respectively, which are the default locations for useful endpoints in the Spring boot executor application. If you use a non-default context path or a servlet path (for example, Server.servletpath=/foo) or manage the endpoint path (for example, management.contextpath=/admin), you need to change these, even for the executor application. Cases:

application.ymleureka:  instance:    statusPageUrlPath: ${management.context-path}/info    healthCheckUrlPath: ${management.context-path}/health

These links appear in the metadata used by clients, and in some cases are used to decide whether to send requests to the application, so if they are accurate, this is helpful.

Registering Secure applications
If your application wants to connect over HTTPS, it can be in eurekainstanceconfig, or eureka.instance, respectively. Two flags are set in [Nonsecureportenabled,secureportenabled]=[false,true]. This will enable Eureka Publishing instance information to display a clear preference for secure communications. Spring Cloud Discoveryclient will always return a https://for services configured in this way ...; URI, and the Eureka (native) instance information will have a secure health check URL.

Because of how Eureka works internally, it will still publish the status and non-secure URLs for the home page unless you explicitly overwrite them. You can use placeholders to configure Eureka instance URLs, such as

application.ymleureka:  instance:    statusPageUrl: https://${eureka.hostname}/info    healthCheckUrl: https://${eureka.hostname}/health    homePageUrl: https://${eureka.hostname}/

(Note that ${eureka.hostname} is a local placeholder that is only available in later versions of Eureka, and you can use the spring placeholder for the same functionality, such as using ${eureka.instance.hostname}.

Attention
If your application runs behind a proxy server and SSL terminates the service in the proxy (for example, if you are running on cloud foundry or other platforms as a service), you need to ensure that the agent "forward" header is intercepted and processed by the application. The embedded Tomcat container in the Spring boot application automatically performs an explicit configuration of the "x-forwarded-\ *" header. One of the signs of your mistake is that the link presented by your application itself is wrong (the wrong host, Port, or protocol).
Eureka Health Check-up
By default, Eureka uses the client heartbeat to determine whether the client is started. Unless otherwise specified, the discovery client will not propagate the current health check state of the application based on the spring boot executor. This means that after successful registration Eureka will always announce that the application is in the "Up" state. You can change this behavior by enabling Eureka Health checks to propagate application state to Eureka. Therefore, each other application will not send traffic to the application in a state other than "up".

application.ymleureka:  client:    healthcheck:      enabled: true

Warning
Eureka.client.healthcheck.enabled=true can only be set in Application.yml. Setting the value in Bootstrap.yml will result in undesirable side effects, such as registering in a Eureka with a unknown status.
If you need more control over your health checks, you may want to consider implementing your own com.netflix.appinfo.HealthCheckHandler.

Eureka metadata for instances and clients
It's worth taking a moment to understand how the Eureka metadata works so that you can use it on the platform. There are standard metadata such as host name, IP address, port number, status page, and health check. These are published in the service registry and are used by customers to contact the service directly. Additional metadata can be added to the instance registration in Eureka.instance.metadataMap, and this is accessible in remote clients, but generally does not change the behavior of the client unless it is aware of the meaning of the metadata. Several special cases are described below, where spring cloud has already specified the meaning of the metadata map.

Use Eureka on Cloudfoundry
Cloudfoundry has a global router, so all instances of the same application have the same host name (as in other PAAs solutions with similar schemas). This is not necessarily an obstacle to using Eureka, but if you use a router (which is recommended, or even mandatory, depending on how your platform is set up), you need to explicitly set the hostname and port number (secure or non-secure) so that they can use the router. You may also want to use instance metadata so that you can differentiate between instances on the client (for example, in a custom load balancer). By default, Eureka.instance.instanceId is vcap.application.instance_id. For example:

application.ymleureka:  instance:    hostname: ${vcap.application.uris[0]}    nonSecurePort: 80

Service Discovery: Eureka Server
How to include a Eureka server
To include Eureka servers in your project, use the initiator of group Org.springframework.cloud and artifact ID spring-cloud-starter-eureka-server. For more information about using the current Spring Cloud Publishing list settings to build your system, see the Spring Cloud project page.

How to run the Eureka server
Example Eureka Server;

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

I will not talk about the other, there is a lot of information about the registration center, you can check the relevant data.

From now on, I will be documenting the process and essence of the recent development of the spring cloud micro-service cloud architecture to help more friends who are interested in developing the Spring cloud framework to explore the process of building the spring cloud architecture and how to use it in enterprise projects.

Integrate spring Cloud cloud service architecture-Eureka Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.