服务注册与发现-Eureka集群模式

1 篇文章 0 订阅

服务注册与发现-Eureka集群模式


Eureka单机模式下.我们可以看出.在分布式应用开发下.单机模式并不能适用于生产环境.

前言: 在使用Eureka过程中.Client会定时连接Eureka Server.获取服务注册表中的信息并缓存到本地.微服务在消费远程服务时总是先使用本地缓存中的数据.那么.即便是Eureka Server发生了宕机.也不会影响到服务之间的调用.但是如果某个微服务也出现了不可用的情况.Eureka Client中的缓存将不会被更新.就会影响到服务的调用.甚至影响到了整个应用系统.于是.集群模式便应运而生

1.集群配置

进入代码:

把上一次单机模式的代码拉过来
1.修改application.yml文件

spring:
  application: micro-service-discover-eureka-ha
  profiles:
    active: server2
---
spring:
  profiles: server1
server:
  port: 8761

eureka:
  instance:
    hostname: ${spring.profiles}
  client:
    register-with-eureka: true  #是否将自己注册要服务中心去,默认为true.
    fetch-registry: true #表示是否从Eureka Server中获取注册信息.默认为true
    service-url:
      defaultZone: http://server2:8762/eureka/ #查询服务和注册服务都需要依赖这个地址.默认是http://localhost:8761/eureka,多个地址使用,分隔

---
spring:
  profiles: server2
server:
  port: 8762

eureka:
  instance:
    hostname: ${spring.profiles}
  client:
    register-with-eureka: true  #是否将自己注册要服务中心去,默认为true.
    fetch-registry: true #表示是否从Eureka Server中获取注册信息.默认为true
    service-url:
      defaultZone: http://server1:8761/eureka/ #查询服务和注册服务都需要依赖这个地址.默认是http://localhost:8761/eureka,多个地址使用,分隔
      

注:在server1中,我将defaultZone修改为server2的地址.server2的defaultZone修改为server1.使得两个server之间相互注册复制

2.配置hosts文件.增加主机名称
同时修改我们系统的host文件,我用的是win7,在C:\Windows\System32\drivers\etc\中

#在最底下插入.如果已存在127.0.0.1的话.只需要在后面追加server1 server2即可
127.0.0.1 server1 server2

3.分别打包server1和server2,各自启动.第一个启动的jar将会抛出cannot execute request on any known server异常.这个是因为Eureka要相互注册.而第一位启动的jar因为没有可以正常返回响应的服务.

4.两个服务启动成功之后.我们分别访问http://localhost:8761/http://localhost:8762/ 出现如下界面即配置成功
Eureka首页
5.启动我们的client客户端.分别刷新两个Eureka首页.可看到新增了一个服务实例.
Eureka首页
先看看我client的application.yml配置

server:
  port: 8080
spring:
  application:
    name: micro-provider-user #指定注册到Eureka Server上的应用名称
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true #表示将自己的ip地址注册到Eureka Server.如果不设置改属性.或将该属性设置为false,则表示注册微服务所在操作系统的hostname到Eureka Server

我这里的defaultZone只向server1的Eureka Server注册了.而两个Eureka Server的首页都出现了.这就是相互集群模式下的复制了…这里我们也可写上所有的Eureka Server的地址.用,隔开即可

2.Eureka权限认证-额外的安全性

1.这里需要通过spring-security来实现.首先引入spring-boot-starter-security依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2.重启Eureka Server.访问localhost:8762/如图可见
Eureka首页
因为security的密码默认是随机生成的.需要我们手动配置用户名和密码

  security:
    user:
      name: user #自定义的用户民
      password: root #自定义的密码

3.因为我们Eureka Server增加了权限认证.那么我们的client向服务进行注册的时候.defaultZone就不能再和之前一样了.我们需要把用户和密码添加到我们的defaultZone上去,如下所示.

defaultZone: http://user:root@localhost:8762/eureka/

改完之后问题又来了…我的微服务死活注册不上Eureka Server.网上说要开启Security.bisic.enable.但是新版的security里面已经禁用这个配置了…不慌.问题不大.新增一个SecurityConfig类,继承SecurityConfigurerAdapter,重写configure方法.开启这个配置

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

重启之后还是注册不上…然后一查百度.发现是这个新版的默认开启了csrf.关闭!!!代码修改之后如下

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

.重启Eureka和微服务.控制台输出,嗯.注册成功了.
Eureka注册控制台打印日志
Eureka首页

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值