Recently, we used http interface calls in our project. We need to use spring cloud feign to record them.
First, to install consul registry locally, you need to register related services to consul. See my other article for details.
Relevant dependencies:
<!-- SpringCloud-Consul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- SpringCloud-OpenFeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
Service Provider Code:
These two annotations need to be added to the startup class:
@EnableDiscoveryClient @EnableFeignClients
Provide a common controller:
@RestController @RequestMapping("/command") public class CommandController { /** * Journal */ private static final Logger LOGGER = LoggerFactory.getLogger(CommandController.class); @Autowired private ICommandService commandService; @Autowired WebSocketServer webSocketServer; /** * <Transfer Instruction > * * @param checkCommand checkCommand * @return Result * @throws */ @RequestMapping(value = "/transferCommand", method = RequestMethod.POST) public ResultEntity<Boolean> transferCommand(@RequestBody CheckCommand checkCommand) { try { LOGGER.info( "[ConsumeCommandTask] id is {}, type is {}, input is {}, output is {}, start is {}, end is {}, time is {}.", checkCommand.getId(), checkCommand.getType(), checkCommand.getInput(), checkCommand.getOutput(), checkCommand.getStart(), checkCommand.getEnd(), checkCommand.getTimes()); webSocketServer.sendToAll(CommandSerializer.serialize(checkCommand)); // return commandService.transferCommand(checkCommand); } catch (Exception e) { LOGGER.error("Transfer instruction failed for:", e); return ResultEntity.fail("400", "Data abnormity"); } return ResultEntity.success(true); } }
Configuration file:
# The service name when spring configuration registers spring cloud is used in the following service Name spring.application.name=cmdserver # Configuration of consul cluster spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 # Service Discovery Configuration # Registration Service Open spring.cloud.consul.discovery.register=true # Service name spring.cloud.consul.discovery.serviceName=${spring.application.name} # Open Service Health Examination spring.cloud.consul.discovery.healthCheckPath=/actuator/health # Check interval spring.cloud.consul.discovery.healthCheckInterval=15s spring.cloud.consul.discovery.tags=urlprefix-/${spring.application.name} # Service Unique Identification spring.cloud.consul.discovery.instanceId=${spring.application.name}:aa356dsfe21312fafd00asdf
Service caller code:
@Headers({"Content-Type: application/json", "Accept: application/json"}) @FeignClient(name = "cmdserver", url = "${spring.cloud.consul.discovery.ip-address}") public interface VideoDao { @RequestMapping(value = "/command/transferCommand", method = RequestMethod.POST) public ResultEntity<Boolean> transferCommand(CheckCommand checkCommand); }
Call the above code at the service layer.
At the same time, annotations should be added to the startup class:
@EnableFeignClients