Dubbo usage steps

Dubbo usage steps

SpringBook approach

Step 1: Introducing dependencies

Add the following dependencies to the pom.xml file in the service provider and consumer Engineering

<!-- Dubbo Spring Boot Starter -->
<!-- Introduce Dubbo Core library and introduction SpringBoot Automatic Configuration Dependency -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.3</version>
</dependency>

<!-- Introduce SpringBoot rely on -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- Introduce Zookeeper The service registry depends on (optionally, depending on what registry is used, if so). Redis´╝îReplace it with Jedis Dependency) -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>${dubbo.version}</version>
    <type>pom</type>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Usually, service interfaces provided by service providers are also extracted into a single service interface. maven Modules are added to service providers and consumers'projects in the form of dependencies to facilitate maintenance -->

Step 2: Configure Dubbo-related parameters

service provider

Service providers need to configure:

  • apply name
  • Service Registry Address
  • Protocol, Port, Host for Service Exposure (optional)

application.yml

dubbo:
  application:
    name: demo-provider
    qos-enable: false #The monitoring service for operation and maintenance is open by default, and the port is 222222. In order to avoid port conflicts, it is recommended to turn off the development environment directly.

  registry:
    address: zookeeper://10.10.14.120:2181?client=curator
  protocol:
    name: dubbo
    port: 20883
    host: 10.10.14.236

Service consumer

Consumers need to allocate:

  • apply name
  • Service Registry Address

application.yml

dubbo:
  application:
    name: demo-consumer
    qos-enable: false

  registry:
    address: zookeeper://10.10.14.120:2181?client=curator

Step 3: Write code

Define service interfaces
public interface IHello {
	String sayHello();
	String sayHello(String name);
}
Implementing service providers
import com.lanou3g.dubbo.api.IHello;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

@Service(version = "1.0.0")  // Exposing services using the @Service annotation under apache package
public class HelloServiceImpl implements IHello {

	@Value("${dubbo.protocol.host}")
	private String host;
	@Value("${dubbo.protocol.port}")
	private int port;

	public String sayHello() {
		String msg = "Current service port: " + port;
		msg += ", host: " + host;
		msg += " ----> Hello Dubbo";
		System.out.println(msg);
		return msg;
	}

	public String sayHello(String name) {
		String msg = "Current service port: " + port;
		msg += ", host: " + host;
		msg += " ----> "+ name;
		System.out.println(msg);
		return msg;
	}
}

Startup class:

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

import java.io.IOException;

@EnableDubbo(scanBasePackages = "com.lanou3g.dubbo.service.impl")
@SpringBootApplication
public class ProviderApplication {

	public static void main(String[] args) throws IOException {
		SpringApplication.run(ProviderApplication.class, args);
		System.out.println("Provider Start up");
	}
}
Implementing Service Consumers
import com.lanou3g.dubbo.api.IHello;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.io.IOException;

@SpringBootApplication
public class ConsumerApplication {

	@Reference(version = "1.0.0")  // Use the @Reference annotation under the apache package to reference remote services, and then use it as if it were calling local code
	private IHello helloService;

	@PostConstruct
	public void testInvokeService() {
		String msg = helloService.sayHello();
		System.out.println(msg);
	}

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

}

Spring mode

Step 1: Introducing dependencies

Service providers and service consumers depend on:

<dependency>
    <groupId>com.lanou3g</groupId>
    <artifactId>demo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
</dependency>
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
</dependency>

<!-- Redis Registry dependency -->
<!--<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>-->
<!-- Zookeeper Registry dependency -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
</dependency>

Step 2: Configure Dubbo-related beans

Service Provider

<?xml version="1.0" encoding="UTF-8"?>
<!-- join dubbo schema -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- To configure dubbo Applying Relevant Parameters -->
    <dubbo:application name="demo-provider">
       <dubbo:parameter key="qos.enable" value="false" />
        <!-- <dubbo:parameter key="qos.enable" value="true" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
        <dubbo:parameter key="qos.port" value="22222" />-->
    </dubbo:application>

    <!-- Use multicast Radio Registry Exposure Service Address -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
    <!-- Use redis Registry Exposure Service Address -->
    <!--<dubbo:registry address="redis://127.0.0.1:6379" />-->
    <!-- Use zookeeper Registry Exposure Service Address -->
    <!--<dubbo:registry address="zookeeper://10.10.14.120:2181" />-->
    <!--<dubbo:registry protocol="zookeeper" address="10.10.14.120:2181,10.10.14.149:2182,10.10.14.163:2183" client="curator" />-->
    <dubbo:registry protocol="zookeeper" address="10.10.14.120:2181,10.10.14.120:2182,10.10.14.120:2183" client="curator" />

    <!-- use dubbo Protocol Exposure Service at Port 20880 -->
    <dubbo:protocol name="dubbo" port="20880" host="10.10.14.236" />

    <!-- Declare service interfaces that need to be exposed -->
    <dubbo:service interface="com.lanou3g.dubbo.api.IHello" ref="helloService" version="1.0.0" />

    <!-- And local bean Implementing services as well -->
    <bean id="helloService" class="com.lanou3g.dubbo.service.impl.HelloServiceImpl" />
</beans>

Service consumers

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer">
       <dubbo:parameter key="qos.enable" value="false" />
        <!-- <dubbo:parameter key="qos.enable" value="true" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
        <dubbo:parameter key="qos.port" value="33333" />-->
    </dubbo:application>

    <!-- Use multicast Radio Registry Exposure Discovery Service Address -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />-->
    <!-- Use redis Registry Discovery Service Address -->
    <!--<dubbo:registry address="redis://127.0.0.1:6379" />-->
    <!-- Use zookeeper Registry Exposure Service Address -->
    <!--<dubbo:registry address="zookeeper://10.10.14.120:2181" />-->
    <dubbo:registry protocol="zookeeper" address="10.10.14.120:2181,10.10.14.120:2182,10.10.14.120:2183" client="curator" />
    <!--<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183" client="curator" />-->

    <!-- Generate remote service proxies that can be local and local bean Same use helloService -->
    <!-- Reference to remote services through the service registry -->
    <!-- id Attribute cannot be omitted, name can be and provider id It's not the same, but it can't be saved. -->
    <dubbo:reference id="helloService" interface="com.lanou3g.dubbo.api.IHello" version="1.0.0"/>
    <!-- In this case, the registry can not be configured (commonly used in development test environments). -->
    <!--<dubbo:reference id="helloService" interface="com.lanou3g.dubbo.api.IHello" version="1.0.0" url="dubbo://10.10.14.236:20880" />-->

</beans>

Step 3: Write code

Define service interfaces

Refer to the interface IHello defined in the SpringBook way above.

Implementing service providers

Exposed Service Interface Implementation Classes

public class HelloServiceImpl implements IHello {
	public String sayHello() {
		System.out.println("Hello Dubbo");
		return "Hello Dubbo";
	}

	public String sayHello(String name) {
		String msg = "Hello, " + name;
		System.out.println(msg);
		return msg;
	}
}

Service Provider Application Startup Class

public class ProviderLauncher {

	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		ctx.start();

		System.out.println("Provider Start up");

		// Prevent the program from exiting until an arbitrary character is entered.
		System.in.read();

	}
}

Implementing Service Consumers

import com.lanou3g.dubbo.api.IHello;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ConsumerLauncher {

	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		ctx.start();

		// Refer to remote Service as if using a local bean
		IHello helloService = ctx.getBean(IHello.class);
		String msg = helloService.sayHello();
		System.out.println(msg);


		String msg1 = helloService.sayHello("World!");
		System.out.println(msg1);

		System.in.read();
	}

}

Tags: Dubbo Apache Zookeeper Spring

Posted on Wed, 02 Oct 2019 00:05:31 -0400 by phpfreakjav