有的神仙可能比较有以为啊。。为什么之前的博客使用的eclipse呢?这篇博客又用Idea呢?我任性
说白了就是小编很久没有用eclipse开发了,然后突然心血来潮,就操作了一手eclipse,而为什么现在又用Idea呢?嗯…我是不会告诉你的
回来回来,别跑,主题要开始了!!!!!!!!!!!!
传送门来一波,怕神仙你迷路
不知道之前的代码案例各位神仙是否有搞定,如果没搞定的,上面传送门回去重新制造一波
单刀直入。。。项目结构 刚刚好够截图
编码过程大致和之前的差不多(只是结构不同,之前是Maven,现在是SpringBoot)
第一步创建三个项目(三个项目中只有consumer需要勾选SpringWeb,其他都不需要勾选)
boot-user-service-common(公共包。用来做数据向上抽取的)
boot-user-service-provide(模拟生产端)
boot-usre-service-consumer(模拟消费端)
简单说,慢慢品
提供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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.onlyKing</groupId>
<artifactId>boot-user-service-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-user-service-common</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
划重点:需要注意版本问题
pom直接提供给各位(基本都是正常操作,就是可能导入相关的dubbo的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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.onlyKing</groupId>
<artifactId>boot-user-service-provide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-user-service-provide</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.6</dubbo.version>
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--依赖公共包-->
<dependency>
<groupId>com.onlyKing</groupId>
<artifactId>boot-user-service-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--导入dubbo-->
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.6</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--监控平台要加入-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserServiceImpl(使用Dubbo的Service来简化配置)
BootUserServiceProvideApplication(启动类加配置)
application.properties
boot-usre-service-consumer
pom.xml(多导入了一个SpringWeb)
<?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.onlyKing</groupId>
<artifactId>boot-user-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-user-service-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<dubbo.version>2.7.6</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.onlyKing</groupId>
<artifactId>boot-user-service-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--导入dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.6</version>
</dependency>
<!--监控平台要加入-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
OrderController
OrderServiceImpl
BootUserServiceConsumerApplication
application.properties
这里后面小编又在排查别的错误所以将8082端口改成了8083端口,其实没有什么问题…
踩坑:
一号坑:
java.lang.NoClassDefFoundError: org/apache/curator/framework/recipes/cache/TreeCacheListener
在pom文件中加入这两个依赖即可
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
二号坑:
在UserServiceImpl的@Server中导入是的apache中的dubbo的jar包,不用看错了,也不要想当然自动导入的是dubbo的
三好坑:
@Reference注解这个把小编搞得课辛苦了,大概就是和上面一样的问题,以为@Reference不像@Server注解这个是以前有使用过的,所以小编在导入的时候就是直接导入进去,找了好久才发现是这个@Reference注解导入错误导致的问题一直不发访问页面