版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012437781/article/details/82960457
前言
上一章节中我们学习了服务注册与发现的不同类型的机制以及各自的优劣势,本节我们将通过时间里来学习一下在SpringCloud构建的为服务中如何使用Eureka来实现服务的注册与发现。
案例
-
Eureka Server 端编写
-
项目结构
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-deal-eureka</artifactId> <packaging>jar</packaging> <name>microservice-deal-eureka</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>microservice-deal-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-Eureka Server核心组件-> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
-
properties.yml
server: port: 8001 # 指定该Eureka实例的端口 eureka: server: enableSelfPreservation: false client: registerWithEureka: false #Server端不做自我注册 fetchRegistry: false serviceUrl: defaultZone: http://localhost:8001/eureka/ #Eureka Client注册路径 healthcheck: enabled: true #开启健康检查
-
Core Code:开启EnableEurekaServer特性,声明这是一个Eureka Server
@SpringBootApplication @EnableEurekaServer //Server端必须开启EnableEurekaServer特性 public class MicroserviceDealEurekaApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDealEurekaApplication.class, args); } }
-
说明
Eureka Server只展示核心代码与要点,另外所有案例都是用Maven的Parent-Sub管理(后续的所有案例也将延续),pom中的一些通用配置在父类的pom中统一配置,此处不赘述。
-
-
Eureka Client端编写
-
项目结构
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>microservice-deal-cloud</artifactId> <packaging>jar</packaging> <name>microservice-deal-cloud</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>microservice-deal-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <!-- Eureka Client核心组件 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
-
properties.yml
server: port: 7900 spring: application: name: microservice-deal-cloud jpa: generate-ddl: false show-sql: true hibernate: ddl-auto: none datasource: # 指定数据源 platform: h2 # 指定数据源类型 schema: classpath:schema.sql # 指定h2数据库的建表脚本 data: classpath:data.sql # 指定h2数据库的数据脚本 logging: # 配置日志级别,让hibernate打印出执行的SQL level: root: INFO org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE eureka: client: serviceUrl: defaultZone: http://localhost:8001/eureka/ #Eureka Client 注册地址 #Authorization #defaultZone: http://Dustyone:bai5331359@localhost:8001/eureka/ instance: prefer-ip-address: true metadata-map: zone: Asia #Eureka可以解析的metadata,会影响到客户端的行为 customizedMetadata: eurekaCustomizedMetadata #Eureka不能解析的metadata,不会影响客户端行为 块通过 http://localhost:8761/eureka/apps/{serviceName}查找 instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} feign: hystrix: enabled: true
-
Core Code
@SpringBootApplication @EnableDiscoveryClient //此处开启EnableDiscoveryClient特性,声明设施一个·这是一个Eureka的Client public class MicroserviceDealApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDealApplication.class, args); } } ```
-
-
验证服务注册与发现
先启动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的优化配置项。
- 本节学习源代码在这里。