【易实战】Spring Cloud Greenwich Eureka:服务注册与发现

写作时间:2020-02-10
Spring Cloud: Greenwich, Spring Boot: 2.1, JDK: 1.8, IDE: IntelliJ IDEA

说明

服务治理
由于Spring Cloud为服务治理做了一层抽象接口,所以在Spring Cloud应用中可以支持多种不同的服务治理框架,比如:Netflix Eureka、Consul、Zookeeper。在Spring Cloud服务治理抽象层的作用下,我们可以无缝地切换服务治理实现,并且不影响任何其他的服务注册、服务发现、服务调用等逻辑。

Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。

Spring Cloud Netflix主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。

本文将对搭建Eureka注册中心,搭建Eureka客户端,搭建Eureka集群及给Eureka注册中心添加登录认证进行介绍。

Spring Cloud是在Spring Boot的基础上开发的,如需要了解更多知识请参考专栏【易实战Sprint Boot 2.1】。

1. Eureka简介

在微服务架构中往往会有一个注册中心,每个微服务都会向注册中心去注册自己的地址及端口信息,注册中心维护着服务名称与服务实例的对应关系。每个微服务都会定时从注册中心获取服务列表,同时汇报自己的运行情况,这样当有的服务需要调用其他服务时,就可以从自己获取到的服务列表中获取实例地址进行调用,Eureka实现了这套服务注册与发现机制。

2. 搭建Eureka注册中心

2.1 创建一个新项目Create New Project
在这里插入图片描述
2.2 选择Sprint Boot项目Spring Initializr
在这里插入图片描述
2.3 填写应用信息
在这里插入图片描述
2.4 选择Spring Cloud 组件Spring Cloud Discovery** > **Eureka Server
在这里插入图片描述
2.5 创建完成后会发现pom.xml文件中已经有了eureka-server的依赖

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

2.6 在启动类上添加@EnableEurekaServer注解来启用Euerka注册中心功能

package com.zgpeace.cloud.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

2.7 在配置文件application.yml中添加Eureka注册中心的配置

server:
  port: 8001 #指定运行端口
spring:
  application:
    name: eureka-server #指定服务名称
eureka:
  instance:
    hostname: localhost #指定主机地址
  client:
    fetch-registry: false #指定是否要从注册中心获取服务清单 (注册中心不需要获取服务清单)
    register-with-eureka: false #指定是否要注册到注册中心 (注册中心不需要开启)
  server:
    enable-self-preservation: false

2.8 使用IDEA的Run Dashboard来运行SpringCloud应用

如果第一次用Run Dashboard, 需要安装下面的步骤开启。
I. 点击Idea头部菜单Edit Configurations...
在这里插入图片描述
II. 选择Defaults > 点击+号 > 添加Spring Boot
在这里插入图片描述
III. 在左下角就会有Run Dashboard的视图
在这里插入图片描述
IV. 另一种打开方式View > Tool Windows > Run Dashboard
在这里插入图片描述
V. 运行项目Run Dashboard > Spring Boot > Configured > 右键EurekaServerApplication > Run. (或者点击左侧的运行按钮)
在这里插入图片描述
2.9 运行完成后访问地址http://localhost:8001/可以看到Eureka注册中心的界面
在这里插入图片描述

3. 搭建Eureka客户端

3.1 新建一个eureka-client项目,并在pom.xml中添加如下依赖

<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-web</artifactId>
</dependency>

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

3.2 在启动类上添加@EnableDiscoveryClient注解表明是一个Eureka客户端

package com.zgpeace.cloud.eurekaclient;

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);
  }

}

3.3 添加根路径返回的json数据"Greetings from Spring Boot!"

package com.zgpeace.cloud.eurekaclient.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

  @RequestMapping("/")
  public String index() {
    return "Greetings from Spring Boot!";
  }
}

3.4 在配置文件application.yml中添加Eureka客户端的配置

server:
  port: 8101  #运行端口号
spring:
  application:
    name: eureka-client #服务名称
eureka:
  client:
    register-with-eureka: true  #注册到Eureka的注册中心
    fetch-registry: true  #获取注册实例列表
    service-url:
      defaultZone: http://localhost:8001/eureka/ #配置注册中心地址

3.5 在Run Dashboard运行eureka-client
在这里插入图片描述
3.5 查看客户端启动页面http://localhost:8101/

% curl http://localhost:8101/
Greetings from Spring Boot!% 

3.6 查看注册中心http://localhost:8001/发现Eureka客户端已经成功注册
在这里插入图片描述

4. 搭建Eureka注册中心集群

搭建两个注册中心

由于所有服务都会注册到注册中心去,服务之间的调用都是通过从注册中心获取的服务列表来调用,注册中心一旦宕机,所有服务调用都会出现问题。所以我们需要多个注册中心组成集群来提供服务,下面将搭建一个双节点的注册中心集群。

4.1 给eureka-sever添加配置文件application-replica1.yml配置第一个注册中心

server:
  port: 8002
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: replica1
  client:
    service-url:
      defaultZone: http://replica2:8003/eureka/ #注册到另一个Eureka注册中心
    fetch-registry: true
    register-with-eureka: true

4.2 给eureka-sever添加配置文件application-replica2.yml配置第二个注册中心

server:
  port: 8003
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: replica2
  client:
    serviceUrl:
      defaultZone:  http://replica1:8002/eureka/  #注册到另一个Eureka注册中心
    fetch-registry: true
    register-with-eureka: true

4.3 这里我们通过两个注册中心互相注册,搭建了注册中心的双节点集群,由于defaultZone使用了域名,所以还需在本机的host文件中配置一下。

% sudo vim /etc/hosts
# 添加如下ip与域名映射
127.0.0.1 replica1
127.0.0.1 replica2

4.4 运行Eureka注册中心集群

在IDEA中我们可以通过使用不同的配置文件来启动同一个SpringBoot应用。

添加两个配置,分别以application-replica1.yml和application-replica2.yml来启动eureka-server.
方法: 从原启动配置中复制出来, Edit Configurations > 选择项目 EurekaServerApplication > 点击复制按钮
在这里插入图片描述
4.5 配置启动的配置文件
在这里插入图片描述
4.6 启动两个eureka-server,访问其中一个注册中心http://replica1:8002/发现另一个已经成为其备份。(注意:先启动的server会报错,因为配置的备份server还没启动,对报错的server再重启一次就好。)
在这里插入图片描述
4.7 修改Eureka-client,让其连接到集群
添加eureka-client的配置文件application-replica.yml,让其同时注册到两个注册中心。

server:
  port: 8102  #运行端口号
spring:
  application:
    name: eureka-client #服务名称
eureka:
  client:
    register-with-eureka: true  #注册到Eureka的注册中心
    fetch-registry: true  #获取注册实例列表
    service-url:
      defaultZone: http://localhost:8002/eureka/, http://localhost:8003/eureka/ #配置注册中心地址

4.8 以该配置文件启动后访问任意一个注册中心节点都可以看到eureka-client
在这里插入图片描述

5. 给Eureka注册中心添加认证

5.1 创建一个eureka-security-server模块,在pom.xml中添加以下依赖.

<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>

5.2 添加application.yml配置文件

server:
  port: 8004
spring:
  application:
    name: eureka-security-server
  security: # 配置SpringSecurity登录用户名和密码
    user:
      name: zgpeace
      password: 123456
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false

5.3 添加Java配置WebSecurityConfig

默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。

package com.zgpeace.cloud.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;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().ignoringAntMatchers("/eureka/**");
    super.configure(http);
  }
}

5.4 启用Eureka服务注册中心

package com.zgpeace.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurikaSecurityServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(EurikaSecurityServerApplication.class, args);
  }

}

5.5 Run Dashboard运行eureka-security-server,访问http://localhost:8004发现需要登录认证
在这里插入图片描述
5.6 eureka-client注册到有登录认证的注册中心
配置文件中需要修改注册中心地址格式

http://${username}:${password}@${hostname}:${port}/eureka/

添加application-security.yml配置文件,按格式修改用户名和密码

server:
  port: 8103  #运行端口号
spring:
  application:
    name: eureka-client #服务名称
eureka:
  client:
    register-with-eureka: true  #注册到Eureka的注册中心
    fetch-registry: true  #获取注册实例列表
    service-url:
      defaultZone: http://zgpeace:123456@localhost:8004/eureka/  #配置注册中心地址

5.7 Run Dashboardapplication-security.yml配置运行eureka-client,可以在注册中心界面看到eureka-client已经成功注册
在这里插入图片描述

6. Eureka的常用配置

eureka:
  client: #eureka客户端配置
    register-with-eureka: true #是否将自己注册到eureka服务端上去
    fetch-registry: true #是否获取eureka服务端上注册的服务列表
    service-url:
      defaultZone: http://localhost:8001/eureka/ # 指定注册中心地址
    enabled: true # 启用eureka客户端
    registry-fetch-interval-seconds: 30 #定义去eureka服务端获取服务列表的时间间隔
  instance: #eureka客户端实例配置
    lease-renewal-interval-in-seconds: 30 #定义服务多久去注册中心续约
    lease-expiration-duration-in-seconds: 90 #定义服务多久不去续约认为服务失效
    metadata-map:
      zone: guangzhou #所在区域
    hostname: localhost #服务主机名称
    prefer-ip-address: false #是否优先使用ip来作为主机名
  server: #eureka服务端配置
    enable-self-preservation: false #关闭eureka服务端的保护机制

使用到的模块

springcloud-learning
├── eureka-server – eureka注册中心
├── eureka-security-server – 带登录认证的eureka注册中心
└── eureka-client – eureka客户端

代码下载

https://github.com/zgpeace/SpringCloudGreenwich

参考

https://juejin.im/post/5d78cd53f265da03d55e8351

https://stackoverflow.com/questions/49424266/how-to-activate-ideas-run-dashboard-feature

https://www.w3cschool.cn/spring_cloud/spring_cloud-2hgl2ixf.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值