- 服务提供者 and 消费者 添加pom依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.7.8</version>
</dependency>
- 服务提供者配置properties dubbo相关配置
dubbo.protocol.name=dubbo #协议名称
dubbo.protocol.port=20882 #对外暴露端口 可以配置-1 自动生成端口
dubbo.scan.base-packages=com.xlw.service.impl #扫描服务所在的包
spring.cloud.nacos.discovery.server-addr=192.168.25.5:8848 #nacos注册中心地址
- 服务消费者配置properties dubbo相关配置
spring.cloud.nacos.discovery.server-addr=192.168.25.5:8848 #如果不需要对外提供服务,只需要配置注册中心地址
- 服务提供者相关代码demo
package com.xlw.service.impl;
import com.xlw.service.LoginService;
import org.apache.dubbo.config.annotation.DubboService;
/**
* @author xlw
* @datetime 17:05
* LoginServiceImpl
*/
//在dubbo2.7.7之前使用的使用@Service(服务发布)@Reference(服务引用),在2.7.8被替代@DubboService
@DubboService
public class LoginServiceImpl implements LoginService {
@Override
public String sayHello() {
return "hello boss";
}
}
- 服务消费者相关代码demo
package com.xlw.consumer;
import com.xlw.service.LoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class MySpringCloudDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringCloudDubboConsumerApplication.class, args);
}
//在dubbo2.7.7之前使用的使用@Service(服务发布)@Reference(服务引用),在2.7.8被替代@DubboReference
@DubboReference
LoginService loginService;
@GetMapping("/get")
public String get(){
return loginService.sayHello();
}
}
dubbo与spring boot集成
dubbo如果存在多注册中心,与spring cloud进行集成会需要关闭相关默认配置,比较繁琐,与spring boot集成会更加灵活
- 提供者and消费者引入相关pom依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.8</version>
</dependency>
- 服务生产者配置多注册中心properties
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
#注册中心1 nacos
dubbo.registries.shanghai.address=nacos://192.168.25.5:8848
#注册中心1 zookeeper
dubbo.registries.beijing.address=zookeeper://192.168.25.5:2181
dubbo.registries.beijing.timeout=100000 #zookeeper需要配置一定时间的超时时间(有的10足够,按照自己所需配置),否则可能导致连接不上。
- 发布服务相关代码demo
package com.xlw.dubbo.service.impl;
import com.xlw.service.SayHelloService;
import org.apache.dubbo.config.annotation.DubboService;
/**
* @author xlw
* @datetime 19:06
* SayHelloServiceImpl
*可以发布到多个注册中心上
*/
@DubboService(registry = {"shanghai","beijing"})
public class SayHelloServiceImpl implements SayHelloService {
@Override
public String sayHello() {
return "Hello Boss";
}
}
- 服务消费者properties
#注册中心1 nacos
dubbo.registries.shanghai.address=nacos://192.168.25.5:8848
#注册中心1 zookeeper
dubbo.registries.beijing.address=zookeeper://192.168.25.5:2181
dubbo.registries.beijing.timeout=100000
- 服务消费者引用服务相关代码demo
package com.xlw.dubbo.consumer;
import com.xlw.service.SayHelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringBootDuboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDuboConsumerApplication.class, args);
}
@DubboReference
SayHelloService sayHelloService;
@GetMapping("/say")
public String say(){
return sayHelloService.sayHello();
}
}