当前位置:   article > 正文

Springboot+Dubbo+Nacos实现RPC调用_dubbo rpc + nacos

dubbo rpc + nacos

Springboot+Dubbo+Nacos 注解方式实现微服务调用

1、项目结构

|-- dubbo-demo (父级工程)
    |-- dubbo-demo-core (基础工程)
    |-- dubbo-consumer (消费者)
    |-- dubbo-provider (生产者)
  • 1
  • 2
  • 3
  • 4

SpringBoot版本:2.2.x

Dubbo版本:2.7.3

Nacos版本:1.1.4

2、启动Nacos注册中心

在这里插入图片描述
在这里插入图片描述

3、搭建项目

dubbo-demo父工程版本控制:

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.9.RELEASE</version>
</parent>

<properties>
	<java.version>1.8</java.version>
	<compiler.plugin.version>3.8.1</compiler.plugin.version>
	<war.plugin.version>3.2.3</war.plugin.version>
	<jar.plugin.version>3.1.2</jar.plugin.version>
	<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
	<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
	<dubbo-spring-boot-starter.version>2.7.3</dubbo-spring-boot-starter.version>
	<nacos-client.version>1.1.4</nacos-client.version>
</properties>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-dependencies</artifactId>
			<version>${spring-cloud-alibaba.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>${dubbo-spring-boot-starter.version}</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba.nacos</groupId>
			<artifactId>nacos-client</artifactId>
			<version>${nacos-client.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo-registry-nacos</artifactId>
			<version>${dubbo-spring-boot-starter.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.dubbo</groupId>
			<artifactId>dubbo</artifactId>
			<version>${dubbo-spring-boot-starter.version}</version>
		</dependency>
	</dependencies>
</dependencyManagement>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

Consumer和Provider的Maven依赖如下:

<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-registry-nacos</artifactId>
		<version>${dubbo-spring-boot-starter.version}</version>
	</dependency>
	<dependency>
		<groupId>org.apache.dubbo</groupId>
		<artifactId>dubbo</artifactId>
	</dependency>
	<dependency>
		<groupId>org.example</groupId>
		<artifactId>dubbo-demo-core</artifactId>
		<version>1.0.0</version>
	</dependency>
</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

Provider配置如下:

server.port=8081
spring.application.name=dubbo-provider-demo

# Dubbo
dubbo.scan.base-packages=com.nari.dubbo.provider.service
dubbo.application.name=${spring.application.name}
# 禁用QOS同一台机器可能会有端口冲突现象
#dubbo.application.name.qos-enable=false
#dubbo.application.name.qos-accept-foreign-ip=false
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
## Dubbo Registry
dubbo.registry.address=nacos://localhost:8848
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Consumer配置如下:

server.port=8082
spring.application.name=dubbo-consumer-demo
#dubbo.application.name=${spring.application.name}

## Dubbo Registry
dubbo.registry.address=nacos://localhost:8848
#dubbo.consumer.timeout=4000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4、core工程编写

package com.nari.dubbo.core;

public interface DemoService {
    String sayHello(String name);
}
  • 1
  • 2
  • 3
  • 4
  • 5

5、Provider工程编写

在启动类上面不要忘记加上@EnableDubbo注解

package com.nari.dubbo.provider;

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

@EnableDubbo
@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

实现DemoService接口,注意这里的@Serivce引用的是dubbo的包

package com.nari.dubbo.provider.service;

import com.nari.dubbo.core.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;//dubbo包下
import org.springframework.beans.factory.annotation.Value;

@Slf4j
@Service(version = "${dubbo-demo.version}")
public class DemoServiceImpl implements DemoService {

    @Value("${dubbo.application.name}")
    private String serviceName;

    @Override
    public String sayHello(String name) {
        log.info("provider被调用");
        return String.format("[%s] : Hello, %s", serviceName, name);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

6、Consumer工程编写

和Provider工程的启动类一样,加上@EnableDubbo注解

编写测试接口:

package com.nari.dubbo.consumer.controller;

import com.nari.dubbo.core.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/test")
public class TestController {
    @Reference(version = "${dubbo-demo.version}")
    private DemoService demoService;

    @GetMapping(value = "/test/{name}")
    public String sayHello(@PathVariable("name") String name) {
        return demoService.sayHello(name);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

7、测试

启动Provider工程和Consumer工程,这个时候Nacos会有对应的服务:
在这里插入图片描述

使用浏览器请求consumer:

http://localhost:8082/test/test/测试

在这里插入图片描述

provider控制台打印日志,说明调用成功
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/165644
推荐阅读
相关标签
  

闽ICP备14008679号