SpringBoot uses the basic instance of Dubbo and Zookeeper (super simple!!!)

brief introduction

Dubbo is a high-performance and excellent open source service framework of Alibaba company, which enables applications to realize service output and input functions through high-performance RPC, and can be seamlessly integrated with Spring framework. You can go there for more details https://blog.csdn.net/qq_43222167/article/details/107048655

Download and installation of zookeeper: https://blog.csdn.net/qq_43222167/article/details/106096290

Create project


Here, we create a separate project for the service interface, that is, the service layer, which is often called Dubbo service

Dubbo provider: service provider
Dubbo consumer: service consumer

There is only one simple interface in Dubbo service

package com.jbit.service;

public interface HelloDubboService {
    public String print();
}

Then there is the POM of the total project XML file!!! Pay special attention to the jar package dependency here. If necessary, you can see: https://blog.csdn.net/qq_43222167/article/details/107048857

Parent dependency

 <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.4.RELEASE</version>
</parent>

dependencies

<dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
		<!--project dubbo-service Dependency of-->
        <dependency>
            <groupId>com.jbit.dubbo</groupId>
            <artifactId>dubbo-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--lead into dubbo Dependency of-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- lead into zookeeper Dependency of -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j</artifactId>
                    <groupId>log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

The dependency of Dubbo service is changed according to its own

Next is Dubbo provider

dubbo-provider

Implementation of HelloDubboService HelloDubboServiceImpl

package com.jbit.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.jbit.service.HelloDubboService;

@Service(version = "1.0.0",interfaceClass = HelloDubboService.class)
public class HelloDubboServiceImpl implements HelloDubboService {

    public String print() {
        return "-------hello Dubbo-------";
    }
}

Especially note that the @Service annotation is under dubbo

Startup class

package com.jbit;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class,args);
    }
}

application.yml

server:
  port: 8081

dubbo:
  application:
    name: dubbo-provider
  registry:
    protocol: zookeeper
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20880
  scan: com.jbit.service.impl.HelloDubboServiceImpl

Pay attention to the alignment of spaces and the port number of your own dubbo, that is

dubbo-consumer

The configuration is roughly the same. Because the service provider has exposed the interface, we don't need to write any more, just write the corresponding control layer call
DubboController
Note here that the @Reference annotation is also dubbo's

package com.jbit.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jbit.service.HelloDubboService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@RequestMapping("dubbo")
public class DubboController {

    @Reference(version = "1.0.0")
    private HelloDubboService helloDubboService;

    @RequestMapping("print")
    public String print() {
     	System.out.println(helloDubboService);
        return helloDubboService.print();
    }
}

Start class DubboConsumerApplication

package com.jbit;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class,args);
    }
}

application.yml

server:
  port: 8082

dubbo:
  application:
    name: dubbo-consumer
  registry:
    protocol: zookeeper
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20881

test

Start zookeeper first, and then start the service provider, that is, Dubbo provider. After loading, start Dubbo consumer to access http://localhost:8082/dubbo/print (if you have changed, call your corresponding)

You can see that it has been successful!!!

General process

The general process here is that after the service provider starts, it registers its own service in zookeeper, and then consumers can use whatever service they need to use when starting

Tags: Java Spring Boot Zookeeper rpc Dubbo

Posted by Dan400007 on Wed, 01 Jun 2022 20:07:48 +0530