当前位置 博文首页 > RtxTitanV的博客:SpringCloud 服务注册与发现Eureka Hoxton版本
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
版本。
通过Maven新建一个名为spring-cloud-netflix-eureka-server
的项目。
<?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>
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注册中心。
在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/}
由于这里引入了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);
}
}
启动eureka-server
,访问http://localhost:8080,结果见下图,由于Eureka注册中心添加了认证,需要输入用户名和密码。
登录认证后访问结果见下图,进入了注册中心界面,说明单节点Eureka注册中心启动成功,此时还没有任何服务注册到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