一、新建项目

新建项目,只放置接口,用于暴露 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 集成的更多相关文章

  1. Spring Cloud Alibaba 初体验(一) Nacos 配置中心

    一.Nacos 下载与初始化配置 本文使用1.2.0,下载地址:https://github.com/alibaba/nacos/releases Nacos 单机模式支持持久化配置到 MySQL 数 ...

  2. Spring Cloud Alibaba 初体验(二) Nacos 服务注册与发现 + 集成 Spring Cloud Gateway

    一.服务注册 添加依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ...

  3. Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用

    一.下载与运行 本文使用 Seata 1.1.0:https://github.com/seata/seata/releases Windows 环境下双击 bin/seata-server.bat ...

  4. Spring Cloud Alibaba 初体验(四) Sentinel

    一.Sentinel 下载与运行 本文使用 Sentinel 1.7.1:https://github.com/alibaba/Sentinel/releases 使用自定义端口 8089 运行 Se ...

  5. Spring Cloud Alibaba 初体验(五) SkyWalking

    一.下载与运行 本文使用 SkyWalking 7.0.0:https://www.apache.org/dyn/closer.cgi/skywalking/7.0.0/apache-skywalki ...

  6. Spring Cloud Alibaba基础教程:Nacos配置的多环境管理

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  7. Spring Cloud Alibaba基础教程:Nacos的集群部署

    继续说说生产环境的Nacos搭建,通过上一篇<Spring Cloud Alibaba基础教程:Nacos的数据持久化>的介绍,我们已经知道Nacos对配置信息的存储原理,在集群搭建的时候 ...

  8. Spring Cloud Alibaba基础教程:Nacos的数据持久化

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

  9. Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置

    前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...

随机推荐

  1. 【SpringBoot】15. Spring Boot核心注解

    Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...

  2. leetcode117:search-rotated-sorted-array

    题目描述 给出一个转动过的有序数组,你事先不知道该数组转动了多少 (例如,0 1 2 4 5 6 7可能变为4 5 6 7 0 1 2). 在数组中搜索给出的目标值,如果能在数组中找到,返回它的索引, ...

  3. VBA_50段代码总结

    ''                                                          30个有用的VBA代码 '目录: '1--合理使用数组:'2--一次保存并关闭所 ...

  4. S5830 android 2.3.4和2.3.7

    12年元旦买的手机S5830,原机自带2.3.4的系统. 看到人家的机子2.3.6的效果稍微绚一点,动了想刷机的念头. 前两天刷了2.3.7,效果还满意,用的还舒服,感觉就是有些费电, 本来就对智能手 ...

  5. offsetWidth与offsetHeight

    HTMLElement.offsetWidth 是一个只读属性,返回一个元素的布局宽度.一个典型的(译者注:各浏览器的offsetWidth可能有所不同)offsetWidth是测量包含元素的边框(b ...

  6. 主动关闭 time-wait 2msl 处理

    先上传后面整理 /* * This routine is called by the ICMP module when it gets some * sort of error condition. ...

  7. tcp 接收被动关闭 fin

    void tcp_rcv_established(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th, unsigned int ...

  8. Java的注释-标识符和关键字

    1.Java注释 单行注释 多行注释 文档注释 代码示例 public class Hello{    public static void main(String[] args) {         ...

  9. Spring Boot优雅地处理404异常

    背景 在使用SpringBoot的过程中,你肯定遇到过404错误.比如下面的代码: @RestController @RequestMapping(value = "/hello" ...

  10. [LeetCode题解]83. 删除排序链表中的重复元素 | 递归 + 迭代

    方法一:递归 解题思路 通过递归法,每次判断目前头节点与给定的节点是否相等.如是,继续判断下一个节点,否则保存当前头节点,设置 next 指向下次递归得到的节点,然后返回当前节点. 代码 /** * ...