服务间的Feign调用

 

a服务   调用    b服务    

首先两个服务必须在eureka中先注册一下 (feign调用必须是两个服务端有eureka作为桥梁)

然后 在两个服务中都要加入 openfeign  feign调用依赖

 

      <!--服务间调用-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

 

b服务  的controller 层

  

@RestController
@RequestMapping("/hobby")
@CrossOrigin //用于前后端分离
public class HobbyController {

    @Autowired
    private IHobbyService hobbyService;

    //查询列表
    @GetMapping
    public List<Hobby> list(){
        return hobbyService.list();
    }

    //批量修改爱好人数
    @PutMapping("hobbyNum")
    public int updHobbyNum(@RequestParam("shlist") List<Integer> shlist,@RequestParam("num") Integer num){
        return hobbyService.updHobbyNum(shlist,num);
    }


}

 

在a服务的启动类中加入  @EnableFeignClients  注解

在a 服务  中建立feign调用接口

 

 

@FeignClient(name = "hobby")
@RequestMapping("/hobby")
public interface FeignHobbyService {

    //查询列表
    @GetMapping
    public List<Hobby> list();

    //批量修改爱好人数
    @PutMapping("/hobbyNum")
    public int updHobbyNum(@RequestParam("shlist") List<Integer> shlist, @RequestParam("num") Integer num);

}

 

 

在a服务的controller中

@RestController
@RequestMapping("/stu")
@CrossOrigin
public class StuController {

    @Autowired
    private FeignHobbyService feignHobbyService;
    @GetMapping("/hobbylist")
    public List<Hobby> hlist(){
    return feignHobbyService.list();
   }

}

 

 

  

 

 

 




 

posted on 2022-02-16 21:02  菊_酒  阅读(261)  评论(0)    收藏  举报