Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Feb 11, 2020
1 parent
44903f5
commit f830ba3
Showing
11 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?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>labx-01</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>labx-03-sc-feign-demo03-consumer</artifactId> | ||
|
||
<properties> | ||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version> | ||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version> | ||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version> | ||
</properties> | ||
|
||
<!-- | ||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。 | ||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系 | ||
--> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>${spring.cloud.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-alibaba-dependencies</artifactId> | ||
<version>${spring.cloud.alibaba.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入 DemoProvider API 包 --> | ||
<dependency> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<artifactId>labx-03-sc-feign-demo03-provider-api</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
</dependency> | ||
|
||
<!-- 引入 Spring Cloud OpenFeign 相关依赖,使用 OpenFeign 提供声明式调用,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-openfeign</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
41 changes: 41 additions & 0 deletions
41
...c/main/java/cn/iocoder/springcloud/labx03/feigndemo/consumer/DemoConsumerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cn.iocoder.springcloud.labx03.feigndemo.consumer; | ||
|
||
import cn.iocoder.springcloud.labx03.feigndemo.consumer.feign.DemoProviderFeignClient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@SpringBootApplication | ||
public class DemoConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoConsumerApplication.class, args); | ||
} | ||
|
||
@Configuration | ||
@EnableFeignClients | ||
public class OpenFeignConfiguration { | ||
|
||
} | ||
|
||
@RestController | ||
static class TestController { | ||
|
||
@Autowired | ||
private DemoProviderFeignClient demoProviderFeignClient; | ||
|
||
@GetMapping("/hello02") | ||
public String hello02(String name) { | ||
// 使用 Feign 调用接口 | ||
String response = demoProviderFeignClient.echo(name); | ||
// 返回结果 | ||
return "consumer:" + response; | ||
} | ||
|
||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...03-consumer/src/main/java/cn/iocoder/springcloud/labx03/feigndemo/consumer/FeignDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* 用于展示纯 Feign 的示例 | ||
*/ | ||
package cn.iocoder.springcloud.labx03.feigndemo.consumer; | ||
|
||
import feign.Feign; | ||
import feign.Param; | ||
import feign.RequestLine; | ||
|
||
// 商品 API | ||
interface ProductAPI { | ||
|
||
// 获得商品详情 | ||
@RequestLine("POST /products/{id}") | ||
String get(@Param("id") Integer id); | ||
|
||
} | ||
|
||
public class FeignDemo { | ||
|
||
public static void main(String[] args) { | ||
// 创建 ProductAPI 对象 | ||
ProductAPI productAPI = Feign.builder().target(ProductAPI.class, | ||
"http://www.iocoder.cn"); // 目标地址 | ||
|
||
// 调用获得商品 | ||
String product = productAPI.get(1); | ||
System.out.println(product); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
.../java/cn/iocoder/springcloud/labx03/feigndemo/consumer/feign/DemoProviderFeignClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package cn.iocoder.springcloud.labx03.feigndemo.consumer.feign; | ||
|
||
import cn.iocoder.springcloud.labx03.feigndemo.provider.api.TestService; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
|
||
@FeignClient(name = "demo-provider") | ||
public interface DemoProviderFeignClient extends TestService { | ||
|
||
// @GetMapping("/echo") | ||
// String echo(@RequestParam("name") String name); | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
labx-03/labx-03-sc-feign-demo03-consumer/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
spring: | ||
application: | ||
name: demo-consumer # Spring 应用名 | ||
cloud: | ||
nacos: | ||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类 | ||
discovery: | ||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址 | ||
|
||
server: | ||
port: 28080 # 服务器端口。默认为 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?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>labx-01</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>labx-03-sc-feign-demo03-provider-api</artifactId> | ||
|
||
<dependencies> | ||
<!-- 引入 SpringMVC 依赖 --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>5.2.3.RELEASE</version> | ||
<scope>provided</scope> <!-- scope 为 provided 提供级别即可,真正版本的依赖,交给服务提供者和消费者 --> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
11 changes: 11 additions & 0 deletions
11
...r-api/src/main/java/cn/iocoder/springcloud/labx03/feigndemo/provider/api/TestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cn.iocoder.springcloud.labx03.feigndemo.provider.api; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
public interface TestService { | ||
|
||
@GetMapping("/echo") | ||
String echo(@RequestParam("name") String name); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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>labx-01</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>labx-03-sc-feign-demo03-provider</artifactId> | ||
|
||
<properties> | ||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version> | ||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version> | ||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version> | ||
</properties> | ||
|
||
<!-- | ||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。 | ||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系 | ||
--> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>${spring.cloud.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-alibaba-dependencies</artifactId> | ||
<version>${spring.cloud.alibaba.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入 DemoProvider API 包 --> | ||
<dependency> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<artifactId>labx-03-sc-feign-demo03-provider-api</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
53 changes: 53 additions & 0 deletions
53
...c/main/java/cn/iocoder/springcloud/labx03/feigndemo/provider/DemoProviderApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cn.iocoder.springcloud.labx03.feigndemo.provider; | ||
|
||
import cn.iocoder.springcloud.labx03.feigndemo.provider.api.TestService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@SpringBootApplication | ||
public class DemoProviderApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoProviderApplication.class, args); | ||
} | ||
|
||
@RestController | ||
static class TestController implements TestService { | ||
|
||
private Logger logger = LoggerFactory.getLogger(TestController.class); | ||
|
||
@Value("${server.port}") | ||
private Integer serverPort; | ||
|
||
public String echo(String name) { | ||
// 模拟执行 100ms 时长。方便后续我们测试请求超时 | ||
try { | ||
Thread.sleep(100L); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
// 记录被调用的日志 | ||
logger.info("[echo][被调用啦 name({})]", name); | ||
|
||
return serverPort + "-provider:" + name; | ||
} | ||
|
||
// @GetMapping("/echo") | ||
// public String echo(String name) throws InterruptedException { | ||
// // 模拟执行 100ms 时长。方便后续我们测试请求超时 | ||
// Thread.sleep(100L); | ||
// | ||
// // 记录被调用的日志 | ||
// logger.info("[echo][被调用啦 name({})]", name); | ||
// | ||
// return serverPort + "-provider:" + name; | ||
// } | ||
|
||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
labx-03/labx-03-sc-feign-demo03-provider/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
spring: | ||
application: | ||
name: demo-provider # Spring 应用名 | ||
cloud: | ||
nacos: | ||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类 | ||
discovery: | ||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址 | ||
|
||
server: | ||
port: ${random.int[10000,19999]} # 服务器端口。默认为 8080 | ||
# port: 18080 # 服务器端口。默认为 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters