spring-cloud中的feign入门,声明式调用
spring-cloud中的feign是为了简化消费者去调用服务提供者的方法,通俗来说就是简化开发,那么怎么简化开发了呢?入门案例跑一跑就知道!
注:
(服务消费者) consumer
(服务提供者)provider
此处的注册中心是用的nacos
1.导入feign的坐标,在consumer的pom文件中导入哈,因为是consumer调用服务provider中的资源,所以要在consumer中导入feign的坐标
<dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--feign的坐标-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--nacos的坐标-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
2.在resouce包下
创建application.yml文件输入
server:
port: 9000
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # 配置nacos 服务端地址
application:
name: nacos-consumer # 服务名称
3.创建一个consumer的引导类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient // 激活DiscoveryClient
@SpringBootApplication
@EnableFeignClients //开启feign功能
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}
4.创建feign类这里起名GoodsFeign类
import com.itheima.consumer.domain.Goods;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
//此处注解的value是指的是服务提供者在nacos注册的名字
@FeignClient(value="nacos-provider")
public interface GoodsFeign {
@GetMapping("goods/findOne/{id}") //此处的路径就是服务提供者的访问路径
public Goods findOne(@PathVariable("id") int id);
}
5.下面开始写controller层代码
//实现思路
//因为这里要调用服务提供者,通过服务提供者去调用方法获取数据
//注入feign对象,通过feign对象去根据服务提供者的名字在nacos中去获取服务提供者的资源
import com.itheima.consumer.domain.Goods;
import com.itheima.consumer.feign.GoodsFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private GoodsFeign goodsFeign;
@GetMapping("/goods/{id}")
public Goods findGoodsById(@PathVariable("id") int id){
return goodsFeign.findOne(id);
}
}
6.服务提供方的controller层
import com.itheima.provider.domain.Goods;
import com.itheima.provider.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Goods Controller 服务提供方
*/
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@GetMapping("/findOne/{id}")
public Goods findOne(@PathVariable("id") int id){
Goods goods = goodsService.findOne(id);//通过id查询goods对象
return goods;
}
}
这样就ok啦,拿走不谢,有不足的地方可以说出来哈,初来博客,还望指点