欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

springboot整合dubbo

时间:2023-08-03
pom.xml

org.springframework.boot spring-boot-starter-parent 2.6.3 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.apache.dubbo dubbo-spring-boot-starter 2.7.6

dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8" ?>

dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8" ?>

service

public interface DemoService { String sayHello(String name);}

import com.example.springbootdubbo.service.DemoService;import org.apache.dubbo.rpc.RpcContext;import java.text.SimpleDateFormat;import java.util.Date;public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { System.out.println("["+new SimpleDateFormat("HH:mm:ss").format(new Date()) +"] " + "Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hellot" + name + ",tresponse from provider:t" + RpcContext.getContext().getLocalAddress(); }}

服务提供方

import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.concurrent.CountDownLatch;public class DemoProvider { public static void main(String[] args) throws InterruptedException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml"); context.start(); System.out.println("dubbo service started"); new CountDownLatch(1).await(); }}

服务消费方

import com.example.springbootdubbo.service.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;public class DemoConsumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml"); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); String hello = demoService.sayHello("world"); System.out.println(hello); }}

本来是准备用spring直接搭dubbo的,但网上没找到spring5.x版本和dubbo的对应版本,就准备先用springboot和dubbo的搭配启起来后,再用对应的依赖版本整spring与dubbo的搭配,目前还是失败,先记录下,后续看问题所在

二月 03, 2022 9:19:22 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@79fc0f2f: startup date [Thu Feb 03 09:19:22 CST 2022]; root of context hierarchyException in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/ErrorCodedat java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java:763)at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)at java.net.URLClassLoader.access$100(URLClassLoader.java:74)at java.net.URLClassLoader$1.run(URLClassLoader.java:369)at java.net.URLClassLoader$1.run(URLClassLoader.java:363)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:362)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)at java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java:763)at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)at java.net.URLClassLoader.access$100(URLClassLoader.java:74)at java.net.URLClassLoader$1.run(URLClassLoader.java:369)at java.net.URLClassLoader$1.run(URLClassLoader.java:363)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:362)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:200)at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:126)at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)at com.example.rpcdemo.DemoProvider.main(DemoProvider.java:15)Caused by: java.lang.ClassNotFoundException: org.springframework.core.ErrorCodedat java.net.URLClassLoader.findClass(URLClassLoader.java:382)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)..、31 more

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。