您的位置:首页 > 房产 > 家装 > Spring Cloud Eureka快读入门Demo

Spring Cloud Eureka快读入门Demo

2024/10/7 0:16:58 来源:https://blog.csdn.net/dot_life/article/details/140477963  浏览:    关键词:Spring Cloud Eureka快读入门Demo

1.什么是Eureka?

Eureka 由 Netflix 开发,是一种基于REST(Representational State Transfer)的服务,用于定位服务(服务注册与发现),以实现中间层服务的负载均衡和故障转移,此服务被称为 Eureka Server。同时,它还附带了基于 Java 的客户端组件:Eureka Client,它使得客户端与 Eureka Server 的交互变得更加的容易。 以下就是一个简单的服务调用过程:

  1. 由服务提供方将服务注册到 Eureka Server
  2. 服务消费者通过 Eureka Server 获取服务提供方的真实地址
  3. 服务消费者通过真实的地址调用服务

2.代码工程

实验目的

搭建Eureka集群

pom.xml

<?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"><parent><artifactId>eureka</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId><version>${eureka.version}</version></dependency></dependencies></project>

EurekaServerApplication

package com.et.eureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}

节点1配置信息

server:port: 8761eureka:instance:hostname: node1 # eureka nameprefer-ip-address: falseclient:fetch-registry: falseregister-with-eureka: true# eureka urlservice-url:defaultZone: http://node2:8762/eureka
spring:application:name: "eureka-server-ha"

节点2配置信息

server:port: 8762eureka:instance:hostname: node2 # eureka nameprefer-ip-address: falseclient:fetch-registry: falseregister-with-eureka: true# eureka urlservice-url:defaultZone: http://node1:8761/eureka
spring:application:name: "eureka-server-ha"

配置本机host

/etc/hosts

127.0.0.1 node1
127.0.0.1 node2

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • GitHub - Harries/springcloud-demo: Spring Cloud tutorial about hystrix,eureka,config,admin,skywalking

3.测试

启动node1,node2

1721136414553

访问http://127.0.0.1:8761/

1721136705842

4.引用

  • Spring Cloud Netflix
  • Spring Cloud 3: Eureka Clustering | Nick Li
  • Spring Cloud Eureka快读入门Demo | Harries Blog™

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com