关于Eureka与Spring Cloud Config 集群部署配置总结

集群准备

  • 版本:spring.boot.version=2.2.2.RELEASE,spring-cloud=Hoxton.SR1
  • Eureka集群:peer1、peer2、peer3 三个节点(2个节点也可正常提供高可用),对应端口7001、7002、7001
  • Config集群:2个+节点,对应端口7005、7005
  • 一个客户服务测试
  • 记得将peer1 peer2 peer3 加入到自己host文件中

部署示意图

Eureka节点

  • pom.xml 关键依赖
<!--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-security</artifactId>
</dependency>
  • 整个工程目录结构

  • EurekaApp
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {

    public static void main(String args[]) {
    	//执行函数
        SpringApplication.run(EurekaApp.class, args);
    }

}
  • application.yml
spring:
  application:
    name: spc-eureka-service
  security:
    user:
      name: ${eureka.user}
      password: ${eureka.password}
  profiles:
    active: native

server:
  port: 7001

eureka:
  # eureka认证信息配置
  user: eureka
  password: eureka2021
  instance:
    hostname: localhost #eureka服务端的实例名称,单机版本
    prefer-ip-address: false
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    serviceUrl:
      # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址,单机模式
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#  server:
#    关闭自我保护机制,保证不可用服务被及时剔除
#    enable-self-preservation: false
#    eviction-interval-timer-in-ms: 2000
  • application-peer1.yml
server:
  port: 7001

eureka:
  client:
    serviceUrl:
      defaultZone: "http://${eureka.user}:${eureka.password}@peer2:7002/eureka,http://${eureka.user}:${eureka.password}@peer3:7003/eureka"
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: peer1
  • application-peer2.yml
server:
  port: 7002

eureka:
  client:
    serviceUrl:
      defaultZone: "http://${eureka.user}:${eureka.password}@peer1:7001/eureka,http://${eureka.user}:${eureka.password}@peer3:7003/eureka"
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: peer2
  • application-peer3.yml
server:
  port: 7003

eureka:
  client:
    serviceUrl:
      defaultZone: "http://${eureka.user}:${eureka.password}@peer1:7001/eureka,http://${eureka.user}:${eureka.password}@peer2:7002/eureka"
    register-with-eureka: true
    fetch-registry: true
  instance:
    hostname: peer3
  • WebSecurityConfig 解决Eureka集群部署下且开启安全认证问题
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
       // 关闭csrf
        http.csrf().disable();
        // 开启认证
        super.configure(http);
    }
}
  • 启动3个Eureka服务,分别带上参数

spring.profiles.active=peer1 spring.profiles.active=peer2 spring.profiles.active=peer3

  • 分别进入http://127.0.0.1:7001/等3个Eureka面板,输入用户名密码,查看集群效果

Spring Cloud Config

  • pom.xml
<!-- spring Cloud  Config  -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

<!--eureka-client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • application.yml
server:
  port: 7005

spring:
  application:
    name: spc-config-service
  profiles:
    # 激活使用哪种配置存储?支持 [git native svn]
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: file:///D:/data/code/ebs-spc-demo/spc-config-file
        git:
          # Git地址信息
          uri: https://gitee.com/7sh/ebs-spc-demo.git
          # 搜索路径
          search-paths: spc-config-file
          # 默认分支
          default-label: master
          # 用户名密码
          # username: xxxx
          # password: xxxxx

eureka:
  # eureka认证信息配置
  user: eureka
  password: eureka2021
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
	  #defaultZone: http://192.168.1.188:7001/eureka #单机版
      defaultZone: 
	 #集群版 http://${eureka.user}:${eureka.password}@peer1:7001/eureka,http://${eureka.user}:${eureka.password}@peer2:7002/eureka,http://${eureka.user}:${eureka.password}@peer3:7003/eureka
  instance:
    # instance-id: spc-config-service-qiy
    # 访问路径可以显示IP地址
    prefer-ip-address: true
    # Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-renewal-interval-in-seconds: 1
    # Eurekaf服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
    lease-expiration-duration-in-seconds: 2
  • 两个节点本地启动只需要修改下端口即可
  • 启动两个实例,看下效果

客户端(Eureka Client 与 Config Client)

  • pom.xml
<!-- Spring Cloud Config 客户端 -->
<!--eureka-client-->
<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-config</artifactId>
</dependency>
  • bootstrap.yaml
server:
  port: 8001

spring:
  application:
    name: spc-consumer-service
  profiles:
    active: local
  cloud:
    # 配置中心客户端相关配置
    config:
      # 配置文件名称,默认为微服务名称
      name: config,consumer
      # Git 分支
      label: master
      # 配置中心服务端地址,集群情况下配置多个,但不建议
      # uri: http://localhost:7005,http://localhost:7006
      # config 集群模式下,推荐通过如下2个配置项,从配置中心获取到config的服务地址
      discovery:
        enabled: true
        service-id: spc-config-service

eureka:
  #Eureka认证信息配置
  user: eureka
  password: eureka2021
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      #单机版
      #defaultZone: http://192.168.1.188:7001/eureka
      #集群版
      defaultZone: http://${eureka.user}:${eureka.password}@peer1:7001/eureka,http://${eureka.user}:${eureka.password}@peer2:7002/eureka,http://${eureka.user}:${eureka.password}@peer3:7003/eureka
  instance:
    #instance-id: consumer-order
    #访问路径可以显示IP地址
    prefer-ip-address: true
    #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-renewal-interval-in-seconds: 1
    #Eurekaf服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
    lease-expiration-duration-in-seconds: 2
  • 大功告成,最后启动下所有服务,分别看下3个Eureka面板效果

补充

  • 最后再记录一个yml配置文件小坑
  • 我们都知道,yml中的配置key,支持驼峰及减号分隔两种方式,且可兼容
  • 比如我配置一个key为 eureka.client.serviceUrl.defaultZone 在代码中可以通过 eureka.client.service-url.default-zone 获取到
  • 此时,如果用jar包启动时,在启动命令带上 --eureka.client.service-url.default-zone参数覆盖该值
  • 如果正好这个 eureka.client.serviceUrl.defaultZone 参数被 另外某个变量引用,例如:
registry:
  eureka:
    serviceUrl: ${eureka.client.serviceUrl.defaultZone}
  • 如果在用启动命令带上 --eureka.client.service-url.default-zone参数覆盖该值时,注意,原生使用该值的变量会被覆盖
  • 而通果占位符引用该配置的变量值,将无法识别到命令行参数值
  • 此坑需谨记,所以命名最好都统一,至少针对某个配置项key
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值