SpringCloud配置Feign案例

一、引入相关jar

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

二、在公共接口微服务模块创建公用接口并申明为@FeignClient

@FeignClient(value = "MICROSERVICECLOUD-DEPT")
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相关注解

@SpringBootApplication
@EnableEurekaClient//开启Eureka客户端
@EnableFeignClients(basePackages= {"com.zhq.springcloud"})
@ComponentScan("com.zhq.springcloud")
public class DeptConsumer80_Feign_App {

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

}

四、引用所编写的接口 

package com.zhq.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhq.springcloud.entities.Dept;
import com.zhq.springcloud.service.DeptClientService;

@RestController
public class DeptController_Consumer {

	@Autowired
	private DeptClientService service = null;

	@RequestMapping(value = "/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id) {
		return this.service.get(id);
	}

	@RequestMapping(value = "/consumer/dept/list")
	public List<Dept> list() {
		return this.service.list();
	}

	@RequestMapping(value = "/consumer/dept/add")
	public Object add(Dept dept) {
		return this.service.add(dept);
	}

}

猜你喜欢

转载自blog.csdn.net/xm393392625/article/details/88931352