purpose:
The first common way
The second way Sao operation
Eureka Cluster Setup
He said point is easy to understand, with a project deployed on multiple servers.
When the registry could not carry high concurrent time, this time to use clusters to carry;
Today introduced two ways to build:
The first way ordinary way:
And then built on the blog post on the two projects, and the configuration of these two projects can not are local, open three virtual machines too much trouble, so we direct to configure local hosts to achieve the machine domain mapping
into the your system file: C: \ Windows \ System32 \ the Drivers \ etc to find the hosts file , and then add your domain name mapping, behind the configuration file in yml
127.0.0.1 eureka2001.ht.com
127.0.0.1 eureka2002.ht.com
127.0.0.1 eureka2003.ht.com
Create three POM dependent on the same project Springboot
microservice-eureka-server-2001
microservice-eureka-server-2002
microservice-eureka-server-2003
pom-dependent
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.ht</groupId> <artifactId>htSpringCloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-eureka-server-2003</artifactId> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
The second step yml configuration file
microservice-eureka-server-2001
Server: Port: 2001 context -path: / Eureka: instance: # single hostname: localhost #eureka registry instance name hostname: eureka2001.ht.com # cluster Client: the Register the -with-Eureka: false # false Because the application is registered center, so set to false, the representative does not register itself with the registry. FETCH -registry: false # false because the responsibility is to maintain a registry of service instances, it does not need to retrieve the service, so it is set to false Service - url: defaultzone: HTTP: // eureka2002.ht.com:2002/eureka/ , http://eureka2003.ht.com : 2003 / Eureka / cluster #
microservice-eureka-server-2002
Server: Port: 2002 context -path: / Eureka: instance: # single hostname: localhost #eureka registry instance name hostname: eureka2002.ht.com # cluster Client: the Register the -with-Eureka: false # false Because the application is registered center, so set to false, the representative does not register itself with the registry. FETCH -registry: false # false because the responsibility is to maintain a registry of service instances, it does not need to retrieve the service, so it is set to false Service - url: defaultzone: HTTP: // eureka2001.ht.com:2001/eureka/ , http://eureka2003.ht.com : 2003 / Eureka / cluster # # single defaultZone: http:// $ {eureka.instance.hostname}: $ {server.port} / Eureka / Eureka # Set the registry interactive address, use the query service and registration services
microservice-eureka-server-2003
Server: Port: 2003 context -path: / Eureka: instance: # single hostname: localhost #eureka registry instance name hostname: eureka2003.ht.com # cluster Client: the Register the -with-Eureka: false # false Because the application is registered center, so set to false, the representative does not register itself with the registry. FETCH -registry: false # false because the responsibility is to maintain a registry of service instances, it does not need to retrieve the service, so it is set to false Service - url: defaultzone: HTTP: // eureka2001.ht.com:2001/eureka/ , http://eureka2002.ht.com : 2002 / Eureka / cluster # # single defaultZone: http:// $ {eureka.instance.hostname}: $ {server.port} / Eureka / Eureka # Set the registry interactive address, use the query service and registration services
To the service provider to modify application.yml reconfigure eureka
Server: Port: 1001 context -path: / the Spring: the DataSource: of the type: com.alibaba.druid.pool.DruidDataSource Driver - class - name: com.mysql.jdbc.Driver url: jdbc: MySQL: // localhost: 3306 / ? useUnicode the Test & characterEncoding to true = = utf8 username: root password: root JPA: Hibernate: DDL - Auto: Update Show -sql: to true Eureka: instance: #eureka instance name of the client host hostname: localhost # client service name appname: microservice-student # The client instance name instance-the above mentioned id: microService-Student: 1001 # show IP the prefer-ip-address: to true Client: Service-url: defaultzone: http://eureka2001.ht.com:2001/eureka/, HTTP: / /eureka2002.ht.com : 2002 / Eureka /, http://eureka2003.ht.com : 2003 / Eureka / info: groupId: com.ht.htSpringCloud artifactId: microService -student-Provider-1001 Version: 1.0- SNAPSHOT userName : HTTP: // ht.com Phone: 123456
Remember annotate @EnableEurekaServer start in each class
Run the file path to your own configuration
127.0.0.1 eureka2001.ht.com
127.0.0.1 eureka2002.ht.com
127.0.0.1 eureka2003.ht.com
The second way Sao operation
The above eureka service structures, in addition to yml file is not the same, other files are the same, so is there any way we can be more eureka set of services to a project go?
Create a Eureka-Server-microService (triple) sub-projects
Pom-dependent
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.ht</groupId> <artifactId>htSpringCloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-eureka-server</artifactId> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Yml configuration file
--- server: port: 2001 context-path: / eureka: instance: hostname: eureka2001.ht.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2002.ht.com:2002/eureka/,http://eureka2003.ht.com:2003/eureka/ spring: profiles: eureka2001 --- server: port: 2002 context-path: / eureka: instance: hostname: eureka2002.ht.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2001.ht.com:2001/eureka/,http://eureka2003.ht.com:2003/eureka/ spring: profiles: eureka2002 --- server: port: 2003 context-path: / eureka: instance: hostname: eureka2003.ht.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2001.ht.com:2001/eureka/,http://eureka2002.ht.com:2002/eureka/ spring: profiles: eureka2003
Remember to add in the startup class @EnableEurekaServer
package com.ht.microserviceeurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class MicroserviceEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceEurekaServerApplication.class, args); } }
效果:
Eureka自我保护机制
如果在服务注册中心注册过的服务提供者突然失去了联系的话,服务注册中心不会立即注销掉该服务提供者的信息,而是会等个90秒再注销。
这样就使得Eureka集群更加的健壮和稳定
谢谢观看!