1.在公共引用项目debugggcloud_api项目加入引用Feign的支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
2.在消费者项目里面加入该引用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
3.在公共引用项目创建DeptClientService接口类
package com.debuggg.cloud.service;
import com.debuggg.cloud.entities.Dept;
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 java.util.List;
@FeignClient(value = "DEBUGGGCLOUD-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);
}
注意接口上面的@FeignClient(value="DEBUGGGCLOUD-DEPT"),此处需要该注解
4.将消费者项目里面的DeptConsumerController的代码的调用方式由RestTemplate的调用方式修改为DeptClientService调用方式调用
原来代码:
package com.debuggg.cloud.controller;
import com.debuggg.cloud.entities.Dept;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptConsumerController {
// private static final String PREFIX = "http://localhost:8001/";
private static final String PREFIX = "http://DEBUGGGCLOUD-DEPT";
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list(){
return restTemplate.getForObject(PREFIX + "/dept/list", List.class);
}
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return restTemplate.getForObject(PREFIX + "/dept/get/" + id, Dept.class);
}
@RequestMapping(value = "/consumer/dept/add")
public Boolean add(Dept dept){
return restTemplate.postForObject(PREFIX + "/dept/add" ,dept, Boolean.class);
}
//测试@EnableDiscoveryClient,消费端可以调用服务发现
@RequestMapping(value="/consumer/dept/discovery")
public Object discovery()
{
return restTemplate.getForObject(PREFIX+"/dept/discovery", Object.class);
}
}
修改后代码:
package com.debuggg.cloud.controller;
import com.debuggg.cloud.entities.Dept;
import com.debuggg.cloud.service.DeptClientService;
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 org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptConsumerController {
@Autowired
private DeptClientService service = null;
@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list(){
return this.service.list();
}
@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return this.service.get(id);
}
@RequestMapping(value = "/consumer/dept/add")
public Boolean add(Dept dept){
return this.service.add(dept);
}
}
5.在启动类加入注解@EnableFeignClients
package com.debuggg.cloud;
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;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.debuggg.cloud"})
public class DeptConsumer10001_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer10001_App.class,args);
}
}