SpringCloud搭建 — 消费者服务

tech2026-01-24  4

项目结构:

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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- 不指定路径则从maven库中查找父级 --> </parent> <groupId>com.aaron</groupId> <artifactId>consumer</artifactId> <version>1.0-SNAPSHOT</version> <name>consumer</name> <description>Demo project for Spring Boot</description> <!-- 指定统一版本 --> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- hystrix熔断器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- Feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- Slf4j注解依赖包(日志) --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </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> </project>

application.yml配置文件:

#静态常量 static: ip: 127.0.0.1 eurekaport: 8016 #端口 server: port: 8018 spring: application: #服务名称 name: consumer eureka: client: serviceUrl: defaultZone: http://${static.ip}:${static.eurekaport}/eureka instance: prefer-ip-address: true #设置心跳检测检测与续约时间 lease-renewal-interval-in-seconds: 30 lease-expiration-duration-in-seconds: 60 feign: hystrix: enabled: true ribbon: #请求连接的超时时间 ReadTimeout: 60000 #请求处理的超时时间 ConnectTimeout: 60000 hystrix: command: default: execution: timeout: enabled: true isolation: thread: #断路器的超时时间需要大于ribbon的超时时间,不然不会触发重试。 timeoutInMilliseconds: 70000

Feign声明式调用,可以调用注册在Eureka上的其他服务。

hystrix当前服务宕机或者异常自动熔断,防止服务雪崩。

application启动类:

package com.aaron.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }

Eureka:

 

最新回复(0)