Spring Cloud Eureka快读入门Demo

海哥  金牌会员 | 2024-7-21 22:21:56 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 974|帖子 974|积分 2922

1.什么是Eureka?

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

  • 由服务提供方将服务注册到 Eureka Server
  • 服务消耗者通过 Eureka Server 获取服务提供方的真实地址
  • 服务消耗者通过真实的地址调用服务
2.代码工程

实验目的

搭建Eureka集群
pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>eureka</artifactId>
  7.         <groupId>com.et</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>eureka-server</artifactId>
  12.     <properties>
  13.         <maven.compiler.source>8</maven.compiler.source>
  14.         <maven.compiler.target>8</maven.compiler.target>
  15.     </properties>
  16.     <dependencies>
  17.         <dependency>
  18.             <groupId>org.springframework.boot</groupId>
  19.             <artifactId>spring-boot-starter-web</artifactId>
  20.         </dependency>
  21.         <dependency>
  22.             <groupId>org.springframework.boot</groupId>
  23.             <artifactId>spring-boot-starter-actuator</artifactId>
  24.         </dependency>
  25.         <dependency>
  26.             <groupId>org.springframework.boot</groupId>
  27.             <artifactId>spring-boot-starter-test</artifactId>
  28.             <scope>test</scope>
  29.         </dependency>
  30.         <dependency>
  31.             <groupId>org.projectlombok</groupId>
  32.             <artifactId>lombok</artifactId>
  33.             <optional>true</optional>
  34.         </dependency>
  35.         <dependency>
  36.             <groupId>org.springframework.cloud</groupId>
  37.             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  38.             <version>${eureka.version}</version>
  39.         </dependency>
  40.     </dependencies>
  41. </project>
复制代码
EurekaServerApplication

  1. package com.et.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @EnableEurekaServer
  6. @SpringBootApplication
  7. public class EurekaServerApplication {
  8.   public static void main(String[] args) {
  9.     SpringApplication.run(EurekaServerApplication.class, args);
  10.   }
  11. }
复制代码
节点1配置信息
  1. server:
  2.   port: 8761
  3. eureka:
  4.   instance:
  5.     hostname: node1 # eureka name
  6.     prefer-ip-address: false
  7.   client:
  8.     fetch-registry: false
  9.     register-with-eureka: true
  10.     # eureka url
  11.     service-url:
  12.       defaultZone: http://node2:8762/eureka
  13. spring:
  14.   application:
  15.     name: "eureka-server-ha"
复制代码
节点2配置信息
  1. server:
  2.   port: 8762
  3. eureka:
  4.   instance:
  5.     hostname: node2 # eureka name
  6.     prefer-ip-address: false
  7.   client:
  8.     fetch-registry: false
  9.     register-with-eureka: true
  10.     # eureka url
  11.     service-url:
  12.       defaultZone: http://node1:8761/eureka
  13. spring:
  14.   application:
  15.     name: "eureka-server-ha"
复制代码
配置本机host

/etc/hosts
  1. 127.0.0.1 node1
  2. 127.0.0.1 node2
复制代码
以上只是一些关键代码,所有代码请参见下面代码仓库
代码仓库



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

启动node1,node2
   
  访问http://127.0.0.1:8761/
   
  4.引用



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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

海哥

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表