SpringCloud之服务的注册与发现Eureka(Finchley版本)

avatar 2018年07月25日10:59:16 8 4663 views
博主分享免费Java教学视频,B站账号:Java刘哥

一、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有一定的了解。  

二、创建服务注册中心

在这里,我还是采用Eureka作为服务注册与发现的组件。 2.1 首先创建一个maven主工程。 首先创建一个主Maven工程,在其pom文件引入依赖,spring Boot版本为2.0.3.RELEASE,Spring Cloud版本为Finchley.RELEASE。这个pom文件作为父pom文件,起到依赖版本控制的作用,其他module工程继承该pom。这一系列文章全部采用这种模式,其他文章的pom跟这个pom一样。再次说明一下,以后不再重复引入。代码如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <groupId>com.liuyanzhao.cloud</groupId>
  6.     <artifactId>SpringCloud</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <packaging>pom</packaging>
  9.     <name>SpringCloud</name>
  10.     <description>Demo project for Spring Boot</description>
  11.     <parent>
  12.         <groupId>org.springframework.boot</groupId>
  13.         <artifactId>spring-boot-starter-parent</artifactId>
  14.         <version>2.0.3.RELEASE</version>
  15.         <relativePath/>
  16.     </parent>
  17.     <modules>
  18.         <module>eureka-server</module>
  19.         <module>service-hi</module>
  20.         <module>service-ribbon</module>
  21.         <module>service-feign</module>
  22.     </modules>
  23.     <properties>
  24.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  25.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  26.         <java.version>1.8</java.version>
  27.         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  28.     </properties>
  29.     <dependencies>
  30.         <dependency>
  31.             <groupId>org.springframework.boot</groupId>
  32.             <artifactId>spring-boot-starter-test</artifactId>
  33.             <scope>test</scope>
  34.         </dependency>
  35.     </dependencies>
  36.     <dependencyManagement>
  37.         <dependencies>
  38.             <dependency>
  39.                 <groupId>org.springframework.cloud</groupId>
  40.                 <artifactId>spring-cloud-dependencies</artifactId>
  41.                 <version>${spring-cloud.version}</version>
  42.                 <type>pom</type>
  43.                 <scope>import</scope>
  44.             </dependency>
  45.         </dependencies>
  46.     </dependencyManagement>
  47.     <build>
  48.         <plugins>
  49.             <plugin>
  50.                 <groupId>org.springframework.boot</groupId>
  51.                 <artifactId>spring-boot-maven-plugin</artifactId>
  52.             </plugin>
  53.         </plugins>
  54.     </build>
  55. </project>
  2.2 然后创建2个model工程: 一个model工程作为服务注册中心,即Eureka Server,另一个作为Eureka Client (即 SpringBoot 项目)。 下面以创建server为例子,详细说明创建过程: 右键工程->创建model-> 选择spring initialir 如下图:   然后填写一些项目信息   下一步->选择cloud discovery->eureka server ,然后一直下一步就行了。   创建完项目后,将 src 目录删除 创建完后的工程,其pom.xml继承了父pom文件,并引入spring-cloud-starter-netflix-eureka-server的依赖,代码如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <groupId>com.liuyanzhao.cloud</groupId>
  6.     <artifactId>eureka-server</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <packaging>jar</packaging>
  9.     <name>eureka-server</name>
  10.     <description>Demo project for Spring Boot</description>
  11.     <parent>
  12.         <groupId>com.liuyanzhao.cloud</groupId>
  13.         <artifactId>SpringCloud</artifactId>
  14.         <version>0.0.1-SNAPSHOT</version>
  15.     </parent>
  16.     <dependencies>
  17.         <dependency>
  18.             <groupId>org.springframework.cloud</groupId>
  19.             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  20.         </dependency>
  21.     </dependencies>
  22. </project>
  2.3 启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
  1. package com.liuyanzhao.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. @EnableEurekaServer
  7. public class EurekaServerApplication {
  8.     public static void main(String[] args) {
  9.         SpringApplication.run(EurekaServerApplication.class, args);
  10.     }
  11. }
  2.4 eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
  1. server:
  2.   port: 8761
  3. eureka:
  4.   instance:
  5.     hostname: localhost
  6.   client:
  7.     registerWithEureka: false
  8.     fetchRegistry: false
  9.     serviceUrl:
  10.       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  11. spring:
  12.   application:
  13.     name: eurka-server
通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.   2.5 eureka server 是有界面的,启动工程,打开浏览器访问: http://localhost:8761 ,界面如下:   No application available 没有服务被发现 ……^_^ 因为没有注册服务当然不可能有服务被发现了。  

三、创建一个服务提供者 (eureka client)

当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。 创建过程同server类似,创建完pom.xml如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <groupId>com.liuyanzhao.cloud</groupId>
  6.     <artifactId>service-hi</artifactId>
  7.     <version>0.0.1-SNAPSHOT</version>
  8.     <packaging>jar</packaging>
  9.     <name>service-hi</name>
  10.     <description>Demo project for Spring Boot</description>
  11.     <parent>
  12.         <groupId>com.liuyanzhao.cloud</groupId>
  13.         <artifactId>SpringCloud</artifactId>
  14.         <version>0.0.1-SNAPSHOT</version>
  15.     </parent>
  16.     <dependencies>
  17.         <dependency>
  18.             <groupId>org.springframework.cloud</groupId>
  19.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  20.         </dependency>
  21.         <dependency>
  22.             <groupId>org.springframework.boot</groupId>
  23.             <artifactId>spring-boot-starter-web</artifactId>
  24.         </dependency>
  25.     </dependencies>
  26.     <build>
  27.         <plugins>
  28.             <plugin>
  29.                 <groupId>org.springframework.boot</groupId>
  30.                 <artifactId>spring-boot-maven-plugin</artifactId>
  31.             </plugin>
  32.         </plugins>
  33.     </build>
  34. </project>
  通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
  1. package com.liuyanzhao.cloud;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @SpringBootApplication
  10. @EnableEurekaClient
  11. @RestController
  12. public class ServiceHiApplication {
  13.     public static void main(String[] args) {
  14.         SpringApplication.run( ServiceHiApplication.class, args );
  15.     }
  16.     @Value("${server.port}")
  17.     String port;
  18.     @RequestMapping("/hi")
  19.     public String home(@RequestParam(value = "name", defaultValue = "saysky") String name) {
  20.         return "hi " + name + " ,i am from port:" + port;
  21.     }
  22. }
  仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:
  1. server:
  2.   port: 8080
  3. spring:
  4.   application:
  5.     name: service-hi
  6. eureka:
  7.   client:
  8.     serviceUrl:
  9.       defaultZone: http://localhost:8761/eureka/
需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。 启动工程,打开http://localhost:8761 ,即eureka server 的网址:   你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862 这时打开 http://localhost:8080/hi?name=言曌 ,你会在浏览器上看到 :       代码地址:https://github.com/saysky/Spring-Cloud-Study-Demo

四、参考资料

http://blog.csdn.net/forezp/article/details/69696915 http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html  
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:2   待审核评论数:0
  1. avatar hhh

    666学习

  2. avatar ccg5230

    需要引入spring-cloud-starter-config依赖,否则使用Finchley.SR2 版本启动报错,java.lang.NoClassDefFoundError: org/springframework/cloud/context/environment/EnvironmentChangeEvent