Learn Springcloud every day (v): How to use highly available Eureka

Source: Internet
Author: User

Previous articles we talked about the basic use of Eureka, but there is a very important problem, we are talking about a single version of the situation, if this time the Eureka service hangs, then our service providers and service consumers are not all waste? If the service provider and the consumer are dead, then does this program have any meaning?

So let's talk about how to solve the problem today. I believe we all know that this is a problem that we have to go through to provide highly available services. The solution is to add a cluster, so let's see how Eureka implements the cluster.

1. First of all, we take the original Cloud-demo-eureka project responsible for a copy named Cloud-demo-eureka-hign (High-availability service, haha)

Note children who have not seen the previous articles can find the GitHub address at the bottom of the article first look at the code.

For the sake of convenience, we first remove the reliance on security.

<?xml version="1.0" encoding="UTF-8"?><Projectxmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" ><Parent><Artifactid>spring-cloud-demo</Artifactid><Groupid>cn.org.zhixiang</Groupid><Version>0.0.1-snapshot</Version></Parent><modelversion>4.0.0</Modelversion><Artifactid>cloud-demo-eureka-high</Artifactid><Properties><Project.build.sourceencoding>utf-8</Project.build.sourceencoding><Project.reporting.outputencoding>utf-8</Project.reporting.outputencoding><java.version>1.8</Java.version></Properties><Dependencies><dependency>      <groupid>org.springframework.cloud</< Span class= "Hljs-name" >groupid>      <!-- Note that the dependency here is SpringBoot2.0 later, if you use a springboot version less than 2.0, use spring-cloud-starter-eureka-server-->       <artifactid> Spring-cloud-starter-netflix-eureka-server</artifactId>     </dependency>  </dependencies></ PROJECT>             

2. Start-up class just need to change the name of the other unchanged

@SpringBootApplication@EnableEurekaServerpublic class Clouddemoeurehighapplication { main (string[] args) {
         
          springapplication
          . Run (clouddemoeurehighapplication.class, args); }}
         

3. We're going to play something different now, we've used 4 configuration files for this project. They are application.yml, application-dev1.yml, Application-dev2.yml, APPLICATION-DEV3.YML, respectively. This explains why the use of 4 configuration files, if we in the production environment here should actually have three programs, but for testing convenience, we use this program as three use.

Let's see what our application.yml file looks like.

Spring:  profiles:    active:dev1  application:    name:eureka-server-highDev: host:localhost876187628763        

We can see that the top-most is a spring.profiles.active property, which is usually useful for switching configurations in our different environments. For example, we may have a different environment for development, testing, production, and so on, which is certainly not a database, and it would be exhausting if we had to modify the configuration file every time in each environment. And the Spring.profiles.active property, that is, specifies that our project launches the loaded configuration file, for example, we have a value of DEV1, then we will load the Application-dev1.yml file when we start the project.

Followed by Spring.application.name, I believe we all know, our application name is Eureka-server-high.

The following is actually my custom configuration, because this time we want to use three Eureka services to form a cluster, so I first specify the port number of these three services, and why in this designation believe to see the following three configuration files you understand.

4. First Look at Application-dev1.yml

Server:  ${dev.dev1port}Eureka:  Client: service-url:http://${dev.host}:${ Dev.dev2port}/eureka,http://${dev.host}:${dev.dev3port}/eureka    

You can see that the port number is defined first as the port number 8761 that we defined in the Application.yml file, and then the address it registered becomes two, which is the program with port number 8762 and 8763.

Keep looking at application-dev2.yml.

Server:  ${dev.dev2port}Eureka:  Client: service-url:http://${dev.host}:${ Dev.dev1port}/eureka,http://${dev.host}:${dev.dev3port}/eureka    

It's registered 8761 and 8763 programs.

Well, now I think you should have guessed what Application-dev3.yml wrote.

Server:  ${dev.dev3port}Eureka:  Client: service-url:http://${dev.host}:${ Dev.dev1port}/eureka,http://${dev.host}:${dev.dev2port}/eureka    

5. We can start the configuration file after it's done.

First we go to the Clouddemoeurehighapplication class right-click Run to start a program, here you will find two suspects:

Project start error AH. Haha, actually do not fear, error is actually normal, not error is strange. Do you think of the reason for the error? The profile we started using was dev1, and he started to register a project with a port number of 8762 and a port number of 8763, none of the two items, so it would be an error. But it doesn't matter, Eureka actually started successfully, now you open the browser to access the following localhost:8671 is actually can see Eureka has registered successfully.

There is also a doubt is: Hey, small make up you do not say that we use a program to simulate three, but I started this after the start is not restarted it? How do I start a three?

In fact, because of the small part of the coup, and now we 8761 have been activated, right, next step with me to go

Click on the Edit Configuration first and then look

According to the label of the picture, click the plus icon First, and then a different name than clouddemoeurekahighapplication, here I added a-3, and then in the third position is the path of our startup class, the last position is to select the module to start. Now a new startup mode is done, and then we put spring in the Application.yml file.

Profiles.active changed into Dev3.

Then select the startup mode that you just defined in order, and click Debugger to start. Now is not found Dev3 program also started. I think you should know the reason, though it's still wrong.

Then follow the same, we start dev2, remember not to forget to modify the application.yml.

Now that three services are up, is our cluster a success?

Browser access localhost:8761 or 8762 or 8763 did you all see the three nodes?

If you see such an effect, then congratulations to you Eureka Cluster has been built successfully.

6. Use:

Now that the cluster has been built successfully, then you can use, remember how we put the client registration server, do not remember words please hurry up to review: every day to learn a bit Springcloud (ii): Service Registration and Discovery Eureka

When we registered, because there was no cluster, so it should be written.

Eureka:  client:    true    service-url:http://localhost:8761/eureka   

If you ask me if I can do it now, I tell you yes, it's OK, but I don't recommend it. Why, although we are now using a cluster, when our service provider registration should be three nodes will have this service provider, even if the 8761 node is hung up does not matter. However, if our service provider is registered at the time of 8761 has been hung, then it is not registered, others 8762 and 8763 now do not know it. So the way I recommend it to you is:

http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/ Eureka    

Github:https://github.com/2388386839/spring-cloud-demo

Code Cloud: Https://gitee.com/zhixiang_blog/spring-cloud-demo

If you have any help, please remember to help a star oh.

This article is from HTTPS://ZHIXIANG.ORG.CN/#/BLOG/READ/7C4E3FF7-786C-47FB-BDC3-D0856C8415FF, reproduced please retain.

Learn Springcloud every day (v): How to use highly available Eureka

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.