SpringCloud——服务治理与发现Eureka

Eureka自我保护机制
EurekaServer在一定时间内没有接收到某个微服务实例的心跳,将会注销该实例(默认90秒)
但是有时候仅仅是网络故障,本不应该注销这个微服务,此时会启动自我保护模式。
故障现象如下:
某时刻某一个微服务不可用了(DOWN),eureka不会立即清理,依旧会对该微服务的信息进行保存
可以在EurekaService项目中的yam配置文件中使用eureka.server.enable-self-preservation=false禁用自我保护模式
在这里插入图片描述

配置服务中心Eureka

创建模块工程
一、引入依赖

<dependency> 
	<groupid>org.springframework.cloud</groupid> 
	<artifactid>spring-cloud-starter-netflix-eureka-server</artifactid
</dependency>

二、启用Eureka
启动类上添加注解@EnableEurekaServer

@SpringBootApplication 
@EnableEurekaServer
public class ChapterServerApplication{
	public static void main(String[] args){
		SpringApplication.run(ChapterServerApplication.class,args);
	}
}

三、配置applicaiton.properties(两个中心相互注册)
关键spring.application.nane=server保持一致

# Spring 项目名称
spring.application.nane=server
# 服务器端口
server.port=7001 
# Eureka 注册服务器名称
eureka.instance.hostname=localhost 
# 是否注册给服务中心
eureka.client.register-with-eureka=false 
# 是否检索服务
eureka.client.fetch-registry=false 
# 治理客户端服务域 http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.client.serviceUrl.defaultZone=http://localhost:7002/eureka/
# Spring 项目名称
spring.application.nane=server
# 服务器端口
server.port=7002 
# Eureka 注册服务器名称
eureka.instance.hostname=localhost 
# 是否注册给服务中心
eureka.client.register-with-eureka=false 
# 是否检索服务
eureka.client.fetch-registry=false 
# 治理客户端服务域
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/

四、浏览器输入http://local:7001/

配置生产者

创建模块工程
一、导入依赖

spring-cloud-starter-netflix-eureka-client

二、启用
SpringCloud旧版本@EnableDiscoveryClinet
新版本就不需要了

@SpringBootApplication
//@EnableDiscoveryClient
public class ChapterProductApplication{
	public static void main(String[] args){
		SpringApplication.run(ChapterProductApplication.class, args);
	}
}

三、配置文件application.properties

#服务器端口
server.port=9001 
#Spring 服务名称
spring.application.name=product 
#治理客户端服务域(两个注册中心)
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/,http://localhost:7002/eureka/

配置消费者

创建模块工程
一、导入依赖

spring-cloud-starter-netflix-eureka-client

二、启用
SpringCloud旧版本@EnableDiscoveryClinet
新版本就不需要了

@SpringBootApplication
//@EnableDiscoveryClient
public class ChapterProductApplication{
	public static void main(String[] args){
		SpringApplication.run(ChapterProductApplication.class, args);
	}
}

三、配置文件application.properties

#服务器端口
server.port=8001 
#Spring 服务名称
spring.application.name=user
#治理客户端服务域(两个注册中心)
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/,http://localhost:7002/eureka/

===============================================================================

使用IDEA创建SpringCloud应用

  1. 创建一个eureka-server模块,并使用Spring Initializer初始化一个SpringBoot项目
    在这里插入图片描述

  2. 填写应用信息
    在这里插入图片描述

  3. 选择你需要的SpringCloud组件进行创建

在这里插入图片描述
创建完成后会发现pom.xml文件中已经有了eureka-server的依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. 在启动类上添加@EnableEurekaServer注解来启用Euerka注册中心功能
@EnableEurekaServer
@SpringBootApplication
publicclass EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}
  1. 在配置文件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#关闭保护模式

使用IDEA的Run Dashboard来运行SpringCloud

打开Run Dashboard,默认情况下,当IDEA检查到你的项目中有SpringBoot应用时,会提示你开启,如果你没开启,可以用以下方法开启
在这里插入图片描述
运行SpringCloud应用
在这里插入图片描述
运行完成后访问地址http://localhost:8001/可以看到Eureka注册中心的界面
在这里插入图片描述

搭建Eureka客户端

  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>
  1. 在启动类上添加@EnableDiscoveryClient注解表明是一个Eureka客户端
@EnableDiscoveryClient
@SpringBootApplication
publicclass EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

}
  1. 配置文件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/#配置注册中心地址

运行eureka-client
在这里插入图片描述

查看注册中心http://localhost:8001/发现Eureka客户端已经成功注册
在这里插入图片描述

搭建Eureka注册中心集群

通过两个注册中心互相注册,搭建了注册中心的双节点集群,由于defaultZone使用了域名,所以还需在本机的host文件中配置一下
修改本地host文件
在这里插入图片描述

  1. 给eureka-server添加配置文件application-replica1.yml配置第一个注册中心
server:
  port: 8002
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: replica1
  client:
    serviceUrl:
      defaultZone: http://replica2:8003/eureka/#注册到另一个Eureka注册中心
    fetch-registry: true
    register-with-eureka: true

给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

运行Eureka注册中心集群
在IDEA中我们可以通过使用不同的配置文件来启动同一个SpringBoot应用
添加两个配置,分别以application-replica1.yml和application-replica2.yml来启动eureka-server
在这里插入图片描述
在这里插入图片描述
启动两个eureka-server,访问其中一个注册中心http://replica1:8002/发现另一个已经成为其备份

在这里插入图片描述
修改Eureka-client,让其连接到集群

server:
  port: 8102
spring:
  application:
    name: eureka-client
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://replica1:8002/eureka/,http://replica2:8003/eureka/#同时注册到两个注册中心

以该配置文件启动后访问任意一个注册中心节点都可以看到eureka-client
在这里插入图片描述

给Eureka注册中心添加认证

在这里插入图片描述

  1. 创建一个eureka-security-server模块,添加SpringSecurity依赖
<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>
  1. 配置文件中配置登录注册中心的用户名和密码
server:
  port: 8004
spring:
  application:
    name: eureka-security-server
  security: #配置SpringSecurity登录用户名和密码
    user:
      name: macro
      password: 123456
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false
    register-with-eureka: false

添加Java配置WebSecurityConfig
默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
	
	@Override
	protected void configure(HttpSecurity http)throws Exception{
		http.csrf().ignoringAntMatchers("/eureka/**");
		super.configure(http);
	}
}

运行eureka-security-server,访问http://localhost:8004发现需要登录认证
在这里插入图片描述

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
    fetch-registry: true
    service-url:
      defaultZone: http://macro:123456@localhost:8004/eureka/

以application-security.yml配置运行eureka-client,可以在注册中心界面看到eureka-client已经成功注册
在这里插入图片描述

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: jiangsu#所在区域
    hostname: localhost#服务主机名称
    prefer-ip-address: false#是否优先使用ip来作为主机名
  server: #eureka服务端配置
    enable-self-preservation: false#关闭eureka服务端的保护机制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值