feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似提供调用的service层接口。
spring cloud 是spring推出的大型项目,spring cloud包含了很多独立的项目,如
spring-cloud-netflix、spring-cloud-config、spring-cloud-security、spring-cloud-zookeeper、spring-cloud-gateway等
spring cloud netflix包括eureka服务注册与发现、hystrix熔断器、ribbon客户端负载均衡、feign声明式服务调用组件、zuul网关。
feign的使用:
- 服务A的启动类加上@EnableFeignClients;
- 服务B的启动类加上@EnableFeignClients
- 服务B想调用服务A的服务接口时
需要在服务B中的service层添加一个interface,并且添加@FeignClient(name = “服务A”)和@Service,里面定义方法加上@GetMapping("/user/getUser"),其中url是服务A提供的接口地址;然后在服务B的service层的其他类中调用在B中添加的interface接口的方法调用服务A的接口获取数据。
上代码示例
eureka的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
eureka的启动类
@SpringBootApplication
@EnableEurekaServer
public class SpringEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringEurekaApplication.class, args);
}
}
eureka的applicaton.properties
server.port=19999
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
服务1代码示例
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
//@EnableFeignClients
public class SpringbootFeignServer1Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootFeignServer1Application.class, args);
}
}
server.port=8081
spring.application.name=spring-eureka-feign-server1
eureka.client.serviceUrl.defaultZone=http://localhost:19999/eureka/
eureka.instance.preferIpAddress=true
@RestController
@RequestMapping("/rest1")
public class DemoController {
@RequestMapping("/hello1")
public Object hello1() {
return "rest1-------------hello1";
}
}
服务2代码示例:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringbootFeignServer2Application {
public static void main(String[] args) {
SpringApplication.run(SpringbootFeignServer2Application.class, args);
}
}
@Service
@FeignClient(name = "spring-eureka-feign-server1")
public interface Api {
@RequestMapping(value = "/rest1/hello1")
public String getHello1();
}
@Autowired
private Api api;
public Object hello4() {
String result = api.getHello1();
return "通过api调用服务1的接口获取数据:" + result;
}
server.port=8082
spring.application.name=spring-eureka-feign-server2
eureka.client.serviceUrl.defaultZone=http://localhost:19999/eureka/
eureka.instance.preferIpAddress=true
以上已测试ok,还需加深理解及应用。

本文介绍Feign作为声明式的WebService客户端如何简化微服务间的调用,包括其在Spring Cloud项目中的作用,以及如何在服务间使用Feign进行接口调用的详细步骤。同时,提供了eureka服务注册与发现的配置示例。
3761

被折叠的 条评论
为什么被折叠?



