dubbo SpringCloud
先定义父工程,springboot版本为idea自己生成的2.3.7.RELEASE
springcloud版本为Hoxton.SR12
springcloud alibaba版本为2.2.2.RELEASE
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modules>
<module>dubbo-api</module>
<module>common</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
</parent>
<groupId>moe.macrohard</groupId>
<artifactId>springcloud-dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
1. 公共接口和公共POJO
dubbo服务之间调用通过接口类名实现,因此公共的服务接口全类名要相同
声明一个接口,其全类名为moe.macrohard.common.service.ProviderService
package moe.macrohard.common.service;
public interface ProviderService {
String getProviderData();
}
2. 服务提供者
-
pom依赖
java依赖如下,其中
common
为之前的公共接口和pojo包<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- nacos依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- dubbo依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>moe.macrohard</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency>
-
yaml配置
# 应用名称 spring: application: name: dubbo-api main: # Spring Boot 2.1 allow-bean-deFinition-overriding: true # Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html # Nacos认证信息 cloud: nacos: discovery: username: nacos password: nacos # Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口 server-addr: localhost:8848 # 注册到 nacos 的指定 namespace,默认为 public #spring.cloud.nacos.discovery.namespace=public # dubbo 协议 dubbo: protocol: name: dubbo # dubbo 协议端口( -1 表示自增端口,从 20880 开始) port: -1 # dubbo 服务扫描基准包 scan: base-packages: moe.macrohard.dubboapi registry: # 配置注册中心 address: nacos://localhost:8848 # 应用服务 WEB 访问端口 server: port: 8989
-
实现服务接口
创建实现类,实现类要使用
@dubboService
注解package moe.macrohard.dubboapi.service.impl; import moe.macrohard.common.service.ProviderService; import org.apache.dubbo.config.annotation.dubboService; @dubboService public class ProviderServiceImpl implements ProviderService { @Override public String getProviderData() { return "Hello"; } }
-
nacos配置类
表示启用nacos服务
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional @R_840_4045@ion regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package moe.macrohard.dubboapi.nacosdiscovery; import org.springframework.cloud.client.discovery.EnablediscoveryClient; import org.springframework.context.annotation.Configuration; /** * @author <a href="mailto:[email protected]">theonefx</a> */ @EnablediscoveryClient @Configuration public class NacosdiscoveryConfiguration { }
3.服务消费者
-
pom配置
服务消费者同样需要公共接口包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>moe.macrohard</groupId> <artifactId>common</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency>
-
yaml配置
# 应用名称 spring: application: name: consumer # Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html # Nacos认证信息 cloud: nacos: discovery: username: nacos password: nacos # Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口 server-addr: localhost:8848 # 注册到 nacos 的指定 namespace,默认为 public #spring.cloud.nacos.discovery.namespace=public # dubbo 协议 dubbo: protocol: id: dubbo name: dubbo # dubbo 协议端口( -1 表示自增端口,从 20880 开始) port: -1 # dubbo 消费端订阅服务端的应用名,多个服务提供者用逗号分隔 # 表示要订阅服务的服务名,能够配置'*', # 表明订阅全部服务,不推荐使用。若需订阅多应用,使用 "," 分割 cloud: subscribed-services: dubbo-api # dubbo 服务扫描基准包 scan: base-packages: moe.macrohard.consumer registry: # 配置注册中心 address: nacos://localhost:8848 # 应用服务 WEB 访问端口 server: port: 8990
-
定义消费者的接口以及实现类
消费者接口
package moe.macrohard.consumer.service; public interface ConsumerService { String getProviderData(); }
接口实现类
注入服务提供者接口使用的注解是
@dubboReference
package moe.macrohard.consumer.service.impl; import moe.macrohard.common.service.ProviderService; import moe.macrohard.consumer.service.ConsumerService; import org.apache.dubbo.config.annotation.dubboReference; import org.springframework.stereotype.Service; @Service public class ConsumerServiceImpl implements ConsumerService { @dubboReference ProviderService providerService; @Override public String getProviderData() { return providerService.getProviderData(); } }
-
nacos配置类
和服务提供者的nacos配置类一样
-
消费者的controller
调用消费者服务即可
package moe.macrohard.consumer.controller; import moe.macrohard.consumer.service.ConsumerService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class ConsumerController { @Resource private ConsumerService consumerService; @GetMapping("/") public String hello() { return consumerService.getProviderData(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。