高可用连接方式
1、双机部署:Eureka的server端相互注册,自动相互同步应用信息。Eureka的client端注册到任意一个上面即可,但为了保险起见,可以同时注册到两个上面,防止client注册到server1后,server1挂掉,client重启后注册不上。
2、集群部署:至少需要3个Eureka实例才能满足高可用,配置方法如下:准备三个节点node1、node2、node3。在每个实例的application.xml文件里加入eureka.client.service-url.defaultZone:{address},address是其他节点的地址。如果是node1,address就是http://node2/eureka、http://node3/eureka,其他节点依次类推。启动三个实例,注册信息会在他们之间互相同步。
3、负载均衡:客户端可以向多个Eureka节点发起请求,通过负载均衡算法选择一个节点来获取服务。这种方式可以在多个Eureka节点之间实现负载均衡和故障转移。
以上是Eureka高可用连接方式的一些实现方式,可以根据实际场景选择适合的方式来实现高可用连接。
双机部署方式–实践
架构图
1、Eureka-server1
java代码
package com.ldzg;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;import java.net.InetAddress;
import java.net.UnknownHostException;@Slf4j
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) throws UnknownHostException {ConfigurableApplicationContext application = SpringApplication.run(EurekaServerApplication.class, args);Environment env = application.getEnvironment();String ip = InetAddress.getLocalHost().getHostAddress();String port = env.getProperty("server.port");String path ="";// env.getProperty("server.servlet.context-path");log.info("\n----------------------------------------------------------\n\t" +"Application Eureka is running! Access URLs:\n\t" +"Local: \t\thttp://localhost:" + port + path + "/\n\t" +"External: \thttp://" + ip + ":" + port + path + "/\n\t" +"swagger-ui: \thttp://" + ip + ":" + port + path + "/swagger-ui.html\n\t" +"Doc: \t\thttp://" + ip + ":" + port + path + "/doc.html\n" +"----------------------------------------------------------");}}
package com.ldzg.config;import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** 默认情况下SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,* Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。* Created by macro on 2024/1/12.*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().ignoringAntMatchers("/eureka/**");super.configure(http);}
}
pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-learning</artifactId><groupId>com.ldzg</groupId><version>1.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-server1</artifactId><dependencies><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-security</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml配置文件说明
spring:profiles:active: replica1 #需要使用的配置文件的后缀
application-replica1.yml
server:port: 60001
spring:application:name: eureka-serversecurity: #配置SpringSecurity登录用户名和密码basic:enabled: trueuser:name: ldzgpassword: 123456
eureka:instance:hostname: replica1client:serviceUrl:defaultZone: http://ldzg:123456@replica2:60002/eureka/ #注册到另一个Eureka注册中心fetch-registry: falseregister-with-eureka: false
运行结果
2、Eureka-server2
java代码
package com.ldzg;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;import java.net.InetAddress;
import java.net.UnknownHostException;@Slf4j
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) throws UnknownHostException {ConfigurableApplicationContext application = SpringApplication.run(EurekaServerApplication.class, args);Environment env = application.getEnvironment();String ip = InetAddress.getLocalHost().getHostAddress();String port = env.getProperty("server.port");//String path = env.getProperty("server.servlet.context-path");log.info("\n----------------------------------------------------------\n\t" +"Application Eureka is running! Access URLs:\n\t" +"Local: \t\thttp://localhost:" + port + "/\n\t" +"External: \thttp://" + ip + ":" + port + "/\n\t" +"swagger-ui: \thttp://" + ip + ":" + port + "/swagger-ui.html\n\t" +"Doc: \t\thttp://" + ip + ":" + port + "/doc.html\n" +"----------------------------------------------------------");}}
package com.ldzg.config;import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** 默认情况下SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,* Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。* Created by macro on 2024/1/12.*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().ignoringAntMatchers("/eureka/**");super.configure(http);}
}
pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-learning</artifactId><groupId>com.ldzg</groupId><version>1.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-server2</artifactId><dependencies><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-security</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml配置文件说明
spring:profiles:active: replica2 #需要使用的配置文件的后缀
application-replica2.yml
server:port: 60002
spring:application:name: eureka-serversecurity: #配置SpringSecurity登录用户名和密码basic:enabled: trueuser:name: ldzgpassword: 123456
eureka:instance:hostname: replica2client:serviceUrl:defaultZone: http://ldzg:123456@replica1:60001/eureka/ #注册到另一个Eureka注册中心fetch-registry: trueregister-with-eureka: true
运行结果
3、Eureka-client
java代码
package com.ldzg;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaClientApplication.class, args);}}
pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-learning</artifactId><groupId>com.ldzg</groupId><version>1.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-client</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-client</name><description> Spring cloud</description><!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<!-- </dependencies>--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml配置文件说明
spring:profiles:active: replica #需要使用的配置文件的后缀
application-replica.yml
server:port: 8102
spring:application:name: eureka-client
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://ldzg:123456@replica1:60001/eureka/,http://ldzg:123456@replica2:60002/eureka/ #同时注册到两个注册中心
运行结果
4、Eureka-consumer
java代码
package com.ldzg;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class, args);}}
pom.xml文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springcloud-learning</artifactId><groupId>com.ldzg</groupId><version>1.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>springcloud-consumer</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
application.yml配置文件说明
spring:profiles:active: replica #需要使用的配置文件的后缀
application-replica.yml
server:port: 61002
spring:application:name: eureka-consumer
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://ldzg:123456@replica1:60001/eureka/,http://ldzg:123456@replica2:60002/eureka/ #同时注册到两个注册中心
运行结果
5、注册服务中心结果页面
注册中心1
注册中心2
- 欢迎关注我的公众号,不仅为你推荐最新的博文,还有更多惊喜和资源在等着你!一起学习共同进步!