dubbo+Springboot的实现

一、简介
1、现在dubbo成为了apache的顶级项目,阿里巴巴重新开源了dubbo项目,为了兼容性更强、更完善,添加了dubbo-spring-boot-starter 的依赖库,同时提高了快速开发效率。
2、首先我们创建三个项目dubbo-openapi、dubbox-consumer、dubbox-provider、dubbo-zookeeper。
3、
dubbo-openapi:主要的提供对象属性与方法、其实意思上:抽取dubbox-consumer、dubbox-provider服务中相同的代码。
dubbo-consumer:客户端
dubbo-provider:服务端
dubbo-zookeeper:注册中心
二、搭建项目
1、搭建dubbo-openapi、创建maven的jar项目
dubbo+Springboot的实现_第1张图片
pom.xml


  4.0.0

  com.citydo
  dubbo-openapi
  0.0.1-SNAPSHOT
  jar

  dubbo-openapi
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
  

User.java

package com.citydo.dubbo_openapi.model;

import java.io.Serializable;

public class User implements Serializable{
	
	 private String name;
	 private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User[name=" + name + ", age=" + age + "]";
	}
	public User(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
}

UserService.java

package com.citydo.dubbo_openapi.service;

import com.citydo.dubbo_openapi.model.User;

public interface UserService {
	
	User getUser();

}

2、搭建服务端dubbox-consumer、采用Springboot框架
dubbo+Springboot的实现_第2张图片
pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.example
	dubbox-consumer
	0.0.1-SNAPSHOT
	dubbox-consumer
	jar
	Demo project for Spring Boot

	
	UTF-8
		UTF-8
		1.8
	

	
	    
	    
			 com.citydo
             dubbo-openapi
             0.0.1-SNAPSHOT
		
	    
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter
		
		
		
			com.alibaba.boot
			dubbo-spring-boot-starter
			0.2.0
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

application.properties

# Spring boot application
spring.application.name = dubbox-consumer
server.port = 9090
management.port = 9091


# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbox-consumer
dubbo.application.name = dubbox-consumer
dubbo.scan.base-packages=com.citydo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

3、搭建客户端端dubbox-provider、采用Springboot框架
dubbo+Springboot的实现_第3张图片
pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.example
	dubbox-provider
	0.0.1-SNAPSHOT
	dubbox-provider
	jar
	Demo project for Spring Boot

	
		1.8
	

	
	
	     
			 com.citydo
             dubbo-openapi
             0.0.1-SNAPSHOT
		
	    
	   
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-aop
		

		
			com.alibaba.boot
			dubbo-spring-boot-starter
			0.2.0
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

application.properties

# Spring boot application
spring.application.name = dubbox-provider
server.port = 7070
management.port = 7071

# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
dubbo.scan.basePackages  =com.citydo.impl

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbox-provider
dubbo.application.name = dubbox-provider

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

## RegistryConfig Bean
dubbo.registry.id = my-registry
dubbo.registry.address = N/A

UserServiceImpl.java

package com.citydo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.citydo.dubbo_openapi.model.User;
import com.citydo.dubbo_openapi.service.UserService;

@Service(
        version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"

)
public class UserServiceImpl implements UserService {

	@Override
	public User getUser() {
		// TODO Auto-generated method stub
		return new User("张三", 19);
	}

}

三、dubbo与中间件的使用
1、zookeeper
https://blog.csdn.net/qq_32447301/article/details/87892621
2、netty
待续
3、admin
待续
四、错误总结
1、

    Exception in thread "main" java.lang.UnsupportedClassVersionError: com/citydo/DubboxConsumerApplication : Unsupported major.minor version 52.0
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    	at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    	at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
解决办法:
看报错信息,是因为使用的jar包版本不对。这个52.0是什么呢?
J2SE 8 = 52

2、

2019-02-15 09:43:29 ERROR [o.s.w.c.ContextLoader] [] [] [] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [beans-dao.xml]: Initialization of bean failed;
nested exception is java.lang.reflect.MalformedParameterizedTypeException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)

解决办法:换JDK或者jar包依赖冲突

3、

Caused by: java.lang.IllegalArgumentException: No enum constant

解决办法:springboot项目中需要在application.properties或者枚举的数据不能为空

4、解决Tomcat启动时项目重复加载问题
寻找tomcat的目录下的server.xml、修改配置

		        
                 
                 
                 
	            

5、解决has been blocked by CORS policy: Response to preflight request doesn't pass access control
在nginx跨域请求cors配置如下:

     location / {
                    add_header         'Access-Control-Allow-Origin' '*';
                    add_header         "Access-Control-Allow-Credentials" "true";
                    add_header         "Access-Control-Allow-Headers" "x-requested-with,content-type";
                    proxy_pass         http://localhost:8080;
               }

4、参考代码:https://github.com/ibywind/dubbo-learn

你可能感兴趣的:(dubbo和dubbox)