SpringCloud(Hoxton.SR3)基础篇:第三章Eureka集群 高可用的认证服务实现与搭建
Posted 圣痕道心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud(Hoxton.SR3)基础篇:第三章Eureka集群 高可用的认证服务实现与搭建相关的知识,希望对你有一定的参考价值。
一、Eureka Server高可用搭建(服务注册中心)
1.1MAVEN相关依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.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> <!-- jdk版本 --> <java.version>1.8</java.version> <!-- SpringCloud版本号,官方最新稳定版本 --> <spring-cloud.version>Hoxton.SR3</spring-cloud.version> </properties> <!--依赖管理,用于管理spring-cloud的依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- eureka服务端依赖jar包 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- eureka安全组件jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies>
1.2 application.yml 相关配置
eureka服务一配置
server:
port: 8761
#安全认证配置
spring:
application:
name: EUREKA-HA
security:
basic:
enabled: true
user:
name: user
password: password123
eureka:
#Eureka实例名,集群中根据这里相互识别
instance:
hostname: peer1
client:
serviceUrl:
#注册中心地址
defaultZone: http://user:password123@peer2:8762/eureka/,http://user:password123@peer3:8763/eureka/
eureka服务二配置
server:
port: 8762
#安全认证配置
spring:
application:
name: EUREKA-HA
security:
basic:
enabled: true
user:
name: user
password: password123
eureka:
#Eureka实例名,集群中根据这里相互识别
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://user:password123@peer1:8761/eureka/,http://user:password123@peer3:8763/eureka/
eureka服务三配置
server:
port: 8763
#安全认证配置
spring:
application:
name: EUREKA-HA
security:
basic:
enabled: true
user:
name: user
password: password123
eureka:
#Eureka实例名,集群中根据这里相互识别
instance:
hostname: peer3
client:
serviceUrl:
defaultZone: http://user:password123@peer1:8761/eureka/,http://user:password123@peer2:8762/eureka/
1.2.2 配置hosts文件,识别peer1,peer2,peer3地址
windows系统hosts文件路径:C:\\Windows\\System32\\drivers\\etc
修改步骤:以管理员的身份运行CMD,输入notepad打开记事本。用记事本往hosts文件内添加内容
hosts文件加入peer1,peer2,peer3内容
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
1.3 启动类代码
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication //该注解表明应用为eureka服务,有可以联合多个服务作为集群,对外提供服务注册以及发现功能 @EnableEurekaServer public class EurekaHaApplication { public static void main(String[] args) { SpringApplication.run(EurekaHaApplication.class, args); } }
1.4 解决页面中Instances currently registered with Eureka下面并没得注入的别的服务
禁用Spring Security的CSRF保护,添加一个配置类禁用csrf
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; /** * eureka开启服务无法连接注册中心 * spring Cloud 2.0 以上 security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false * @author computer * */ @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); super.configure(http); } }
1.5 eureka页面出现unavailable-replicas(不可用分片)问题
最后成功启动三个eureka服务,结果如图
参考文献:https://blog.csdn.net/longguo321/article/details/80493618
https://blog.csdn.net/liupeifeng3514/java/article/details/85273961
以上是关于SpringCloud(Hoxton.SR3)基础篇:第三章Eureka集群 高可用的认证服务实现与搭建的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud(Hoxton.SR3)基础篇:第四章Hystrix请求熔断与服务降级
SpringCloud(Hoxton.SR3)基础篇:第五章Hystrix-request collapsing(请求合并)