企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 添加依赖 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 完整版 ```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"> <parent> <artifactId>cloudframe</artifactId> <groupId>com.gosuncn</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>sys-consumer</artifactId> <dependencies> <dependency> <groupId>com.gosuncn</groupId> <artifactId>common</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/gosuncn/dao/*.xml</include> </includes> </resource> </resources> </build> </project> ``` # 修改主启动类 ```java package com.gosuncn; @EnableFeignClients // 启动FeignClients @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } ``` # 创建ProviderClient ```java package com.gosuncn.feign; @FeignClient(value = "provider") public interface ProviderClient { @GetMapping("/provider") CommonResult createProvider(); } ``` # 创建ProviderService ```java package com.gosuncn.service; @Service public class ProviderService { @Autowired private ProviderClient providerClient; @Autowired private ConsumerMapper consumerMapper; public void feignTest() { providerClient.createProvider(); Consumer consumer = new Consumer(); consumer.setName("Hi feignTest."); consumerMapper.createConsumer(consumer); } } ``` # 修改ConsumerController ```java package com.gosuncn.controller; @RestController public class ConsumerController { @Autowired private ConsumerService consumerService; @Autowired private ProviderService providerService; @GetMapping("/") public CommonResult findAll() { return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), consumerService.findAll().orElse(null)); } @GetMapping("/consumer") public CommonResult createConsumer() { Consumer consumer = new Consumer(); consumer.setName("Hi Boy."); return new CommonResult<>(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), consumerService.createConsumer(consumer)); } @GetMapping("/feign") public void feignTest() { providerService.feignTest(); } } ```