写作时间: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 Dashboard
以application-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