ApacheDubbo的基本使⽤
节点⾓⾊说明
Provid暴露服务的服务提供⽅
Consumer调⽤远程服务的服务消费⽅
Registry服务注册与发现的注册中⼼
Monitor统计服务的调⽤次数和调⽤时间的监控中⼼
Container 服务运⾏容器
Dubbo涉及版本 2.7.7 ~ 2.7.8
Dubbo怎么去使⽤
代码实例地址:
(客户端)
(服务端)
springcloud怎么读音发布⼀个dubbo服
发布服务之后,提供⼀个协议url地址
服务器的IP:容器的端⼝/ mapping
dubbo://192.168.1.104:20880/com.bx.dubbo.server.ILoginService
dubbo⽀持的注册中⼼
consu
zookeeper
eureka
redis
etcd
nacos
....
Dubbo Spring Cloud
创建⼀个项⽬:
代码实例地址:
创建⼀个spring-cloud-dubbo-example的maven⼯程,分别添加三个模块
1. spring-cloud-dubbo-sample-api
2. spring-cloud-dubbo-sample-provider
3. spring-cloud-dubbo-sample-consumer
其中后⾯两个模块都是spring boot的应⽤
修改 spring-cloud-dubbo-sample-provider 这个模块中
将dependencyManagement部分的依赖移动到l
修改spring-cloud-dubbo-sample-provider中的l,增加parent模块的依赖<parent>
<groupId>com.bx.dubbo</groupId>
<artifactId>spring-cloud-dubbo-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
添加maven依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.bx.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>spring-cloud-dubbo-sample-api</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
定义服务接⼝
在spring-boot-dubbo-sample-api模块中,定义接⼝
public interface IHelloService {
String sayHello();
}
实现服务
在spring-boot-dubbo-sample-provider中,实现IHelloService接⼝
public class HelloServiceImpl implements IHelloService{
@Override
public String sayHello() {
return"Hello GuPao";
}
}
添加 @EnableDiscoveryClient 注解
@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudDubboSampleProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDubboSampleProviderApplication.class,
args);
}
}
配置dubbo服务发布
在服务实现类中添加 @Service 注解
@Service
public class HelloServiceImpl implements IHelloService{
@Override
public String sayHello() {
return"Hello GuPao";
}
}
Dubbo Spring Boot
基于spring boot集成Dubbo⽅式
代码实例地址:
创建⼀个spring-boot-dubbo-example的maven⼯程,分别添加三个模块
1. spring-boot-dubbo-sample-api[maven]
2. spring-boot-dubbo-sample-provider[spring boot]
3. spring-boot-dubbo-sample-consumer[spring boot]
添加jar包依赖
从2.7开始,dubbo的版本和dubbo-spring-boot的版本是保持⼀致的。
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</dependency>
添加服务以及发布
@DubboService
public class SayHelloServiceImpl implements ISayHelloService {
@Override
public String sayHello(String msg) {
return"[version1.0]Hello,"+msg+"";
}
}
spring.application.name=spring-boot-dubbo-sample-provider
dubbo.scan.basepackages=com.bx.springboot.dubbo.springbootdubbosampleprovider.service dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
编写服务引⽤代码
添加jar包依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.bx.springboot.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>spring-boot-dubbo-sample-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</dependency>
添加web测试
@DubboReference
ISayHelloService sayHelloService;
@GetMapping("/say")
public String say(){
return sayHelloService.sayHello("sayHello");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论