美文网首页SpringCloud极简入门手册
SpringCloud极简入门(二)服务注册与发现

SpringCloud极简入门(二)服务注册与发现

作者: 叩丁狼教育 | 来源:发表于2018-04-24 10:59 被阅读229次

    作者:陈刚,叩丁狼高级讲师。原创文章,转载请注明出处。

    一.服务治理

    服务治理从名字上理解就是对服务的管理,我们知道在微服务架构下,我们的业务模块会被抽成一个个独立的服务,服务治理就是对那些独立的服务实现自动化注册与发现。在服务实例不多的情况下服务之间的通信我们可以使用静态配置来实现服务调用,但是随着服务的不断增多,再加上服务地址的变动,服务名的变动等,使用静态配置就会显得越来越难维护,而服务治理模块就很好的解决了这个问题,服务治理框架有很多,我们学习的是Netflix Eureka。

    二.Netflix Eureka

    在 Spring Cloud 中我们使用 Netflix Eureka 来实现服务注册与发现,它是一种基于REST的服务,Eureka包含了服务端(Eureka server)和客户端(Eureka client)两部分,Eureka服务端即注册中心,主要用于服务定位,以实现中间层服务器的负载平衡和故障转移,而Eureka客户端主要处理服务的注册与发现,并通过心跳机制来实现服务续约。

    例如订单服务(消费者)需要和用户服务(提供者)通信,那订单服务和用户服务都需要在注册中心进行注册,而注册中心的服务注册列表会记录每一个注册过的服务的通信地址,服务ID等相关信息,如果订单服务需要和用户服务进行通信,订单服务需要先向注册中心发起咨询服务请求获取用户服务的通信地址等信息,当订单服务发起调用时就根据拿到的通信地址实现通信。图例:

    image.png

    三.搭建 Eureka Server 服务注册中心

    1.在我们的项目中使用多模块的方式进行搭建应用,所以我们这里事先创建好一个空的项目,在空项目的基础上以模块的方式去搭建多个应用,这里全程使用idea演示,建立空项目如下:

    image.png

    2,在搭建好的空项目中选择 file -> new -> Module ,使用“start.spring.io”搭建SpringBoot项目

    img2.png

    3.此处勾选Eureka Server项,我们要搭建Eureka服务端,点击next完成项目的搭建,我的项目名为EurekaServer

    img3.png

    4.搭建好的项目EurekaServer的pom文件中的依赖包如下:

    <!--SpringBoot基础依赖-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
        </properties>
    
        <dependencies>
            <!--eureka 依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
      spring-cloud-starter-netflix-eureka-server :包括了EurekaServer和EurekaClient相关依赖
    

    5.找到主程序配置类打上 @EnableEurekaServer标签,开启Eureka server

    @SpringBootApplication  //SpringBoot 核心配置
    @EnableEurekaServer     //开启 Eureka server
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    

    6.Eureka server 配置,resources目录下新建application.yml配置如下(也可以使用application.properties文件进行配置):

    server:
      port: 1111    #服务端口号为 1111
    eureka:
      instance:
        hostname: localhost #主机名
      client:
        registerWithEureka: false #禁止注册中心注册自己
        fetchRegistry: false      #禁止检索服务
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #注册中心地址
    
    

    7.启动服务并访问:运行主程序中的main方法启动程序,访问:http://localhost:1111,即可看到以下界面:

    img4.png
    其中 Instances currently registered with Eureka 项是空的代表还没有任何服务进行注册
    

    四.Eureka Client 服务客户端

    1.搭建Eureka Client客户端应用:选择 file -> new -> Module ,使用“start.spring.io”搭建SpringBoot项目,搭建过程同服务注册中心一样,pom依赖也一样,我的项目名为Producer。

    2.找到主程序类ProducerApplication打上@EnableEurekaClient注解,该注解赋予了服务注册功能和去注册表中查找其他服务的功能,但是该注解只能在Eureka中使用,除了该注解我们也可以使用 @EnableDiscoveryClient,如下:

    @SpringBootApplication
    @EnableEurekaClient     //开启 eureka客户端,也可以使用@EnableDiscoveryClient注解
    public class ProducerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProducerApplication.class, args);
        }
    }
    
    

    2.配置文件:application.yml

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:1111/eureka/  #注册中心服务地址
    server:
      port: 2222  #当前服务端口
    spring:
      application:
        name: producer  #当前服务名
    

    3.启动注册中心EurekaServer,再启动Producer,此时访问注册中心地址http:localhost:1111地址,将会看到Instances currently registered with Eureka 项目下已经有一个服务被注册,服务名为:“PRODUCER”

    img5.png WechatIMG7.jpeg

    相关文章

      网友评论

      • 阿卡莎喵喵:我照着教程试了一下,老是提示找不到producer,服务端和消费端都显示已经注册了

      本文标题:SpringCloud极简入门(二)服务注册与发现

      本文链接:https://www.haomeiwen.com/subject/ucwdlftx.html