溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Dubbo怎么用

發(fā)布時間:2021-11-17 10:39:08 來源:億速云 閱讀:160 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要介紹了Dubbo怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

依賴:

<?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 http://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.1.7.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
        <!--接口定義-->
		<dependency>
			<groupId>com.example</groupId>
			<artifactId>api</artifactId>
			<version>1.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.7.3</version>
		</dependency>

        <!--zookeeper 依賴-->
		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>4.2.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>4.2.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		
	</dependencies>


</project>

服務(wù)提供方配置

package com.example.order.service;

import com.example.api.Order;
import com.example.api.OrderService;
import org.apache.dubbo.config.annotation.Service;
import java.util.Date;


@Service
public class OrderServiceImpl implements OrderService {

    @Override
    public Order getOrder() {
        Order order = new Order();
        order.setId(1L);
        order.setOrderName("xxx");
        order.setCreateTime(new Date());
        return order;
    }

}

 application.yml

server:
  port: 4059
  tomcat:
    uri-encoding: UTF-8
  servlet:
    context-path: /order
#    session:
#      timeout: 300s

dubbo:
  application:
    ###########注冊到注冊中心的名稱############
    name: ordder
  ###########采用協(xié)議和端口號################
  protocol:
    ###########采用dubbo協(xié)議####################
    name: dubbo
    ###########發(fā)布dubbo端口號為40591###########
    port: 40591
  registry:
    ###########注冊中心地址#####################
    address: zookeeper://192.168.0.18:2181
  scan:
    ############實現(xiàn)類掃包范圍###################
    base-packages: com.example.order.service

spring:
  output:
    ansi:
      enabled: always

服務(wù)消費(fèi)者

package com.example.user.controller;

import com.example.api.OrderService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

    @Reference
    private OrderService orderService;


    @RequestMapping("/order")
    public Object getUser() {
        return this.orderService.getOrder();
    }

}

application.yml

server:
  port: 4058
  tomcat:
    uri-encoding: UTF-8
  servlet:
    context-path: /user
#    session:
#      timeout: 300s

dubbo:
  application:
    ##### 注冊服務(wù)的名稱
    name: user
  ##### 注冊中心地址
  registry:
    address: zookeeper://192.168.0.18:2181
  ##### 調(diào)用服務(wù)超時時間
  consumer:
    timeout: 5000

spring:
  output:
    ansi:
      enabled: always

注意項目啟用要使用  @EnableDubbo 
接口注冊要使用: Service
接口引用要使用:Reference

@EnableDubbo
org.apache.dubbo.config.annotation.Service
org.apache.dubbo.config.annotation.Reference
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.impl")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static public class ProviderConfiguration {

}
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.simple.annotation.action")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(value = {"org.apache.dubbo.samples.simple.annotation.action"})
static public class ConsumerConfiguration {

}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Dubbo怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI