SpringBoot工程中整合Dubbo + Nacos

SpringBoot工程中整合Dubbo + Nacos


文章目录

    • SpringBoot工程中整合Dubbo + Nacos
    • 一、前言
    • 二、服务注册
      • 添加依赖
      • 在 application.properties 中配置
      • 创建服务接口
      • 创建服务接口实现
      • 启动SpringBoot工程 发布dubbo服务
    • 二、服务消费
      • 添加依赖
      • 在 application.properties 中配置
      • Dubbo服务消费者
      • 启动SpringBoot工程 测试服务调用


一、前言

  • Dubbo:服务调用的RCP框架,测试版本:2.7.8
  • Nacos : 服务注册中心,代替传统的服务注册中心zookeeper
  • SpringBoot : 简化了Spring应用的开发

二、服务注册

添加依赖

<parent>
       <groupId>org.springframework.bootgroupId>
       <artifactId>spring-boot-starter-parentartifactId>
       <version>2.2.11.RELEASEversion>
       <relativePath/> 
   parent>
   <properties>
       <java.version>1.8java.version>
   properties>
   <dependencies>
       <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-webartifactId>
       dependency>

       <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-testartifactId>
           <scope>testscope>
       dependency>

       
       <dependency>
           <groupId>org.apache.dubbogroupId>
           <artifactId>dubbo-spring-boot-starterartifactId>
           <version>2.7.8version>
       dependency>

       <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-actuatorartifactId>
       dependency>

       <dependency>
           <groupId>com.alibaba.nacosgroupId>
           <artifactId>nacos-clientartifactId>
           <version>1.3.3version>
       dependency>

       <dependency>
           <groupId>org.springframework.bootgroupId>
           <artifactId>spring-boot-starter-testartifactId>
           <scope>testscope>
           <exclusions>
               <exclusion>
                   <groupId>org.junit.vintagegroupId>
                   <artifactId>junit-vintage-engineartifactId>
               exclusion>
           exclusions>
       dependency>
   dependencies>

在 application.properties 中配置

# Spring boot application
spring.application.name = dubbo-provider-demo
server.port = 9998
management.port = 9988

# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
dubbo.scan.basePackages  = com.tbex.service

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

## RegistryConfig Bean
# dubbo.registry.id = my-registry
# dubbo.registry.address = N/A

#================ nacos registry====================
dubbo.registry.id = nacos
dubbo.registry.address = 127.0.0.1
dubbo.registry.port = 8848
dubbo.registry.protocol = nacos

创建服务接口

public interface HelloService {

    public String sayHello(String name);
}

创建服务接口实现

import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;

/**
 * @author gaozhy
 * @describe
 * @date 2020/11/2 8:02 下午
 */
@DubboService(
        version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "Hello:"+name;
    }
}

启动SpringBoot工程 发布dubbo服务

SpringBoot工程中整合Dubbo + Nacos_第1张图片

二、服务消费

添加依赖

<parent>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-parentartifactId>
	<version>2.3.5.RELEASEversion>
	<relativePath/> 
parent>
<properties>
	<java.version>1.8java.version>
properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-webartifactId>
	dependency>

	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-testartifactId>
		<scope>testscope>
	dependency>

	
	<dependency>
		<groupId>org.apache.dubbogroupId>
		<artifactId>dubbo-spring-boot-starterartifactId>
		<version>2.7.8version>
	dependency>

	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-actuatorartifactId>
	dependency>

	<dependency>
		<groupId>com.alibaba.nacosgroupId>
		<artifactId>nacos-clientartifactId>
		<version>1.3.3version>
	dependency>

	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-testartifactId>
		<scope>testscope>
		<exclusions>
			<exclusion>
				<groupId>org.junit.vintagegroupId>
				<artifactId>junit-vintage-engineartifactId>
			exclusion>
		exclusions>
	dependency>

	<dependency>
		<groupId>com.tbexgroupId>
		<artifactId>dubbo-providerartifactId>
		<version>0.0.1-SNAPSHOTversion>
	dependency>
dependencies>

在 application.properties 中配置


# Spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 9999
management.port = 9989


# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

#================ nacos registry====================
dubbo.registry.id = nacos
dubbo.registry.address = 127.0.0.1
dubbo.registry.port = 8848
dubbo.registry.protocol = nacos

Dubbo服务消费者

package com.tbex;

import com.tbex.service.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DubboConsumerApplication {

    @DubboReference(version = "1.0.0",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.id}")
    private HelloService helloService;

    @RequestMapping("/sayHello/{name}")
    public @ResponseBody
    String sayHello(@PathVariable String name) {
        return helloService.sayHello(name);
    }


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

}

启动SpringBoot工程 测试服务调用

SpringBoot工程中整合Dubbo + Nacos_第2张图片

你可能感兴趣的:(微服务,java,spring,boot,rpc,spring)