From be6f985f7f8183967e1b16f78779bad01e8597cd Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Tue, 10 Jan 2023 13:51:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:feign=E7=9A=84=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springcloud-service-portal/pom.xml | 7 ++++ .../kwan/springcloud/PortalApplication.java | 3 ++ .../kwan/springcloud/api/ComputeClient.java | 25 +++++++++++ .../controller/FeignController.java | 41 +++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 springcloud-service-portal/src/main/java/com/kwan/springcloud/api/ComputeClient.java create mode 100644 springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/FeignController.java diff --git a/springcloud-service-portal/pom.xml b/springcloud-service-portal/pom.xml index a726db8..4bde00f 100644 --- a/springcloud-service-portal/pom.xml +++ b/springcloud-service-portal/pom.xml @@ -34,6 +34,13 @@ + + org.springframework.cloud + spring-cloud-starter-feign + 1.4.7.RELEASE + + + diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java index 2eeef9d..9410f71 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java @@ -3,7 +3,10 @@ package com.kwan.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.openfeign.EnableFeignClients; + +@EnableFeignClients @EnableEurekaClient @SpringBootApplication public class PortalApplication { diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/api/ComputeClient.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/api/ComputeClient.java new file mode 100644 index 0000000..a563cd3 --- /dev/null +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/api/ComputeClient.java @@ -0,0 +1,25 @@ +package com.kwan.springcloud.api; + +import com.kwan.springcloud.response.Result; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.io.Serializable; + +/** + * 使用feign配置goods-service服务 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/1/10 13:45 + */ +@FeignClient("goods-service/kwanGoodsInfo") +public interface ComputeClient { + + @RequestMapping(method = RequestMethod.GET, value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) + Result selectOne(@PathVariable Serializable id); + +} diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/FeignController.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/FeignController.java new file mode 100644 index 0000000..d4fea86 --- /dev/null +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/FeignController.java @@ -0,0 +1,41 @@ +package com.kwan.springcloud.controller; + +import com.kwan.springcloud.api.ComputeClient; +import com.kwan.springcloud.response.Result; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.Serializable; + + +/** + * portal控制器 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/1/8 11:12 + */ +@RestController +@RequestMapping("feign") +public class FeignController { + + @Autowired + private ComputeClient computeClient; + + + /** + * 通过主键查询单条数据 + * + * @param id 主键 + * @return 单条数据 + */ + @GetMapping(value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) + public Result selectOne(@PathVariable Serializable id) { + //调用远程的controller + return computeClient.selectOne(id); + } +} \ No newline at end of file -- GitLab