Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build Eureka Cluster

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to build an Eureka cluster. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Eureka cluster building

0.1 Eureka cluster building

When running the first Eureka application, the server instance and the service provider instance are only started, and do not reflect the high availability features. This section will modify the previous Eureka application so that it can be deployed in a cluster.

0.1.1 this example cluster structure diagram

This example will run two server instances, two service provider instances, and then the service caller requests the service, as shown in figure 3-6.

Figure 3-6 Cluster structure

The first Eureka application uses a browser to access the service caller of Eureka, and after the transformation, in order to see the effect of load balancing, a REST client of HttpClient will be written to access the service published by the service caller.

Since the development environment of this book is only one computer and the operating system is Windows, if you want to build a cluster, you need to modify the hosts file and add the hostname mapping for it. Modify the C:\ Windows\ System32\ drivers\ etc\ hosts file to add the following:

127.0.0.1 slave1 slave2

0.1.2 transform the server side

The new project "first-cloud-server" uses Maven to configure the same server as Serial 4. Since you need to start the same application twice, you need to use profiles in the configuration file (profiles has been discussed in Serial 3). The server configuration file is shown in listing 3-10.

Listing 3-10:codes\ 03\ 3.3\ first-cloud-server\ src\ main\ resources\ application.yml

Server: port: 8761spring: application: name: first-cloud-server profiles: slave1eureka: instance: hostname: slave1 client: serviceUrl: defaultZone: http://slave2:8762/eureka/---server: port: 8762spring: application: name: first-cloud-server profiles: slave2eureka: instance: hostname: slave2 client: serviceUrl: defaultZone: http://slave1:8761/eureka/

Two profiles, named slave1 and slave2, are configured in listing 3-10. In slave1, the application port is configured as 8761 and the host name is slave1. When you use the profiles of salve1 to start the server, you will register yourself with http://slave2:8762/eureka/. If you start the server with salve2, you will register yourself with http://slave1:8761/eureka/. To put it simply, after the two servers are started, they will register with each other.

Modify the startup class so that when it starts, it reads the console input and decides which profiles to use to start the server, as shown in listing 3-11.

Listing 3-11:

Codes\ 03\ 3.3\ first-cloud-server\ src\ main\ java\ org\ crazyit\ cloud\ FirstServer.java

@ SpringBootApplication@EnableEurekaServerpublic class FirstServer {public static void main (String [] args) {/ / read console input and decide which profiles Scanner scan = new Scanner (System.in); String profiles = scan.nextLine (); new SpringApplicationBuilder (FirstServer.class) .profiles (profiles) .run (args);}}

In the startup class, the input of the control is read first, and then the profiles method is called to set the startup profles. It is important to note that the first server to start will throw an exception. The reason for the exception we have described before, and the exception thrown can be ignored.

0.1.3 transform service provider

The service provider also needs to start two instances. The transformation of the service provider is similar to that of the server, copying the "first-ek-service-provider" in side 4 and renaming it to "first-cloud-provider". Modify the configuration file to register the service provider with two servers, as shown in listing 3-12.

Listing 3-12:codes\ 03\ 3.3\ first-cloud-provider\ src\ main\ resources\ application.yml

Spring: application: name: first-cloud-providereureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

Then modify the startup class, in order to avoid port decision, read the console output at startup and decide which port to use to start, as shown in listing 3-13.

Listing 3-13:

Codes\ 03\ 3.3\ first-cloud-provider\ src\ main\ java\ org\ crazyit\ cloud\ FirstServiceProvider.java

@ SpringBootApplication@EnableEurekaClientpublic class FirstServiceProvider {public static void main (String [] args) {/ / read the port entered by the console to avoid port conflicts Scanner scan = new Scanner (System.in); String port = scan.nextLine () New SpringApplicationBuilder (FirstServiceProvider.class) .properties ("server.port=" + port) .run (args);}}

The properties method is used in the startup class to set the startup port. In order to see the effect, you also need to modify the controller to save and return the URL requested by the service caller, as shown in listing 3-14.

Listing 3-14:

Codes\ 03\ 3.3\ first-cloud-provider\ src\ main\ java\ org\ crazyit\ cloud\ FirstController.java

@ RestControllerpublic class FirstController {@ RequestMapping (value = "/ person/ {personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Person findPerson (@ PathVariable ("personId") Integer personId, HttpServletRequest request) {Person person = new Person (personId, "Crazyit", 30) / / to view the result, set the requested URL to person.setMessage (request.getRequestURL () .toString ()) in the Person instance; return person;}}

The findPerson method of the controller saves the requested URL to the message property of the Person instance, and after invoking the service, you can view the requested URL through the message property.

0.1.4 transform service caller

Copy and rename "first-ek-service-invoker" in Serial 4 to "first-cloud-invoker". The service caller in this example only needs to start one instance, so modify the configuration file to use it, as shown in listing 3-15.

Listing 3-15:codes\ 03\ 3.3\ first-cloud-invoker\ src\ main\ resources\ application.yml

Server: port: 9000spring: application: name: first-cloud-invokereureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://slave1:8761/eureka/,http://slave2:8761/eureka/

Modify the configuration to register the service call on both servers.

0.1.5 write a REST client for testing

This example uses HttpClient,HttpClient, an HTTP toolkit provided by Apache. Create a new project named "first-cloud-rest-client" and add the following dependencies to pom.xml:

Org.apache.httpcomponents httpclient 4.5.2

Create a new startup class and write the code to invoke the REST service in the main method, as shown in listing 3-16.

Listing 3-16:

03\ 3.3\ first-cloud-rest-client\ src\ main\ java\ org\ crazyit\ cloud\ TestHttpClient.java

Public static void main (String [] args) throws Exception {/ / create the default HttpClient CloseableHttpClient httpclient = HttpClients.createDefault (); / / invoke the service 6 times and output the result for (int I = 0; I < 6) ) {/ / call the GET method to request the service HttpGet httpget = new HttpGet ("http://localhost:9000/router"); / / get the response HttpResponse response = httpclient.execute (httpget)) / / parse the string System.out.println (EntityUtils.toString (response.getEntity () according to the response;}}

In the main method, the router service on port 9000 is called six times and the result is output. When you are finished writing, start the components in the following order:

 starts two servers, and enter "slave1" and "slave2" in the console.

 starts two service providers, and the console enters 8081 and 8082, respectively.

 starts the service caller.

After starting the entire cluster, run TestHttpClient, and you can see the output as follows:

{"id": 1, "name": "Crazyit", "age": 30, "message": "http://localhost:8081/person/1"}{"id":1,"name":"Crazyit","age":30,"message":"http://localhost:8082/person/1"}{"id":1,"name":"Crazyit","age":30,"message":"http://localhost:8081/person/1"}{"id":1,"name":"Crazyit"," "age": 30, "message": "http://localhost:8082/person/1"}{"id":1,"name":"Crazyit","age":30,"message":"http://localhost:8081/person/1"}{"id":1,"name":"Crazyit","age":30,"message":"http://localhost:8082/person/1"}"

According to the output results, ports 8081 and 8082 have been requested three times respectively, which shows that the purpose of load balancing has been achieved.

On how to achieve Eureka cluster building to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report