在使用微服務架構中,可能遇到一些業務情況會涉及服務之間相互調用,下面通過一個簡單的demo給大家演示下,演示的是oms服務需要調用ump服務。
代碼如下:
1、oms服務提供者
主要是這個註解:
@EnableFeignClients("com.omsserver.*")
完整代碼:
package com.omsserver.service;
//import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* @Description: java類作用描述
* @Author: zhoujh
* @CreateDate: 2019/5/10$ 11:22 AM$
* @Version: 1.0
*/
@Service
@FeignClient("ump-service")//服務生產者名稱
@RequestMapping("/api/umpinfo")//服務路由
public interface UmpInfoService {
@RequestMapping("/umpDetails")
Map<String,Object> umpDetails(String omsId ) ;
}
2、ump服務提供者(被調用)
完整代碼:
package com.umpserver.umpserver.controller;
import com.umpserver.umpserver.service.UmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @Description: java類作用描述
* @Author: zhoujh
* @CreateDate: 2019/5/10$ 11:19 AM$
* @Version: 1.0
*/
@RestController
@RequestMapping("/api/umpinfo")//服務路由
public class UmpController {
@Autowired
UmpService umpService;
@RequestMapping("/umpDetails")
@ResponseBody
public Map<String,Object> selectUmpInfo(@RequestBody String omsId){
Map<String,Object> map = new HashMap<>();
map = umpService.findUmpInfo(omsId);
return map;
}
}