版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingyangV587/article/details/84990837
通过.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: