springboot通过nacos集成dubbo

springboot通过nacos集成dubbo,第1张

springboot通过nacos集成dubbo

服务端(被调用):
pom:

 
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.2.4.RELEASE
        
 
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        

yml:

server:
  port: 9999
spring:
  application:
    name: dtest
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
#dubbo  服务者配置
dubbo:
  application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
    name: Provide
  registry: #注册中心配置,用于配置连接注册中心相关信息。
    address: nacos://127.0.0.1:8848
  protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example.dtest.dubboProvide.service  #服务暴露与发现消费所在的package

最后配置的扫描的dubbo服务

dubbo服务接口:注意这里的@service是dubbo的,后面才能注入到其他服务中

package com.example.dtest.dubboProvide.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.service.DemoService;

@Service(version = "1.0.0")
@org.springframework.stereotype.Service
public class DemoServerImpl implements DemoService {


    @Override
    public String sayHello(String s) {
        System.out.println("远程调用该服务方!");
        return "你好"+s+"!";
    }
}

客户端(调用的/消费端):
pom:

```powershell


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    


        
            com.example
            dtestcommon
            0.0.1-SNAPSHOT
            
            
                org.springframework.boot
                spring-boot-starter-web
            
            
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        


        
            org.springframework.boot
            spring-boot-starter-webflux
        

        
        
            org.springframework.cloud
            spring-cloud-starter-gateway
            2.2.1.RELEASE
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.2.4.RELEASE
        

        
        
            org.apache.commons
            commons-lang3
            3.12.0
        


        
















        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        



    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


yml:

```powershell
server:
  port: 8889

spring:
  cloud:
    gateway:
      routes:
        - id: path_route
          uri: lb://dtest  #配置的实际想访问的动态地址(nacos注册的)
          predicates: #断言,路径相匹配的进行路由
#            - Path=/myWebsocket/000  #路径符合条件的(断言这个路径要是实际路由有的那个地址,要是真实访问地址服务器里面有的地址)
            - Path=/myWebsocket/**  #路径符合条件的(断言这个路径要是实际路由有的那个地址,要是真实访问地址服务器里面有的地址)
    nacos:
      discovery:
        server-addr: localhost:8848

  application:
    name: demogateway

dubbo:
  application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
    name: Consumer
  registry: #注册中心配置,用于配置连接注册中心相关信息。
    address: nacos://127.0.0.1:8848
  protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example.dtestgateway.dubboConsumer.service #服务暴露与发现消费所在的package

消费的dubbo接口类:

package com.example.dtestgateway.dubboConsumer.service.imp;

import com.example.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class DubboTestService {

    @DubboReference(version = "1.0.0")
    public DemoService demoService;

    public String Echo(String s){
        String ret_msg;
        try {
            ret_msg  = demoService.sayHello(s);
        }catch (Exception e){
            e.printStackTrace();
            ret_msg = "出错啦!";
        }
        return  ret_msg;
    }

}

注意:这里的@DubboReference也是dubbo的,才能注入

controller:

package com.example.dtestgateway.controller;

import com.example.dtestcommon.vo.Res;
import com.example.dtestgateway.dubboConsumer.service.imp.DubboTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dubbo")
public class DubboTestController {

    @Autowired
    DubboTestService dubboTestService;

    @GetMapping("/pushString")
    public Res Hello(@RequestParam("name") String name){
        String str = dubboTestService.Echo(name);
        return Res.success(str);
    }

}

公共服务,调用包:服务端是集成的这个公共接口,然后服务端调用注入的也是这个公共接口,他在仅仅作用于调用的公共服务里

package com.example.service;

public interface DemoService {

    String sayHello(String s);

}
测试:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5704955.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存