Spring Cloud官网
Spring Cloud概述
Spring Cloud是通过自动配置为Spring Boot应用程序提供Netflix OSS集成,并绑定到Spring环境和其他Spring编程模型成语。提供的模式包括服务发现(Eureka),断路器(Hystrix),智能路由(Zuul)和客户端负载平衡(Ribbon)等
Eureka服务器
Eureka:服务发现
Netflix服务发现服务器和客户端是Eureka。可以将服务器配置和部署为高可用性,每个服务器将注册服务的状态复制到其他服务器。
Eureka服务端
-
添加maven支持
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
-
在Application中添加注解
@EnableEurekaServer
示例: @SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
-
在application.xml中添加
3.1 单机版示例:server: port: 8001 # 服务端口 spring: application: name: server-eureka # 应用名称 eureka: instance: hostname: localhost # 服务所在主机 client: service-url: defaultZone: http://localhost:8001/eureka # eureka服务器的地址 fetch-registry: false # 获取注册列表 register-with-eureka: true # 是否将自己也注册到其他eureka节点上。 server: enable-self-preservation: false # 测试时关闭自我保护机制,保证不可用服务及时踢出
3.2 高可用(HA):
server: port: 8001 spring: application: name: server-eureka profiles: service1 # 【eureka服务器集群 —— 节点1】 eureka: instance: hostname: localhost # 服务所在主机 client: service-url: defaultZone: http://localhost:8002/eureka,http://localhost:8003/eureka # eureka服务器的地址 fetch-registry: false # 获取注册列表 register-with-eureka: true # 是否将自己也注册到其他eureka节点上。 server: enable-self-preservation: false # 测试时关闭自我保护机制,保证不可用服务及时踢出 --- server: port: 8002 spring: application: name: server-eureka profiles: service2 # 【eureka服务器集群 —— 节点2】 用于main方法启动的时候,用户需要输入的内容。标识启动的是哪一个eureka服务器; eureka: instance: hostname: localhost # 服务所在主机 client: service-url: defaultZone: http://localhost:8001/eureka,http://localhost:8003/eureka # eureka服务器的地址 fetch-registry: false # 获取注册列表 register-with-eureka: true # 是否将自己也注册到其他eureka节点上 --- server: port: 8003 spring: application: name: server-eureka profiles: service3 # 【eureka服务器集群 —— 节点3】 eureka: instance: hostname: server.eureka.service3.com # 服务所在主机 client: service-url: defaultZone: http://localhost:8001/eureka,http://localhost:8002/eureka # eureka服务器的地址 fetch-registry: false # 获取注册列表 register-with-eureka: true # 是否将自己也注册到其他eureka节点上 ```
-
验证
(单机)http://localhost:8001/eureka (HA)http://localhost:8001/eureka ; http://localhost:8002/eureka ; http://localhost:8003/eureka
Eureka客户端
-
maven添加支持
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
在Application中添加注解
@EnableDiscoveryClient #服务发现,注册到eureka服务器上
-
在application.xml中添加
eureka: instance: hostname: localhost # 服务提供者应用所在的主机 lease-renewal-interval-in-seconds: 2 # 心跳时间 lease-expiration-duration-in-seconds: 3 # 告诉服务端,如果3s之内没有发心跳,将其出掉 client: service-url: # 指定eureka服务器集群列表地址 defaultZone: http://localhost:8001/eureka,http://localhost:8002/eureka,http://localhost:8003/eureka
-
验证服务
(单机)http://localhost:8001/eureka (HA)http://localhost:8001/eureka http://localhost:8002/eureka http://localhost:8003/eureka
Eureka添加安全认证
-
添加配置
spring: application: name: server-eureka security: user: name: admin password: admin
-
访问界面