1.新建PayService模块
这是我父工程的部分定义:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.redocloud</groupId>
<artifactId>crazy</artifactId>
<version>1.0</version>
<name>crazy Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring Settings -->
<spring-boot-admin.version>2.1.5</spring-boot-admin.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<spring-cloud-alibaba.version>0.9.0.RELEASE</spring-cloud-alibaba.version>
<dubbo.version>2.7.1</dubbo.version>
</properties>
<dependencies>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
选中工程文件,如我这里是crazy=>new Moudule=>填写模块信息=>Finish
2.定义接口模块
以下是common模块的部分内容:
因为我这里已经有了common模块,就将就用这个模块来定义dubbo服务的接口了
public interface PayService {
void pay(Map map);
}
项目结构如下:
定义玩接口后,执行maven的package或者install命令:
可以在自己的本地maven仓库中看到,common-1.0.jar
3.服务提供者
以下是payservice服务的内容
依赖如下:
<?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">
<parent>
<artifactId>crazy</artifactId>
<groupId>cn.redocloud</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>payservice</artifactId>
<dependencies>
<!--这个就是上一步我们package、install后生成的包 但是这里我用的complie-->
<dependency>
<groupId>cn.redocloud</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<!--这里的${dubbo.version}我是在父工程中定义了版本号的
<dubbo.version>2.7.1</dubbo.version>-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
application.yml配置:
spring:
profiles:
active: dev
application:
name: payservice
dubbo:
registry:
address: nacos://127.0.0.1:8848
##dubbo nacos 命名空间 默认public
parameters[namespace]: 9eeffe66-d561-4b00-a42c-38d13fd5cd09
protocol:
port: -1
name: dubbo
bootstrap.yml配置
spring:
cloud:
nacos:
config:
namespace: 9eeffe66-d561-4b00-a42c-38d13fd5cd09
group: dev
server-addr: 127.0.0.1:8848
file-extension: yaml
discovery:
namespace: 9eeffe66-d561-4b00-a42c-38d13fd5cd09
server-addr: 127.0.0.1:8848
现在写一个服务类:
注意:此处的@Service为dubbo包下的注解而非Spring的注解
代码如下:
package cn.redocloud.service;
import cn.redocloud.api.PayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import java.util.Map;
/**
* @Author: ChenTaoTao
* @Date: 2021/12/29 19:24
* @Describe:
*/
@Service
@Slf4j
public class PayServiceImpl implements PayService {
@Override
public void pay(Map map) {
log.info("数据为{}",map);
}
}
编写一个启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author: ChenTaoTao
* @Date: 2021/12/29 19:42
* @Describe:
*/
@SpringBootApplication
//scanBasePackages 这个就是业务类所在的包路径 启动dubbo
@EnableDubbo(scanBasePackages = "cn.redocloud.service")
public class PayApplication {
public static void main(String[] args) {
SpringApplication.run(PayApplication.class,args);
}
}
4.服务消费者:
以下是uservice模块的内容:
依赖如下:
<?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">
<parent>
<artifactId>crazy</artifactId>
<groupId>cn.redocloud</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.redocloud</groupId>
<artifactId>userservice</artifactId>
<version>1.0</version>
<name>userservice Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.redocloud</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
</project>
application.yml如下
server:
port: 9002
spring:
profiles:
active: dev
application:
name: userservice
dubbo:
registry:
address: nacos://127.0.0.1:8848
##dubbo nacos 命名空间 默认public
parameters[namespace]: 9eeffe66-d561-4b00-a42c-38d13fd5cd09
protocol:
port: -1
name: dubbo
bootstrap.yml如下
spring:
cloud:
nacos:
config:
namespace: 9eeffe66-d561-4b00-a42c-38d13fd5cd09
group: dev
server-addr: 127.0.0.1:8848
file-extension: yaml
discovery:
namespace: 9eeffe66-d561-4b00-a42c-38d13fd5cd09
server-addr: 127.0.0.1:8848
controller中写一个调用payservice的
/**
* @Author: ChenTaoTao
* @Date: 2021/12/20 18:20
* @Describe:
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
PayService payService;
@RequestMapping("/pay")
public String pay() {
payService.pay(new HashMap());
return "ok";
}
}
编写启动类:
/**
* @Author: ChenTaoTao
* @Date: 2021/12/20 18:19
* @Describe:
*/
@SpringBootApplication
@EnableDubbo
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class,args);
}
}
5.启动生产者
启动payservice:
可以在nacos中看到已经多了一个provider,说明生产者注册成功了
6.启动消费者
消费者启动成功后,查看nacos
可以看到多了两个服务,一个是consumer
另外一个是userservice,笔者自己以前公司的项目是用的SpringCloud Alibaba的部分内容,网关使用gateway(继承了均衡负载)、配置中心注册中心使用的nacos、服务间调用用的openfeign(集成了熔断和均衡)、外部调用一般用的别人的官方jar或者是用resttemplate去做调用、reids缓存等等,笔者也在疑惑,如果把nacos-discovery的依赖删除了,是否能将dubbo服务注册上去:
注释discovery的依赖:
启动:
消费者服务正常启动:
此次nacos上只多了一个consumer服务
同理,注释掉生产者payservice的discovery配置:
发现服务正常注册:
说明其实对于dubbo+nacos来说,只需要如下依赖即可:也就是说上面的内容中可以注释掉discovery的内容(因为配置中心还是应该要的,所以这里就没有去掉nacos-config的依赖)
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
其他的就是你自己定义的接口和Spring等的一些包了
注意事项:
以上内容使用了nacos的配置中心,所以需要你在nacos中添加部分配置文件如我的:
其中data id的命名必须与你工程中的命名对应,如:
application.yml
spring:
profiles:
active: dev
application:
name: userservice
bootstrap.yml:
spring:
cloud:
nacos:
config:
namespace: 9eeffe66-d561-4b00-a42c-38d13fd5cd09
group: dev
server-addr: 127.0.0.1:8848
file-extension: yaml
那么你的配置文件的名字应该为:
userservice-dev.yaml (也就是application.name+profiles+file-extension的内容)并且需要严格区分namespace 和 group,file-extension为文件后缀名。
其他服务的配置以此类推。
访问接口:
payservice打印内容(因为我没有传值所以打印的酒就是括号)
dubbo的熔断机制:
在调用者这里实现一个熔断类(和FeignClient差不多)
@Slf4j
public class PayServiceFallback implements PayService {
@Override
public void pay(Map map) {
log.info("服务请求超时");
}
}
在@Refrence注解中设置:
@Reference(mock = "cn.redocloud.fallback;",cluster = "failfast")
PayService payService;
关闭payservice服务后,再次访问接口:
可以看到在fallback类的操作被执行了