Spring Cloud Alibaba 分布式服务调用篇
Posted Dream_it_possible!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Alibaba 分布式服务调用篇相关的知识,希望对你有一定的参考价值。
目录
1. 基于Dubbo Spring Cloud 搭建服务消费方和服务提供方
2. 搭建common-interface模块定义dubbo服务接口
Dubbo Spring Cloud 基于Spring Cloud Commons 抽象实现Dubbo 服务注册与发现,无需添加任何外部化的配置,就能桥接到所有原生的Spring Cloud 注册中心:
- eureka
- zookeeper
- nacos
- consul
Dubbo Spring Cloud 相当于Spring Cloud plus, 是对原生Spring Cloud 的封装和增强,主要包含了分布式配置、服务注册与发现、负载均衡、服务熔断、服务调用、链路追踪等功能。
一、Dubbo Spring Cloud 应用
怎么将添加Dubbo Spring Cloud引入到项目中,在pom.xml文件中添加依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
共有依赖组件:
- Nacos Service Discovery : 服务发现注册组件。
- Spring Web: web mvc组件。
- Spring Boot Actuator: 应用健康监控检查组件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1. 基于Dubbo Spring Cloud 搭建服务消费方和服务提供方
服务消费方和提供方的依赖相同, 主要依赖有:
- Spring Boot 版本: 2.3.0.RELEASE
- Dubbo Spring Cloud 版本: 2.2.1.RELEASE
Dubbo Spring Cloud 通过<dependencyManagement> 标签引入。
<?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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.alibaba</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider</name>
<description>服务提供者示例</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba-version>2.2.1.RELEASE</spring-cloud-alibaba-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.example.alibaba</groupId>
<artifactId>common-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!--nacos discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>
</plugin>
</plugins>
</build>
</project>
服务提供方
1) 配置文件里新增dubbo相关配置:
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.application.name=provider
server.port=8001
spring.cloud.nacos.discovery.user-name=nacos
spring.cloud.nacos.discovery.password=nacos
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
# dubbo spring cloud 配置
dubbo.scan.base-packages=com.example.alibaba
dubbo.protocol.name=dubbo
# -1 表示自增,从20880开始
dubbo.protocol.port=-1
2) 提供API接口的实现类
package com.example.alibaba.service;
import com.example.alibaba.user.UserInterface;
import org.apache.dubbo.config.annotation.Service;
/**
* @decription:
* @author: zhengbing.zhang
* @date: 2021/7/17 16:06
*/
@Service
public class UserService implements UserInterface {
@Override
public String getUserPassWordByUserName(String username) {
return "hello ," + username;
}
}
注意: 实现类需必须添加@Service注解,该注解是 org.apache.dubbo.config.annotation.Service,没有该注解dubbo会扫描不到该实现类Bean。
服务消费方
服务消费方只要拿到接口的Bean就能够远程调用到目标方法,其实是获取到代理类。用@Reference注解来装配接口的Bean。此注解为org.apache.dubbo.config.annotation.Reference
package com.example.alibaba.controller;
import com.example.alibaba.user.UserInterface;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @decription:
* @author: zhengbing.zhang
* @date: 2021/7/17 16:54
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
private UserInterface userInterface;
@GetMapping("/query")
public String queryUser() {
String result = userInterface.getUserPassWordByUserName("zhang zheng bing ");
return result;
}
}
订阅服务。 服务消费方需要配置订阅哪个微服务,如果有多个可以用"," 逗号隔开。
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.application.name=consumer
server.port=8002
spring.cloud.nacos.discovery.user-name=nacos
spring.cloud.nacos.discovery.password=nacos
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
# dubbo cloud
dubbo.cloud.subscribed-services=provider
2. 搭建common-interface模块定义dubbo服务接口
common-interface 定义提供API的模块,由服务暴露方去实现,由服务消费者去消费, 在此定义了一个UserInterface接口。
public interface UserInterface {
String getUserPassWordByUserName(String username);
}
3. 将API模块部署到本地仓库
在终端切换到模块路径,执行 mvn clean install 命令, 可以将项目打成jar包存放到本地仓库。
解决打jar包出现的BOOT-INF文件夹问题?
springboot的maven插件添加一个配置<skip> true</skip>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
将打好的jar包引入到项目中:
<dependency>
<groupId>com.example.alibaba</groupId>
<artifactId>common-interface</artifactId>
</dependency>
可以在External Libraries里查看我们打好的jar包:
访问: http://localhost:8002/user/query
至此,dubbo spring cloud 实现微服务的调用过程全部实现了。
二、 Dubbo 迁移Spring Cloud 应用
Dubbo Spring Cloud 提供了优秀的服务治理能力,同时也提供了将SpringCloudOpenFeign或者@LoadBalanced`RestTemplate迁移为Dubbo服务的能力。可以通过@DubboTransported注解来将Spring Cloud OpenFeign 接口以及@LoadBalance的RestTemplateBean 底层调用走dubbo , 而服务提供方只需要在原来的@Restcontroller 类上追加一个@Service注解即可。
以上是关于Spring Cloud Alibaba 分布式服务调用篇的主要内容,如果未能解决你的问题,请参考以下文章