feat(openFeign): 调用第三方接口和微服务调用

This commit is contained in:
Bunny 2025-02-09 19:48:58 +08:00
parent 1a18153ed7
commit f4fc71e738
10 changed files with 36 additions and 115 deletions

View File

@ -1,10 +1,11 @@
package cn.bunny.controller;
import cn.bunny.feign.CallOtherApiFeignClient;
import cn.bunny.feign.CloudFeignClient;
import cn.bunny.feign.CloudFeignClient1;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -14,11 +15,12 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/api")
public class CloudController {
@Qualifier("cn.bunny.feign.CloudFeignClient")
@Autowired
private CloudFeignClient cloudFeignClient;
@Autowired
private CloudFeignClient1 cloudFeignClient1;
private CallOtherApiFeignClient callOtherApiFeignClient;
@Operation(summary = "本体接口调用", description = "feignCloud2")
@GetMapping("feignCloud1")
@ -26,10 +28,10 @@ public class CloudController {
return "feignCloud1" + id;
}
@Operation(summary = "调用cloud2中的接口")
@GetMapping("feignAccept1")
public String feignAccept1() {
return cloudFeignClient1.feignAccept1();
@Operation(summary = "使用OpenFeign调用其它第三方API")
@GetMapping("posts")
public Object posts() {
return callOtherApiFeignClient.posts("sss");
}
@Operation(summary = "调用服务2中接口", description = "feignByCloud2")

View File

@ -0,0 +1,12 @@
package cn.bunny.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "third-party-service", url = "http://jsonplaceholder.typicode.com")
public interface CallOtherApiFeignClient {
@GetMapping("/posts")
Object posts(@RequestParam("skey") String skey);
}

View File

@ -1,11 +1,9 @@
package cn.bunny.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
@FeignClient(value = "service-cloud2", path = "/api")
public interface CloudFeignClient {

View File

@ -1,10 +0,0 @@
package cn.bunny.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "service-cloud2", path = "/api")
public interface CloudFeignClient1 {
@GetMapping("feignAccept1")
String feignAccept1();
}

View File

@ -1,13 +0,0 @@
package cn.bunny.feign.fallback;
import cn.bunny.feign.CloudFeignClient1;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CloudFeignClientFallBack implements CloudFeignClient1 {
@Override
public String feignAccept1() {
return "出错了的回调";
}
}

View File

@ -0,0 +1,13 @@
package cn.bunny.feign.fallback;
import cn.bunny.feign.CloudFeignClient;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CloudFeignFallBackClientFallBack implements CloudFeignClient {
@Override
public String getFeignCloud2(Long id) {
return "getFeignCloud2 出错了的回调";
}
}

View File

@ -1,6 +1,5 @@
server:
port: 8000
spring:
application:
name: service-cloud1
@ -26,3 +25,5 @@ spring:
# 压缩响应请求
response:
enabled: true
# main:
# allow-bean-definition-overriding: true

View File

@ -1,38 +0,0 @@
package cn.bunny;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

View File

@ -22,12 +22,6 @@ public class CloudController {
return "feign" + id;
}
@Operation(summary = "接受其他接口调用1", description = "接受其他接口调用1")
@GetMapping("feignAccept1")
public String feignAccept1() {
return "feignAccept1---成功";
}
@Operation(summary = "调用服务1中接口", description = "feignByCloud1")
@GetMapping("feignByCloud1")
public String feignByCloud1(Long id) {

View File

@ -1,38 +0,0 @@
package cn.bunny;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}