Spring Cloud Alibaba 初体验(三) Nacos 与 Dubbo 集成

摘要:
将Service2的依赖项添加为consumer2.1&lt@OverridepublicStringgreeting(){return“hellofromport:scan:@RequestMapping(“/dubboGreeting”)publicStringdubboGreeting(”{returngreetingService.greeting();协议:地址:
一、新建项目

新建项目,只放置接口,用于暴露 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 集成》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇解决IE8的兼容问题iOS开发证书和配置文件的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue简洁及基本用法

初识Vue 一.什么是Vue vue属于前端知识,在学习vue之前,需要简单了解HTML、css、JavaScript的知识。Vue是框架。 Vue是一套构建用户界面的渐进式架构。vue只关注视图层,采用自底向上增量开发的设计。 Vue是为了通过简单的API实现响应的数据绑定 和 组合的视图组件。 二.IDEA中使用Vue 打开idea,找到file...

addEventListener() 方法

首先需要了解addEventListener()的工作原理是将实现EventListener的函数或对象添加到调用它的EventTarget上的指定事件类型的事件侦听器列表中。 语法 element.addEventListener(event, function, useCapture) 参数值 参数 描述 event 必须。字符串,指定事件名...

ElementUI中的el-table实现递增的序号列

场景 ElementUI中的el-table中实现动态添加一行、删除一行、清空所有行: https://mp.csdn.net/console/editor/html/107815187 在上面中能实现动态添加一行并实现序号递增的效果。 这里记录下递增序号是怎样实现的。 注: 博客: https://blog.csdn.net/badao_liumang_...

转载:SQL注入演示demo

网上看到的一个SQL注入的演示demo,很完整所以转载过来,分享给大家。先要感谢作者!!   作者:潘良虎链接:http://www.zhihu.com/question/22953267/answer/80141632来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 Web安全简史在Web1.0时代,人们更多是关注服务器端...

Spring 注解(二)注解工具类

本文转载自Spring 注解(二)注解工具类 导语 首先回顾一下 AnnotationUtils 和 AnnotatedElementUtils 这两个注解工具类的用法: @Test @GetMapping(value = "/GetMapping", consumes = MediaType.APPLICATION_JSON_VALUE) public...

lambda表达式封装对数据库的查询

前言: 1.为什么要封装lambda表达式数据库查询,原因有一下几点: 1.1.在以往的开发中进行数据库表查询时,其实所需要的字段就是其中几个,但是在开发中,开发者往往习惯select * 进行查询,当数据多和用户量多时,查询的效率会降低。 1.2.在写查询where条件的时候,总是用string.format去拼接字符串,开发效率低。 1.3.代码不够优...