写作时间: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: replica1client:service-url:defaultZone: http://replica2:8003/eureka/ #注册到另一个Eureka注册中心fetch-registry: trueregister-with-eureka: true

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

server:port: 8003
spring:application:name: eureka-server
eureka:instance:hostname: replica2client:serviceUrl:defaultZone:  http://replica1:8002/eureka/  #注册到另一个Eureka注册中心fetch-registry: trueregister-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-serversecurity: # 配置SpringSecurity登录用户名和密码user:name: zgpeacepassword: 123456
eureka:instance:hostname: localhostclient:fetch-registry: falseregister-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 {@Overrideprotected 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

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

  1. Spring Cloud入门 -- Eureka服务注册与发现(Hoxton.SR5版)

    什么是Spring Cloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.智能路由.消息总 ...

  2. Spring Cloud入门 -- Consul服务注册与发现(Hoxton.SR5版)

    Consul简介 Consul 是 HashiCorp 公司推出的开源产品,用于实现分布式系统的服务发现.服务隔离.服务配置,这些功能中的每一个都可以根据需要单独使用,也可以同时使用所有功能.Cons ...

  3. spring cloud 学习之 服务注册和发现(Eureka)

    一:服务注册和发现(Eureka) 1:采用Eureka作为服务注册和发现组件 2:Eureka 项目中 主要在启动类加上 注解@EnableEurekaServer @SpringBootAppli ...

  4. spring cloud gateway之服务注册与发现

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 在之前的文章介绍了Spring Cloud Gateway的Predict(断言).Filter( ...

  5. 【夯实Spring Cloud】Spring Cloud中的Eureka服务注册与发现详解

    本文属于[夯实Spring Cloud]系列文章,该系列旨在用通俗易懂的语言,带大家了解和学习Spring Cloud技术,希望能给读者带来一些干货.系列目录如下: [夯实Spring Cloud]D ...

  6. 详解Eureka服务注册与发现和Ribbon负载均衡【纯理论实战】

    Eureka服务注册与发现 Eureka简介 在介绍Eureka前,先说一下CAP原则 CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability ...

  7. Spring Cloud的Eureka Server(注册中心)在程序启动的时候报错:Cannot execute request on any known server

    问题描述:Spring Cloud的Eureka Server(注册中心)在程序启动的时候报错:com.netflix.discovery.shared.transport.TransportExce ...

  8. Spring Cloud之acos服务注册与Dubbo

    Spring Cloud之acos服务注册与Dubbo nacos是springcloud的扩展,注册中心功能通过NacosDiscoveryClient 继承DiscoveryClient,在spr ...

  9. 跟着狂神学SpringCloud(Rest环境搭建+Eureka服务注册与发现+ribbon+Feign负载均衡+Hystrix+服务熔断+Zuul路由网关+SpringCloud config分布)

    跟着狂神学SpringCloud SpringCloud 回顾之前的知识- JavaSE 数据库 前端 Servlet Http Mybatis Spring SpringMVC SpringBoot ...

  10. SpringCloud——Eureka服务注册和发现

    一.SpringCloud和Dubbo SpringCloud整合了一套较为完整的微服务解决方案框架,而Dubbo只是解决了微服务的几个方面的问题. content Dubbo SpringCloud ...

最新文章

  1. 探索JAVA并发 - 线程池详解
  2. redux源码分析之一:createStore.js
  3. visualSVN-server的安装图解
  4. java numberutil_NumberUtil
  5. nodejs正则提取html,Nodejs正则表达式函数之match、test、exec、search、split、replace
  6. ffmpeg开发指南(使用 libavformat 和 libavcodec)
  7. 服务器搜索文件命令,Centos文件搜索命令的讲解
  8. 利用wcf传递字节的简单例子
  9. Linux常用命令介绍(三)——基础操作命令
  10. Shell 编程入门到精通
  11. 装什么软件测试笔记本耐用,我买了新电脑,用什么软件测试比较好?
  12. 【小编教你有效的保护视力】
  13. 最萌吸血鬼猎人,螺旋猫COS『BLOOD C』更衣小夜
  14. 湖人行--(kobe bryant)
  15. 分割评价指标MIOU
  16. 用卷积神经网络实现对小狗品种的识别
  17. skycc广告群发软件v9.2专业版
  18. C#通过操作注册表检测office版本
  19. oj 3014 文件格式变换
  20. 冥想心理训练有效缓解长期压力:来自头发中皮质醇浓度的检测

热门文章

  1. C语言程序设计课题分析,C语言程序设计综合实践性教学课题报告.doc
  2. spring cloud微服务分布式云架构-Gateway入门 1
  3. 前嗅ForeSpider教程:字段的取值与清洗
  4. 配置防盗链 访问控制Directory 访问控制FilesMatch
  5. 2018-05-21 Linux学习
  6. JAVA的序列化不得不说的事
  7. Chrome快捷键, Mac 下 Chrome 浏览器 快捷键
  8. 编写可靠 shell 脚本的 8 个建议
  9. 单例模式专集细节讲述
  10. 管理之道(七) - 不可奖励员工错误的行为