一.先配置Feign接口java
1.1 在core包中導入須要的jarweb
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
1.2 在包中編寫接口類spring
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信息app
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.net
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.在啓動類上面配置相應的註解code
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); } }