SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心

资源相关

  • Linux下搭建Dubbo集群及Monitor监控中心【推荐】
  • boot-dubbo示例源码链接

Provider服务提供者

  • pomxml
 <dependencies>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.bootgroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>0.2.0version>
        dependency>

        
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
            <version>2.12.0version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
  • application.yml
dubbo:
  application:
    #服务名称
    name: ticket-dubbo-provider
  protocol:
    #协议
    name: dubbo
    # 协议端口
    port: 20880
  registry:
    #集群配置
    address: zookeeper://192.168.44.110:2181?backup=192.168.44.111:2181,192.168.44.112:2181
    protocol: zookeeper
  scan:
    #Dubbo组件扫描路径
    base-packages: com.hf.boot.service.impl
  consumer:
    #检测服务是否可用
    check: false
  monitor:
    #Monitor监控配置
    address: 192.168.44.110:7070

#应用名
spring:
  application:
    name: boot-dubbo-provider
#端口
server:
  port: 8081
  • TicketService
package com.hf.boot.service;

/**
 * @:Copyright (C), 2016-2019 hf
 * @:FileName: TicketService
 * @:Author: hf
 * @:Date: 2019/10/7 16:50
 * @:Description: 卖票服务接口提供
 */
public interface TicketService {


    //获得一张票
    String getTicket();
}

  • TicketServiceImpl
package com.hf.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.hf.boot.service.TicketService;
import org.springframework.stereotype.Component;

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: TicketServiceImpl
 * @Author: hf
 * @Date: 2019/10/7 16:53
 * @Description: 卖票服务接口实现
 */
@Service(timeout = 5000, interfaceClass = TicketService.class) //发布服务
@Component
public class TicketServiceImpl implements TicketService {

    @Override
    public String getTicket() {
        return "《中国机长》";
    }
}
  • 主启动类标注@EnableDubbo注解
  • 启动Provider服务
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第1张图片

Consumer服务消费者

  • demo结构图
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第2张图片
  • pom.xml
 <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.bootgroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>0.2.0version>
        dependency>

        
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
            <version>2.12.0version>
        dependency>


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
  • application.yml
spring:
  application:
    name: boot-dubbo-consumer
server:
  port: 8085
dubbo:
  application:
    #服务名称
    name: user-ticket-consumer
  protocol:
    #协议
    name: dubbo
    # 协议端口
    port: 20880
  registry:
    #集群配置
    address: zookeeper://192.168.44.110:2181?backup=192.168.44.111:2181,192.168.44.112:2181
    protocol: zookeeper
  scan:
    #Dubbo组件扫描路径
    base-packages: com.hf.boot.service.impl
  consumer:
    #检测服务是否可用
    check: false
  monitor:
    #Monitor监控配置
    address: 192.168.44.110:7070
    #可以自动发现或者直连
    #protocol: registry
  • 将TicketService接口加入到consumer项目中【注意包名必须和Provider提供者包名一致】
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第3张图片
  • UserService
package com.hf.boot.service;

/**
 * @:Copyright (C), 2016-2019 hf
 * @:FileName: UserService
 * @:Author: hf
 * @:Date: 2019/10/7 17:50
 * @:Description: 用户消费接口
 */
public interface UserService {

     void ticketConsumer();
}

  • UserServiceImpl
package com.hf.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hf.boot.service.UserService;
import com.hf.boot.service.TicketService;
import org.springframework.stereotype.Service;

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: UserService
 * @Author: hf
 * @Date: 2019/10/7 16:55
 * @Description: 用户消费服务
 */
@Service
public class UserServiceImpl implements UserService {

    @Reference
    TicketService ticketService;

    @Override
    public void ticketConsumer() {
        String ticket = ticketService.getTicket();

        System.out.println("买到票了:" + ticket);
    }
}

  • UserController
package com.hf.boot.controller;

import com.hf.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: UserController
 * @Author: hf
 * @Date: 2019/10/7 18:02
 * @Description: 用户控制层
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/hello")
    public Object hello() {
        userService.ticketConsumer();
        return "Success";
    }
}

  • 主启动类标注@EnableDubbo注解
  • 启动Consumer消费者
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第4张图片
  • 测试远程调用
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第5张图片

Monitor服务监控配置

  • 访问地址 http://192.168.44.110:80801/
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第6张图片
  • 查看服务调用信息
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第7张图片
  • 查看服务调用图表信息
    SpringBoot 2.1.x整合Dubbo集群及Monitor监控中心_第8张图片

你可能感兴趣的:(SpringBoot,SpringBoot,Dubbo)