首页 > 编程 > Java > 正文

Spring Cloud构建Eureka应用的方法

2019-11-26 10:12:18
字体:
来源:转载
供稿:网友

Eureka 介绍

Eureka提供基于REST的服务,在集群中主要用于服务管理。Eureka提供了基于Java语言的客户端组件,客户端组件实现了负载均衡的功能,为业务组件的集群部署创造了条件。使用该框架,可以将业务组件注册到Eureka容器中,这些业务组件可进行集群部署,Eureka主要维护这些服务的列表并自动检查它们的状态。

程序结构

创建Eureka Server

maven依赖

  <dependencyManagement>    <dependencies>      <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-dependencies</artifactId>        <version>Dalston.SR1</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement>  <dependencies>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-eureka-server</artifactId>    </dependency>  </dependencies>

更改spring boot 启动端口 在application.yml

server: port: 8761

开启Eureka服务注解 @EnableEurekaServer

@EnableEurekaServer@SpringBootApplicationpublic class EKServerApplication {  public static void main(String[] args) {    new SpringApplicationBuilder(EKServerApplication.class).run(args);  }}

启动springboot

[Thread-11] o.s.c.n.e.server.EurekaServerBootstrap: Initialized server context[main] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http)[main] .s.c.n.e.s.EurekaAutoServiceRegistration: Updating port to 8761[main] c.b.firstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)

启动期间会出现一个无法连接到服务器的异常 这个是由于Eureka在启动的时候会把自己当作一个客户端去服务器抓取注册信息

复制代码 代码如下:

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

增加如下配置启动时便不会再出现该异常

eureka: client:  registerWithEureka: false  fetchRegistry: false

registerWithEureka 声明是否将自己的信息注册到Eureka服务器,默认值为true。

fetchRegistry 声明是否到Eureka服务器中抓取注册信息,默认值为true。

在浏览器中访问 http://localhost:8761 查看Eureka控制台 输入图片说明

创建服务提供者

依赖

    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-config</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-ribbon</artifactId>    </dependency>

在 application.yml 中配置端口、Eureka实例名称和Eureka服务地址

server: port: 8080spring: application:  name: ek-providereureka: instance:  hostname: localhost client:   serviceUrl:    defaultZone: http://localhost:8761/eureka/

创建一个 REST 服务

@RestControllerpublic class HelloController {  @RequestMapping("/hello")  public String hello(HttpServletRequest request) {    return "hello:" + request.getRequestURL();  }}

开启Eureka客户端注解 @EnableEurekaServer

@EnableEurekaClient@SpringBootApplicationpublic class EkProviderApplication {  public static void main(String[] args) {    new SpringApplicationBuilder(EkProviderApplication.class).run(args);  }}

启动之后在 Eureka 控制台可以看到服务提供者已经在 Eureka 中注册

创建服务调用者

依赖

    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-config</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-ribbon</artifactId>    </dependency>

在 application.yml 中配置端口、Eureka实例名称和Eureka服务地址

server: port: 9000spring: application:  name: ek-invokeeureka: instance:  hostname: localhost client:   serviceUrl:    defaultZone: http://localhost:8761/eureka/

编写一个 REST 服务 调用服务提供者的 “/hello”

@RestController@Configurationpublic class InvokeController {  @Bean  @LoadBalanced  public RestTemplate getRestTemplate() {    return new RestTemplate();  }  @RequestMapping("/invoke")  public String invoke() {    RestTemplate restTemplate = getRestTemplate();    return restTemplate.getForObject("http://ek-provider/hello", String.class);  }}

在传统模式中,我们通常会用Apache中的Httpclient来调用 REST 服务,在这里我们使用 Spring 提供调用 REST 服务的组件 RestTemplate。 RestTemplate 本身并不具备调用分布式服务的能力,但是RestTemplate的bean被@LoadBalanced注解修饰后,这个RestTemplate实例就具有访问分布式服务的能力,这得益于 Spring 为其提供的各种拦截器

开启Eureka客户端注解 @EnableEurekaServer

@EnableEurekaClient@SpringBootApplicationpublic class EkInvokeApplication {  public static void main(String[] args) {    new SpringApplicationBuilder(EkInvokeApplication.class).run(args);  }}

启动之后在 Eureka 控制台可以看到服务调用者已经在 Eureka 中注册

之后在浏览器访问服务调用者的 “invoke” 接口 返回如下

总结

Eureka 服务器 通过心跳链接来维护最新的注册信息,这些注册信息都保存在内存中。

Eureka 服务提供者 主要进行:

  1. 向 Eureka 服务器注册服务
  2. 发送心跳到 Eureka服务器,使 Eureka 服务器注册信息保持最新
  3. 向服务器获取最新的注册列表,通常 Eureka 客户端既是服务提供者也是服务调用者,但其主要职责为服务提供。

Eureka 服务调用者 主要进行:

  1. 向 Eureka 服务器注册服务
  2. 发送心跳到 Eureka服务器,使 Eureka 服务器注册信息保持最新
  3. 向服务器获取最新的注册列表,通常 Eureka 客户端既是服务提供者也是服务调用者,但其主要职责为发现与调用。

源码地址:https://github.com/xc564864894/springcloud/tree/master/Eureka(%E4%B8%80)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表