浅入浅出feign使用方法

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

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,还需加深理解及应用。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值