服务调用
需要进行模块之间的服务调用时,使用Feign技术封装服务发现、负载均衡等细节,实现本地化形式的调用。
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>配置
开发者需要在入口类上配置@EnableFeignClients启用Feign的支持。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}远程接口定义
在服务调用端,需要根据服务提供者的接口定义声明远程接口。其中name属性对应服务提供者在注册中心的serviceId,path属性对应@RequestMapping配置。
对应的服务端代码:
远程服务调用
在业务逻辑层中,只需注入远程接口的实例,和本地方法一样进行调用即可。
参考文档
关于OpenFeign技术的更多内容请参考官方文档:https://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_openfeign.html
Last updated