知识点说明:
nacos官网地址:https://nacos.io/zh-cn/
nacos-server下载地址:https://github.com/alibaba/nacos/releases
dubbo官网地址:https://dubbo.apache.org/zh/docs/v2.7/user/references/
目录
5.2、访问 http://localhost:10092/api/order/get
1、父工程
1.1、项目结构
1.2 、父工程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.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>spring_cloud_alibaba</artifactId>
<version>1.0.0</version>
<name>spring_cloud_alibaba</name>
<description>自己的SpringCloud电商项目</description>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>account</module>
<module>order</module>
<module>order_delivery</module>
<module>product</module>
<module>product_stock</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring.boot.dependencies.version>2.1.7.RELEASE</spring.boot.dependencies.version>
<mybatis.spring.boot.starter.version>2.0.1</mybatis.spring.boot.starter.version>
<mysql.connector.java.version>5.1.47</mysql.connector.java.version>
<fastjson.version>1.2.67</fastjson.version>
<guava.version>18.0</guava.version>
<druid.spring.boot.starter.version>1.1.16</druid.spring.boot.starter.version>
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<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>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!--<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.spring.boot.starter.version}</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<!-- 模块说明:这里声明多个子模块 -->
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--子版本依赖-->
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</dependency>
<!--子版本依赖-->
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>account</artifactId>
<version>1.0.0</version>
</dependency>
<!--子版本依赖-->
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>order</artifactId>
<version>1.0.0</version>
</dependency>
<!--子版本依赖-->
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>order_delivery</artifactId>
<version>1.0.0</version>
</dependency>
<!--子版本依赖-->
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>product</artifactId>
<version>1.0.0</version>
</dependency>
<!--子版本依赖-->
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>product_stock</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、基础工程
2.1、基础项目图
2.2、服务层TProductService代码
/**
* @Description //TODO
* @Date 2020/10/22 1:17
* @Author huangwb
**/
public interface ITProductService {
/**
* @return java.lang.String
* @Author huangwb
* @Description //TODO 获取商品名称测试
* @Date 2020/10/22 22:29
* @Param []
*
**/
String getProductName();
}
2.3、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>com.bean.spring_cloud_alibaba</groupId>
<artifactId>spring_cloud_alibaba</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<artifactId>common</artifactId>
<version>1.0.0</version>
<name>common</name>
<description>基础项目</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
3、子工程商品服务
3.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>com.bean.spring_cloud_alibaba</groupId>
<artifactId>spring_cloud_alibaba</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<artifactId>product</artifactId>
<version>1.0.0</version>
<name>product</name>
<description>商品服务</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</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-alibaba-nacos-config</artifactId>
</dependency>
<!--整合dubbo-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>bootstrap-dev.yml</exclude>
<exclude>bootstrap-prod.yml</exclude>
<exclude>bootstrap-test.yml</exclude>-->
<exclude>bootstrap.yml</exclude>
</excludes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>bootstrap-${profileActive}.yml</include>
<include>bootstrap.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<repositories><!-- 代码库 -->
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public//</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
</project>
3.2、商品服务项目图
3.3、商品服务配置
bootstrap.yml
spring:
profiles:
active: @profileActive@
application:
name: spring-cloud-alibaba-product #商品服务
server:
port: 10091
bootstrap-dev.yml
nacos:
group: PRODUCT_GROUP #nacos组信息
spring:
cloud:
nacos:
config:
#nacos注册中心
server-addr: ip:8848
#file-extension: yaml
#通过nacos命名空间进行隔离
namespace: 6058d723-26ee-4619-8415-9fd23b0046b8
#配置多个配置文件直接引入
extension-configs:
- data-id: application.yml
refresh: true
group: ${nacos.group}
discovery:
#nacos注册中心
server-addr: ip:8848
#通过nacos命名空间进行隔离
namespace: 6058d723-26ee-4619-8415-9fd23b0046b8
dubbo:
scan:
base-packages: com.bean.spring_cloud_alibaba.service #dubbo 的api实现类扫描包(默认扫描包会自动添加进Spring的依赖中 因为Dubbo会使用componentScan)
protocol:
name: dubbo #通信协议:dubbo
port: -1 #dubbo通信端口
registry:
address: nacos://ip:8848 #注册中心地址,使用springcloud的注册中心
# 配置namespace隔离
parameters[namespace]: 6058d723-26ee-4619-8415-9fd23b0046b8
TProductController
import com.bean.spring_cloud_alibaba.service.ITProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description //TODO 商品服务控制器测试
* @Date 2020/10/21 23:55
* @Author huangwb
**/
@RestController
@RefreshScope //刷新配置
@RequestMapping("/api/product")
public class TProductController {
@Autowired
private ITProductService itProductService;
@GetMapping("getProductName")
public String getProductName() {
return itProductService.getProductName();
}
}
TProductServiceImpl
import com.bean.spring_cloud_alibaba.service.ITProductService;
import org.apache.dubbo.config.annotation.Service;
/**
* @Description //TODO
* @Date 2020/10/21 23:54
* @Author huangwb
**/
@Service(version = "1.0.0")
public class TProductServiceImpl implements ITProductService {
@Override
public String getProductName() {
return "获取到商品名1称";
}
}
4、子工程订单服务
4.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>com.bean.spring_cloud_alibaba</groupId>
<artifactId>spring_cloud_alibaba</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<artifactId>order</artifactId>
<version>1.0.0</version>
<name>order</name>
<description>订单</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.bean.spring_cloud_alibaba</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</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-alibaba-nacos-config</artifactId>
</dependency>
<!--整合dubbo-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>bootstrap-dev.yml</exclude>
<exclude>bootstrap-prod.yml</exclude>
<exclude>bootstrap-test.yml</exclude>-->
<exclude>bootstrap.yml</exclude>
</excludes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>bootstrap-${profileActive}.yml</include>
<include>bootstrap.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<repositories><!-- 代码库 -->
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public//</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
</project>
4.2、订单服务项目图
4.3、订单服务配置
bootstrap.yml
spring:
profiles:
active: @profileActive@
application:
name: spring-cloud-alibaba-order #订单服务名称
server:
port: 10092
bootstrap-dev.yml
nacos:
group: ORDER_GROUP #nacos组信息
spring:
cloud:
nacos:
config:
#nacos注册中心
server-addr: ip:8848
#file-extension: yaml
#通过nacos命名空间进行隔离
namespace: 6058d723-26ee-4619-8415-9fd23b0046b8
#配置多个配置文件直接引入
extension-configs:
- data-id: application.yml
refresh: true
group: ${nacos.group}
discovery:
#nacos注册中心
server-addr: ip:8848
#通过nacos命名空间进行隔离
namespace: 6058d723-26ee-4619-8415-9fd23b0046b8
dubbo:
scan:
base-packages: com.bean.spring_cloud_alibaba.service #dubbo 的api实现类扫描包(默认扫描包会自动添加进Spring的依赖中 因为Dubbo会使用componentScan)
protocol:
name: dubbo #通信协议:dubbo
port: -1 #dubbo通信端口
registry:
address: nacos://ip:8848 #注册中心地址,使用springcloud的注册中心
# 配置namespace隔离
parameters[namespace]: 6058d723-26ee-4619-8415-9fd23b0046b8
TOrderController
import com.bean.spring_cloud_alibaba.service.ITProductService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description //TODO
* @Date 2020/10/22 1:15
* @Author huangwb
**/
@RestController
@RequestMapping("/api/order")
public class TOrderController {
@Reference(version = "1.0.0")
public ITProductService itProductService;
@GetMapping("/get")
public String get() {
return itProductService.getProductName();
}
}
OrderApplication
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
5、项目启动
5.1、登录nacos管理界面查看服务是否注册
5.2、访问 http://localhost:10092/api/order/get
6、注意事项
6.1、依赖版本问题
注册SpringCloudAlibaba和Dubbo的依赖版本,否则版本过高或者相差过高,会导致一系列的问题。 我试过其他版本的使用方式,会存在项目启动时候提示找不到对应的Class的Jar包,所以大家根据自己使用版本进行调整版本依赖,有时候不是你配置有问题或者代码有问题,是因为依赖的版本导致的。
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
附带上阿里官方推荐版本图