方案一:使用Feign进行消费(推荐)
1、在maven中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.2</version>
</dependency>
2、启动类上加上@EnableFeignClients
@EnableHystrix
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {“com.aaa.aurora”})
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = “com.aaa.aurora”)
@ImportResource(locations= {“classpath:spring.xml”,”spring-security.xml”})
@MapperScan(“com.aaa.aurora.mapper”)
public class AuroraWebApplication {
public static void main(String[] args) {
SpringApplication.run(AuroraWebApplication.class, args);
}
}
3、编写service接口
@FeignClient(url = “${pangu.url}”,name = “panguUrl”)
public interface PanGuService {
@RequestMapping(value = “/pangu/restful/check”,method = RequestMethod.POST)
JSONObject check(@RequestParam(name=”queryEngine”) String queryEngine, @RequestParam(name=”querySql”) String querySql, @RequestParam(name=”jobNo”) String jobNo);
}
其中:pangu.url是配置在application.properties中的ip及端口
pangu.url = 192.168.1.3:8080
/pangu/restful/check是要调的接口名
4、代码中调用
@Autowired
private PanGuService panGuService;
JSONObject jsonObject = null;
try {
jsonObject = panGuService.auroraPriviledge(PRESTO_DRIVER, query.get(“sql”), user.getWorkNo());
} catch (Exception e) {
throw new Exception(“请求系统异常”);
}
if (PANGU_FAIL.equals(jsonObject.get(“code”))) {
LOG.info(jsonObject.get(“msg”).toString());
throw new BusinessException(jsonObject.get(“msg”).toString());
}