一 Eureka的基本架构

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。

Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。

而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。

二 Eureka的三大角色

  • Eureka Server 提供服务注册和发现

  • Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到

  • Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务

三 构建步骤

  • 创建eureka-server项目

    • pom.xml


     
     <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <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>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
    registerWithEureka: 是否将自己注册到Eureka服务中,本身就是所有无需注册
    fetchRegistry : 是否从Eureka中获取注册信息
    serviceUrlEureka: 客户端与Eureka服务端进行交互的地址
    • yml

      server:
      port: 8761

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

      @SpringBootApplication
      @EnableEurekaServer
      public class EurekaServerApplication {

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

      }
    • 启动结果

  • 创建producer-service

    • pom.xml

         <dependencies>
      <dependency>
      3 <groupId>org.springframework.boot</groupId>
      4 <artifactId>spring-boot-starter-actuator</artifactId>
      5 </dependency>
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      </dependency>
      </dependencies>

      <dependencyManagement>
      <dependencies>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
      </dependency>
      </dependencies>
      </dependencyManagement>
      <!-- 微服务info内容详细信息 -->
      <build>
      <finalName>microservicecloud</finalName>
      <resources>
      <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      </resource>
      </resources>
      <plugins>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <configuration>
      <delimiters>
      <delimit>$</delimit>
      </delimiters>
      </configuration>
      </plugin>
      </plugins>
      </build>
    • yml

      eureka:
      client:
      serviceUrl:
      defaultZone: http://localhost:8761/eureka/
      instance:
      instance-id: product-service8080 #主机名称:服务名称修改
      prefer-ip-address: true #访问路径可以显示IP地址

      server:
      port: 8080
      spring:
      application:
      name: product-service
      #微服务info内容详细信息
      info:
      app.name: product-service
      company.name: www.topcheer.com

       注info信息到最好还是访问的/actuator/info接口

启动类

@SpringBootApplication
#从Spring Cloud Edgware版本开始, @EnableDiscoveryClient 或 @EnableEurekaClient 可
  • #省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上 public class ProducerServiceApplication { ​ public static void main(String[] args) { SpringApplication.run(ProducerServiceApplication.class, args); } ​ } ​

结果:

Eureka中的元数据获取

/**
* 参数:商品id
* 通过订单系统,调用商品服务根据id查询商品信息
* 1.需要配置商品对象
* 2.需要调用商品服务
* 使用java中的urlconnection,httpclient,okhttp
*/
@RequestMapping(value = "/buy1/{id}",method = RequestMethod.GET)
public Product findById1(@PathVariable Long id) {
//调用discoveryClient方法
//已调用服务名称获取所有的元数据
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//获取唯一的一个元数据
ServiceInstance instance = instances.get(0);
//根据元数据中的主机地址和端口号拼接请求微服务的URL
Product product = null;
//如何调用商品服务?
product = restTemplate.getForObject("http://service-product/product/1",Product.class);
return product;
}

最新文章

  1. [Fluent NHibernate]一对多关系处理
  2. LeetCode OJ-- Single Number II **@
  3. php练习题:投票
  4. Xcode5创建自己的静态库详解
  5. ubuntu 14.04 root破解
  6. tomcat+redis实现session共享缓存
  7. Caused by: org.springframework.beans.factory.BeanCreationException
  8. mysql 查看数据库中所有表的记录数
  9. 用js提取字符串中的某一段字符
  10. Spring Cloud Alibaba基础教程:使用Nacos作为配置中心
  11. os.path.dirname使用方法
  12. 【原】cpu消耗高,查看对应的线程栈信息
  13. 九度OJ-1042-最长公共子序列(LCS)
  14. python中判断实例可迭代地几种方式
  15. 「Vue」路由
  16. Scrum:The Definition of Done —— 作业有没有写完呢?
  17. 6.查找单链表中的倒数第k个结点
  18. MongoDB(五)-- 副本集(replica Set)
  19. [2019BUAA软工]第0次代码作业
  20. Eclipse 工具下Maven 项目的快速搭建

热门文章

  1. Gradle 梳理:安装、入门使用方法
  2. cvc-complex-type.2.3: Element &#39;dependency&#39; cannot have character [children], because the type&#39;s cont
  3. javaweb技术入门
  4. 《老师说的都对》第一次作业:OUC网上课程评价系统
  5. 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).
  6. window连接远程服务器报函数不支持之解决方案
  7. 【Visual Studio】关于vs 打开网站时报错 配置iis express失败 无法访问IIS元数据库...
  8. redis系列之------简单的动态字符串(SDS)
  9. Linux入门(磁盘与挂载)
  10. DDR3 DDR4 FPGA实现