Spring Cloud Alibaba 初体验(三) Nacos 与 Dubbo 集成
一、新建项目
新建项目,只放置接口,用于暴露 Dubbo 服务接口
public interface GreetingService {
    String greeting();
}
二、provider
本文以上文中的 Service1 作为 provider,以 Service2 作为 consumer
2.1 添加依赖
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
2.2 实现接口
@Service
@Component
public class GreetingServiceImpl implements GreetingService {
    @Value("${server.port:0}")
    private Integer port;
    @Override
    public String greeting() {
        return "hello from port: " + port;
    }
}
@Service 为 org.apache.dubbo.config.annotation.Service (旧的版本为 com.alibaba.dubbo.config.annotation.Service),此处的 @Service 只能供 Dubbo RPC 调用,如果需要像之前一样正常在 Controller 层调用需要再添加 @Component 注解 (或者直接不添加 @Component 在项目内也通过 Dubbo 调用)
2.3 新增配置
dubbo:
  scan:
    base-packages: com.karonda.service1.service.impl
  protocol:
    name: dubbo
    port: -1 # -1 表示端口自增
  registry:
    address: nacos://192.168.92.1:8848
三、consumer
3.1 调用服务
    @Reference
    private GreetingService greetingService;
    @RequestMapping("/dubboGreeting")
    public String dubboGreeting() {
        return greetingService.greeting();
    }
@Reference 为 org.apache.dubbo.config.annotation.Reference (旧的版本为 com.alibaba.dubbo.config.annotation.Reference)
3.2 新增配置
dubbo:
  protocol:
    name: dubbo
    port: -1
  registry:
    address: nacos://192.168.92.1:8848
  consumer:
    check: false # 不加此配置项目启动时可能会失败
启动服务,访问 http://localhost:8079/dubboGreeting 可以看到结果
Spring Cloud Alibaba 初体验(三) Nacos 与 Dubbo 集成的更多相关文章
- Spring Cloud Alibaba 初体验(一) Nacos 配置中心
		一.Nacos 下载与初始化配置 本文使用1.2.0,下载地址:https://github.com/alibaba/nacos/releases Nacos 单机模式支持持久化配置到 MySQL 数 ... 
- Spring Cloud Alibaba 初体验(二) Nacos 服务注册与发现 + 集成 Spring Cloud Gateway
		一.服务注册 添加依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ... 
- Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用
		一.下载与运行 本文使用 Seata 1.1.0:https://github.com/seata/seata/releases Windows 环境下双击 bin/seata-server.bat ... 
- Spring Cloud Alibaba 初体验(四) Sentinel
		一.Sentinel 下载与运行 本文使用 Sentinel 1.7.1:https://github.com/alibaba/Sentinel/releases 使用自定义端口 8089 运行 Se ... 
- Spring Cloud Alibaba 初体验(五) SkyWalking
		一.下载与运行 本文使用 SkyWalking 7.0.0:https://www.apache.org/dyn/closer.cgi/skywalking/7.0.0/apache-skywalki ... 
- Spring Cloud Alibaba基础教程:Nacos配置的多环境管理
		前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ... 
- Spring Cloud Alibaba基础教程:Nacos的集群部署
		继续说说生产环境的Nacos搭建,通过上一篇<Spring Cloud Alibaba基础教程:Nacos的数据持久化>的介绍,我们已经知道Nacos对配置信息的存储原理,在集群搭建的时候 ... 
- Spring Cloud Alibaba基础教程:Nacos的数据持久化
		前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ... 
- Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置
		前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ... 
随机推荐
- 深入探索Spring Data JPA, 从Repository 到  Specifications 和 Querydsl
			数据访问层,所谓的CRUD是后端程序员的必修课程,Spring Data JPA 可以让我们来简化CRUD过程,本文由简入深,从JPA的基本用法,到各种高级用法. Repository Spring ... 
- C++ 基础 2:C++ 对 C 语言的拓展
			1 引用 1.1 定义及编程实践 引用,是某个已存在变量的另一个名字. 一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量. 注意: 引用没有定义,是一种关系型声明.声明它和原有某一 ... 
- Spider--实战--bs静态网页爬取TOP250电影
			import requests from bs4 import BeautifulSoup def gettop250(): headers={ 'user-agent':'Mozilla/5.0 ( ... 
- Spider_基础总结6--动态网页抓取--selenium
			# 有些网站使用 '检查元素'也不能够好使,它们会对地址进行加密,此时使用Selenium 调用浏览器渲染引擎可以模拟用户的操作,完成抓取: # 注:selenium既可以抓取静态网页也可以抓取动态网 ... 
- vue 切换主题(换肤)功能
			一:先写好两个css样式放在static文件夹中 二:在index.html中添加css link链接 <link rel="stylesheet" id="sty ... 
- python之路《八》装饰器
			装饰器是个好东西啊 那么装饰器是个什么样的东西呢,他又能做些什么呢? 1.为什么装饰器 当我们一个程序已经构建完成,并且已经发布出去了,但是现在需要增加一个活动,例如淘宝给你发送一个今日优惠,或者开启 ... 
- 如何测量Ceph OSD内存占用
			前言 这个工具我第一次看到是在填坑群里面看到,是由研发-北京-蓝星同学分享的,看到比较有趣,就写一篇相关的记录下用法 火焰图里面也可以定位内存方面的问题,那个是通过一段时间的统计,以一个汇总的方式来查 ... 
- less和more的区别
			more: 顾名思义显示更多less: 由于more不能后退,就取more的反义词less加上后退功能所以Linux里流传着这样一句话:"less is more". 总结下mor ... 
- 写的太细了!Spring MVC拦截器的应用,建议收藏再看!
			Spring MVC拦截器 拦截器是Spring MVC中强大的控件,它可以在进入处理器之前做一些操作,或者在处理器完成后进行操作,甚至是在渲染视图后进行操作. 拦截器概述 对于任何优秀的MVC框架, ... 
- 看看吧!月薪20K以上的程序员才能全部掌握RabbitMq知识,你掌握了多少
			一.RabbitMq基础知识 0.概述 消息队列的作用就是接收消息生产者的消息,然后将消息发送到消费者 1.信道channel 我的理解是生产者/消费者和rabbitmq交互的一个通道,负责交换机.队 ... 
