服务端(被调用):
pom:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
yml:
server:
port: 9999
spring:
application:
name: dtest
cloud:
nacos:
discovery:
server-addr: localhost:8848
#dubbo 服务者配置
dubbo:
application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
name: Provide
registry: #注册中心配置,用于配置连接注册中心相关信息。
address: nacos://127.0.0.1:8848
protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
name: dubbo
port: 20880
scan:
base-packages: com.example.dtest.dubboProvide.service #服务暴露与发现消费所在的package
最后配置的扫描的dubbo服务包
dubbo服务接口:注意这里的@service是dubbo的,后面才能注入到其他服务中
package com.example.dtest.dubboProvide.service.imp;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.service.DemoService;
@Service(version = "1.0.0")
@org.springframework.stereotype.Service
public class DemoServerImpl implements DemoService {
@Override
public String sayHello(String s) {
System.out.println("远程调用该服务方!");
return "你好"+s+"!";
}
}
客户端(调用的/消费端):
pom:
```powershell
<?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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 要把web的去掉-->
<dependency>
<groupId>com.example</groupId>
<artifactId>dtestcommon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- <!–gateway–>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>dubbo</artifactId>-->
<!-- <version>2.5.3</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework</groupId>-->
<!-- <artifactId>spring</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.jboss.netty</groupId>-->
<!-- <artifactId>netty</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml:
```powershell
server:
port: 8889
spring:
cloud:
gateway:
routes:
- id: path_route
uri: lb://dtest #配置的实际想访问的动态地址(nacos注册的)
predicates: #断言,路径相匹配的进行路由
# - Path=/myWebsocket/000 #路径符合条件的(断言这个路径要是实际路由有的那个地址,要是真实访问地址服务器里面有的地址)
- Path=/myWebsocket/** #路径符合条件的(断言这个路径要是实际路由有的那个地址,要是真实访问地址服务器里面有的地址)
nacos:
discovery:
server-addr: localhost:8848
application:
name: demogateway
dubbo:
application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
name: Consumer
registry: #注册中心配置,用于配置连接注册中心相关信息。
address: nacos://127.0.0.1:8848
protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
name: dubbo
port: 20880
scan:
base-packages: com.example.dtestgateway.dubboConsumer.service #服务暴露与发现消费所在的package
消费的dubbo接口类:
package com.example.dtestgateway.dubboConsumer.service.imp;
import com.example.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service
public class DubboTestService {
@DubboReference(version = "1.0.0")
public DemoService demoService;
public String Echo(String s){
String ret_msg;
try {
ret_msg = demoService.sayHello(s);
}catch (Exception e){
e.printStackTrace();
ret_msg = "出错啦!";
}
return ret_msg;
}
}
注意:这里的@DubboReference也是dubbo的,才能注入
controller:
package com.example.dtestgateway.controller;
import com.example.dtestcommon.vo.Res;
import com.example.dtestgateway.dubboConsumer.service.imp.DubboTestService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/dubbo")
public class DubboTestController {
@Autowired
DubboTestService dubboTestService;
@GetMapping("/pushString")
public Res Hello(@RequestParam("name") String name){
String str = dubboTestService.Echo(name);
return Res.success(str);
}
}
公共服务,调用包:服务端是集成的这个公共接口,然后服务端调用注入的也是这个公共接口,他在仅仅作用于调用的公共服务里
package com.example.service;
public interface DemoService {
String sayHello(String s);
}