史上最完美的SpringCloud软件|第一篇:平台的注册中心Eureka(Finchley版本)

原创
小哥 3年前 (2022-10-28) 阅读数 3 #大杂烩

请注明转载来源:
http://blog.csdn.net/forezp/article/details/81040925
本文出自 方志鹏的博客

个人博客纯版: https://www.fangzhipeng.com/springcloud/2018/08/01/sc-f1-eureka.html

一、spring cloud简介

鉴于最简单的Spring Cloud《教程》很受读者欢迎。再一次,我特意升级了版本。当前支持的版本为Spring Boot版本2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。

Finchley该版本的官方文档如下:
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

spring cloud 为开发人员提供快速构建分布式系统的工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策活动、分布式会话等。它运行在一个简单的环境中,可以在开发人员的计算机上运行。其他说明spring cloud是基于springboot,因此有必要发展springboot有一定的了解,如果你不懂,可以读这篇文章: 2小时学会springboot 。也适用于“微服务体系结构” 如果你不知道,你可以通过搜索引擎搜索“微服务架构”。

2.创建服务注册表

在这里,我仍然领养Eureka作为服务注册和发现的一个组件,如Consul 之后,将对文章进行详细介绍。

2.1 首先创建一个maven主工程。

首先创建一个母版。Maven工程学,在ITS中pom文件引入了依赖关系,spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。这个pom作为父文件的文件pom文件,充当依赖于版本的控件,其他module该项目继承pom。这一系列文章都采用了这种模式,其他文章也是如此pom跟这个pom一样的。再说一次,它不会在未来再次推出。代码如下:



    4.0.0

    com.forezp
    sc-f-chapter1
    0.0.1-SNAPSHOT
    pom

    sc-f-chapter1
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
        
    

    
        eureka-server
        service-hi
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    

    
        
            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
            
        
    

2.2 然后创建2个model工程:一个model工程即服务注册,即E。Eureka Server,另一个Eureka Client。

下面要创建的内容server例如,详细介绍了创建过程:

右键工程->创建model-> 选择spring initialir 如下图:

下一步->选择cloud discovery->eureka server ,那就去做吧。

在该项目创建后,其。pom.xml继承了父pom文件,并介绍了spring-cloud-starter-netflix-eureka-server代码如下:



    4.0.0

    com.forezp
    eureka-server
    0.0.1-SNAPSHOT
    jar

    eureka-server
    Demo project for Spring Boot

    
        com.forezp
        sc-f-chapter1
        0.0.1-SNAPSHOT
    

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

2.3 启动服务注册表 ,则只需要一个批注@EnableEurekaServer,此批注需要位于springboot该项目的启动application类上加:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run( EurekaServerApplication.class, args );
    }
}

2.4 eureka是一个没有后端缓存的高可用性组件,默认情况下,每个实例都需要在注册后向注册表发送心跳(因此可以在内存中完成)。erureka server也是一个eureka client ,您必须指定一个 server。eureka server的简介appication.yml:

server:
  port: 8761

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

spring:
  application:
    name: eurka-server

通过eureka.client.registerWithEureka:false和fetchRegistry:false以显示他是一个eureka server.

2.5 eureka server 有一个界面,启动项目,打开浏览器访问:
http://localhost:8761 ,界面如下:

No application available 未找到任何服务 …… _
因为没有注册的服务,所以当然不能发现任何服务。

3.创建服务提供商 (eureka client)

当client向server注册时,它提供一些元数据,如主机和端口,URL、主页等。Eureka server 从每个client该实例接收心跳消息。 如果心跳超时,则通常从注册该实例。server中删除。

创建过程是相同的。server类似,创建完pom.xml如下:



    4.0.0

    com.forezp
    service-hi
    0.0.1-SNAPSHOT
    jar

    service-hi
    Demo project for Spring Boot

    
        com.forezp
        sc-f-chapter1
        0.0.1-SNAPSHOT
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

通过注解@EnableEurekaClient 表明你是一名eurekaclient.

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceHiApplication.class, args );
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

}

仅仅@EnableEurekaClient这是不够的,您还需要在配置文件中指明您的服务注册中心的地址,application.yml配置文件如下:

server:
  port: 8762

spring:
  application:
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

需要指明spring.application.name,这是非常重要的,这通常是基于这一点,在未来的服务和服务相互调用。name 。
启动项目,打开http://localhost:8761 ,即eureka server 的网址:

您会发现服务中已经注册了一个服务。服务名称为SERVICE-HI ,端口为7862

这时打开 http://localhost:8762/hi?name=forezp ,您将在浏览器上看到它。 :

hi forezp,i am from port:8762

源代码下载: https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter1

更多阅读

历史上最简单的 SpringCloud 教程汇总

SpringBoot教程汇总

Java系列访谈纪要

4.参考资料

http://blog.csdn.net/forezp/article/details/69696915

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html


扫码,注意公号,惊喜

(请注明转载本网站文章的作者和来源 方志鹏的博客

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除