标签:ble blog 随机 cti 查询 host 节点 服务注册 均衡
Spring Cloud版本:Hoxton.SR5
没有服务注册中心时,服务之间通过ip、端口进行接口调用。当某一个服务进行迁移时其他关联服务也需要修改对应的调用地址。
当有了服务注册中心后,服务之间通过服务注册中心进行调用。
创建一个Spring Boot项目,引入spring-cloud-starter-netflix-eureka-server
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在启动类上添加@EnableEurekaServer
注解
@EnableEurekaServer
@SpringBootApplication
public class SclEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SclEurekaServerApplication.class, args);
}
}
配置文件
spring:
application:
name: eureka-server
server:
port: 8100 # 监听端口
servlet:
context-path: /eureka-server1
eureka:
instance:
hostname: test1
client:
register-with-eureka: false # 是否注册到服务注册中心,因为本身就是服务注册中心,所以设置为false即可
fetch-registry: false # 是否从注册中心抓取数据,因为不涉及到服务调用,所以设置为false即可
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/${server.servlet.context-path}/eureka # 注册地址
此时已实现一个单节点的服务注册中心。启动后访问
上一步已搭建了一个单节点的服务注册中心,但集群才是王道,当其中某些节点出现故障时可以保证业务的正常运行。
修改配置文件,没有新建
application.yml
spring:
application:
name: eureka-server # 服务名称,同一服务的名称需保持一致
profiles:
active: server1 # 选择启动的配置文件
application-server1.yml
server:
port: 8100 # 监听端口
servlet:
context-path: /eureka-server1
eureka:
instance:
hostname: test1 # 主机名称
client:
register-with-eureka: true # 是否注册到服务注册中心,本身是服务注册中心但是需要注册到其他的服务注册中心,所以设置为true
fetch-registry: false # 是否从注册中心抓取数据,因为不涉及到服务调用,所以设置为false即可
service-url:
defaultZone: http://test2:8200/eureka-server2/eureka,http://test3:8300/eureka-server3/eureka
application-server2.yml
server:
port: 8200 # 监听端口
servlet:
context-path: /eureka-server2
eureka:
instance:
hostname: test2
client:
register-with-eureka: true # 是否注册到服务注册中心,本身是服务注册中心但是需要注册到其他的服务注册中心,所以设置为true
fetch-registry: false # 是否从注册中心抓取数据,因为不涉及到服务调用,所以设置为false即可
service-url:
defaultZone: http://test1:8100/eureka-server1/eureka,http://test3:8300/eureka-server3/eureka
application-server3.yml
server:
port: 8300 # 监听端口
servlet:
context-path: /eureka-server3
eureka:
instance:
hostname: test3
client:
register-with-eureka: true # 是否注册到服务注册中心,本身是服务注册中心但是需要注册到其他的服务注册中心,所以设置为true
fetch-registry: false # 是否从注册中心抓取数据,因为不涉及到服务调用,所以设置为false即可
service-url:
defaultZone: http://test1:8100/eureka-server1/eureka,http://test2:8200/eureka-server2/eureka
在hosts文件里添加如下内容
127.0.0.1 test1
127.0.0.1 test2
127.0.0.1 test3
修改application.yml配置文件里的spring.profiles.active为server1、server2、server3后分别启动访问:
上一步搭建了高可用的服务注册中心,但只要知道注册地址就可将自己的服务注册到注册中心中,很不安全。若有人进行恶意注册,会加重服务注册中心的负载容易出现问题。因此在注册的时候进行鉴权增强安全性。
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
修改配置文件
application.yml
spring:
application:
name: eureka-server
profiles:
active: server1
security:
user:
name: root # 用户名
password: 123456 # 密码
application-server1.yml
server:
port: 8100 # 监听端口
servlet:
context-path: /eureka-server1
eureka:
instance:
hostname: test1
client:
register-with-eureka: true
fetch-registry: false
service-url:
defaultZone: http://root:123456@test2:8200/eureka-server2/eureka,http://root:123456@test3:8300/eureka-server3/eureka
application-server2.yml、application-server3.yml按照application-server1.yml进行修改即可
按照上一步的方式进行启动并访问,会先进入登录界面,登录成功后调转到监控界面
eureka server默认每30s与微服务示例进行一次心跳,若90s内没有收到某个微服务的心跳,则会从注册中心移除该实例。
但若是网络发生故障,导致微服务与Eureka Server无法通信,但是此时微服务是正常运行的,若将微服务从注册中心移除会导致业务不可用。所以Eureka引入了自我保护机制
如果在15分钟内超过85%的客户端节点都没有正常的心跳,Eureka Server就会自动进入自我保护机制。
自我保护机制:
可通过如下设置关闭自我保护机制(不推荐)。
eureka:
server:
enable-self-preservation: false # 生产环境建议开启自我保护机制
修改心跳时间和移除示例时间
eureka:
instance:
lease-renewal-interval-in-seconds: 10 # 心跳时间
lease-expiration-duration-in-seconds: 30 # 超过该时间没有收到心跳,则移除该instance
高版本的Spring Security默认开启了CSRF校验,导致微服务无法注册,因此需关闭服务注册中心的CSRF校验
新增WebSecurityConfig配置类
package com.chy.scleurekaserver.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;
/**
* Web安全配置类
*
* @author chy
* @date 2020-06-15 10:54
*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf校验,解决无法注册到服务注册中心的问题
http.csrf().disable();
super.configure(http);
}
}
标签:ble blog 随机 cti 查询 host 节点 服务注册 均衡
原文地址:https://www.cnblogs.com/jinjiyese153/p/13181837.html