SpringCloud微服务 之 Eureka(二)

朱雀 2022-05-09 12:28 371阅读 0赞

前言

上一章节中我们学习了服务注册与发现的不同类型的机制以及各自的优劣势,本节我们将通过时间里来学习一下在SpringCloud构建的为服务中如何使用Eureka来实现服务的注册与发现。

案例

  • Eureka 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. <artifactId>microservice-deal-eureka</artifactId>
      6. <packaging>jar</packaging>
      7. <name>microservice-deal-eureka</name>
      8. <description>Demo project for Spring Boot</description>
      9. <parent>
      10. <groupId>com.example</groupId>
      11. <artifactId>microservice-deal-parent</artifactId>
      12. <version>0.0.1-SNAPSHOT</version>
      13. </parent>
      14. <!-Eureka Server核心组件->
      15. <dependencies>
      16. <dependency>
      17. <groupId>org.springframework.cloud</groupId>
      18. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      19. </dependency>
      20. </dependencies>
      21. </project>
    • properties.yml

      1. server:
      2. port: 8001 # 指定该Eureka实例的端口
      3. eureka:
      4. server:
      5. enableSelfPreservation: false
      6. client:
      7. registerWithEureka: false #Server端不做自我注册
      8. fetchRegistry: false
      9. serviceUrl:
      10. defaultZone: http://localhost:8001/eureka/ #Eureka Client注册路径
      11. healthcheck:
      12. enabled: true #开启健康检查
    • Core Code:开启EnableEurekaServer特性,声明这是一个Eureka Server

      1. @SpringBootApplication
      2. @EnableEurekaServer //Server端必须开启EnableEurekaServer特性
      3. public class MicroserviceDealEurekaApplication {
      4. public static void main(String[] args) {
      5. SpringApplication.run(MicroserviceDealEurekaApplication.class, args);
      6. }
      7. }
    • 说明
      Eureka Server只展示核心代码与要点,另外所有案例都是用Maven的Parent-Sub管理(后续的所有案例也将延续),pom中的一些通用配置在父类的pom中统一配置,此处不赘述。
  • Eureka Client端编写

    • 项目结构
      在这里插入图片描述
    • 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. <artifactId>microservice-deal-cloud</artifactId>
      6. <packaging>jar</packaging>
      7. <name>microservice-deal-cloud</name>
      8. <description>Demo project for Spring Boot</description>
      9. <parent>
      10. <groupId>com.example</groupId>
      11. <artifactId>microservice-deal-parent</artifactId>
      12. <version>0.0.1-SNAPSHOT</version>
      13. </parent>
      14. <dependencies>
      15. <dependency>
      16. <groupId>org.springframework.boot</groupId>
      17. <artifactId>spring-boot-starter-data-jpa</artifactId>
      18. </dependency>
      19. <dependency>
      20. <groupId>com.h2database</groupId>
      21. <artifactId>h2</artifactId>
      22. </dependency>
      23. <!-- Eureka Client核心组件 -->
      24. <dependency>
      25. <groupId>org.springframework.cloud</groupId>
      26. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      27. </dependency>
      28. </dependencies>
      29. </project>
    • properties.yml

      1. server:
      2. port: 7900
      3. spring:
      4. application:
      5. name: microservice-deal-cloud
      6. jpa:
      7. generate-ddl: false
      8. show-sql: true
      9. hibernate:
      10. ddl-auto: none
      11. datasource: # 指定数据源
      12. platform: h2 # 指定数据源类型
      13. schema: classpath:schema.sql # 指定h2数据库的建表脚本
      14. data: classpath:data.sql # 指定h2数据库的数据脚本
      15. logging: # 配置日志级别,让hibernate打印出执行的SQL
      16. level:
      17. root: INFO
      18. org.hibernate: INFO
      19. org.hibernate.type.descriptor.sql.BasicBinder: TRACE
      20. org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
      21. eureka:
      22. client:
      23. serviceUrl:
      24. defaultZone: http://localhost:8001/eureka/ #Eureka Client 注册地址
      25. #Authorization
      26. #defaultZone: http://Dustyone:bai5331359@localhost:8001/eureka/
      27. instance:
      28. prefer-ip-address: true
      29. metadata-map:
      30. zone: Asia #Eureka可以解析的metadata,会影响到客户端的行为
      31. customizedMetadata: eurekaCustomizedMetadata #Eureka不能解析的metadata,不会影响客户端行为 块通过 http://localhost:8761/eureka/apps/{serviceName}查找
      32. instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
      33. feign:
      34. hystrix:
      35. enabled: true
    • Core Code

      1. @SpringBootApplication
      2. @EnableDiscoveryClient //此处开启EnableDiscoveryClient特性,声明设施一个·这是一个Eureka的Client
      3. public class MicroserviceDealApplication {
      4. public static void main(String[] args) {
      5. SpringApplication.run(MicroserviceDealApplication.class, args);
      6. }
      7. }
      8. ```
  • 验证服务注册与发现
    先启动Eureka Server 服务,然后启动Eureka Client 然后访问:http://localhost:8001/
    在这里插入图片描述

小结

  • Eureka Server端必须依赖Eureka Server组件,并且在启动类上开启@EnableEurekaServer特性,同时在配置文件中配置Eureka Server包括但不限于禁止自我注册、Eureka Client注册地址等。
  • Eureka Client端必须依赖Eureka Client组件,并且在启动类上开启@EnableEurekaClient特性,同时在配置文件中配置Eureka Client包括但不限于Eureka Client注册地址、注册认证等。
  • 在后面的学习过程中会详细讲解Eureka Server & Client的优化配置项。
  • 本节学习源代码在这里。

发表评论

表情:
评论列表 (有 0 条评论,371人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringCloud服务 Eureka()

    前言 上一章节中我们学习了服务注册与发现的不同类型的机制以及各自的优劣势,本节我们将通过时间里来学习一下在SpringCloud构建的为服务中如何使用Eureka来实现服