springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目

在上一篇依赖方式多模块的基础上对项目进行改造。主要改造user-service项目,service要配置mapper。mybatis及数据库相关的东西,后面的接口消费方user就不再需要了

注意:以下代码是在不同场所的机器上写的,数据库什么的会有不同,结构也会有不同,最终的代码会以其中一个传递到本人git上,这里记录的是本人总结的一些思路什么的,稍微修改配置,配置一致,就可以运行的

代码如下:

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sharp-user-service</artifactId>
<packaging>jar</packaging> <name>sharp-user-service</name>
<description>Forward project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-entity</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-mapper</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-user-service-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins> </build> </project>

yml文件

spring:
profiles:
active:
- local
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dealer?serverTimezone=GMT%2B8
username: root
password: 123456
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.sharp.forward.entity

local

server:
port: 8886
zookeeper.address: 192.168.1.105:2181

dubbo

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="userService"/>
<!-- use multicast registry center to discover service -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.1.105:2181"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="userService" class="com.sharp.forward.user.service.impl.UserServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.sharp.forward.user.service.UserService" ref="userService"/>
</beans>

项目启动类

package com.sharp.forward;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource; @SpringBootApplication
@ImportResource("config/application-dubbo.xml")
@MapperScan("com.sharp.forward.mapper")
public class UserServiceApplication { public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}

结构

springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目

运行后

 SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/javaEnvironment/repo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/javaEnvironment/repo/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.6.RELEASE) 2018-12-02 20:13:12.238 INFO 32764 --- [ main] c.sharp.forward.UserServiceApplication : Starting UserServiceApplication on litan with PID 32764 (D:\worksheet\sharp-user-service\target\classes started by litan in D:\worksheet\sharp-user-service)
2018-12-02 20:13:12.241 INFO 32764 --- [ main] c.sharp.forward.UserServiceApplication : The following profiles are active: local
2018-12-02 20:13:12.353 INFO 32764 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@49139829: startup date [Sun Dec 02 20:13:12 CST 2018]; root of context hierarchy
2018-12-02 20:13:13.301 INFO 32764 --- [ main] o.s.b.f.xml.XmlBeanDefinitionReader : Loading XML bean definitions from class path resource [config/application-dubbo.xml]
log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2018-12-02 20:13:13.545 INFO 32764 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'userService' with a different definition: replacing [Generic bean: class [com.sharp.forward.user.service.impl.UserServiceImpl]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\worksheet\sharp-user-service\target\classes\com\sharp\forward\user\service\impl\UserServiceImpl.class]] with [Generic bean: class [com.sharp.forward.user.service.impl.UserServiceImpl]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [config/application-dubbo.xml]]
2018-12-02 20:13:15.970 INFO 32764 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8886 (http)
2018-12-02 20:13:16.059 INFO 32764 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-02 20:13:16.059 INFO 32764 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-12-02 20:13:16.077 INFO 32764 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\java\java1.8\jre1.8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;D:\java\oracle\product\11.2.0\dbhome_1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\lenovo\easyplussdk\bin;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.2 & MySQL Utilities 1.5.2 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.2 & MySQL Utilities 1.5.2 1.5\Doctrine extensions for PHP\;D:\java\java1.8\jdk1.8\bin;JAVA_HOME%\jre\bin;D:\java\Z_review\apache-tomcat-7.0.67\bin;D:\我的软件;办公软;\svnServer\bin;%ma;en_home%\bin;D:\我的软件\数据库\putty\;C:\Pro;ram Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;";D:\我的软件\专业软件\zookeeper\zookeeper-3.4.9/bin; D:\我的软件\专业软件\zookeeper\zookeeper-3.4.9/conf";D:\java\java1.8\jdk1.8\bin;C:\Program Files\TortoiseGit\bin;C:\Program Files\Git\cmd;C:\WINDOWS\System32\OpenSSH\;D:\java软件安装资源汇总\maven建项目\apache-maven-3.3.9-bin\apache-maven-3.3.9\bin;D:\protobufc\bin;C:\Users\litan\AppData\Local\Microsoft\WindowsApps;D:\java软件安装资源汇总\2017-6-25\apache-maven-3.3.9-bin\apache-maven-3.3.9\bin;;.]
2018-12-02 20:13:16.372 INFO 32764 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-12-02 20:13:16.373 INFO 32764 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4024 ms
2018-12-02 20:13:16.532 INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-12-02 20:13:16.634 INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-12-02 20:13:16.635 INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-12-02 20:13:16.635 INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-12-02 20:13:16.635 INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2018-12-02 20:13:18.268 INFO 32764 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-12-02 20:13:18.731 INFO 32764 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@49139829: startup date [Sun Dec 02 20:13:12 CST 2018]; root of context hierarchy
2018-12-02 20:13:18.889 INFO 32764 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-12-02 20:13:18.893 INFO 32764 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-12-02 20:13:18.959 INFO 32764 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-12-02 20:13:18.959 INFO 32764 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-12-02 20:13:19.490 INFO 32764 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-12-02 20:13:19.492 INFO 32764 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-12-02 20:13:19.502 INFO 32764 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-12-02 20:13:20.860 INFO 32764 --- [ main] o.a.c.f.imps.CuratorFrameworkImpl : Starting
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 00:39 GMT
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:host.name=windows10.microdone.cn
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:java.version=1.8.0_161
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:java.vendor=Oracle Corporation
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:java.home=D:\java\java1.8\jre1.8
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:java.class.path=D:\java\java1.8\jre1.8\lib\resources.jar;D:\java\java1.8\jre1.8\lib\rt.jar;D:\java\java1.8\jre1.8\lib\jsse.jar;D:\java\java1.8\jre1.8\lib\jce.jar;D:\java\java1.8\jre1.8\lib\charsets.jar;D:\java\java1.8\jre1.8\lib\jfr.jar;D:\java\java1.8\jre1.8\lib\ext\access-bridge-64.jar;D:\java\java1.8\jre1.8\lib\ext\cldrdata.jar;D:\java\java1.8\jre1.8\lib\ext\dnsns.jar;D:\java\java1.8\jre1.8\lib\ext\jaccess.jar;D:\java\java1.8\jre1.8\lib\ext\jfxrt.jar;D:\java\java1.8\jre1.8\lib\ext\localedata.jar;D:\java\java1.8\jre1.8\lib\ext\nashorn.jar;D:\java\java1.8\jre1.8\lib\ext\sunec.jar;D:\java\java1.8\jre1.8\lib\ext\sunjce_provider.jar;D:\java\java1.8\jre1.8\lib\ext\sunmscapi.jar;D:\java\java1.8\jre1.8\lib\ext\sunpkcs11.jar;D:\java\java1.8\jre1.8\lib\ext\zipfs.jar;D:\worksheet\sharp-user-service\target\classes;D:\worksheet\sharp-common\target\classes;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-starter-web\2.0.6.RELEASE\spring-boot-starter-web-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-starter-json\2.0.6.RELEASE\spring-boot-starter-json-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\com\fasterxml\jackson\core\jackson-databind\2.9.7\jackson-databind-2.9.7.jar;D:\javaEnvironment\repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\javaEnvironment\repo\com\fasterxml\jackson\core\jackson-core\2.9.7\jackson-core-2.9.7.jar;D:\javaEnvironment\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.7\jackson-datatype-jdk8-2.9.7.jar;D:\javaEnvironment\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.7\jackson-datatype-jsr310-2.9.7.jar;D:\javaEnvironment\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.7\jackson-module-parameter-names-2.9.7.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-starter-tomcat\2.0.6.RELEASE\spring-boot-starter-tomcat-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\org\apache\tomcat\embed\tomcat-embed-core\8.5.34\tomcat-embed-core-8.5.34.jar;D:\javaEnvironment\repo\org\apache\tomcat\embed\tomcat-embed-el\8.5.34\tomcat-embed-el-8.5.34.jar;D:\javaEnvironment\repo\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.34\tomcat-embed-websocket-8.5.34.jar;D:\javaEnvironment\repo\org\hibernate\validator\hibernate-validator\6.0.13.Final\hibernate-validator-6.0.13.Final.jar;D:\javaEnvironment\repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\javaEnvironment\repo\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\javaEnvironment\repo\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;D:\javaEnvironment\repo\org\springframework\spring-web\5.0.10.RELEASE\spring-web-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\spring-webmvc\5.0.10.RELEASE\spring-webmvc-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-starter-test\2.0.6.RELEASE\spring-boot-starter-test-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-test\2.0.6.RELEASE\spring-boot-test-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-test-autoconfigure\2.0.6.RELEASE\spring-boot-test-autoconfigure-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;D:\javaEnvironment\repo\net\minidev\json-smart\2.3\json-smart-2.3.jar;D:\javaEnvironment\repo\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;D:\javaEnvironment\repo\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\javaEnvironment\repo\junit\junit\4.12\junit-4.12.jar;D:\javaEnvironment\repo\org\assertj\assertj-core\3.9.1\assertj-core-3.9.1.jar;D:\javaEnvironment\repo\org\mockito\mockito-core\2.15.0\mockito-core-2.15.0.jar;D:\javaEnvironment\repo\net\bytebuddy\byte-buddy\1.7.11\byte-buddy-1.7.11.jar;D:\javaEnvironment\repo\net\bytebuddy\byte-buddy-agent\1.7.11\byte-buddy-agent-1.7.11.jar;D:\javaEnvironment\repo\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\javaEnvironment\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\javaEnvironment\repo\org\hamcrest\hamcrest-library\1.3\hamcrest-library-1.3.jar;D:\javaEnvironment\repo\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;D:\javaEnvironment\repo\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\javaEnvironment\repo\org\springframework\spring-test\5.0.10.RELEASE\spring-test-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\xmlunit\xmlunit-core\2.5.1\xmlunit-core-2.5.1.jar;D:\worksheet\sharp-entity\target\classes;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-starter\2.0.6.RELEASE\spring-boot-starter-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot\2.0.6.RELEASE\spring-boot-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-autoconfigure\2.0.6.RELEASE\spring-boot-autoconfigure-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-starter-logging\2.0.6.RELEASE\spring-boot-starter-logging-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\javaEnvironment\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\javaEnvironment\repo\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;D:\javaEnvironment\repo\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;D:\javaEnvironment\repo\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\javaEnvironment\repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\javaEnvironment\repo\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;D:\worksheet\sharp-mapper\target\classes;D:\javaEnvironment\repo\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;D:\javaEnvironment\repo\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;D:\worksheet\sharp-user-service-api\target\classes;D:\javaEnvironment\repo\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.2\mybatis-spring-boot-starter-1.3.2.jar;D:\javaEnvironment\repo\org\springframework\boot\spring-boot-starter-jdbc\2.0.6.RELEASE\spring-boot-starter-jdbc-2.0.6.RELEASE.jar;D:\javaEnvironment\repo\com\zaxxer\HikariCP\2.7.9\HikariCP-2.7.9.jar;D:\javaEnvironment\repo\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.2\mybatis-spring-boot-autoconfigure-1.3.2.jar;D:\javaEnvironment\repo\org\apache\zookeeper\zookeeper\3.4.13\zookeeper-3.4.13.jar;D:\javaEnvironment\repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\javaEnvironment\repo\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;D:\javaEnvironment\repo\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\javaEnvironment\repo\jline\jline\0.9.94\jline-0.9.94.jar;D:\javaEnvironment\repo\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar;D:\javaEnvironment\repo\io\netty\netty\3.10.6.Final\netty-3.10.6.Final.jar;D:\javaEnvironment\repo\com\alibaba\dubbo\2.6.2\dubbo-2.6.2.jar;D:\javaEnvironment\repo\org\springframework\spring-context\5.0.10.RELEASE\spring-context-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\spring-aop\5.0.10.RELEASE\spring-aop-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\spring-expression\5.0.10.RELEASE\spring-expression-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;D:\javaEnvironment\repo\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\javaEnvironment\repo\org\springframework\spring-jdbc\5.1.1.RELEASE\spring-jdbc-5.1.1.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\spring-beans\5.0.10.RELEASE\spring-beans-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\spring-core\5.0.10.RELEASE\spring-core-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\spring-jcl\5.0.10.RELEASE\spring-jcl-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\org\springframework\spring-tx\5.0.10.RELEASE\spring-tx-5.0.10.RELEASE.jar;D:\javaEnvironment\repo\mysql\mysql-connector-java\8.0.12\mysql-connector-java-8.0.12.jar;D:\javaEnvironment\repo\com\google\protobuf\protobuf-java\2.6.0\protobuf-java-2.6.0.jar;D:\javaEnvironment\repo\org\apache\curator\curator-framework\2.8.0\curator-framework-2.8.0.jar;D:\javaEnvironment\repo\org\apache\curator\curator-client\2.8.0\curator-client-2.8.0.jar;D:\javaEnvironment\repo\com\google\guava\guava\16.0.1\guava-16.0.1.jar;D:\javaEnvironment\repo\org\apache\curator\curator-recipes\2.8.0\curator-recipes-2.8.0.jar
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:java.library.path=D:\java\java1.8\jre1.8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;D:\java\oracle\product\11.2.0\dbhome_1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Common Files\lenovo\easyplussdk\bin;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.2 & MySQL Utilities 1.5.2 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5.2 & MySQL Utilities 1.5.2 1.5\Doctrine extensions for PHP\;D:\java\java1.8\jdk1.8\bin;JAVA_HOME%\jre\bin;D:\java\Z_review\apache-tomcat-7.0.67\bin;D:\我的软件;办公软;\svnServer\bin;%ma;en_home%\bin;D:\我的软件\数据库\putty\;C:\Pro;ram Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;";D:\我的软件\专业软件\zookeeper\zookeeper-3.4.9/bin; D:\我的软件\专业软件\zookeeper\zookeeper-3.4.9/conf";D:\java\java1.8\jdk1.8\bin;C:\Program Files\TortoiseGit\bin;C:\Program Files\Git\cmd;C:\WINDOWS\System32\OpenSSH\;D:\java软件安装资源汇总\maven建项目\apache-maven-3.3.9-bin\apache-maven-3.3.9\bin;D:\protobufc\bin;C:\Users\litan\AppData\Local\Microsoft\WindowsApps;D:\java软件安装资源汇总\2017-6-25\apache-maven-3.3.9-bin\apache-maven-3.3.9\bin;;.
2018-12-02 20:13:20.875 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:java.io.tmpdir=C:\Users\litan\AppData\Local\Temp\
2018-12-02 20:13:20.876 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:java.compiler=<NA>
2018-12-02 20:13:20.876 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:os.name=Windows 10
2018-12-02 20:13:20.876 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:os.arch=amd64
2018-12-02 20:13:20.876 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:os.version=10.0
2018-12-02 20:13:20.876 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:user.name=litan
2018-12-02 20:13:20.876 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:user.home=C:\Users\litan
2018-12-02 20:13:20.876 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Client environment:user.dir=D:\worksheet\sharp-user-service
2018-12-02 20:13:20.877 INFO 32764 --- [ main] org.apache.zookeeper.ZooKeeper : Initiating client connection, connectString=192.168.1.105:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@56a4f272
2018-12-02 20:13:26.570 ERROR 32764 --- [ main] org.apache.curator.ConnectionState : Connection timed out for connection string (192.168.1.105:2181) and timeout (5000) / elapsed (5696) org.apache.curator.CuratorConnectionLossException: KeeperErrorCode = ConnectionLoss
at org.apache.curator.ConnectionState.checkTimeouts(ConnectionState.java:197) [curator-client-2.8.0.jar:na]
at org.apache.curator.ConnectionState.getZooKeeper(ConnectionState.java:87) [curator-client-2.8.0.jar:na]
at org.apache.curator.CuratorZookeeperClient.getZooKeeper(CuratorZookeeperClient.java:115) [curator-client-2.8.0.jar:na]
at org.apache.curator.framework.imps.CuratorFrameworkImpl.getZooKeeper(CuratorFrameworkImpl.java:477) [curator-framework-2.8.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:172) [curator-framework-2.8.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:161) [curator-framework-2.8.0.jar:na]
at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107) [curator-client-2.8.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl.pathInForeground(ExistsBuilderImpl.java:158) [curator-framework-2.8.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:148) [curator-framework-2.8.0.jar:na]
at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:36) [curator-framework-2.8.0.jar:na]
at com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.checkExists(CuratorZookeeperClient.java:117) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.remoting.zookeeper.support.AbstractZookeeperClient.create(AbstractZookeeperClient.java:58) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.doRegister(ZookeeperRegistry.java:114) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.registry.support.FailbackRegistry.register(FailbackRegistry.java:131) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.registry.integration.RegistryProtocol.register(RegistryProtocol.java:125) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:145) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:54) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:98) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:55) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:506) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:358) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:317) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:216) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:123) [dubbo-2.6.2.jar:2.6.2]
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:49) [dubbo-2.6.2.jar:2.6.2]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:886) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
at com.sharp.forward.UserServiceApplication.main(UserServiceApplication.java:14) [classes/:na] 2018-12-02 20:13:32.211 INFO 32764 --- [168.1.105:2181)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server 192.168.1.105/192.168.1.105:2181. Will not attempt to authenticate using SASL (unknown error)
2018-12-02 20:13:32.213 INFO 32764 --- [168.1.105:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established to 192.168.1.105/192.168.1.105:2181, initiating session
2018-12-02 20:13:32.241 INFO 32764 --- [168.1.105:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 192.168.1.105/192.168.1.105:2181, sessionid = 0x10002652c2f0001, negotiated timeout = 40000
2018-12-02 20:13:32.248 INFO 32764 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
2018-12-02 20:13:33.823 INFO 32764 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8886 (http) with context path ''
2018-12-02 20:13:33.830 INFO 32764 --- [ main] c.sharp.forward.UserServiceApplication : Started UserServiceApplication in 22.568 seconds (JVM running for 23.492)

查看dubbo-admin后看到

springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目

服务注册成功,然后就是消费端的架构了

消费者这块不再需要和数据库有任何交互,也不需要jdbc等相关依赖,所以不需要的部分全部去掉即可pom.xml如下:

 <?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">
<modelVersion>4.0.0</modelVersion> <artifactId>sharp-user</artifactId>
<packaging>jar</packaging> <name>sharp-user</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.sharp</groupId>
<artifactId>sharp-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 微服务项目使不需要以下实现类的配置,只需要配置好dubbo即可 -->
<!-- <dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> -->
<!-- <dependency>
<groupId>com.sharp</groupId>
<artifactId>sharp-mapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

application.properties的数据库和mybatis相关注释掉不需要了

 spring.profiles.active=local

 #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://192.168.135.129:3306/sharp
#spring.datasource.username=root
#spring.datasource.password=LCY123456lcy! #mybatis配置
#指定全局配置文件位置
#mybatis.config-location=classpath:mybatis/mybatis-config.xml
##指定别名包
#mybatis.type-aliases-package=com.sharp.forward.entity
##指定xml文件位置
#mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

application-local

spring.profiles.active=local

#mysql连接
server.port=8887

dubbo配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"
default-lazy-init="true">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="user"/>
<!-- use multicast registry center to discover service -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="userService" interface="com.sharp.forward.user.service.UserService"/> </beans>

启动类去掉了扫描mapper无用的e

 package com.sharp.forward;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource; @SpringBootApplication
@ImportResource("config/application-user-dubbo.xml")
@EnableAutoConfiguration
public class SharpUserApplication { public static void main(String[] args) {
SpringApplication.run(SharpUserApplication.class, args);
}
}

结构如下:

springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目

然后启动服务,查看注册情况

springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目

正常是服务提供方和消费方均正常

然后用postman测试也OK

springboot的maven多模块项目架构微服务搭建——依赖方式的多模块演化为微服务项目

上一篇:读书笔记 effective c++ Item 11 在operator=中处理自我赋值


下一篇:springboot的maven多模块项目架构微服务搭建——构建多模块项目(依赖方式)