We saw in the previous section SpringCloud components: Building had a service registry study to create a single service registry, but in real combat deployment of the single mode does not advocate, because there are many kinds of reasons may lead to service registry downtime, if the outage there will be some disastrous problem, so to ensure the live operation of the service registry is important!!!!!!
Objective in this chapter
The Eureka service registry is deployed in the ha cluster.
Build the project
Use the IDEA development tool to create a SpringBoot project and add the Eureka Server dependency. The pom.xml configuration file is as follows:
. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > at 2.0.5. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <! --Eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
</dependency>
</dependencies>
......
Copy the code
In this chapter, we mainly complete the cluster configuration of Eureka Server, so we only need to add the spring-cloud-starter-Netflix-Eureka-server dependency.
Enable the Eureka Server
Add the @enableEurekaserver annotation to the entry class XxxApplication to enable the EurekaServer service and instantiate some dependencies as follows:
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaHighApplication {
//....
}
Copy the code
Eureka service configuration
Now that the dependencies have been added, we need to write the relevant configuration information in application.yml. Since the test environment is on our machine, there are two ways to simulate the test running simultaneously:
- Create two different projects
- Use an item based on
spring.profiles.active
Set up to run different environments
The second approach we used, for demonstration purposes, mainly felt that there was no need to create another project, so how would our Profiles environment be configured? Keep reading.
Profile Configure multiple environments
We create a configuration file named application-node1.yml in the SRC /main/resources directory and add the following configuration to the file:
# Eureka client configuration
eureka:
client:
service-url:
defaultZone: http://node2:10002/eureka/
instance:
Configure host name registration
hostname: node1
# Configure the instance number
instance-id: ${eureka.instance.hostname}:${server.port}:@project.version@
Read timeout between cluster nodes. Unit: millisecond
server:
peer-node-read-timeout-ms: 1000
# Service port number
server:
port: 10001
Copy the code
Go ahead and create a configuration file named application-node2.yml under SRC /main/resources as follows:
# Eureka client configuration
eureka:
client:
service-url:
defaultZone: http://node1:10001/eureka/
instance:
Configure host name registration
hostname: node2
# Configure the instance number
instance-id: ${eureka.instance.hostname}:${server.port}:@project.version@
Read timeout between cluster nodes. Unit: millisecond
server:
peer-node-read-timeout-ms: 1000
server:
port: 10002
Copy the code
Node1, node2, node2, node1, node2
Hostname Settings
Mac
orLinux
Configuration mode if you are usingosx
System. You can find the/etc/hosts
File and add the following:
127.0.0.1 node1 127.0.0.1 2Copy the code
Generally, the configuration takes effect after it is complete. If your configuration does not take effect, you can restart it.
Windows
Configuration mode if you are usingwindows
System, you can modifyC:\Windows\System32\drivers\etc\hosts
File, add content withMac
In the same way.
Eureka Sever mutual registration
application-node1.yml
Eureka. Client. The service – url. DefaultZone this configuration parameter values, configuration is http://node2:10002/eureka/, what is that the 2 here? This is the hostname we configured in the hosts file, and the port number we configured is 10002. According to the hostname and port, we can see that node1 is registered with Node2.
application-node2.yml
Within 2 environment configuration eureka. Client. The service – url. DefaultZone is pointing to http://node1:10001/eureka/, the same 2 registered the node1.
This mutual registration firmly binds the two service registries together.
Run the test
Let’s first run a test to see if Eureka Server cluster is feasible. In the next section, we will explain how to register the service provider to the Eureka cluster. The test steps are as follows:
- Clean && Package This project (The diEA tool provides shortcuts for maven common operation commands, which are displayed in the right navigation bar
Maven Projects -> Lifecycle
)- Open a terminal
cd
projecttarget
directory- Run the following command to start the service
node1
Environment:
Java jar hengboy - spring - the cloud - eureka - high - 0.0.1 - the SNAPSHOT. Jar -- spring. Profiles. The active = node1Copy the code
- Open up another terminal, again
cd
The projecttarget
Run the following command to start the vmnode2
Environment:
Java jar hengboy - spring - the cloud - eureka - high - 0.0.1 - the SNAPSHOT. Jar -- spring. Profiles. The active = 2Copy the code
- access
http://node1:10001
To viewnode1
The environmentEureka
Management center- access
http://node2:10002
To viewnode2
The environmentEureka
Management center
The effect is shown below:
conclusion
This chapter explains how to build Eureka Server in a cluster environment to make it more robust. In the next chapter, we will look at how to register service providers in a Eureka Server cluster.
Suggestion: In actual situations, you are advised to put Eureka Server nodes on different servers and register them with each other using host names or Intranet.
Source location
- SpringBoot matching source address: access code cloud view source code, access GitHub view source code
- SpringCloud source code address (
The source code of this chapter is here
) :Access code cloud to view the source code.Visit GitHub to view the source code
Have a question?
If you have any technical questions you would like to consult Hengyu Teenager, please go to the left navigation bar of the blog home page, click On the knowledge planet wechat scan code to join my Planet.
Face to face with Hengyu youth
If you like heng Yu teenager related articles, then go to wechat public number (Heng Yu teenager) follow me!! Of course, you can also go to SpringCloud code cloud source code project bottom scan wechat public qr code to follow me, thank you for reading!!
Study catalog recommendation
- SpringCloud series related to visit: www.jianshu.com/p/64e4aaada.
- SpringBoot related series please visit: www.jianshu.com/p/9a08417e4.
- QueryDSL related series please visit: www.jianshu.com/p/99a5ec5c3.
- SpringDataJPA related series please visit: www.jianshu.com/p/615ed9c1f.
Open source information
During this period of time, I have been writing relevant open source frameworks, devoting myself to the framework upgrade and open source plan used by the company, and upgrading and reconstructing the tools and plug-ins used by the company and opening source.
- Code Builder
code-builder
Code generators are based on the template files you provide (currently supportedfreemarker
) automatically generate entity classes, can greatly improve the efficiency of development.Gitee address
:Gitee.com/hengboy/cod…Making the address
:Github.com/hengyuboy/c… - Persistence Framework (MyBatis-Enhance)
mybatis-enhance
Is a rightmybatis
The enhanced encapsulation of the framework provides a series of internal methods to perform operations on single-table data, and multi-table data is providedDSL
Mode to perform operations.Gitee address
:Gitee.com/hengboy/myb…Making the address
:Github.com/hengyuboy/m… - Automatic paging plug-in
MyBatis-Pageable
Is a plug-in for automatic paging, based onMyBatis
Internal pluginsInterceptor
Interceptor written, interceptExecutor.query
The two overloaded methods of compute the paging information as well as the database based on the configurationDialect
Automatic execution of different query statements to complete the total number of statistics.Gitee address
:Gitee.com/hengboy/myb…