温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Dubbo怎么用

发布时间:2021-11-17 10:39:08 来源:亿速云 阅读:168 作者:小新 栏目:大数据

这篇文章主要介绍了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>

服务提供方配置

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
  ###########采用协议和端口号################
  protocol:
    ###########采用dubbo协议####################
    name: dubbo
    ###########发布dubbo端口号为40591###########
    port: 40591
  registry:
    ###########注册中心地址#####################
    address: zookeeper://192.168.0.18:2181
  scan:
    ############实现类扫包范围###################
    base-packages: com.example.order.service

spring:
  output:
    ansi:
      enabled: always

服务消费者

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:
    ##### 注册服务的名称
    name: user
  ##### 注册中心地址
  registry:
    address: zookeeper://192.168.0.18:2181
  ##### 调用服务超时时间
  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 {

}

感谢你能够认真阅读完这篇文章,希望小编分享的“Dubbo怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI