1.生产者整合[使用idea初始化一个springboot项目]
1.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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company</groupId>
<artifactId>springboot-nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-nacos</name>
<description>springboot-nacos</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<!-- Dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
<exclusions>
<exclusion>
<artifactId>org.apache.dubbo</artifactId>
<groupId>dubbo</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.7</version>
</dependency>
<!-- Nacos依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.生产者注册服务到nacos
安装nacos链接:mac中安装nacos_weixin_44919041的博客-CSDN博客
我这里使用的是javaConfig的形式进行配置
package com.company.nacos.config;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Dubbo相关配置
*/
@Configuration
@DubboComponentScan(basePackages = "com.company.nacos.webservice.impl")
public class DubboConfig {
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("testApi-provider");
return applicationConfig;
}
/**
* 注入dubbo注册中心配置,基于nacos
* @return
*/
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
//注册中心服务地址
registryConfig.setAddress("121.4.38.117:8848");
registryConfig.setClient("curator");
registryConfig.setCheck(false);
registryConfig.setProtocol("nacos");
registryConfig.setTimeout(60000);
return registryConfig;
}
/**
* dubbo协议,port值-1 表示让dubbo自行找一个可用的port
* @return
*/
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
//注册中心地址协议,支持dubbo, http, local三种协议,分别表示:dubbo地址、http地址、本地注册中心
protocolConfig.setName("dubbo");
protocolConfig.setPort(-1);
return protocolConfig;
}
/**
* 默认基于dubbo协议提供服务
* @param registryConfig
* @param protocolConfig
* @return
*/
@Bean
public ProviderConfig getProviderConfig(RegistryConfig registryConfig, ProtocolConfig protocolConfig){
ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setCluster("failover");
//远程服务调用重试次数,不包括第一次调用,不需要重试请设为0
providerConfig.setRetries(1);
//服务方法调用超时时间(毫秒)
providerConfig.setTimeout(60000);
providerConfig.setLoadbalance("random");
providerConfig.setGroup("testApi");
providerConfig.setOwner("testApi");
providerConfig.setProtocol(protocolConfig);
providerConfig.setRegistry(registryConfig);
return providerConfig;
}
}
@DubboComponentScan(basePackages = "com.company.nacos.webservice.impl")代表对外暴露接口的包路径
3.生产者对外暴露接口
接口类:
package com.company.nacos.webservice;
public interface ApiService {
public String caseClosingCallback(String json);
}
实现类:
package com.company.nacos.webservice.impl;
import com.company.nacos.webservice.ApiService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0", group = "testApi", interfaceClass = ApiService.class)
public class ApiServiceimpl implements ApiService {
@Override
public String caseClosingCallback(String json) {
return "123456";
}
}
4.启动生产者服务[启动类加上@EnableDubbo注解]:
启动后查看nacos注册中心可以看到提供者已经注册成功
2.消费者整合和生产者基本一致[使用idea初始化一个springboot项目]
1.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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.company</groupId>
<artifactId>springboot-nacos-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-nacos-consumer</name>
<description>springboot-nacos-consumer</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
<exclusions>
<exclusion>
<artifactId>org.apache.dubbo</artifactId>
<groupId>dubbo</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.7</version>
</dependency>
<!-- Nacos依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>com.company</groupId>
<artifactId>apiService</artifactId>
<version>1.0.1</version>
</dependency>-->
<dependency>
<groupId>com.company</groupId>
<artifactId>apiService</artifactId>
<version>1.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/apiService.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>lib</extdirs><!--指定外部lib-->
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.消费者注册到nacos并进行消费
package com.company.nacosconsumer.config;
import com.company.nacos.webservice.ApiService;
import org.apache.dubbo.config.*;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Dubbo相关配置,既是消费者也是提供者
*/
@Configuration
@DubboComponentScan(basePackages = "com.company.nacosconsumer.webservice.impl")
public class DubboConfig {
@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("consumer-provider");
return applicationConfig;
}
/**
* 注入dubbo注册中心配置,基于nacos
* @return
*/
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
//注册中心服务地址
registryConfig.setAddress("121.4.38.117:8848");
registryConfig.setClient("curator");
registryConfig.setCheck(false);
registryConfig.setProtocol("nacos");
registryConfig.setTimeout(60000);
return registryConfig;
}
/**
* dubbo协议,port值-1 表示让dubbo自行找一个可用的port
* @return
*/
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
//注册中心地址协议,支持dubbo, http, local三种协议,分别表示:dubbo地址、http地址、本地注册中心
protocolConfig.setName("dubbo");
protocolConfig.setPort(-1);
return protocolConfig;
}
/**
* 默认基于dubbo协议提供服务
* @param registryConfig
* @param protocolConfig
* @return
*/
@Bean
public ProviderConfig getProviderConfig(RegistryConfig registryConfig, ProtocolConfig protocolConfig){
ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setCluster("failover");
//远程服务调用重试次数,不包括第一次调用,不需要重试请设为0
providerConfig.setRetries(1);
//服务方法调用超时时间(毫秒)
providerConfig.setTimeout(60000);
providerConfig.setLoadbalance("random");
providerConfig.setGroup("consumer");
providerConfig.setOwner("consumer");
providerConfig.setProtocol(protocolConfig);
providerConfig.setRegistry(registryConfig);
return providerConfig;
}
/**
* 使用api的方式获取远程调用接口 注入到spring容器中
* @return
*/
@Bean(name="apiService")
public ApiService getApiService(RegistryConfig registryConfig){
ReferenceConfig<ApiService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setInterface(ApiService.class);
referenceConfig.setCheck(false);
referenceConfig.setGroup("testApi");
referenceConfig.setVersion("1.0");
referenceConfig.setRetries(0);
referenceConfig.setTimeout(60000);
referenceConfig.setRegistry(registryConfig);
//该方法在dubbo2.7.5之后弃用了 可以在application.yml配置配置
referenceConfig.setApplication(applicationConfig());
ApiService apiService = referenceConfig.get();
return apiService;
}
}
注: getApiService(RegistryConfig registryConfig)是以javaConfig的形式获取然后注入到spring中进行管理,使用的时候可以@Autowired从spring容器中获取
3.application.yml配置
server:
port: 1119
dubbo:
application:
name: consumer-provider
4.消费提供者提供的接口
package com.company.nacosconsumer.controller;
import com.company.nacos.webservice.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("consumer")
public class ConsumerController {
//可以使用@DubboReference进行远程调用
//@DubboReference(group = "callcenterapps",version = "1.0" ,interfaceClass= ApiService.class,check = false)
@Autowired
ApiService apiService;
@RequestMapping("test")
public void nacosConsumer(){
String s = apiService.caseClosingCallback("消费者测试");
System.out.println(s);
}
}
5.启动消费者,使用postman进行请求输出结果如下
这是查看nacos注册中心可以看到消费者已经注册上去并消费了提供者提供的接口