spring cloud微服务整合dubbo和nacos

版本说明

spring-cloud-dependencies版本:Greenwich.SR5
spring-cloud-alibaba-dependencies版本:2.1.0.RELEASE
dubbo-spring-boot-starter版本:2.7.1
dubbo版本:2.7.3
nacos-client版本:1.1.1

源码流程图

dubbo启动及请求调用流程图
若小伙伴还没有注册过processon,可以使用以下链接注册,支持小编3个文件数 -_-
注册processon

spring cloud微服务

微服务代码较多,读者可以下载源码自行参考

服务端 helle-service-dubbo模块

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springCloudDemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>helle-service-dubbo</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>hello-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

spring:
  application:
    name: hello-service-dubbo-provider
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.220.125:3306/mall?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: Root123

mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml

dubbo:
  application:
    name: hello-service-dubbo-provider
  protocol:
    name: dubbo
    port: -1
  registry:
    id: nacos-registry
    address: nacos://192.168.220.125:8848
  config-center:
    address: nacos://192.168.220.125:8848
  metadata-report:
    address: nacos://192.168.220.125:8848

启动类

package org.example;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class ServiceDubboApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceDubboApplication.class, args);
    }
}

service类,注意这里@Service注解使用的是org.apache.dubbo.config.annotation.Service

package org.example.service;

import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
import org.example.service.api.HelloService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    @Transactional
    public String sayHello(String name) {
        System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name;
    }
}

客户端 consumer-dubbo模块

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springCloudDemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-dubbo</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>hello-service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

application.yml

server:
  port: 8765
spring:
  application:
    name: consumer-dubbo

dubbo:
  application:
    name: consumer-dubbo
  protocol:
    name: dubbo
    port: -1
  registry:
    id: nacos-registry
    address: nacos://192.168.220.125:8848
  config-center:
    address: nacos://192.168.220.125:8848
  metadata-report:
    address: nacos://192.168.220.125:8848

启动类

package org.example;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}

controller

package org.example.controller;

import org.apache.dubbo.config.annotation.Reference;
import org.example.service.api.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DubboHelloController {

    @Reference
    HelloService helloService;

    @GetMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.sayHello( name );
    }
}

测试验证

分别启动服务端和客户端,然后访问nacos,可以看到服务已经注册上来了
在这里插入图片描述
现在可以去浏览器验证一下服务是否可用
http://localhost:8765/hi?name=numberone
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot应用中使用DubboNacos可以通过以下步骤实现: 1. 添加DubboNacos的依赖 ```xml <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>${nacos.version}</version> </dependency> ``` 2. 在application.properties文件中配置DubboNacos ```properties #Dubbo配置 dubbo.application.name=demo-provider dubbo.registry.address=nacos://${nacos.host}:${nacos.port} dubbo.scan.basePackages=com.example.demo.service.impl #Nacos配置 nacos.host=127.0.0.1 nacos.port=8848 ``` 3. 创建Dubbo服务接口和实现类 ```java public interface DemoService { String sayHello(String name); } @Service(version = "1.0.0") public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return "Hello " + name; } } ``` 4. 在启动类上添加注解@EnableDubbo和@EnableDiscoveryClient ```java @SpringBootApplication @EnableDubbo @EnableDiscoveryClient public class DemoProviderApplication { public static void main(String[] args) { SpringApplication.run(DemoProviderApplication.class, args); } } ``` 5. 启动应用 6. 在Dubbo Consumer端调用服务 ```java @RestController public class DemoController { @Reference(version = "1.0.0") private DemoService demoService; @GetMapping("/hello") public String sayHello(@RequestParam String name) { return demoService.sayHello(name); } } ``` 以上就是在Spring Boot应用中使用DubboNacos的简单实现方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值