1. 程式人生 > >springboot的maven多模組專案架構微服務搭建——依賴方式的多模組演化為微服務專案

springboot的maven多模組專案架構微服務搭建——依賴方式的多模組演化為微服務專案

在上一篇依賴方式多模組的基礎上對專案進行改造。主要改造user-service專案,service要配置mapper。mybatis及資料庫相關的東西,後面的介面消費方user就不再需要了

注意:以下程式碼是在不同場所的機器上寫的,資料庫什麼的會有不同,結構也會有不同,最終的程式碼會以其中一個傳遞到本人git上,這裡記錄的是本人總結的一些思路什麼的,稍微修改配置,配置一致,就可以執行的

程式碼如下:

pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <parent> 7 <groupId>com.sharp</groupId> 8 <artifactId>sharp-pom</
artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 <artifactId>sharp-user-service</artifactId> 12 <packaging>jar</packaging> 13 14 <name>sharp-user-service</name> 15 <description>Forward project for Spring Boot</
description> 16 <dependencies> 17 <dependency> 18 <groupId>com.sharp</groupId> 19 <artifactId>sharp-common</artifactId> 20 <version>${project.parent.version}</version> 21 </dependency> 22 <dependency> 23 <groupId>com.sharp</groupId> 24 <artifactId>sharp-entity</artifactId> 25 <version>${project.parent.version}</version> 26 </dependency> 27 <dependency> 28 <groupId>com.sharp</groupId> 29 <artifactId>sharp-mapper</artifactId> 30 <version>${project.parent.version}</version> 31 </dependency> 32 <dependency> 33 <groupId>com.sharp</groupId> 34 <artifactId>sharp-user-service-api</artifactId> 35 <version>${project.parent.version}</version> 36 </dependency> 37 <dependency> 38 <groupId>org.mybatis.spring.boot</groupId> 39 <artifactId>mybatis-spring-boot-starter</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>org.apache.zookeeper</groupId> 43 <artifactId>zookeeper</artifactId> 44 </dependency> 45 <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo --> 46 <dependency> 47 <groupId>com.alibaba</groupId> 48 <artifactId>dubbo</artifactId> 49 <version>2.6.2</version> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-jdbc</artifactId> 54 </dependency> 55 <dependency> 56 <groupId>mysql</groupId> 57 <artifactId>mysql-connector-java</artifactId> 58 <scope>runtime</scope> 59 </dependency> 60 <dependency> 61 <groupId>org.apache.curator</groupId> 62 <artifactId>curator-framework</artifactId> 63 <version>2.8.0</version> 64 </dependency> 65 <dependency> 66 <groupId>org.apache.curator</groupId> 67 <artifactId>curator-recipes</artifactId> 68 <version>2.8.0</version> 69 </dependency> 70 71 </dependencies> 72 73 <build> 74 <plugins> 75 <plugin> 76 <groupId>org.springframework.boot</groupId> 77 <artifactId>spring-boot-maven-plugin</artifactId> 78 </plugin> 79 <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin --> 80 <plugin> 81 <groupId>org.apache.maven.plugins</groupId> 82 <artifactId>maven-surefire-plugin</artifactId> 83 <version>2.21.0</version> 84 <configuration> 85 <skipTests>true</skipTests> 86 </configuration> 87 </plugin> 88 </plugins> 89 90 </build> 91 92 93 </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);
    }
}

結構

執行後

  1 SLF4J: Class path contains multiple SLF4J bindings.
  2 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]
  3 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]
  4 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
  5 SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
  6 
  7   .   ____          _            __ _ _
  8  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
  9 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 10  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 11   '  |____| .__|_| |_|_| |_\__, | / / / /
 12  =========|_|==============|___/=/_/_/_/
 13  :: Spring Boot ::        (v2.0.6.RELEASE)
 14 
 15 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)
 16 2018-12-02 20:13:12.241  INFO 32764 --- [           main] c.sharp.forward.UserServiceApplication   : The following profiles are active: local
 17 2018-12-02 20:13:12.353  INFO 32764 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.ser[email protected]49139829: startup date [Sun Dec 02 20:13:12 CST 2018]; root of context hierarchy
 18 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]
 19 log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
 20 log4j:WARN Please initialize the log4j system properly.
 21 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
 22 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]]
 23 2018-12-02 20:13:15.970  INFO 32764 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8886 (http)
 24 2018-12-02 20:13:16.059  INFO 32764 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
 25 2018-12-02 20:13:16.059  INFO 32764 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
 26 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;;.]
 27 2018-12-02 20:13:16.372  INFO 32764 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
 28 2018-12-02 20:13:16.373  INFO 32764 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4024 ms
 29 2018-12-02 20:13:16.532  INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
 30 2018-12-02 20:13:16.634  INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
 31 2018-12-02 20:13:16.635  INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
 32 2018-12-02 20:13:16.635  INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
 33 2018-12-02 20:13:16.635  INFO 32764 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
 34 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.
 35 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]
 36 2018-12-02 20:13:18.731  INFO 32764 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.ser[email protected]49139829: startup date [Sun Dec 02 20:13:12 CST 2018]; root of context hierarchy
 37 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)
 38 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)
 39 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]
 40 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]
 41 2018-12-02 20:13:19.490  INFO 32764 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
 42 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
 43 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]
 44 2018-12-02 20:13:20.860  INFO 32764 --- [           main] o.a.c.f.imps.CuratorFrameworkImpl        : Starting
 45 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
 46 2018-12-02 20:13:20.875  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:host.name=windows10.microdone.cn
 47 2018-12-02 20:13:20.875  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.version=1.8.0_161
 48 2018-12-02 20:13:20.875  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.vendor=Oracle Corporation
 49 2018-12-02 20:13:20.875  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.home=D:\java\java1.8\jre1.8
 50 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
 51 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;;.
 52 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\
 53 2018-12-02 20:13:20.876  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:java.compiler=<NA>
 54 2018-12-02 20:13:20.876  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.name=Windows 10
 55 2018-12-02 20:13:20.876  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.arch=amd64
 56 2018-12-02 20:13:20.876  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:os.version=10.0
 57 2018-12-02 20:13:20.876  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.name=litan
 58 2018-12-02 20:13:20.876  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.home=C:\Users\litan
 59 2018-12-02 20:13:20.876  INFO 32764 --- [           main] org.apache.zookeeper.ZooKeeper           : Client environment:user.dir=D:\worksheet\sharp-user-service
 60 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 [email protected]
 61 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)
 62 
 63 org.apache.curator.CuratorConnectionLossException: KeeperErrorCode = ConnectionLoss
 64     at org.apache.curator.ConnectionState.checkTimeouts(ConnectionState.java:197) [curator-client-2.8.0.jar:na]
 65     at org.apache.curator.ConnectionState.getZooKeeper(ConnectionState.java:87) [curator-client-2.8.0.jar:na]
 66     at org.apache.curator.CuratorZookeeperClient.getZooKeeper(CuratorZookeeperClient.java:115) [curator-client-2.8.0.jar:na]
 67     at org.apache.curator.framework.imps.CuratorFrameworkImpl.getZooKeeper(CuratorFrameworkImpl.java:477) [curator-framework-2.8.0.jar:na]
 68     at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:172) [curator-framework-2.8.0.jar:na]
 69     at org.apache.curator.framework.imps.ExistsBuilderImpl$2.call(ExistsBuilderImpl.java:161) [curator-framework-2.8.0.jar:na]
 70     at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107) [curator-client-2.8.0.jar:na]
 71     at org.apache.curator.framework.imps.ExistsBuilderImpl.pathInForeground(ExistsBuilderImpl.java:158) [curator-framework-2.8.0.jar:na]
 72     at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:148) [curator-framework-2.8.0.jar:na]
 73     at org.apache.curator.framework.imps.ExistsBuilderImpl.forPath(ExistsBuilderImpl.java:36) [curator-framework-2.8.0.jar:na]
 74     at com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.checkExists(CuratorZookeeperClient.java:117) [dubbo-2.6.2.jar:2.6.2]
 75     at com.alibaba.dubbo.remoting.zookeeper.support.AbstractZookeeperClient.create(AbstractZookeeperClient.java:58) [dubbo-2.6.2.jar:2.6.2]
 76     at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.doRegister(ZookeeperRegistry.java:114) [dubbo-2.6.2.jar:2.6.2]
 77     at com.alibaba.dubbo.registry.support.FailbackRegistry.register(FailbackRegistry.java:131) [dubbo-2.6.2.jar:2.6.2]
 78     at com.alibaba.dubbo.registry.integration.RegistryProtocol.register(RegistryProtocol.java:125) [dubbo-2.6.2.jar:2.6.2]
 79     at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:145) [dubbo-2.6.2.jar:2.6.2]
 80     at com.alibaba.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:54) [dubbo-2.6.2.jar:2.6.2]
 81     at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:98) [dubbo-2.6.2.jar:2.6.2]
 82     at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:55) [dubbo-2.6.2.jar:2.6.2]
 83     at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) [dubbo-2.6.2.jar:2.6.2]
 84     at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:506) [dubbo-2.6.2.jar:2.6.2]
 85     at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:358) [dubbo-2.6.2.jar:2.6.2]
 86     at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:317) [dubbo-2.6.2.jar:2.6.2]
 87     at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:216) [dubbo-2.6.2.jar:2.6.2]
 88     at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:123) [dubbo-2.6.2.jar:2.6.2]
 89     at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:49) [dubbo-2.6.2.jar:2.6.2]
 90     at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
 91     at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
 92     at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
 93     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
 94     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
 95     at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:886) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
 96     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
 97     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) [spring-context-5.0.10.RELEASE.jar:5.0.10.RELEASE]
 98     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
 99     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
100     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
101     at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
102     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
103     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.6.RELEASE.jar:2.0.6.RELEASE]
104     at com.sharp.forward.UserServiceApplication.main(UserServiceApplication.java:14) [classes/:na]
105 
106 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)
107 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
108 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
109 2018-12-02 20:13:32.248  INFO 32764 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager     : State change: CONNECTED
110 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 ''
111 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)
View Code

檢視dubbo-admin後看到

服務註冊成功,然後就是消費端的架構了

消費者這塊不再需要和資料庫有任何互動,也不需要jdbc等相關依賴,所以不需要的部分全部去掉即可pom.xml如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <artifactId>sharp-user</artifactId>
 7     <packaging>jar</packaging>
 8 
 9     <name>sharp-user</name>
10     <description>Demo project for Spring Boot</description>
11 
12     <parent>
13         <groupId>com.sharp</groupId>
14     <artifactId>sharp-pom</artifactId>
15     <version>0.0.1-SNAPSHOT</version>
16     </parent>
17 
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-web</artifactId>
23         </dependency>
24         <dependency>
25             <groupId>com.sharp</groupId>
26             <artifactId>sharp-entity</artifactId>
27             <version>0.0.1-SNAPSHOT</version>
28         </dependency>
29         <dependency>
30             <groupId>com.sharp</groupId>
31             <artifactId>sharp-common</artifactId>
32             <version>0.0.1-SNAPSHOT</version>
33         </dependency>
34         <dependency>
35             <groupId>com.sharp</groupId>
36             <artifactId>sharp-service-api</artifactId>
37             <version>0.0.1-SNAPSHOT</version>
38         </dependency>
39         <!-- 微服務專案使不需要以下實現類的配置,只需要配置好dubbo即可 -->
40 <!--         <dependency>
41             <groupId>com.sharp</groupId>
42             <artifactId>sharp-service</artifactId>
43             <version>0.0.1-SNAPSHOT</version>
44         </dependency> -->
45     <!--     <dependency>
46             <groupId>com.sharp</groupId>
47             <artifactId>sharp-mapper</artifactId>
48             <version>0.0.1-SNAPSHOT</version>
49         </dependency> -->
50         <dependency>
51             <groupId>org.springframework.boot</groupId>
52             <artifactId>spring-boot-starter-test</artifactId>
53             <scope>test</scope>
54         </dependency>
55         <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
56         <dependency>
57             <groupId>com.alibaba</groupId>
58             <artifactId>dubbo</artifactId>
59             <exclusions>
60                 <exclusion>
61                     <groupId>org.springframework</groupId>
62                     <artifactId>spring</artifactId>
63                 </exclusion>
64             </exclusions>
65         </dependency>
66         <dependency>
67               <groupId>com.101tec</groupId>
68               <artifactId>zkclient</artifactId>
69               <version>0.9</version>
70         </dependency>
71 
72     </dependencies>
73 
74     <build>
75         <plugins>
76             <plugin>
77                 <groupId>org.springframework.boot</groupId>
78                 <artifactId>spring-boot-maven-plugin</artifactId>
79             </plugin>
80         </plugins>
81     </build>
82 
83 
84 </project>

application.properties的資料庫和mybatis相關注釋掉不需要了

 1 spring.profiles.active=local
 2 
 3 #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 4 #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 5 #spring.datasource.url=jdbc:mysql://192.168.135.129:3306/sharp
 6 #spring.datasource.username=root
 7 #spring.datasource.password=LCY123456lcy!
 8 
 9 #mybatis配置
10 #指定全域性配置檔案位置
11 #mybatis.config-location=classpath:mybatis/mybatis-config.xml
12 ##指定別名包
13 #mybatis.type-aliases-package=com.sharp.forward.entity
14 ##指定xml檔案位置
15 #mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

application-local

spring.profiles.active=local

#mysql連線
server.port=8887

dubbo配置

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

啟動類去掉了掃描mapper無用的e

 1 package com.sharp.forward;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.context.annotation.ImportResource;
 7 
 8 @SpringBootApplication
 9 @ImportResource("config/application-user-dubbo.xml")
10 @EnableAutoConfiguration
11 public class SharpUserApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(SharpUserApplication.class, args);
15     }
16 }

結構如下:

然後啟動服務,檢視註冊情況

正常是服務提供方和消費方均正常

然後用postman測試也OK