zhuangjinxin / dubbo Goto Github PK
View Code? Open in Web Editor NEWSpringBoot整合Dubbo框架做分布式微服务项目
SpringBoot整合Dubbo框架做分布式微服务项目
问题背景:
当搭建的基础框架开始考虑到具体业务的时候,如何解决这些数据传递的问题?
当前系统环境:
Github上下载最新版的Dubbo,进入dubbo-admin目录执行打包命令:
mvn package -Dmaven.test.skip=true
报错信息:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.230 s
[INFO] Finished at: 2017-02-22T20:14:31+08:00
[INFO] Final Memory: 10M/133M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project dubbo-admin: Could not resolve dependencies for project com.alibaba:dubbo-admin:war:2.5.4-SNAPSHOT: Could not find artifact com.alibaba:dubbo:jar:2.5.4-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
SpringBoot+Dubbo+Gradle 构建分布式微服务架构
build.gradle依赖关系:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '1.5.1.RELEASE'
compile (group: 'com.alibaba', name: 'dubbo', version: '2.5.3'){
exclude group: 'org.springframework',module:'spring'
}
compile (group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.4.9'){
exclude group: 'org.slf4j',module:'slf4j-log4j12'
exclude group: 'log4j',module:'log4j'
exclude group: 'org.slf4j',module:'slf4j-api'
}
compile (group: 'com.101tec', name: 'zkclient', version: '0.10'){
exclude group: 'org.slf4j',module:'slf4j-api'
}
}
运行项目,有如下警告:
objc[6367]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/java (0x10275d4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1027f44e0). One of the two will be used. Which one is undefined.
Connected to the target VM, address: '127.0.0.1:58118', transport: 'socket'
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/zhuangjinxin/.m2/repository/ch/qos/logback/logback-classic/1.1.9/logback-classic-1.1.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/zhuangjinxin/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.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]
执行gradle clean,gradle build编译报错:
error: package com.sun.org.apache.xml.internal.security.utils does not exist
import com.sun.org.apache.xml.internal.security.utils.Base64;
^
但:右键‘RUN’ 能够启动程序并编译过去
启动程序警告
objc[9709]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/java (0x1041bb4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1042d24e0). One of the two will be used. Which one is undefined.
Connected to the target VM, address: '127.0.0.1:49832', transport: 'socket'
系统重构时,由于历史原因对外提供的接口文档参数都是大写字母开头。比如:
{
"UserNmae":"zhuangjinxin",
"Password":"123456"
}
注意:参数的首字母都是大写的。UserName的U,Password的P。
接收请求的处理方式:
@RestController
public class UserController {
@RequestMapping("/user")
public void userLogin(@RequestBody User user){}
}
注:为什么把参数校验放在实体类User里面,而不是直接放在userLogin()方法的括号内作为参数传进来?
1. 接口对外暴露的时候,规定传JSON格式的数据;
2. 接口参数非常多,如果都写在userLogin()方法的括号内,代码非常乱;
User实体类:
public class User {
private String userName;
private String password;
... setter and getter
}
注意:实体类User里的属性都是小写字母开头的。userName的u,password的p。
这种情况下,调用者传入的JSON格式的数据始终对应不上User实体类的属性值,User实体类中的userName和password一直是null;
在User的属性上加上注解@JsonProperty(value = ""),这样就能接收接口调用者传入的大写参数字段的值。还有一点好处,如果返回实体类User时,又回自动转换成大写参数返回给接口调用者。
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
@JsonProperty(value = "UserName")
private String userName;
@JsonProperty(value = "Password")
private String password;
... setter and getter
}
🔗 项目链接:https://github.com/zhuangjinxin/spring-boot-samples/blob/master/spring-boot-sample-validation
系统重构需要添加http请求日志记录到数据库的功能,以方便查询交易记录。要求每一条记录中包含请求的IP、请求方法(GET/POST)、请求参数、返回数据等。接口请求报文格式为JSON。
日志记录到数据库:
logback-spring.xml
<appender name="db" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
<driverClass>com.mysql.jdbc.Driver</driverClass>
<url>jdbc:mysql://*.*.*.*:3306/database</url>
<user>user</user>
<password>password</password>
</connectionSource>
</appender>
<root level="INFO">
<appender-ref ref="db" />
</root>
引入MySQL JDBC驱动包:
build.gradle
dependencies {
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
}
启动报错:
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.db.DriverManagerConnectionSource@232a7d73 - Could not load JDBC driver class: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
ERROR in ch.qos.logback.core.joran.spi.Interpreter@19:20 - RuntimeException in Action for tag [appender] java.lang.IllegalStateException: DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect
at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:315)
at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:73)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:336)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.lakala.mfs.boot.ApplicationStarter.main(ApplicationStarter.java:23)
在Spring Boot整合CXF webservice时,出现如下错误:
java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/ServletRegistrationBean
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_111]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_111]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_111]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:570) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:697) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:640) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:425) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at sample.ws.WsApplicationStart.main(WsApplicationStart.java:10) [main/:na]
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.embedded.ServletRegistrationBean
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_111]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_111]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_111]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_111]
... 23 common frames omitted
此时build.gradle配置情况如下:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.2.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web-services', version: '1.5.2.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.2.RELEASE'
compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.1.10'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Dubbo Monitor:
打包mvn package -Dmaven.test.skip=true
错误信息如下:
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/zhuangjinxin/Projects/Dubbo/dubbo-master/dubbo-simple/dubbo-monitor-simple/src/main/java/com/alibaba/dubbo/monitor/simple/RegistryContainer.java:[212,64] cannot access org.springframework.context.support.ClassPathXmlApplicationContext
class file for org.springframework.context.support.ClassPathXmlApplicationContext not found
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.123 s
[INFO] Finished at: 2017-02-23T12:45:16+08:00
[INFO] Final Memory: 22M/295M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project dubbo-monitor-simple: Compilation failure
[ERROR] /Users/zhuangjinxin/Projects/Dubbo/dubbo-master/dubbo-simple/dubbo-monitor-simple/src/main/java/com/alibaba/dubbo/monitor/simple/RegistryContainer.java:[212,64] cannot access org.springframework.context.support.ClassPathXmlApplicationContext
[ERROR] class file for org.springframework.context.support.ClassPathXmlApplicationContext not found
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
程序启动失败,报如下错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver class for database type NONE
Action:
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
编译出问题:
process.setReturncode(o.getContext().getModel("_ReturnCode"));
process.setReturnmsg(o.getContext().getModel("_ReturnMsg"));
问题起因:
Dubbo服务生产者(Provider)提供服务,服务消费者(Consumer)作为Web Service层调用Service层的服务,Web Service 与Service分成两个项目分别部署。
解决思路:
服务层通过API的方式暴露服务,把服务的API打包成jar文件。Service层实现服务API,Web Service层通过API调用服务。
如何使用Gradle把服务的API打包成jar文件,并放入本地的.m2仓库中,方便Service层和Web Service层添加依赖。
服务API项目的build.gradle文件中添加如下内容,并使用gradle install
完成打包和添加到仓库
install {
repositories.mavenInstaller {
pom.groupId = "com.zhuangjinxin.dubbo.service"
pom.artifactId = "service-api"
pom.version = "1.0.0"
}
}
Service层和Web Service层在各自的build.gradle文件中添加依赖:
dependencies {
compile group:'com.zhuangjinxin.dubbo.service',name:'service-api',version:'1.0.0'
}
Gradle多模块项目现在打包时,每个模块都会各自的jar包或war包,如何配置gradle文件让其只生成一个可以执行但又包含其他模块的包
集成Oracle数据源,添加Oracle JDBC依赖:
dependencies {
compile group: 'com.oracle', name: 'ojdbc14', version: '10.2.0.4.0'
}
刷新依赖,出现如下警告
Warning:
root project '***': Unable to resolve additional project configuration.
Details: org.gradle.api.artifacts.ResolveException: Could not resolve all dependencies for configuration ':com.***.***.boot:compileClasspath'.
Caused by: org.gradle.internal.resolve.ArtifactNotFoundException: Could not find ojdbc14.jar (com.oracle:ojdbc14:10.2.0.4.0).
Searched in the following locations:
http://repo.spring.io/libs-snapshot/com/oracle/ojdbc14/10.2.0.4.0/ojdbc14-10.2.0.4.0.jar
Warning:
project ':com.***.***.boot': Unable to resolve additional project configuration.
Details: org.gradle.api.artifacts.ResolveException: Could not resolve all dependencies for configuration ':com.***.***.boot:compileClasspath'.
Caused by: org.gradle.internal.resolve.ArtifactNotFoundException: Could not find ojdbc14.jar (com.oracle:ojdbc14:10.2.0.4.0).
Searched in the following locations:
http://repo.spring.io/libs-snapshot/com/oracle/ojdbc14/10.2.0.4.0/ojdbc14-10.2.0.4.0.jar
Warning:
root project '***': Unable to resolve additional project configuration.
Details: org.gradle.api.artifacts.ResolveException: Could not resolve all dependencies for configuration ':com.***.***.boot:compileClasspath'.
Caused by: org.gradle.internal.resolve.ArtifactNotFoundException: Could not find ojdbc14.jar (com.oracle:ojdbc14:10.2.0.4.0).
Searched in the following locations:
http://repo.spring.io/libs-snapshot/com/oracle/ojdbc14/10.2.0.4.0/ojdbc14-10.2.0.4.0.jar
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.