Eureka 介绍
在 Spring Cloud Netflix 整合技术栈中, Eureka 既可以作为服务注册中心也可以用于服务发现对整个微服务架构起着最核心的整合作用.
Eureka 是基于 REST(Representational State Transfer) 服务, 主要以 AWS 云服务为支撑, 提供服务发现并实现负载均衡和故障转移. 在 Netflix, 为 Eureka 提供更为复杂的负载均衡方案进行封装, 以实现高可用, 它包括基于流量, 资源利用率以及请求返回状态的加权负载均衡.
现有的 Eureka 2.0 开源工作已经停止. 作为 2.x 分支上现有工作存储库的一部分发布的代码库和工件被认为是自担风险使用的.
虽然 Eureka 开源工作已经停止, 但是它以前提供的版本的功能足以对需要项目的业务场景适用.
除了 Eureka, 还可以使用 Feature,Consul,zookeeper 等来作为服务注册中心.
父工程创建
现在创建一个 cloud-spring 的 pom 工程作为本次 Spring Cloud 所有章篇的一个父工程.
父工程 pom.xml 配置
这次我们使用 Spring Cloud Finchley.RELEASE 版, 这个版本相对是比较稳定的.
- <!-- 定义依赖版本进行依赖管理 -->
- <properties>
- <spring-cloud-version>Finchley.RELEASE</spring-cloud-version>
- <spring-boot-version>2.0.4.RELEASE</spring-boot-version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <!-- SpringCloud -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud-version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- SpringBoot -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot-version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <!-- 插件 -->
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>build-info</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <target>${java.version}</target>
- <source>${java.version}</source>
- <encoding>${project.reporting.outputEncoding}</encoding>
- </configuration>
- </plugin>
- </plugins>
- </pluginManagement>
- </build>
使用 Spring Cloud Finchley.RELEASE 版注意事项: Spring Boot 版本请勿低于 2.0.3 否则导致很多依赖启动报错
搭建 Eureka 服务注册中心
在父工程下创建子模块, 工程名: cloud-netfix-eureka-server
在 pom.xml 中添加依赖:
- <dependencies>
- <!-- Eureka 服务端 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
创建启动类 CommenEurekaServerApplication.java
- /**
- * Eureka 注册中心服务
- * @author SimpleWu
- */
- @SpringBootApplication
- @EnableEurekaServer
- public class CommenEurekaServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(CommenEurekaServerApplication.class,args);
- }
- }
通过 @EnableEurekaServer 注解启动一个服务注册中心提供给其他应用进行对话
然后在稍微写下配置即可搭建好一个服务注册中心, 下面编写 application.properties(YAML):
- server.port=10001
- spring.application.name=cloud-netflix-eureka-server
- eureka.instance.hostname=localhost
- # 由于该应用为注册中心, 所以设置为 false, 代表不向注册中心注册自己
- eureka.client.register-with-eureka=false
- # 由于注册中心的职责就是维护服务实例, 他并不需要去检索服务, 所以也设置为 false
- eureka.client.fetch-registry=false
- # 关闭自我保护
- eureka.server.enableSelfPreservation=false
- eureka.client.serviceUrl.defaultZone=http://${
- eureka.instance.hostname
- }:${
- server.port
- }/eureka/
- # 清理无效节点的频率 30 秒
- eureka.server.eviction-interval-timer-in-ms=30000
- # 表示 Eureka Server 开启自我保护的系数, 默认: 0.85.
- eureka.server.renewal-percent-threshold=0.85
在默认配置下 Eureka 服务端会主动注册自己, 在这里我们只作为服务注册中心所以不需要注册到服务中心, 在以后的集群模式需要将 Eureka 服务高可用集群时需要注册到其他的注册中心.
并且 Eureka 提供服务事件监听, 如下:
- /**
- * Eureka 事件监听
- * @author SimpleWu
- */
- @Component
- public class EurekaListener {
- private final Logger log = LoggerFactory.getLogger(EurekaListener.class);
- @EventListener
- public void listen(EurekaInstanceCanceledEvent event) {
- log.debug(event.getServerId() + "\t" + event.getAppName() + "服务下线");
- }
- @EventListener
- public void listen(EurekaInstanceRegisteredEvent event) {
- InstanceInfo instanceInfo = event.getInstanceInfo();
- log.debug(instanceInfo.getAppName() + "进行注册");
- }
- @EventListener
- public void listen(EurekaInstanceRenewedEvent event) {
- log.debug(event.getServerId() + "\t" + event.getAppName() + "服务进行续约");
- }
- @EventListener
- public void listen(EurekaRegistryAvailableEvent event) {
- log.debug("注册中心 启动");
- }
- @EventListener
- public void listen(EurekaServerStartedEvent event) {
- log.debug("Eureka Server 启动");
- }
- }
然后我们通过启动类 CommenEurekaServerApplication.java 允许 main 函数进行启动.
访问: http://localhost:10001/ 即可看到 Eureka 服务治理中心, 其中 Instances currently registered with Eureka 还没有注册任何实例.
该案例代码可参考: https://github.com/450255266/open-doubi/tree/master/SpringCloud
来源: https://www.cnblogs.com/SimpleWu/p/11177200.html