前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Feign的使用

Feign的使用

作者头像
用户5927264
发布2019-08-01 10:56:26
1K0
发布2019-08-01 10:56:26
举报
文章被收录于专栏:OSChinaOSChina

一.先配置Feign接口

1.1 在core包中导入需要的jar

代码语言:javascript
复制
<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>

1.2 在包中编写接口类

代码语言:javascript
复制
package com.shi.core.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.shi.core.model.Dept;

@FeignClient(value = "SPRINGCLOUD04-PRODECT-8001") //指定为哪个微服务提供接口
public interface DeptClientService
{
	@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
	public Dept get(@PathVariable("id") long id);

	@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
	public List<Dept> list();

	@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
	public boolean add(Dept dept);
}

二.在服务消费方配置Feign信息

2.1 导入需要的jar

代码语言:javascript
复制
        <!-- feign -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>       
        <!-- Ribbon相关 (ribbon需要和eureka配合使用) -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

2 编写Controller

代码语言:javascript
复制
package com.shi.customer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shi.core.model.Dept;
import com.shi.core.service.DeptClientService;

@Controller
public class DeptController {
	
	@Autowired
	private DeptClientService deptClientService;
	
	@GetMapping("/consumer/dept/add")
	@ResponseBody
	public boolean add(Dept dept) {
		return deptClientService.add(dept);
	}
	
	@GetMapping("/consumer/dept/get/{id}")
	@ResponseBody
	public Object get(@PathVariable("id") Integer id) {
		return deptClientService.get(id);
	}
	
	@GetMapping("/consumer/dept/list")
	@ResponseBody
	public Object list(){
		Object list = deptClientService.list();
		return list;
	}
	
}

3.在启动类上面配置相应的注解

代码语言:javascript
复制
package com.shi.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.shi.core.service"})//feign服务类的包名
public class Customer7001Feign {

	public static void main(String[] args) {
		SpringApplication.run(Customer7001Feign.class, args);
	}

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档