项目结构:

server 消息提供者

nacos-consumer 消息消费者

server-dubbo-provider-api api接口

SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_ide


父项目依赖:

<?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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.jq</groupId>
<artifactId>test</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<modules>
<!--
子工程
-->
<module>server</module>
<module>nacos-consumer</module>
<module>nacos-config</module>
<module>nacos-feign</module>
<module>server-dubbo-provider-api</module>
</modules>

<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<nacos.version>0.2.2.RELEASE</nacos.version>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<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.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>

1.创建提供方SpringBoot项目

项目命名为server

引入pom依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>test</artifactId>
<groupId>com.jq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.jq</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>Demo project for Spring Boot</description>

<dependencies>
<!-- cloud-nacos依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- cloud-dubbo依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>

<!-- 引入provider-api -->
<dependency>
<groupId>com.jq</groupId>
<artifactId>server-dubbo-provider-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

在application.yml配置

server:
port: 10000

spring:
application:
name: server #项目名称


cloud:
nacos:
discovery:
server-addr: 192.168.230.136:8848 #注册中心地址

dubbo:
scan:
base-packages: com.jq.service #扫描包范围
protocol:
name: dubbo # 遵循的协议
port: 19003 #dubbo服务端口 自定义
registry:
address: spring-cloud:// 192.168.230.136:8848

启动类加注解

package com.jq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ServerApplication {

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

创建控制器

package com.jq.controller;

import com.jq.service.IDubboProviderService;
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.RestController;

@RestController
@RequestMapping("hell")
public class testController {

@Autowired
private IDubboProviderService iDubboProviderService ;

/**
* dubbo 调用
* @param
* @return
*/
@GetMapping("/getDubboRemoteInfo")
public String getDubboRemoteInfo(){

return iDubboProviderService.getRemoteDubboInfo();
}
}

创建DubboProviderServiceImpl 实现类

package com.jq.service.impl;

import com.jq.service.IDubboProviderService;

@org.apache.dubbo.config.annotation.Service
//@org.apache.dubbo.config.annotation.Reference:生成代理对象,就像调用本地方法一样调用远程业务方法
public class DubboProviderServiceImpl implements IDubboProviderService {


public String getRemoteDubboInfo() {
return "你好!!!!!!!!!!!";
}
}

2.创建api接口Maven项目

项目命名为 server-dubbo-provider-api

创建IDubboProviderService接口

package com.jq.service;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

public interface IDubboProviderService {
/**
* 获取信息
* @param
* @return
*/
@GetMapping("/getRemoteDubboInfo")
String getRemoteDubboInfo();
}

SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_spring boot_02


SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_spring_03


访问提供者数据 ,下图为效果 http://localhost:10000/hell/getDubboRemoteInfo

SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_ide_04

3.创建消费方项目

项目命名为nacos-consumer

引入pom依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>test</artifactId>
<groupId>com.jq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.jq</groupId>
<artifactId>nacos-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>Demo project for Spring Boot</description>

<dependencies>
<!-- cloud-nacos依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- 引入provider-api -->
<dependency>
<groupId>com.jq</groupId>
<artifactId>server-dubbo-provider-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<!-- cloud-dubbo依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
</project>

在application.yml配置

server:
port: 10001

spring:
application:
name: nacos-consumer #项目名称


cloud:
nacos:
discovery:
server-addr: 192.168.230.136:8848 #注册中心地址

dubbo:
scan:
base-packages: com.jq.service #扫描包范围
protocol:
name: dubbo # 遵循的协议
port: 19004 #dubbo服务端口 自定义
registry:
address: spring-cloud:// 192.168.230.136:8848

启动类加注解

package com.jq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

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

}

创建控制层

package com.jq.controller;

import com.jq.service.IDubboProviderService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
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("/consumer")
public class ConsumerController {



/***
*
* 方式一
*
*
* */
@org.apache.dubbo.config.annotation.Reference
//@org.apache.dubbo.config.annotation.Reference:生成代理对象,就像调用本地方法一样调用远程业务方法
private IDubboProviderService iDubboProviderService ;


/**
* dubbo 远程调用
* @param
* @return
*/
@GetMapping("/getDubboRemoteInfo")
public String getDubboRemoteInfo(){

return iDubboProviderService.getRemoteDubboInfo();
}


/***
*
* 方式二
*
*
* */


@Autowired
private RestTemplate restTemplate;

@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}

@GetMapping("/consumer")
public String test() {
//server 服务名称
//hell控制层
//getDubboRemoteInfo 方法
return restTemplate.getForObject("http://server/hell/getDubboRemoteInfo",String.class);
}





}

SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_spring_05


SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_maven_06

访问消费者,下图为效果 http://localhost:10001/consumer/getDubboRemoteInfo

SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_spring_07


SpringCloud + Nacos +Springboot  + Dubbo 集成做注册中心(2)_spring boot_08