Merge_Test_1_dubbo+zookeeper

hayeka發表於2020-03-08
名稱 介紹
dubbo-admin 監控管理後臺
dubbo jar包
zookeeper 註冊中心

步驟
前提:zookeeper服務已經開啟

  1. 提供者提供服務
    1.匯入依賴
    2.配置註冊中心的地址,以及服務發現名,和要掃描的包
    3.在想要註冊的服務上面,增加一個註解Service
  2. 消費者如何消費
    1.匯入依賴
    2.配置註冊中心的地址,培植自己的服務名
    3.從遠端注入服務~@Reference

1.匯入依賴

        <!--匯入依賴-->
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.5</version>
        </dependency>
        <!--zkclient-->
        <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
            <!--排除這個slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2.服務程式碼

TicketService介面

package com.kuang.service;

/**
 * ————————————————————————————————————————————————
 * CREATED BY hayeka ON 2020/3/8 19:11
 * ————————————————————————————————————————————————
 */
public interface TicketService {
    public String getTicket();
}

TicketService介面的實現

package com.kuang.service;

import com.kuang.service.TicketService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

/**
 * ————————————————————————————————————————————————
 * CREATED BY hayeka ON 2020/3/8 19:11
 * ————————————————————————————————————————————————
 */
@Service    //可以被掃描到,在專案一啟動就自動註冊到中心
@Component  //使用了dubbo儘量不要用service註解
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "狂神說java";
    }
}

3.應用配置

服務提供者的application.properties

server.port=8002
#服務應用名字
dubbo.application.name=provider-server
#註冊中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#哪些服務要被註冊
dubbo.scan.base-packages=com.kuang.service

1.增加依賴

        <!--匯入依賴-->
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.5</version>
        </dependency>
        <!--zkclient-->
        <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
            <!--排除這個slf4j-log4j12-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2.消費程式碼

與服務提供者相同的TicketService

package com.kuang.service;

/**
 * ————————————————————————————————————————————————
 * CREATED BY hayeka ON 2020/3/8 19:11
 * ————————————————————————————————————————————————
 */
public interface TicketService {
    public String getTicket();
}

服務消費方的程式碼

package com.kuang.service;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * ————————————————————————————————————————————————
 * CREATED BY hayeka ON 2020/3/8 19:16
 * ————————————————————————————————————————————————
 */
@Service
public class UserService {

    //想拿到provider提供的票
    @Reference
    //引用   pom座標,可以定義路徑相同的介面名
    TicketService ticketService;

    public void buyTicket()
    {
        String ticket = ticketService.getTicket();
        System.out.println("在註冊中心拿到"+ticket);
    }


}

3.應用配置

server.port=8001

#消費者去哪裡拿服務   需要暴露自己的名字
dubbo.application.name=consumer-server
#註冊中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
package com.kuang;

import com.kuang.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ConsumerServerApplicationTests {

    @Autowired
    UserService userService;

    @Test
    void contextLoads() {
        userService.buyTicket();
    }

}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

一個從事軟體開發職業的經濟愛好者