Learning from the Spring cloud-based microservices-3 service governance-spring Cloud Eureka's high-availability registry

Source: Internet
Author: User

What do you mean high availability

High availability generally refers to the redundancy of services, a service is hung, can automatically switch to another service, does not affect the customer experience.

High-Availability Registration center

In a distributed environment such as the microservices architecture, we need to take full account of the failure situation, so that each component must be highly available for deployment in a production environment, as for microservices, and for service centers.

Eureka Server was designed with high availability in mind at the outset, and in Eureka's service governance design, all nodes are both service providers and service consumers, and service registries are no exception. This configuration was used in the previous essay:

Eureka.client.register-with-eureka=false
Eureka.client.fetch-registry=false

Now review the effects of the two properties mentioned in the previous article:

    • Eureka.client.register-with-eureka=false set to not register itself with the service registry (default is True)
    • Eureka.client.fetch-registry=false is set to do not retrieve service (by default, the service registry does not need to retrieve its own services in the case of a single-node service registry)

The high availability of Eureka server is essentially registering itself as a service to other service registries, which can form a set of mutually-registered service registries to synchronize the service inventory to achieve a high availability effect.

Implementing a highly available service registry using spring Cloud Eureka

Build a two-node service registry cluster based on the previous service registration center.

    1. Create the application-peer1.properties as a configuration for the Peer1 Service center and point to the serviceurl (the address to register with the service registry) to Peer2

Spring.application.name=eureka-server
server.port=1111

#也可以省去下面对hostname的配置, use the IP address directly at Defaultzone, but you need to configure eureka.instance.prefer-ip-address=true
Eureka.instance.hostname=peer1
#eureka. client.serviceurl.defaultzone=http://localhost:1111/eureka/
eureka.client.serviceurl.defaultzone=http://peer2:1112/eureka/

2. Create the application-peer2.properties as a configuration for the Peer2 Service center and point to the serviceurl (the address to register with the service registry) to Peer1

Spring.application.name=eureka-server
server.port=1112

#也可以省去下面对hostname的配置, use the IP address directly at the Defaultzone location

Eureka.instance.hostname=peer2
eureka.client.serviceurl.defaultzone=http://peer1:1111/eureka/

3. Add the conversion of Peer1 and Peer2 to the/etc/hosts file, so that the serviceurl configured in the host form can be properly accessed locally; The Windows system path is C:\WINDOWS\SYSTEM32\DRIVERS\ETC \hosts.

127.0.0.1 Peer1
127.0.0.1 Peer2

4. Use MVN clean package-dmaven.skip.test=true to package the project at the root of the project, and then use Java-jar Eureka-server-1.0.0.jar- The Spring.profiles.active=peer1/peer2 command launches Peer1 and Peer2 respectively.

Visit Peer1 's Registration center at this time: http:localhost:1111/

Visit http://localhost:1112/

As we can see in the Peer1, there are already peer2 shards and are registered in Peer2 and Peer1 respectively, and these nodes are in the available shards (available-replicase).

If we close one of the Peer1, the Peer1 node becomes unavailable shard:

5. Modify the service provider to register it in the Eureka server cluster.

Spring.application.name=hello-service
Eureka.client.serviceurl.defaultzone=http://peer1:1111/eureka,http://peer2:1112/eureka

Then start the service to access http://localhost:1111/and http://localhost:1112/:

Try to close peer1:

Continue to visit Localhost:8080/hello

As you can see in the diagram, the Peer1 has been closed, but the Hello-service service can still be accessed, which implements the highly available row in the service registry.

Figure

Create a service consumer

Next, create a service consumer, mainly to complete two goals: Discovery Services, consumer services. Where the service discovery task is done by the Eureka client, the service consumption is done by the Ribbon.

The Ribbon is a client-side load balancer based on HTTP and TCP, which can poll access to achieve a balanced load by ribbonserverlist the list of server configurations configured in the client. When the ribbon is used in conjunction with the Eureka, it can be removed from the Ribbonserverlist configuration and automatically performed by the Eureka. We just need to know that the Ribbon implements a selection strategy for the service instance.

1. Use the Java-jar command to start a Hello-service service with two different ports.

2. Create the Spring Boot project, named: Ribbon-consumer, and introduce the following dependencies in the POM:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

<dependencymanagement>
     <dependencies>
          <dependency>
              <groupid>org.springframework.cloud</groupid>
              <artifactid>spring-cloud-dependencies</artifactid>
              <version>Camden.SR7</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </ Dependencies>
</dependencymanagement>

3. Add the @enablediscoveryclient annotation to the application main class to register it as a Eureka client, enabling it to have the ability to discover the service. At the same time, create an instance of the Resttemplate spring Bean in the main class and turn on client load balancing with the @loadbalanced annotations:

@EnableDiscoveryClient
@SpringBootApplication

public class Ribbonconsumerapplication {

@Bean
@LoadBalanced
Resttemplate Resttemplate () {
return new Resttemplate ();
}
public static void Main (string[] args) {
Springapplication.run (Ribbonconsumerapplication.class, args);
}
}

4. Create the Consumercontroller class and implement the/ribbon-consumer interface. Calls to the/hello interface provided by the Hello-service service are implemented by calling Resttemplate.

@RestController
public class Consumercontroller {
@Autowired
Private Resttemplate resttemplate;

@RequestMapping (value= "/ribbon-consumer", Method=requestmethod.get)
Public String Helloconsumer () {
Return resttemplate.getforentity ("Http://HELLO-SERVICE/hello", String.class). GetBody ();
}
}

It is a very important feature in the service governance framework to use the service name instead of a specific address in the URL. For example, because the service registry maintains many service instances from different hosts that have the same service name, when we need to call

Service, there is no need to know the specific address of the service, the mapping from the service name to the address has been handed over to the service registry (during which many other tasks can be completed).

5. Modify Application.properties

Spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceurl.defaultzone=http://localhost:1111/eureka/

6. After starting the Ribbon-consumer app, access the http://localhost:1111

Verifying Ribbon Load Balancing

In the browser input http://localhost:9000/ribbon-consumer multiple access Ribbon-consumer, respectively, to view the Hello-service console information:

They can be found alternately printed out:/hello, HOST:DESKTOP-48KOCS1, Service_id:hello-service. Demonstrate that the ribbon is in effect and that load balancing is implemented by polling.

Reference documents:

Spring Cloud Project Combat

Learning from the Spring cloud-based microservices-3 service governance-spring Cloud Eureka's high-availability registry

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.