【Eureka】先容与根本使用

打印 上一主题 下一主题

主题 564|帖子 564|积分 1692

Eureka是Netflix开辟的服务发现框架,自己是一个基于REST的服务,主要用于定位运行在AWS域中的中心层服务,以达到负载平衡和中心层服务故障转移的目标。
  SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
  Eureka是Netflix开辟的一个用于实现服务注册和发现的服务。Spring Cloud集成了Eureka,使我们可以非常方便地将Eureka集成到Spring Cloud的微服务架构中。
一个简朴的Eureka服务器的设置方法:

1 在pom.xml中添加Eureka服务器依赖:

  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.springframework.cloud</groupId>
  4.         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5.     </dependency>
  6. </dependencies>
复制代码
2 在application.properties或application.yml中添加Eureka服务器配置:

  1. server:
  2.   port:
  3. eureka:
  4.   instance:
  5.     hostname: localhost
  6.   client:
  7.     registerWithEureka: false
  8.     fetchRegistry: false
  9.     serviceUrl:
  10.       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
3 创建启动类,使用@EnableEurekaServer注解启用Eureka服务器:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  4. @EnableEurekaServer
  5. @SpringBootApplication
  6. public class EurekaServerApplication {
  7.    
  8.         public static void main(String[] args) {
  9.             SpringApplication.run(EurekaServerApplication.class, args);
  10.            }
  11. }
复制代码
启动Eureka服务器后,就可以在http://localhost:8761/上看到Eureka的管理页面。
对于Eureka客户端,通常是指那些将自身服务注册到Eureka服务器,并从Eureka服务器获取其他服务信息的客户端。这通常是指微服务架构中的各个服务。
一个Eureka客户端的设置方法:

1 在pom.xml中添加Eureka客户端依赖:

  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.springframework.cloud</groupId>
  4.         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5.     </dependency>
  6. </dependencies>
复制代码
2 在application.properties或application.yml中添加Eureka客户端配置:

  1. eureka:
  2.   client:
  3.     serviceUrl:
  4.       defaultZone: http://localhost:8761/eureka/
  5.   instance:
  6.     preferIpAddress: true
复制代码
3 在启动类上使用@EnableDiscoveryClient注解来启用服务发现:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. @EnableDiscoveryClient
  5. @SpringBootApplication
  6. public class ClientApplication {
  7.         public static void main(String[] args) {
  8.                     SpringApplication.run(ClientApplication.class, args);
  9.         }
  10. }
复制代码
启动Eureka客户端后,它会自动将自己注册到Eureka服务器,其他服务则可以通过Eureka服务器来发现和调用该客户端的服务。

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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

去皮卡多

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表