1、feign调用流程,消费端调用feign,feign查询注册中心服务提供者,进行负载调用
2、创建一个服务端,一个消费端,一个feign-api
3、启动之前的eureka服务
4、新建模块feign-inherit-api,统一管理feign接口
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
5、新建一个接口类,添加feign注解,指定需要查找的服务
FeignClient注解表明这是一个负载feign,value就是需要查询的服务提供端在eureka中的注册名
@FeignClient(value = "feign-inherit-provide")
public interface UserRemoteClient {
@GetMapping("/user/name")
public String getName();
}
6、新建消费者模块,并pom文件引用feign-api
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>feign-inherit-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
7、新建启动类,因为feign接口不在这个模块中,所以需要通过basePackages指定feign路径
@SpringBootApplication
@EnableFeignClients(basePackages = {"com.wujun"})
@EnableDiscoveryClient
public class ConsumeApp {
public static void main(String[] args) {
SpringApplication.run(ConsumeApp.class,args);
}
}
8、新建配置文件,指定eureka地址等
server.port=8088
eureka.client.service-url.defaultZone = http://wujun:123456@127.0.0.1:8761/eureka/
9、新建controller,调用feign接口
@RestController
public class DemoController {
@Resource
UserRemoteClient userRemoteClient;
@GetMapping("/call")
public String callHello() {
String name = userRemoteClient.getName();
return "调用结果" + name;
}
}
10、启动项目
11、新建消费者模块feign-inherit-provide,并引用feign-api
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>feign-inherit-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
12、新建配置文件,配置eureka注册地址等
spring.application.name=feign-inherit-provide
server.port=8087
eureka.client.service-url.defaultZone = http://wujun:123456@127.0.0.1:8761/eureka/
13、新建controller实现feign接口
@RestController
public class DemoController implements UserRemoteClient {
public String getName() {
return "wujun8787";
}
}
14、新建启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}