栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud构建Eureka应用的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud构建Eureka应用的方法

Eureka 介绍

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

程序结构

创建Eureka Server

maven依赖

  
    
      
 org.springframework.cloud
 spring-cloud-dependencies
 Dalston.SR1
 pom
 import
      
    
  

  
    
      org.springframework.cloud
      spring-cloud-starter-eureka-server
    
  

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

server:
 port: 8761

开启Eureka服务注解 @EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public 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控制台 输入图片说明

创建服务提供者

依赖

    
      org.springframework.cloud
      spring-cloud-starter-config
    
    
      org.springframework.cloud
      spring-cloud-starter-eureka
    
    
      org.springframework.cloud
      spring-cloud-starter-ribbon
    

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

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

创建一个 REST 服务

@RestController
public class HelloController {

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

开启Eureka客户端注解 @EnableEurekaServer

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

  }
}

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

创建服务调用者

依赖

    
      org.springframework.cloud
      spring-cloud-starter-config
    
    
      org.springframework.cloud
      spring-cloud-starter-eureka
    
    
      org.springframework.cloud
      spring-cloud-starter-ribbon
    

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

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

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

@RestController
@Configuration
public 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
@SpringBootApplication
public 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)

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/141449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号