继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

分布式定时任务xxl-job(四)

台农一号
关注TA
已关注
手记 5
粉丝 6
获赞 13

本节主要内容:

    1、nacos安装与介绍

    2、xxl-job-executor服务注册到nacos

    3、创建remote-module服务并将其注册到nacos

    4、xxl-job-executor服务通过openfeign远程调用remote-module服务


流程图

    http://img2.sycdn.imooc.com/62bacb28000180fd18120340.jpg

    

一、nacos安装与介绍

    1.1 nacos既是一个注册中心也是一个配置中心,相当于eureka+Apollo的组合(eureka-注册中心,Apollo-配置中心)

    1.2 nacos官网:https://nacos.io/zh-cn/index.html

    1.3 安装(本次例子讲解时在windows环境下,linux安装请查看官网文档)

        1.3.1 文档地址 https://nacos.io/zh-cn/docs/quick-start.htmlrelease notes 选择快速开始->nacos->版本选择->release notes-> 选择nacos-server-2.0.4版本下载到本地(稳定版)->解压zip包

http://img1.sycdn.imooc.com/62bab55f0001b0aa25041455.jpg

http://img1.sycdn.imooc.com/62bab5b40001135325301471.jpg

http://img4.sycdn.imooc.com/62bab6580001c82c12880973.jpg

        1.3.2 初始化数据库表结构

            nacos目录->conf目录->nacos-mysql.sql->创建表结构

        http://img4.sycdn.imooc.com/62bab8d20001e4a825361539.jpg

        1.3.3 修改数据库连接配置

            打开nacos目录->conf目录->application.properties文件

            http://img4.sycdn.imooc.com/62bab9700001c89025331518.jpg

        1.3.4 启动nacos

             startup.cmd -m standalone

http://img4.sycdn.imooc.com/62bab6580001d68419021032.jpg

        1.3.5 通过浏览器访问nacos

                地址:http://localhost:8848/nacos/#/login

http://img1.sycdn.imooc.com/62babe4c00010c8325091466.jpghttp://img1.sycdn.imooc.com/62babe4b0001f3d425151467.jpg

二、xxl-job-executor服务注册到nacos

    2.1 添加依赖

        在外层的pom.xml添加依赖管理

<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-dependencies</artifactId>
         <version>2021.0.3</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
      <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-alibaba-dependencies</artifactId>
         <version>2021.0.1.0</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>${spring-boot.version}</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>

        http://img3.sycdn.imooc.com/62baba200001b05325261543.jpg

            在executor模块加入nacos依赖

<!--nacos-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <!-- 排除ribbon的依赖 -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

http://img1.sycdn.imooc.com/62baba960001887f25001543.jpg

    2.2 添加nacos配置文件

spring.main.allow-bean-definition-overriding=true
spring.application.name=xxl-job-executor-provider
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.group=job-group
spring.cloud.nacos.discovery.namespace=dev

http://img4.sycdn.imooc.com/62babb180001a8f825131530.jpg

        在启动类增加nacos服务注册与发现注解@EnableDiscoveryClient

http://img4.sycdn.imooc.com/62babc66000135c525211534.jpg

        启动服务,出现nacos registry日志打印信息,说明服务已经注册到nacos了

http://img1.sycdn.imooc.com/62babd1600018fe325181528.jpg

        查看nacos服务注册列表

http://img1.sycdn.imooc.com/62bac488000104b825201483.jpg



三、创建remote-module服务并将其注册到nacos

    3.1 创建模块

        在根目录xxl-job右键->new->Module->选择maven 下一步->填写模块名称->完成即可。

http://img4.sycdn.imooc.com/62bac0b90001bb5725131536.jpg

http://img1.sycdn.imooc.com/62bac0b90001925124991535.jpg

http://img4.sycdn.imooc.com/62bac0ba00017e0025101538.jpg

    3.2 添加依赖

        在新建的模块pom.xml文件加入以下依赖配置

<dependencies>
    <!-- starter-web:spring-webmvc + autoconfigure + logback + yaml + tomcat -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <!-- 排除ribbon的依赖 -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

    3.3 创建目录文件和启动类

        3.3.1 创建包目录

            com.remote.module

        3.2.2 在包内创建启动类 

package com.remote.module;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.remote.module.feign")
public class RemoteApplication {

    public static void main(String[] args) {
        SpringApplication.run(RemoteApplication.class);
    }
}

           3.3.2 创建controller测试类

                创建controller包,然后创建测试类TestController,内容如下:

package com.remote.module.controller;

import com.remote.module.dto.TestDto;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/v1/user/")
public class TestController {


    @PostMapping(value = "/test")
    public String insert(@RequestBody TestDto testDto){
        System.out.println("111111111111 "+ testDto.getName());
        return "";
    }
}

            3.3.2 创建一个dto实体类          

package com.remote.module.dto;

import java.io.Serializable;

/**
 *
 */
public class TestDto implements Serializable {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

            3.3.3 创建Fegin接口和熔断器

// feign接口类
package com.remote.module.feign;

import com.remote.module.dto.TestDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Component
@FeignClient(name = "visdom-remote-provider", fallbackFactory = FeignServerImpl.class )
public interface FeignServer {

    @RequestMapping(value = "/v1/user/test", method = RequestMethod.POST)
    String test(TestDto body);

}
// 熔断器类
package com.remote.module.feign;

import com.remote.module.dto.TestDto;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class FeignServerImpl implements FallbackFactory<FeignServer> {

    @Override
    public FeignServer create(Throwable cause) {
        return new FeignServer() {
            @Override
            public String test(TestDto body) {
                System.out.println("fail--------------");
                return null;
            }
        };
    }

}

     3.4 添加nacos配置

        在resource目录下创建bootstrap.yml文件

server:
  port: 8088

spring:
  application:
    name: visdom-remote-provider
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      #      config:
      #        server-addr: localhost:8848
      #        group: job-group
      #        namespace: dev
      discovery:
        server-addr: localhost:8848
        group: job-group
        namespace: dev

http://img3.sycdn.imooc.com/62bac2d80001466e25061537.jpg

    3.4 启动remote-module服务

http://img1.sycdn.imooc.com/62bac36600016bf125211531.jpg

    3.5 查看nacos控制面板

http://img1.sycdn.imooc.com/62bac3ef0001afdb24901438.jpg


四、xxl-job-executor服务通过openfeign远程调用remote-module服务

    4.1 在job-executor引入remote-module依赖

<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>remote-module</artifactId>
    <version>2.4.0-SNAPSHOT</version>
</dependency>

http://img4.sycdn.imooc.com/62bac55d0001d10d24881526.jpg

    4.2 远程调用remote-module接口代码

          4.2.1 在启动类增加feign扫描包

          4.2.2 编写远程调用代码

http://img1.sycdn.imooc.com/62bac6bc0001edd225001539.jpg

http://img2.sycdn.imooc.com/62bac6bc0001666325111537.jpg


    4.5 测试代码

            4.5.1 启动xxl-job-admin服务

            4.5.2 启动xxl-job-executor服务

            4.5.3 启动remote-module服务

            4.5.4 登陆xxl-job控制面板,启动任务

http://img1.sycdn.imooc.com/62bac9600001aae825101396.jpg

            4.5.5 查看xxl-job-executor任务调度的代码日志

http://img4.sycdn.imooc.com/62bac99f0001de2425151541.jpg

            4.5.6 查看你remote-module模块远程调用的日志打印

http://img4.sycdn.imooc.com/62bac9c7000146f825141523.jpg



预告下一篇文章内容:

1、修改xxl-job-admin源代码,对外提供接口服务;

2、remote-module服务远程调用xxl-job-admin api实现动态生成任务;


打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

一步一步走来都完成了,教程很详细谢谢博主,不过我刚才回头发现,我们集成了nacos之后,xxl-job里面的机器地址怎么填写啊,能填写服务在注册中心的名字吗?


查看全部评论