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

SpringCloud之服务注册与发现Spring Cloud Eureka实例代码

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

SpringCloud之服务注册与发现Spring Cloud Eureka实例代码

一、Spring Cloud简介

Spring Cloud是一个基千SpringBoot实现的微服务架构开发 工具。它为微服务架构中涉及的 配置管理、服务治理、 断路器、 智能路由、微代理、 控制总线、 全局锁、 决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。
Spring Cloud包含了多个子项目(针对分布式系统中涉及的多个不同开源产品,还可能会新增),如下所述。

Spring Cloud Config: 配置管理工具、Spring Cloud Netflix: 核心组件、Spring Cloud Bus: 事件、消息总线等等。

二、Spring Cloud Eureka

Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件中的一部分, 它基于 NetflixEureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。 Spring Cloud 通过为Eureka 增加了 Spring Boot 风格的自动化配置,我们只需通过简单引入依赖和注解配置就能让 Spring Boot 构建的微服务应用轻松地与 Eureka 服务治理体系进行整合。

服务治理可以说是微服务架构中最为核心和基础的模块, 它主要用来实现各个微服务实例的自动化注册与发现。

三、实例

(1)工具:IntelliJ IDEA

(2)新建一个空项目


(3)新建一个Eureka Server,Module,名为:eurekaserver

工程右键->创建Module-> 选择spring initialir ->填好项目名,下一步->,如图选择Eureka Server:

(3-1)pom.xml

 
  org.springframework.boot 
  spring-boot-starter-web 
 
 
 
  org.springframework.cloud 
  spring-cloud-starter-eureka-server 
 
 
 
  org.springframework.boot 
  spring-boot-starter-test 
  test 
 

(3-2)application.yml

server: 
 port: 5555 
 
eureka: 
 instance: 
  hostname: localhost 
 client: 
  registerWithEureka: false 
  fetchRegistry: false 
  serviceUrl: 
   defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 

备注:eureka.client.register-with-eureka: 由于该应用为注册中心,所以设置为 false, 代表不向注册中心注册自己。
eureka.client.fetch-registry: 由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为 false。

(3-3)入口类

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); 
  } 
} 

(3-4)启动测试


在完成了上面的配置后,启动应用并访问 http://localhost:5555/。可以看到如下图所示的 Eureka 信息面板, 其中 Instances currently registered with Eureka 栏是空的, 说明该注册中心还没有注册任何服务。

(4)注册服务提供者在完成了服务注册中心的搭建之后,接下来我们尝试将一个既有的 Spring Boot 应用加入 Emeka 的服务治理体系中去。

(5)再新建一个Eureka Client,Module,名为:helloserver,这个helloserver作为eurekaserver的子model


(6)改造父model和子model的pom配置(6-1)eurekaserver的pom.xml配置:

pom 
 
  ../helloserver 
 

(6-2)eurekaserver的全部pom.xml:

 
 
  4.0.0 
  com.example 
  demoeurekaserver 
  0.0.1-SNAPSHOT 
  pom 
   
    ../helloserver 
   
  eurekaserver 
  Demo project for Spring Boot 
 
   
    org.springframework.boot 
    spring-boot-starter-parent 
    1.5.9.RELEASE 
      
   
 
   
    UTF-8 
    UTF-8 
    1.8 
    Edgware.RELEASE 
   
 
   
     
      org.springframework.boot 
      spring-boot-starter-web 
     
 
     
      org.springframework.cloud 
      spring-cloud-starter-eureka-server 
     
 
     
      org.springframework.boot 
      spring-boot-starter-test 
      test 
     
   
 
   
     
       
 org.springframework.cloud 
 spring-cloud-dependencies 
 ${spring-cloud.version} 
 pom 
 import 
       
     
   
   
     
       
 org.springframework.boot 
 spring-boot-maven-plugin 
       
     
   
 

(6-3)helloserver的pom.xml配置:

 
  com.example 
  demoeurekaserver 
  0.0.1-SNAPSHOT 
  ../eurekaserver/pom.xml 
 

(6-4)helloserver的全部pom.xml配置:

 
 
  4.0.0 
   
    com.example 
    demoeurekaserver 
    0.0.1-SNAPSHOT 
    ../eurekaserver/pom.xml 
   
  helloserver 
  jar 
  helloserver 
  Demo project for Spring Boot 
   
    com.example.helloserver.HelloserverApplication 
   
   
     
      org.springframework.boot 
      spring-boot-starter-web 
     
 
     
      org.springframework.boot 
      spring-boot-starter-test 
      test 
     
   
 
   
     
       
 org.springframework.cloud 
 spring-cloud-dependencies 
 ${spring-cloud.version} 
 pom 
 import 
       
     
   
 
   
     
       
 org.springframework.boot 
 spring-boot-maven-plugin 
       
     
   
 

(6-5)helloserver的application.yml配置:

server: 
 port: 5556 
 
spring: 
 application: 
  name: helloserver 
eureka: 
 client: 
  serviceUrl: 
   defaultZone: http://localhost:5555/eureka/ 

(6-6)helloserver的启动类:

@EnableEurekaServer 
@SpringBootApplication 
@RestController 
public class HelloserverApplication { 
  private final Logger log = (Logger) LoggerFactory.getLogger(HelloserverApplication.class); 
  @Autowired 
  private DiscoveryClient client; 
 
  @RequestMapping(name = "/hello", method = RequestMethod.GET) 
  public String index() { 
    ServiceInstance instance = client.getLocalServiceInstance(); 
    log.info("/hello, host:" + instance.getHost() + ",service_id:" + instance.getServiceId()); 
    return "Hello SpringCloud~"; 
  } 
 
  public static void main(String[] args) { 
    SpringApplication.run(HelloserverApplication.class, args); 
  } 
} 

(7)分别启动eurekaserver和helloserver,并测试:

(7-1)访问eurekaserver:(可以很清楚的看到HELLOSERVER信息)


(7-2)访问helloserver:


(7-3)查看helloserver控制台信息:


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

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

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

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