1. 程式人生 > >spring boot + dubbo 服務部署實例

spring boot + dubbo 服務部署實例

artifact group web bubuko config zookeep pac odin info

項目github:https://github.com/nalidou/spring-dubbo

1. 公共組件:dubbo-component

提供了接口定義、實體類等,其他項目可以直接導入jar包,也可以用maven導入。

<?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>com.wzy</groupId> <artifactId>dubbo-component</artifactId> <version>1.0</version> </project>

技術分享圖片

2. 服務生產者:dubbo-producer

實現了公共組件接口

<dependencies>
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0
.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4
.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>com.wzy</groupId> <artifactId>dubbo-component</artifactId> <version>1.0</version> </dependency> </dependencies>

技術分享圖片

package com.wzy.dubbo.producer.server;

import com.alibaba.dubbo.config.annotation.Service;
import com.wzy.component.entity.User;
import com.wzy.component.server.IUserServer;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Service(interfaceClass = IUserServer.class) //這裏用的是dubbo的接口
@Component
public class UserServerImpl implements IUserServer {
    @Override
    public User getById(long id) {
        return new User(id, "小明");
    }

    @Override
    public List<User> getAll() {
        List<User> list = new ArrayList<User>();
        list.add(new User(100, "jerry"));
        list.add(new User(200, "tom"));
        return list;
    }
}

3. web服務(服務消費者):dubbo-web

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

        <dependency>
            <groupId>com.wzy</groupId>
            <artifactId>dubbo-component</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

技術分享圖片

package com.wzy.dubbo.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wzy.component.entity.User;
import com.wzy.component.server.IOrderServer;
import com.wzy.component.server.IUserServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WebCtl {
    @Reference
    private IOrderServer orderServer;
    @Reference
    private IUserServer userServer;

    @RequestMapping("getUser")
    public User getUser() {
        System.out.println("WebCtl getUser...");
        System.out.println(orderServer.getName());
        return userServer.getById(100);
    }
}

4. 在zookeeper上查看註冊節點

技術分享圖片

spring boot + dubbo 服務部署實例