网站代运营做哪些,无网站无产品链接如何做SOHO,嵌入式软件开发文档,php网站建设招聘高可用连接方式
1、双机部署#xff1a;Eureka的server端相互注册#xff0c;自动相互同步应用信息。Eureka的client端注册到任意一个上面即可#xff0c;但为了保险起见#xff0c;可以同时注册到两个上面#xff0c;防止client注册到server1后#xff0c;server1挂掉Eureka的server端相互注册自动相互同步应用信息。Eureka的client端注册到任意一个上面即可但为了保险起见可以同时注册到两个上面防止client注册到server1后server1挂掉client重启后注册不上。 2、集群部署至少需要3个Eureka实例才能满足高可用配置方法如下准备三个节点node1、node2、node3。在每个实例的application.xml文件里加入eureka.client.service-url.defaultZone{address}address是其他节点的地址。如果是node1address就是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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud-learning/artifactIdgroupIdcom.ldzg/groupIdversion1.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdeureka-server1/artifactIddependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/projectapplication.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:123456replica2: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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud-learning/artifactIdgroupIdcom.ldzg/groupIdversion1.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdeureka-server2/artifactIddependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/projectapplication.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:123456replica1: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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud-learning/artifactIdgroupIdcom.ldzg/groupIdversion1.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdeureka-client/artifactIdversion0.0.1-SNAPSHOT/versionnameeureka-client/namedescription Spring cloud/description!-- dependencies--
!-- dependency--
!-- groupIdorg.springframework.boot/groupId--
!-- artifactIdspring-boot-starter-security/artifactId--
!-- /dependency--
!-- /dependencies--buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/projectapplication.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:123456replica1:60001/eureka/,http://ldzg:123456replica2: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 version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud-learning/artifactIdgroupIdcom.ldzg/groupIdversion1.1-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdspringcloud-consumer/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build
/projectapplication.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:123456replica1:60001/eureka/,http://ldzg:123456replica2:60002/eureka/ #同时注册到两个注册中心运行结果
5、注册服务中心结果页面
注册中心1
注册中心2
欢迎关注我的公众号不仅为你推荐最新的博文还有更多惊喜和资源在等着你!一起学习共同进步