SpringCloud 入门笔记(二)Eureka服务注册中心

目录

1 概述

2 Eureka Server

2.1 构建Server项目

2.2 添加Spring Security认证

3 Eureka Client

3.1 构建Client项目

4 测试

5 Eureka Server 集群


1 概述

Eureka是一个服务注册中心,分为Eureka Server和Eureka Client,Server和Client均是SpringBoot应用程序,其中Client即为各个微服务,其需要向Server进行注册,并发送心跳来维护活跃,下面开始构建一个简单的Eureka服务注册环境。

2 Eureka Server

2.1 构建Server项目

通过IDEA的Spring Initializr创建一个包含Eureka Server组件依赖的SpringBoot项目,如下图所示勾选Eureka Server依赖,填写项目的相关信息一路next到finish。

本例创建了一个名为eureka-server的SpringBoot项目,创建成功后,IDEA会自动为我们添加Eureka Server依赖,如下:

<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-test</artifactId>
            <scope>test</scope>
    </dependency>
</dependencies>

下面,对项目进行一些配置,启用Eureka Server的功能。

首先,编写 application.yml 配置文件,内容如下:

server:
  port: 8000
eureka:
  instance:
    hostname: 127.0.0.1 # 服务注册中心IP地址
  client:
    registerWithEureka: false # 向服务注册中心注册自己
    fetchRegistry: false # 检索服务
    service-url: # 指定服务注册中心的位置
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

由于配置的是server,不需要将server本身注册到服务中心,因此需要关闭一些默认为true的配置,最终的eureka注册地址为:http://127.0.0.1:8000/eureka,这也是client进行服务注册的请求地址。

然后,修改启动类,添加 @EnableEurekaServer 注解,如下:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

至此,一个单点的Eureka Server就搭建完成了,在浏览器输入localhost:8000,可以看到如下界面:

但是我们会发现,后台界面不需要任何认证即可访问,接下来,我们来为Server添加Spring Security认证。

2.2 添加Spring Security认证

在pom.xml添加如下依赖:

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

修改 application.yml 配置文件,启用Spring Security,如下:

server:
  port: 8000
spring:
  security:
    user:
      name: admin
      password: admin123
eureka:
  instance:
    hostname: 127.0.0.1 # 服务注册中心IP地址
  client:
    registerWithEureka: false # 是否向服务注册中心注册自己
    fetchRegistry: false # 是否检索服务
    service-url: # 服务注册中心的配置内容,指定服务注册中心的位置
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

需要注意的是,添加Security认证后,注册位置变成 http://admin:admin123@127.0.0.1:8000/eureka

修改启动类,添加@EnableWebSecurity注解,如下:

@SpringBootApplication
@EnableEurekaServer
@EnableWebSecurity
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

网上很多的教程均是通过此种方式进行配置的,但这种方式存在一个问题:Client无法向Server注册服务,错误信息为 Cannot execute request on any known server,具体可以参考SpringCloud下的ISSUE spring boot 2.0,eureka registration failed with spring security

原因为:SpringBoot 2.0版本起,Security中默认启用了CSRF保护,需要关闭CSRF保护。

新建Security配置类,如下:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .and()
                .httpBasic();
    }
}

这里需要注意的是,如果直接通过 http.csrf().disable(); 关闭SCRF保护(网上很多方案均是如此),那么也将关闭其他请求的认证,因此需要为其他的请求配置需要认证。

OK,到这里,Server的配置就完成了,再次访问后台界面时,浏览器就会弹出登录认证了。

3 Eureka Client

3.1 构建Client项目

通过IDEA的Spring Initializr创建一个包含Eureka Discovery组件依赖的SpringBoot项目,勾选如下图所示的Eureka Discovery依赖。Eureka Client即为各个微服务,下面创建一个示例服务eureka-client。

创建成功后,pom.xml依赖如下:

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

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

编写 application.yml 配置文件如下:

server:
  port: 8801
spring:
  application:
    name: eureka-client
eureka:
  client:
    service-url:
      defaultZone: http://admin:admin123@127.0.0.1:8000/eureka/

修改启动类,添加@EnableEurekaClient注解,表示这是一个Eureka Client应用,如下所示:

@SpringBootApplication
@EnableEurekaClient
public class UserMsApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserMsApplication.class, args);
    }
}

4 测试

1. 启动 eureka-server 项目

2. 启动 eureka-client  项目

3. 访问 eureka-server,localhost:8800,输入我们配置的用户名和密码,就可以看到 eureka-client 已经注册到server中了

5 Eureka Server 集群

上面构建的 Eureka Server 是一个单点部署的服务注册中心,这种方式在服务注册中心崩溃时会导致整个系统的瘫痪,通常hi使用集群化部署的方式来避免这个问题。

Eureka Server 集群的配置与单点配置大致相同,唯一的不同之处在于需要在每个 Server 的配置中指定集群中其他 Server 节点的地址,如下所示:

Server1配置:

server:
  port: 8000
eureka:
  instance:
    hostname: server1
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://server2:8001/eureka/,http://server3:8002/eureka/

Server2配置:

server:
  port: 8001
eureka:
  instance:
    hostname: server2
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://server1:8000/eureka/,http://server3:8002/eureka/

Server3配置:

server:
  port: 8002
eureka:
  instance:
    hostname: server2
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://server1:8000/eureka/,http://server2:8001/eureka/

按照如上配置,分别启动多个eureka-server实例即可启动集群化部署。

Client 在指定 Server 注册地址时,可以指定一个或多个地址,多个地址间用","分隔。

源码地址:https://github.com/GreedyStar/spring-cloud-demo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值