SpringBoot 2.1.x整合Dubbo集羣及Monitor監控中心

資源相關

Provider服務提供者

  • pomxml
 <dependencies>

        <!--WEB 依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Dubbo相關 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <!-- ZK -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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服務
    在這裏插入圖片描述

Consumer服務消費者

  • demo結構圖
    在這裏插入圖片描述
  • pom.xml
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Dubbo相關 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <!-- ZK -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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提供者包名一致】
    在這裏插入圖片描述
  • 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消費者
    在這裏插入圖片描述
  • 測試遠程調用
    在這裏插入圖片描述

Monitor服務監控配置

  • 訪問地址 http://192.168.44.110:80801/
    在這裏插入圖片描述
  • 查看服務調用信息
    在這裏插入圖片描述
  • 查看服務調用圖表信息
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章