快速入门
在本节中,我们将通过一个简单示例来展示Spring Cloud Feign在服务客户端定义所带来的便利。下面等示例将继续使用之前我们实现等hello-service服务,这里我们会通过Spring Cloud Feign提供的声明式服务绑定功能来实现对该服务接口的调用。
▪️首先,创建一个Spring Boot基础工程,取名为kyle-service-feign,并在pom.xml中引入spring-cloud-starter-eureka和spring-cloud-starter-feign依赖,具体内容如下所示。
父工程build.gradle 也是注册中心
plugins { id 'org.springframework.boot' version '2.5.6' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { maven { url 'https://maven.aliyun.com/repository/public' } mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' // implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' // implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix' // implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' compileOnly 'org.projectlombok:lombok' runtimeOnly 'mysql:mysql-connector-java' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } ext { set('springCloudVersion', "2020.0.4") } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } test { useJUnitPlatform() } //针对所有project的配置,包含根项目,除此之外还有subprojects 和 project,感兴趣的童鞋可以自行百度查阅 allprojects { //创建项目时的你 //项目是java项目 apply plugin: 'java' //项目是idea项目 apply plugin: 'idea' //jdk版本 sourceCompatibility = 17 } // 子项目配置 subprojects { apply plugin: 'java' //项目是idea项目 apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' //错误写法 ,不能携带版本号 // apply plugin: 'org.springframework.boot:2.5.6' // apply plugin: 'io.spring.dependency-management:1.0.11.RELEASE' repositories { //国内阿里云仓库配置 maven { url 'https://maven.aliyun.com/repository/public' } mavenCentral() } sourceCompatibility = '17' }
启动类
package com.example.feigndemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //@EnableEurekaClient public class FeignEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(FeignEurekaServerApplication.class, args); } }
生产者
plugins { // id 'org.springframework.boot' version '2.5.6' // id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { maven{url 'https://maven.aliyun.com/repository/public'} mavenCentral() } ext { set('springCloudVersion', "2020.0.4") } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' // implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' // implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' compileOnly 'org.projectlombok:lombok' runtimeOnly 'mysql:mysql-connector-java' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } test { useJUnitPlatform() }
package com.example.feigndemo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @ResponseBody @RequestMapping("test") public String test(){ return "Hello World!"; } }
spring: application: name: feign-producer server: port: 8763 eureka: client: #??????????Eureka Server????true? registerWithEureka: true #?????Eureka Server??????????true? fetchRegistry: true #server: #waitTimeInMsWhenSyncEmpty: 0 #???Eureka Server?????????????????????? serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/
消费者
plugins { // id 'org.springframework.boot' version '2.5.6' // id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { maven{url 'https://maven.aliyun.com/repository/public'} mavenCentral() } ext { set('springCloudVersion', "2020.0.4") } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' // implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' compileOnly 'org.projectlombok:lombok' runtimeOnly 'mysql:mysql-connector-java' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } test { useJUnitPlatform() }
package com.example.feigndemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
package com.example.feigndemo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class Test1Controller { @Resource FeignProducerClient feignProducerClient; @Resource FeignDatawayClient feignDatawayClient; @RequestMapping("/") public String hello() { return feignProducerClient.test(); } @RequestMapping("/test2") public String test2() { return feignProducerClient.test(); } @RequestMapping("/test3") public String test3() { return feignDatawayClient.test(); } }
package com.example.feigndemo; import org.springframework.web.bind.annotation.RequestMapping; @org.springframework.cloud.openfeign.FeignClient(name = "feign-producer") public interface FeignProducerClient { @RequestMapping("test") public String test(); }
源码地址
https://gitee.com/stylesmile/spring-boot-study/tree/master/gradle-cloud-project/cloud-feign-demo