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 https://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>3.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2025.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.24</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>-->
<!-- <version>5.0.0</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.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <version>3.8.1</version>-->
<!-- <configuration>-->
<!-- <source>17</source> <!– 需与项目JDK版本一致 –>-->
<!-- <target>17</target>-->
<!-- <encoding>UTF-8</encoding>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.1.0</version>
<!-- 如果不配置 generate-daemons,则打包命令为 mvn clean package appassembler:assemble -->
<!-- 如果配置了 generate-daemons,打包命令可以是 mvn clean package 也可以是 mvn clean package appassembler:assemble -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- flat与lib共同决定将项目用的的所有jar包复制到lib目录下 -->
<repositoryLayout>flat</repositoryLayout>
<!--从哪里copy配置文件-->
<configurationSourceDirectory>src/main/resources</configurationSourceDirectory>
<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
<!--是否copy配置文件-->
<copyConfigurationDirectory>true</copyConfigurationDirectory>
<!--生成的项目的目录位置,这里的client是项目的名称,你可以根据你的需要自己随便命名-->
<assembleDirectory>${project.build.directory}/client</assembleDirectory>
<!--配置文件存放在conf目录路径-->
<configurationDirectory>conf</configurationDirectory>
<!-- 打包的jar,以及maven依赖的jar放到这个目录里面 -->
<repositoryName>lib</repositoryName>
<!-- 可执行脚本的目录 -->
<binFolder>bin</binFolder>
<!-- 生成linux, windows两种平台的执行脚本 -->
<platforms>
<platform>windows</platform>
<platform>unix</platform>
</platforms>
<!-- 针对不同平台生成不同类型的启动脚本 -->
<binFileExtensions>
<unix>.sh</unix>
<windows>.bat</windows>
</binFileExtensions>
<!--编码,日志-->
<encoding>UTF-8</encoding>
<logsDirectory>logs</logsDirectory>
<tempDirectory>tmp</tempDirectory>
<!--java启动参数-->
<extraJvmArguments>-Xms128m</extraJvmArguments>
<programs>
<program>
<!--指定主类,脚本名。会生成shell/bat两种类型,也可用platforms指定运行平台-->
<mainClass>com.example.demo.OpenFeignApplication</mainClass>
<!-- 生成的脚本文件的名称,比如start.sh,你也可以根据你的需要命名成其他名字 -->
<name>start</name>
</program>
</programs>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、application.yml
server:
port: 8088
spring:
datasource:
url: jdbc:mysql://localhost:3306/wscob
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
root: DEBUG
3、启动项
@SpringBootApplication (scanBasePackages = {"com.example.controller","com.example.imp"})
@EnableFeignClients(basePackages = "com.example.imp")
@EnableAspectJAutoProxy(proxyTargetClass = true) // 启用AOP代理 熔断需要使用
public class OpenFeignApplication {
public static void main(String[] args) {
SpringApplication.run(OpenFeignApplication.class, args);
}
}
4、访问FeignClient配置
@FeignClient(name = "${user.service.name}", url = "${user.service.url}")
public interface UserServiceClient {
@GetMapping("/hello")
String sayHello();
}
4、application.properties
spring.application.name=demo
user.service.name=user-service
user.service.url=http://localhost:9081
5、service层
@Service
public class MyService {
@Autowired
private UserServiceClient feignClient;
@CircuitBreaker(name = "checkCircuitBreaker", fallbackMethod = "fallbackForCallExternalService")
public String callExternalService(String param) {
// 模拟调用可能失败的外部服务
String ss=null;
try {
// 可能抛出多种异常的代码
ss= feignClient.sayHello();
} catch (FeignException e) {
// 处理多种异常
throw new RuntimeException("Service call failed!");
}
return ss;
}
// 降级方法(参数和返回类型与主方法一致)
private String fallbackForCallExternalService(String param, Throwable t) {
// 记录异常或执行备用逻辑
return "服务连接异常" + t.getMessage();
}
}
5、controller层
@RestController
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/get-data1")
public String getData1() {
return myService.callExternalService("fail");
}
}
2426

被折叠的 条评论
为什么被折叠?



