springcloud服务之间的调用-feign

需求:订单里调用用户服务,在订单里查询出用户信息

order-service和user-service

1.user-service正常提供controller接口

 /**根据用户id查询用户信息
  * @param
  * @return
  */
 @RequestMapping(value = "/{id}",method= RequestMethod.GET)
 @ApiOperation(value = "根据用户id查询用户信息", notes = "根据用户id查询用户信息")
 public ResponseData getUserInfo(@PathVariable("id") Long id,@RequestParam("userType") int userType) {
     return ResponseDataUtil.buildSuccess("200","查询成功",userService.getUserInfoById(id,userType));
 }

2.user-service里的application,启用feign客户端

 

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@ComponentScan("com.****")
public class TmsUserApplication {
   public static void main(String[] args) {
      SpringApplication.run(TmsUserApplication.class, args);
   }

}

3.order-service里写一个api接口,和user-service里的controller方法定义一样

/**
 * 用户信息
 * @description
 * @author guo
 * @date 2019-10-31
 */
@FeignClient(value = "user-service",configuration = FeignConfiguration.class)//此处configuration为了服务直接传递header,user-service是用户服务名称
public interface UserService {

    /**
     * 查询用户信息
     * @param
     * @return
     */
    @RequestMapping(value = "/api/1/user/users/{id}",method= RequestMethod.GET)
    public Map<String,Object> getUserInfoById(@PathVariable("id") Long id, @RequestParam("userType") int userType);
}

4.order-service里的业务方法,直接引用

 

@Autowired
private UserService userService;

 userService.getUserInfoById(1,1);

上一篇:5.mysql面向对象及ORM


下一篇:ORM之SQLAlchemy