Dubbo

发布时间 2023-08-14 21:46:10作者: lwx_R

1.依赖

  • 父工程
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
  </parent>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>

      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  • 子工程
<dependencies>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>
    <!--dubbo-->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>
  </dependencies>

2.provider模块

  • application.yml
server:
  port: 8000
spring:
  application:
    name: provider
  cloud:
    # nacos注册中心配置
    nacos:
      discovery:
        server-addr: 127.0.0.1
        register-enabled: true
dubbo:
  scan:
    base-packages: org.example # 开启包扫描
  protocols:
    dubbo:
      name: dubbo # 服务协议
      port: -1 # 服务端口
  registry:
    address: nacos://127.0.0.1:8848 # 注册中心
  • App启动类
@EnableDubbo
@SpringBootApplication
@EnableDiscoveryClient
public class App
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class);
    }
}
  • Product
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product implements Serializable {
    private Long id;//商品iD
    private String productName;//商品名称
}
  • ProductService
public interface ProductService {
    Product findByPid(Integer pid);
}
  • ProductServiceImpl
    注意: Service注解是dubbo包下的
@Service
@Component
//暴露服务:注意这里使用的是dubbo提供的注解@Service,而不是Spring的
public class ProductServiceImpl implements ProductService {

    @Override
    public Product findByPid(Integer pid) {
        System.out.println("11111111111111111");
        return new Product(1L,"123");
    }
}

3.consumer模块

  • application.yml
server:
  port: 9000

dubbo:
  registry:
    address: nacos://127.0.0.1:8848 # 注册中心
  cloud:
    subscribed-services: provider # 订阅的提供者名称

spring:
  application:
    name: consumer
  cloud:
    # nacos注册中心配置
    nacos:
      discovery:
        server-addr: 127.0.0.1
        register-enabled: true
  • ProductService Product 同上
  • Controller
注意: Reference注解是dubbo包下的
@RestController
@Slf4j
public class OrderController {
    //引用服务
    @Reference
    private ProductService productService;

    @RequestMapping("/{pid}")
    public void order(@PathVariable("pid") Integer pid) {
        //调用商品微服务,查询商品信息
        System.out.println(pid);
        Product product = productService.findByPid(pid);
        System.out.println(product);
    }
}