1. 程式人生 > >SpringBoot整合Dubbo--consumer

SpringBoot整合Dubbo--consumer

目錄結構:

 

pom檔案:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>springboot</groupId>
    <artifactId>springboot-dubbo-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-dubbo 客戶端:: 整合 Dubbo/ZooKeeper 詳解 SOA 案例</name>

    <!-- Spring Boot 啟動父依賴 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
    </properties>

    <dependencies>

        <!-- Spring Boot Dubbo 依賴 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>${dubbo-spring-boot}</version>
        </dependency>

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

        <!-- Spring Boot Test 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

    </dependencies>
</project>

 

 

 

application.properties:

## 避免和 server 工程埠衝突
server.port=8081

## Dubbo 服務消費者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=org.spring.springboot.dubbo

 

City:
package org.spring.springboot.domain;

import java.io.Serializable;

/**
 * 城市實體類
 */
public class City implements Serializable {

    private static final long serialVersionUID = -1L;

    /**
     * 城市編號
     */
    private Long id;

    /**
     * 省份編號
     */
    private Long provinceId;

    /**
     * 城市名稱
     */
    private String cityName;

    /**
     * 描述
     */
    private String description;

    public City() {
    }

    public City(Long id, Long provinceId, String cityName, String description) {
        this.id = id;
        this.provinceId = provinceId;
        this.cityName = cityName;
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(Long provinceId) {
        this.provinceId = provinceId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

 

 

CityDubboService:
import org.spring.springboot.domain.City;

/**
 * 城市業務 Dubbo 服務層
 */
public interface CityDubboService {

    /**
     * 根據城市名稱,查詢城市資訊
     * @param cityName
     */
    City findCityByName(String cityName);
}

 

 

CityDubboConsumerService:
package org.spring.springboot.dubbo;

import com.alibaba.dubbo.config.annotation.Reference;
import org.spring.springboot.domain.City;
import org.springframework.stereotype.Component;

/**
 * 城市 Dubbo 服務消費者
 */
@Component
public class CityDubboConsumerService {

    @Reference(version = "1.0.0")
    CityDubboService cityDubboService;

    public City printCity() {
        String cityName="忠縣";
        City city = cityDubboService.findCityByName(cityName);
        System.out.println(city.toString());
        return city;
    }
}

 

 

 

CityController:
package org.spring.springboot.controller;

import org.spring.springboot.domain.City;
import org.spring.springboot.dubbo.CityDubboConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 81046 on 2018-08-31
 */
@RestController
public class CityController {

    @Autowired
    private CityDubboConsumerService cityDubboConsumerService;

    @GetMapping("/getCity")
    public String getCity(){
        City city = cityDubboConsumerService.printCity();
        return city.toString();
    }
}

 

 

ClientApplication啟動類:
package org.spring.springboot;

import org.spring.springboot.dubbo.CityDubboConsumerService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * Spring Boot 應用啟動類
 */
// Spring Boot 應用的標識
@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {
        // 程式啟動入口
        // 啟動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 元件
        ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args);
        CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class);
        System.out.println(cityService);
    }
}

 

 

最後訪問連結:http://localhost:8081/getCity

 

消費者成功呼叫服務方提供的服務。

 

觀察dubbo-admin中心的內容:

 

 

打卡,收工!!!