xxl-job整合到consul,openfeign实现接口调用
1.下载xxl-job
2.修改xxl-job执行器application配置文件,添加如下内容:
spring.application.name=meeting-job spring.cloud.consul.host=127.0.0.1 spring.cloud.consul.port=8500 spring.cloud.consul.discovery.health-check-interval=8s spring.cloud.consul.discovery.prefer-ip-address=true spring.cloud.consul.discovery.ip-address=${base.url} spring.cloud.consul.discovery.health-check-path=${server.servlet.context-path}/actuator/health spring.main.allow-bean-definition-overriding=true
3.修改xxl-job执行器pom配置文件,添加如下内容:
<!-- consul注册中心 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.2.6.RELEASE</version> </dependency> <!-- ribbon --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency>
4.启动类XxlJobExecutorApplication添加配置,如下:
@EnableFeignClients @EnableDiscoveryClient
5.添加FeignConfig类,如下:
@Configuration public class FeignConfig { /** *No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessage */ @Bean @ConditionalOnMissingBean public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) { return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList())); } }
6.创建feign调用接口类,如下所示:
@FeignClient(name = "meeting-web", path = "/meeting/v1/service/device", configuration = FeignHeadConfiguration.class) public interface DeviceService { @RequestMapping(value = "/insert", method = RequestMethod.POST) void insert(@RequestBody Device device); @RequestMapping(value = "/update", method = RequestMethod.POST) void update(@RequestBody Device device); @RequestMapping(value = "/getCount", method = RequestMethod.GET) int getCount(@RequestParam(value = "acsDevIndexCode") String acsDevIndexCode);
}
7.服务提供者,如下:
@RestController @RequestMapping(value = "/v1/service/device") @CrossOrigin public class DeviceApi { @Autowired private DeviceService deviceService; @RequestMapping(value = "/insert", method = RequestMethod.POST) public void insert(@RequestBody Device device) { deviceService.insert(device); } @RequestMapping(value = "/update", method = RequestMethod.POST) public void update(@RequestBody Device device) { deviceService.update(device); } @RequestMapping(value = "/getCount", method = RequestMethod.GET) public int getCount(@RequestParam(value = "acsDevIndexCode") String acsDevIndexCode) { return deviceService.getCount(acsDevIndexCode); }
}
8.执行器里面执行调用,如下图所示:
9. 启动执行器,大功告成。