Dubbo使用步骤

Dubbo使用步骤

SpringBoot方式

第一步: 引入依赖

在服务提供者、消费者工程中的pom.xml文件中添加如下依赖

<!-- Dubbo Spring Boot Starter -->
<!-- 引入Dubbo核心库,并引入SpringBoot自动配置依赖 -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.3</version>
</dependency>

<!-- 引入SpringBoot依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- 引入Zookeeper服务注册中心依赖(可选,取决于用什么注册中心,如果是Redis,就换成Jedis依赖) -->
<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>

<!-- 通常还会将服务提供者所提供的服务接口抽成一个单独的maven模块, 通过依赖的形式加到服务提供方和消费方工程中,方便维护 -->

第二步:配置Dubbo相关参数

服务提供方

服务提供方需要配置:

  • 应用名称
  • 服务注册中心地址
  • 服务暴露的协议、端口、Host(可选)

application.yml

dubbo:
  application:
    name: demo-provider
    qos-enable: false #用于运维的监控服务, 默认是开启状态,并且端口是22222。开发环境为了避免端口冲突,建议直接关掉

  registry:
    address: zookeeper://10.10.14.120:2181?client=curator
  protocol:
    name: dubbo
    port: 20883
    host: 10.10.14.236
服务消费方

消费方需要配置:

  • 应用名称
  • 服务注册中心地址

application.yml

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

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

第三步:写代码

定义服务接口
public interface IHello {
	String sayHello();
	String sayHello(String name);
}
实现服务提供者
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")  // 使用apache包下的@Service注解来对外暴露服务
public class HelloServiceImpl implements IHello {

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

	public String sayHello() {
		String msg = "当前服务端口: " + port;
		msg += ", host: " + host;
		msg += " ----> Hello Dubbo";
		System.out.println(msg);
		return msg;
	}

	public String sayHello(String name) {
		String msg = "当前服务端口: " + port;
		msg += ", host: " + host;
		msg += " ----> "+ name;
		System.out.println(msg);
		return msg;
	}
}

启动类:

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启动完成");
	}
}
实现服务消费者
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")  // 使用apache包下的@Reference注解来引用远程服务,然后就可以像调用本地代码一样来使用
	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方式使用

第一步:引入依赖

服务提供者、服务消费者依赖:

<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注册中心依赖 -->
<!--<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>-->
<!-- Zookeeper注册中心依赖 -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
</dependency>

第二步:配置Dubbo相关的Bean

服务提供者
<?xml version="1.0" encoding="UTF-8"?>
<!-- 加入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">

    <!-- 配置dubbo应用相关参数 -->
    <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>

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
    <!-- 使用redis注册中心暴露服务地址 -->
    <!--<dubbo:registry address="redis://127.0.0.1:6379" />-->
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!--<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" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" host="10.10.14.236" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.lanou3g.dubbo.api.IHello" ref="helloService" version="1.0.0" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="helloService" class="com.lanou3g.dubbo.service.impl.HelloServiceImpl" />
</beans>
服务消费者
<?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>

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />-->
    <!-- 使用redis注册中心发现服务地址 -->
    <!--<dubbo:registry address="redis://127.0.0.1:6379" />-->
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!--<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" />-->

    <!-- 生成远程服务代理,可以和本地bean一样使用helloService -->
    <!-- 通过服务注册中心引用远程服务 -->
    <!-- id属性不能省略, 名称可以和提供方id不一样,但是不能省 -->
    <dubbo:reference id="helloService" interface="com.lanou3g.dubbo.api.IHello" version="1.0.0"/>
    <!-- 采用服务直连的方式,实现服务消费者直接调用远程指定地址的服务,这种情况下,可以不配置注册中心(一般用在开发测试环境) -->
    <!--<dubbo:reference id="helloService" interface="com.lanou3g.dubbo.api.IHello" version="1.0.0" url="dubbo://10.10.14.236:20880" />-->

</beans>

第三步:写代码

定义服务接口

参考上面SpringBoot方式定义的接口IHello

实现服务提供者

对外暴露的服务接口实现类

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;
	}
}

服务提供方应用启动类

public class ProviderLauncher {

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

		System.out.println("Provider启动完成");

		// 阻止程序退出, 直到输入一个任意字符后才退出
		System.in.read();

	}
}
实现服务消费者
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();

		// 像使用本地bean一样引用远程的Service
		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();
	}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值