微服务---Dubbo+Zookeeper

dubboAdmin客户端 --监控 && 启动 Zookeeper 客户端 --注册中心

生产者:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.gton</groupId>
    <artifactId>producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>producer</name>

    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--Dubbbo+Zookeeper 核心依赖 -->
        <!-- 从dubbo官网进入github,看下方的帮助文档,找到dubbo-springboot,找到依赖包 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!--zookeeper的包我们去maven仓库下载,zkclient; -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <!-- 引入zookeeper -->
        <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>


        <!--Spring Boot web核心依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

项目如图:
image

application.properties:

# 端口
server.port=8888

#当前应用名字
dubbo.application.name=producer-service
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#配置生效位置包或者注解@service 注意是 Dubbo 下的
dubbo.scan.base-packages=com.gton.service

生产者的服务

接口:
package com.gton.service;

/**
 * @program: ZooKeeper_Dubbo_Demo
 * ClassName: ProducerService
 * @description: RPC 服务生产者
 * @author: GuoTong
 * @create: 2021-05-20 23:22
 * @since JDK 1.8
 **/
public interface ProducerService {

    public String getProducerMsg();
}
实现类:
package com.gton.service.serviceimpl;

import com.gton.service.ProducerService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

/**
 * @program: ZooKeeper_Dubbo_Demo
 * ClassName: ProducerServiceImpl
 * @description:
 * @author: GuoTong
 * @create: 2021-05-20 23:23
 * @since JDK 1.8
 **/
@Component
@DubboService
public class ProducerServiceImpl implements ProducerService {

    @Override
    public String getProducerMsg() {
        return "来自生产者的问候!!!";
    }
}

生产者启动器:

package com.gton;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }

}

消费者
image

# 端口
server.port=8889
#当前应用名字
dubbo.application.name=consumer-service
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#RPC提供服务

远程RPC-dubbo调用

package com.gton.service;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @program: ZooKeeper_Dubbo_Demo
 * ClassName: ConsumerService
 * @description: 消费生产者的物品
 * @author: GuoTong
 * @create: 2021-05-20 23:25
 * @since JDK 1.8
 **/
@Component
public class ConsumerService {

    @DubboReference
    ProducerService producerService;

    public void getProdMsg() {
        String msgToRPC = producerService.getProducerMsg();
        System.out.println(msgToRPC);
    }

}

posted on 2021-06-17 17:37  白嫖老郭  阅读(55)  评论(0编辑  收藏  举报

导航