Spring Cloud Eureka Ribbon

Source: Internet
Author: User
Tags port number
Service governance: Spring Cloud Eureka

Service governance can be said to be the most core and basic module in MicroServices architecture, which is mainly used to realize the automatic registration and discovery of each micro-service instance.

Service registration: In the service governance framework, a registry is typically built, each service unit registers its own services with the registry, informs the Registry of additional information such as host and port number, version number, communication protocol, and so on, and the registry organizes the list of services by service name.

Service Name location
Service A 192.168.0.100:8000,192.168.0.101:8000
Service B 192.168.0.100:9000,192.168.0.101:9000,192.168.0.102:9000

Service discovery: Calls between services are no longer implemented by specifying specific instance addresses, but rather by initiating request calls to service names . The caller needs to consult with the service registry and obtain an instance manifest of all the services to enable access to the specific service instance.

Eueka server, we are also known as the Service Registration center. It supports highly available configurations like other service registries

Eueka client, mainly handles the registration and discovery of services. The client service is embedded in the code of the client application by means of annotations and parameter configuration. The Eueka client registers its services with the registry and periodically sends a heartbeat to update its service lease. At the same time, it can query the current registered service information from the server and cache them locally and periodically refresh the service state. Build a service registration center

Introducing Eureka-server Dependencies

<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-start er-parent</artifactid> <version>1.3.7.RELEASE</version> </parent> <dependencie s> <dependency> <groupId>org.springframework.cloud</groupId> <arti
            Factid>spring-cloud-starter-eureka-server</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test&lt ;/artifactid> <scope>test</scope> </dependency> </dependencies> &L T;dependencymanagement> <dependencies> <dependency> <groupid>org.
                Springframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version&gT 
            Brixton.sr5</version> <type>pom</type> <scope>import</scope>
        </dependency> </dependencies> </dependencyManagement> <build>
                <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins&
    Gt </build>

Starting a registry with @enableeurekaserver annotations

@SpringBootApplication
@EnableEurekaServer Public
class Eurekaserverapplication {public
    static void Main (string[] args) {
        springapplication.run (Eurekaserverapplication.class,args);
    }
}

APPLICATION.YML file

Eureka.client.fetch-registry: Retrieving service instances

Eureka.client.register-with-eureka: Registering yourself as a client

Eureka.client.service-url.defaultzone: Specify a registry address

Server:
  port:1111
Eureka:
  instance:
    hostname:ljh1
  client:
    fetch-registry:true
    Register-with-eureka:true
    service-url:
      defaultzone:http://ljh2:2222/eureka/
Spring:
  Profiles: LJH1
  application:
    NAME:LJH
---
server:
  port:2222
Eureka:
  instance:
    HOSTNAME:LJH2
  client:
    fetch-registry:true
    register-with-eureka:true
    service-url:
      defaultzone:http://ljh1:1111/eureka/
Spring:
  profiles:ljh2
  application:
    NAME:LJH
Registered service Provider

Spring.application.name: Specify the service name

Eureka.client.service-url.defaultzone: Specify a registry address

Server:
  port:8080
Spring:
  application:
    name:hello-service
Eureka:
  client:
    Register-with-eureka:true
    fetch-registry:true
    service-url:
          defaultzone:http://ljh1:1111/eureka/ , http://ljh2:2222/eureka/
@RestController public
class Hellocontroller {
    @Autowired
    private discoveryclient client;

    @RequestMapping ("/hello") public
    String Hello () {
        Serviceinstance instance = client.getlocalserviceinstance ( );
        System.out.println (Instance.gethost () + "" +instance.getport ());
        Return "Hello World";
    }
}

@EnableDiscoveryClient: Activating the discoveryclient implementation in Eureka

@EnableDiscoveryClient
@SpringBootApplication Public
class Application {public
    static void main ( String[] args) {
        new Springapplicationbuilder (Application.class). Web (True). Run (args);
    }
service discovery and consumption

The Service discovery task is done by the Eureka client, and the service consumption task is done by the Ribbon. The Ribbon is a base client load balancer with HTTP and TCP.

Pom.xml

<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-start er-parent</artifactid> <version>1.3.7.RELEASE</version> </parent> <dependencie s> <dependency> <groupId>org.springframework.boot</groupId> <artif actid>spring-boot-starter-web</artifactid> </dependency> <dependency> &lt ;groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-ribbon</artifa ctid> </dependency> <dependency> <groupid>org.springframework.boot</gr oupid> <artifactId>spring-boot-starter-test</artifactId> </dependency> &L T;dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring -cloud-starter-eureka</artifactid> </dependency> </dependencies> <dependencyManagement> <depen
                Dencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>brixton.sr5</ve rsion> <type>pom</type> <scope>import</scope> </
            dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <art ifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build >

Application.yml

Server:
  port:7081
Spring:
  application:
    name:consumer1
Eureka:
  client:
    Fetch-registry:true
    register-with-eureka:true
    service-url:
      defaultzone:http://ljh1:1111/eureka/, http://ljh2:2222/eureka/

@EnableDiscoveryClient: Register the app as Eureka Client

@LoadBalanced: Enable server-side load balancing

@SpringBootApplication
@EnableDiscoveryClient Public
class Application {

    @LoadBalanced
    @Bean Public
    resttemplate resttemplate () {
        return new resttemplate ();
    }

    public static void Main (string[] args) {
        springapplication.run (Application.class,args);
    }
}

interface invocation via resttemplate specified service name

@RestController public
class Consumercontroller {
    @Autowired
    private resttemplate resttemplate;

    @RequestMapping ("/consumer") public
    String Consumerhello () {
       string result = Resttemplate.getforentity ("http ://hello-service/hello ", String.class). GetBody ();
       return result;
    }
}
Eureka Detailed Infrastructure Architecture

Service registry: Eureka provides services to provide service registration and discovery capabilities

Service providers: Applications that provide services, either Spring Boot applications or other technology platforms that follow the Eureka communication mechanism

Service consumers: Consumer apps get a list of services from the service registry, so consumers can know where to call the services they need

Many times, the client is both a service provider and a service consumer services governance mechanism service provider

Service Registration

The service provider registers itself on the Eureka Server at startup by sending a rest request, along with some metadata information for its own service. After the Eureka server receives this rest request, the metadata information is stored in a two-tier structure map, where the first key is the service name, and the second key is the instance name of the specific service.

Service Synchronization

When you configure Eureka Server high availability, when a service provider sends a registration request to a service registry, it forwards the request to another registry that is connected to the cluster, enabling synchronization of services between the registries. With service synchronization, service information for two service providers can be obtained through any of the two service registries

Service Renewal

After registering the service, the service provider maintains a heartbeat that continues to tell Eureka Server: "I'm still alive" to prevent Eureka Server from excluding the service instance from the list of services, which we call a service renewal (Renew).

There are two important attributes to renew a service renewal, and we can focus on and adjust as needed:

#参数用于定义服务续约任务的调用间隔时间, the default is 30 seconds. 
eureka.instance.lease-renewal-interval-in-seconds=30 
#用于定义服务失效的时间, default is 90 seconds
Eureka.instance.lease-expiration-duration-in-seconds=90
Service Consumers

Get Service

When we start the service consumer, it sends a REST request to the service registry to get the list of services registered above. Eureka Server maintains a read-only list of services to be returned to the client, and the cache manifest is updated every 30 seconds

Eureka.client.registry-fetch-interval-seconds=30

Service Invocation

After the service consumer obtains the service list, the service name can obtain the instance name of the service and the metadata information of the instance.

Calls are made by default in the Ribbon for client-side load balancing

For the selection of access instances, there is a concept of region and zone in the Eureka, and a region can contain multiple zone

Each service client needs to be registered in a zone, so each client corresponds to a region and a zone. In the case of service invocation, priority access to the service provider in the same zone, if not, access to the other zone

Services Offline

When the system is running, it will inevitably face the situation of shutting down or restarting an instance of the service. When a service instance makes a graceful shutdown, it triggers a service down-line rest request to Eureka Server. After the server receives the request, it resets the service status to offline (down) and
The Downline event spreads out the service registry

Failure Culling

In some cases, our service instances are not necessarily offline, and may be due to memory overflow, network failure, etc.
Because the service does not work properly, the service registry does not receive a "service offline" request. In order to remove these instances of the service from the list of services, Eurekaserver will create a timed task at boot time, which will be excluded from the current list of services that are timed out and not renewed by default at regular intervals.

Self-protection

When we debug a Eurka-based program locally, we basically encounter an issue in which a red warning message appears in the Information Panel of the service registry. This warning is a trigger for EURKASRVR's self-protection mechanism. Eureka Server runs, it counts whether the rate of heartbeat failure is less than 85% in 15 minutes. Eureka server protects the current instance registration information so that these instances do not expire and protects the registration information as much as possible. However, in this period of protection, if the instance has problems, then the client can easily get a service instance that does not exist, there will be a failure of the call, so the client must have a fault-tolerant mechanism, such as the use of request retry, circuit breakers and other mechanisms common settings server settings

Eureka:
  server:
    enable-self-preservation:false     #服务端关闭保护模式
    eviction-interval-timer-in-ms:5000  #服务端每隔多少毫秒检查剔除客户端实例
Client Configuration (server-side provider)
Eureka:
  instance:
    lease-renewal-interval-in-seconds:5  #客户端每隔5秒续约服务, indicating that it is still alive, default 30 seconds
    Lease-expiration-duration-in-seconds:10  # How long does it take to get a heartbeat? Delete this instance, default 90 seconds
Client Configuration (service consumer)
Eureka:
  client:
    registry-fetch-interval-seconds:10  #客户端每隔10秒刷新下服务提供者信息, default 30 seconds
logging: Level
  :
    com.netflix:debug

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.