SpringCloud微服务之二:服务注册与发现实战
文章目录
前言
使用版本:
springboot版本:1.5.9.RELEASE;
springcloud版本:Edgware.RELEASE;
开发工具:IDEA
主要介绍Eureka服务端配置、Eureka客户端调用、Eureka高可用集群配置、Eureka用户认证、Eureka元数据、Eureka自我保护模式以及部署服务多网卡ip时的选择。
一、Eureka是什么?
Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。
注册中心主要提供三个核心功能:
1.服务注册
服务提供者启动时,会通过 Eureka Client 向 Eureka Server 注册信息,Eureka Server 会存储该服务的信息,Eureka Server 内部有二层缓存机制来维护整个注册表
2. 提供注册表
服务消费者在调用服务时,如果 Eureka Client 没有缓存注册表的话,会从 Eureka Server 获取最新的注册表
3. 同步状态
Eureka Client 通过注册、心跳机制和 Eureka Server 同步当前客户端的状态
三大角色
Eureka Server:提供服务的注册和发现
Server Provider:将自身服务注册到Eureka中,从而使消费方能够找到
Server Consumer:服务消费方从Eureka中获取注册服务列表,从而找到消费服务
二、Eureka服务端
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.yangxf.cloud</groupId>
<artifactId>microservice-discovery-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置添加
server:
port: 8761 # 指定该Eureka实例的端口
eureka:
client:
registerWithEureka: false #是否默认将当前服务注册到Eureka注册汇总新
fetchRegistry: false #是否从Eureka服务上获取注册信息
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #客户端注册配置地址,多个之间通逗号分隔
3、启动项添加注解
/**
* 使用Eureka做服务发现.
* @author linwd
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
三、Eureka客户端
1、pom依赖引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、注解添加
在启动类上添加注解@EnableDiscoveryClient 或@EnableEurekaClient
Spring Cloud Edgware以及更高版本只需要引入依赖即可。
//@EnableDiscoveryClient(autoRegister = false)//默认true,如改成false,就不会注册服务啦
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderUserApplication.class, args);
}
}
3、配置添加
applyication.yml添加如下配置:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
四、Eureka高可用集群
1、Eureka集群配置
准备两个节点(ip1,ip2)服务,添加如下配置:
eureka:
client:
serviceUrl:
defaultZone: http://ip1:8761/eureka/,http://ip2:8762/eureka/
2、客户端调用配置
这样就服务注册到Eureka集群上了,
eureka:
client:
serviceUrl:
defaultZone: http://ip1:8761/eureka/,http://ip2:8762/eureka/
五、Eureka用户认证
实际项目中可能出于安全考虑,必须经过用户认证才允许访问Eureka
1、服务端添加用户认证
- 引入依赖
<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>
- 添加配置
security:
basic:
enabled: true # 开启基于HTTP basic的认证
user:
name: yangxf # 配置登录的账号是yangxf
password: 123456 # 配置登录的密码是123456
server:
port: 8761 # 指定该Eureka实例的端口
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://yangxf:123456@localhost:8761/eureka/
2、客户端调整
配置调整为:http://yangxf:123456@localhost:8761/eureka/
eureka:
client:
serviceUrl:
defaultZone: http://yangxf:123456@localhost:8761/eureka/
六、Eureka元数据
元数据分为标准元数据和自定义元数据;
标准元数据包括主机名,ip地址,端口,状态页,健康检查等信息;
自定义元数据放在eureka.instance.metadata-map下,任意key-value;
元数据数据可以用于服务之间获取并调用。
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
metadata-map:
my-metadata: 我自定义的元数据 # 自定义的元数据,key/value都可以随便写。
可以通过springCloud自带的DiscoveryClient完成信息获取。
package com.yangxf.cloud.study.user.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yangxf.cloud.study.user.entity.User;
@RestController
public class MovieController {
@Autowired
private DiscoveryClient discoveryClient;
/**
* 查询microservice-provider-user服务的信息并返回
* @return microservice-provider-user服务的信息
*/
@GetMapping("/user-instance")
public List<ServiceInstance> showInfo() {
return this.discoveryClient.getInstances("microservice-provider-user");
}
}
七、Eureka的自我保护模式
出现如下红色文字,即表示Eureka进入了自我保护模式;
1、解决方式
出现这情况,解决这种情况的方法主要有几种方式:
- 等待 Eureka Server 自动恢复
正常的情况下,等待网络恢复(或者没有频繁的启动与关闭实例)后,等待一段时间 Eureka Server 会自动关闭自我保护模式,但是如果它迟迟没有关闭该模式,那么便可以尝试手动关闭。- 重启 Eureka Server
通常而言,PRD 环境建议对 Eureka Server 做负载均衡,这样在依次关闭并开启 Eureka Server 后,无效的实例会被清除,并且不会对正常的使用照成影响。- 关闭 Eureka 的自我保护模式
eureka.server.enable-self-preservation=false,测试可以通过这个配置关闭
2、原理
默认情况下,如果 Eureka Server 在一定时间内没有收到某个微服务实例的心跳,Eureka Server 便会将该实例注销。 (默认是90s),
但是 当网络分区发生故障(延迟、卡顿、拥挤)时,微服务与 Eureka Server 之间是无法正常通信的,在这种情况下微服务本身其实是健康的,本来是不应该注销这个服务的,此时 Eureka 便会通过 “自我保护模式” 来解决这个问题。
综上所述,自我保护模式是针对网络异常的安全保护措施。
注:生产环境中不建议关闭 Eureka 的自我保护模式.
服务端配置如下:
eureka:
server:
#关闭自我保护机制,保证不可用服务被及时剔除
enable-self-preservation: false
#清理无效节点的时间间隔,默认60000毫秒,即60秒 (此处时间间隔设置为2s)
eviction-interval-timer-in-ms: 2000
客户端配置如下:
eureka:
instance:
# Eureka客户端向服务端发送心跳的时间间隔,单位为妙(默认是30s)
lease-renewal-interval-in-seconds: 1
# Eureka服务端在收到最后一次心跳后的等待时间上限,单位为秒(默认90s),超时将移除服务
lease-expiration-duration-in-seconds: 2
八、Eureka的多网卡环境下IP选择
1、忽略指定名称ip
spring:
cloud:
inetutils:
ignored-interfaces:
- docker0
- veth.*
eureka:
instance:
prefer-ip-address: true
2、使用正则表达式,指定使用的地址
spring:
cloud:
inetutils:
preferredNetworks:
- 192.168
- 10.0
eureka:
instance:
prefer-ip-address: true
3、手动指定ip
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
ip-address: 127.0.0.1