Spring Cloud Netflix简介:Spring Cloud Netflix是对Netflix OSS组件的集成,主要模块包括服务治理Eureka,断路器Hystrix,客户端负载均衡Ribbon,声明式REST客户端Feign,路由网关Zuul等。
Spring Cloud Eureka简介:Spring Cloud Eureka是Spring Cloud Netflix子项目的核心组件之一,它实现了服务治理的功能。Spring Cloud Eureka提供服务端与客户端,服务端即是Eureka服务注册中心,客户端完成微服务向Eureka服务的注册与发现。
在通过Spring Cloud Eureka实现服务治理之前,需要确定SpringCloud与SpringBoot的版本对应关系,从SpringCloud官网截取的SpringCloud与SpringBoot的版本对应关系如下:
下面通过Spring Cloud Eureka实现服务治理,其中SpringBoot使用的2.2.2.RELEASE
版本,SpringCloud使用的Hoxton.SR1
版本。
一、创建Eureka注册中心
通过Maven新建一个名为spring-cloud-netflix-eureka-server
的项目。
1.编写pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rtxtitanv</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>spring-cloud-netflix-eureka-server</name>
<description>eureka server</description>
<parent>
<!-- SpringBoot 起步依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Cloud Eureka Server 起步依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- SpringSecurity 起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.主启动类
package com.rtxtitanv;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.EurekaServerApplication
* @description eureka server 主启动类
* @date 2020/2/13 11:34
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
@EnableEurekaServer
:表示启动Eureka注册中心。
3.编写配置文件
在application.yml
中进行如下配置:
server:
port: ${PORT:8080}
spring:
application:
name: eureka-server
security:
# 配置spring security登录用户名和密码,给Eureka注册中心添加认证
user:
name: rtxtitanv
password: rtxtitanv
eureka:
instance:
# 该服务实例主机名
hostname: ${EUREKA_HOSTNAME:localhost}
# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ip
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒
lease-renewal-interval-in-seconds: 20
# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒
lease-expiration-duration-in-seconds: 60
server:
# Eureka自我保护模式设置,true:开启,false:关闭,开发测试环境关闭,生产环境开启
enable-self-preservation: false
# 清理失效节点的时间间隔,默认60000毫秒
eviction-interval-timer-in-ms: 60000
client:
# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册
register-with-eureka: false
# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取
fetch-registry: false
healthcheck:
# Eureka的健康检查,只能在application.yml中设置,true:开启,false:关闭
enabled: true
service-url:
# 配置Eureka注册中心即Eureka服务端的地址,单机状态配置自己的地址
defaultZone: ${EUREKA_SERVER:http://rtxtitanv:rtxtitanv@${eureka.instance.hostname}:${server.port}/eureka/}
4.关闭SpringSecurity的CSRF检验
由于这里引入了SpringSecurity来给Eureka注册中心添加认证,默认情况下SpringSecurity启用了CSRF检验,需要手动关闭CSRF检验,代码如下:
package com.rtxtitanv.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;
/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.config.CsrfSecurityConfig
* @description 关闭security的csrf检验
* @date 2020/2/15 12:01
*/
@EnableWebSecurity
public class CsrfSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
5.启动注册中心
(1)单节点Eureka服务
启动eureka-server
,访问http://localhost:8080,结果见下图,由于Eureka注册中心添加了认证,需要输入用户名和密码。
登录认证后访问结果见下图,进入了注册中心界面,说明单节点Eureka注册中心启动成功,此时还没有任何服务注册到Eureka注册中心。
(2)Eureka服务集群
以3个节点为例,这里启动3个Eureka注册中心节点,互相向对方节点注册,首先修改如下配置:
eureka:
client:
# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册
register-with-eureka: true
# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取
fetch-registry: true
然后在主机hosts
文件中添加如下域名信息:
127.0.0.1 eureka-server-01
127.0.0.1 eureka-server-02
127.0.0.1 eureka-server-03
然后在IDEA中创建启动脚本EurekaServer01
、EurekaServer02
、EurekaServer03
EurekaServer01
:
VM options
:
-DPORT=8001
-DEUREKA_HOSTNAME=eureka-server-01
-DEUREKA_SERVER=http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
EurekaServer02
:
VM options
:
-DPORT=8002
-DEUREKA_HOSTNAME=eureka-server-02
-DEUREKA_SERVER=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
EurekaServer03
:
VM options
:
-DPORT=8003
-DEUREKA_HOSTNAME=eureka-server-03
-DEUREKA_SERVER=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/
然后分别启动三个服务,启动后控制台会输出相关日志,UP表示启动成功状态:
2020-02-15 13:41:58.358 INFO 9456 --- [ Thread-27] c.n.e.registry.AbstractInstanceRegistry : Registered instance EUREKA-SERVER/eureka-server:10.0.0.11:8003 with status UP (replication=true)
2020-02-15 13:41:58.359 INFO 9456 --- [ Thread-27] c.n.e.registry.AbstractInstanceRegistry : Registered instance EUREKA-SERVER/eureka-server:10.0.0.11:8002 with status UP (replication=true)
2020-02-15 13:41:58.359 INFO 9456 --- [ Thread-27] c.n.e.registry.AbstractInstanceRegistry : Registered instance EUREKA-SERVER/eureka-server:10.0.0.11:8001 with status UP (replication=true)
2020-02-15 13:41:58.359 INFO 9456 --- [ Thread-27] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 3 instances from neighboring DS node
分别访问三个Eureka注册中心节点:
其中访问eureka-server-01
节点,登录后访问结果见下图并且查看另两个节点的界面也类似,说明Eureka注册中心集群搭建成功。
以上是在本机开发工具IDEA模拟Eureka集群的测试,在生产环境上都是部署在LInux服务器上,传统的部署方式就是先将Eureka服务打jar包,上传到Linux服务器启动运行。这里打的jar包名为spring-cloud-netflix-eureka-server-1.0.0.jar
,用一台Linux服务器来测试,实际部署在多台服务器上方法类似,首先在Linux服务器的hosts
文件中添加域名信息,执行以下命令:
vim /etc/hosts
hosts
中添加域名信息:
127.0.0.1 eureka-server-01
127.0.0.1 eureka-server-02
127.0.0.1 eureka-server-03
将jar包上传到Linux服务器,在jar包所在目录下执行以下命令:
java -jar spring-cloud-netflix-eureka-server-1.0.0.jar --server.port=8001 --eureka.instance.hostname=eureka-server-01 --eureka.client.service-url.defaultZone=http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
java -jar spring-cloud-netflix-eureka-server-1.0.0.jar --server.port=8002 --eureka.instance.hostname=eureka-server-02 --eureka.client.service-url.defaultZone=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
java -jar spring-cloud-netflix-eureka-server-1.0.0.jar --server.port=8003 --eureka.instance.hostname=eureka-server-03 --eureka.client.service-url.defaultZone=http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/
在客户端主机的hosts
文件中添加如下域名信息:
服务器ip eureka-server-01
服务器ip eureka-server-02
服务器ip eureka-server-03
分别访问三个Eureka注册中心节点:
其中访问eureka-server-03
节点,登录后访问结果见下图并且查看另两个节点的界面也类似,说明Eureka注册中心集群搭建成功。
二、创建Eureka客户端
通过Maven新建一个名为spring-cloud-netflix-eureka-client
的项目。
1.编写pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rtxtitanv</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>spring-cloud-netflix-eureka-client</name>
<description>eureka client</description>
<parent>
<!-- SpringBoot 起步依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Cloud Eureka Client 起步依赖 -->
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.主启动类
package com.rtxtitanv;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.EurekaClientApplication
* @description eureka client 主启动类
* @date 2020/2/13 20:22
*/
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
@EnableEurekaClient
:表示该服务为一个Eureka客户端。
3.编写配置文件
在application.yml
中进行如下配置:
server:
port: ${PORT:8090}
spring:
application:
name: eureka-client
eureka:
client:
# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册
register-with-eureka: true
# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取
fetch-registry: true
# 配置Eureka注册中心即Eureka服务端的地址,集群地址以,隔开
service-url:
defaultZone: http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/
# 从Eureka服务器获取注册表信息的间隔时间,单位秒,默认30秒
registry-fetch-interval-seconds: 20
# 更新实例信息的变化到Eureka服务端的间隔时间,单位秒,默认30秒
instance-info-replication-interval-seconds: 20
# 初始化实例信息到Eureka服务端的间隔时间,单位秒,默认40秒
initial-instance-info-replication-interval-seconds: 30
# 询问Eureka Server信息变化的时间间隔,单位秒,默认300秒
eureka-service-url-poll-interval-seconds: 200
# 读取Eureka Server的超时时间,单位秒,默认5秒
eureka-server-connect-timeout-seconds: 10
# 连接Eureka Server的超时时间,单位秒,默认8秒
eureka-server-read-timeout-seconds: 15
instance:
# 将ip地址注册到Eureka注册中心
prefer-ip-address: true
# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ip
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒
lease-renewal-interval-in-seconds: 20
# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒
lease-expiration-duration-in-seconds: 60
4.创建测试用的Controller
package com.rtxtitanv.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author rtxtitanv
* @version 1.0.0
* @name com.rtxtitanv.controller.EurekaClientController
* @description EurekaClientController
* @date 2020/2/13 21:30
*/
@RestController
public class EurekaClientController {
@Value("${server.port}")
private String port;
@Value("${spring.cloud.client.ip-address}")
private String ip;
@GetMapping("/home")
public String home() {
return "ip:" + ip + ",port:" + port;
}
}
5.启动Eureka客户端
在主机hosts
文件中添加如下域名信息:
127.0.0.1 eureka-client-01
127.0.0.1 eureka-client-02
127.0.0.1 eureka-client-03
通过IDEA启动脚本启动3个Eureka客户端节点,分别访问三个Eureka注册中心节点,其中访问eureka-server-01
节点,登录后访问结果见下图并且查看另两个节点的界面也类似,说明3个Eureka客户端节点均成功注册到Eureka注册中心集群。
分别访问以下三个链接:
访问第一个链接的结果见下图,剩下两个链接的访问结果省略。
下面通过URL的方式获取服务的注册信息,这里以eureka-server-01
为例,访问
http://eureka-server-01:8001/eureka/apps,结果见下图:
获取其中一个服务EUREKA-CLIENT
的注册信息,访问
http://eureka-server-01:8001/eureka/apps/EUREKA-CLIENT,结果见下图:
代码示例
- Github:
https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-server
https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-client - Gitee:
https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-server
https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-eureka-client