最全最简单的dubbo教程-以属性配置的形式整合dubbo《三》

通过.properties文件的方式搭建与XML原理都一样,只不过所有的声明信息都填写到.properties里面了。这里是官网给出的解释:
在这里插入图片描述

开始搭建

服务生产者配置

在application.propetries中声明生成者配置信息

#springboot应用
spring.application.name = dubbo-provider-demo
server.port = 9090

#对外暴露服务版本
demo.service.version = 1.0.0

# 扫描带阿里注解的的类(e.g @Service , @Reference)
#如果没有在配置中写dubbo.scan.base-package,还需要使用@EnableDubbo注解
#如果写了dubbo.scan.basePackages就可以不用写@EnableDubbo注解
dubbo.scan.basePackages  =com.gjy.server

# Dubbo Config properties
##应用配置
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo

##协议配置
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

dubbo.service.*.timeout=1000
##注册配置
dubbo.registry.id = my-registry
dubbo.registry.address =zookeeper://localhost:2181
dubbo.registry.dynamic=false
dubbo.registry.check=false
#监控中心
#dubbo.monitor.protocol=registry

#Dubbo Config properties
##应用配置
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo
    
#dubbo 2.6.2版本需要写注册中心配置
dubbo.registry.address =zookeeper://localhost:2181
    
#监控中心
#dubbo.monitor.protocol=registry
#关闭所有消费者启动时检查
#dubbo.consumer.check=false
服务接口实现
package com.gjy.server;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

/**
 * @ClassName: DefaultDemoService
 * @Description:对外暴露接口实现类
 */
//demo.service.version 在application.properties中配置过了
@Service//dubbo注解
@Component
public class DefaultServiceImpl implements DefaultApiService {

    public String defaultMethod(String str) {
        return "hello " + str;
    }

}

这里注意@Service这个注解,是dubbo提供的注解相当于XML配置中的

<dubbo:service interface="com.gjy.service.DefaultApiService" ref="defaultService"  />
服务启动

这里就是普通的springboot启动

package com.gjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Auther: 郭敬仰
 * @Date: 2018/12/13 17:13
 * @Description:
 */
@SpringBootApplication
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}
服务消费者配置
#springboot应用名
spring.application.name = dubbo-consumer-demo
server.port = 8080

#服务版本
demo.service.version = 1.0.0

#Dubbo Config properties
##应用配置
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

#dubbo 2.6.2版本需要写注册中心配置
dubbo.registry.address =zookeeper://localhost:2181

#监控中心
#dubbo.monitor.protocol=registry
#关闭所有消费者启动时检查
#dubbo.consumer.check=false
服务生产者调用
package com.gjy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.gjy.server.DefaultApiService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: DemoConsumerController
 * @Description:web调用服务提供者对外暴露的rpc接口
 */
@RestController
public class DemoConsumerController {

    /**
     * 引入服务提供者
     */
    // com.alibaba.dubbo.config.annotation.Reference
    @Reference
    private DefaultApiService demoService;

    @RequestMapping("/sayHello")
    public String sayHello(@RequestParam String name) {
        return demoService.defaultMethod(name);
    }

}

注意这里的@Reference注解是dubbo提供的注解,相当于XML配置的:

<dubbo:reference id="defaultService" interface="com.gjy.service.DefaultApiService" />
消费者启动
package com.gjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 
 * @ClassName:  DubboConsumerApplication   
 * @Description:消费者
 *
 */
@SpringBootApplication
public class DubboConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(DubboConsumerApplication.class, args);
	}

}

还是普通的springboot启动

测试

访问:http://localhost:8080/sayHello?name=hai
如果出现如图,说明ok:
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值