feat(openFeign): 调用第三方接口和微服务调用
This commit is contained in:
parent
1a18153ed7
commit
f4fc71e738
|
@ -1,10 +1,11 @@
|
||||||
package cn.bunny.controller;
|
package cn.bunny.controller;
|
||||||
|
|
||||||
|
import cn.bunny.feign.CallOtherApiFeignClient;
|
||||||
import cn.bunny.feign.CloudFeignClient;
|
import cn.bunny.feign.CloudFeignClient;
|
||||||
import cn.bunny.feign.CloudFeignClient1;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
@ -14,11 +15,12 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
public class CloudController {
|
public class CloudController {
|
||||||
|
|
||||||
|
@Qualifier("cn.bunny.feign.CloudFeignClient")
|
||||||
@Autowired
|
@Autowired
|
||||||
private CloudFeignClient cloudFeignClient;
|
private CloudFeignClient cloudFeignClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CloudFeignClient1 cloudFeignClient1;
|
private CallOtherApiFeignClient callOtherApiFeignClient;
|
||||||
|
|
||||||
@Operation(summary = "本体接口调用", description = "feignCloud2")
|
@Operation(summary = "本体接口调用", description = "feignCloud2")
|
||||||
@GetMapping("feignCloud1")
|
@GetMapping("feignCloud1")
|
||||||
|
@ -26,10 +28,10 @@ public class CloudController {
|
||||||
return "feignCloud1:" + id;
|
return "feignCloud1:" + id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "调用cloud2中的接口")
|
@Operation(summary = "使用OpenFeign调用其它第三方API")
|
||||||
@GetMapping("feignAccept1")
|
@GetMapping("posts")
|
||||||
public String feignAccept1() {
|
public Object posts() {
|
||||||
return cloudFeignClient1.feignAccept1();
|
return callOtherApiFeignClient.posts("sss");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "调用服务2中接口", description = "feignByCloud2")
|
@Operation(summary = "调用服务2中接口", description = "feignByCloud2")
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -1,11 +1,9 @@
|
||||||
package cn.bunny.feign;
|
package cn.bunny.feign;
|
||||||
|
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
@Component
|
|
||||||
@FeignClient(value = "service-cloud2", path = "/api")
|
@FeignClient(value = "service-cloud2", path = "/api")
|
||||||
public interface CloudFeignClient {
|
public interface CloudFeignClient {
|
||||||
|
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
|
@ -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 "出错了的回调";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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 出错了的回调";
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
server:
|
server:
|
||||||
port: 8000
|
port: 8000
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
application:
|
application:
|
||||||
name: service-cloud1
|
name: service-cloud1
|
||||||
|
@ -25,4 +24,6 @@ spring:
|
||||||
min-request-size: 4096 # 压缩最小值 为4096 = 4KB
|
min-request-size: 4096 # 压缩最小值 为4096 = 4KB
|
||||||
# 压缩响应请求
|
# 压缩响应请求
|
||||||
response:
|
response:
|
||||||
enabled: true
|
enabled: true
|
||||||
|
# main:
|
||||||
|
# allow-bean-definition-overriding: true
|
|
@ -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 );
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -22,12 +22,6 @@ public class CloudController {
|
||||||
return "feign:" + id;
|
return "feign:" + id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "接受其他接口调用1", description = "接受其他接口调用1")
|
|
||||||
@GetMapping("feignAccept1")
|
|
||||||
public String feignAccept1() {
|
|
||||||
return "feignAccept1---成功";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Operation(summary = "调用服务1中接口", description = "feignByCloud1")
|
@Operation(summary = "调用服务1中接口", description = "feignByCloud1")
|
||||||
@GetMapping("feignByCloud1")
|
@GetMapping("feignByCloud1")
|
||||||
public String feignByCloud1(Long id) {
|
public String feignByCloud1(Long id) {
|
||||||
|
|
|
@ -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 );
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue