一.先配置Feign接口
1.1 在core包中导入需要的jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
1.2 在包中编写接口类
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
<!-- 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
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.在启动类上面配置相应的注解
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);
}
}