分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

基于url发布无注册中心dubbo服务demo

发布时间:2023-09-06 02:24责任编辑:苏小强关键词:url

1:创建dubbo-server maven工程

2:分别在dubbo-server下创建2个模块server-api(定义接口)和server-provider(实现接口)

   server-api:

package com.cn.dubbo;

public interface IGpHello {

???String sayHello(String msg);

}

server-provider:
package com.cn.dubbo;

public class GpHelloImpl implements IGpHello {
???public String sayHello(String msg) {
???????return "hello yulong "+msg;
???}
}

package com.cn.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Bootstrap {
???public static void main(String[] args) throws IOException {
???????ClassPathXmlApplicationContext context=
???????????????new ClassPathXmlApplicationContext("META-INF/spring/dubbo-server.xml");
???????context.start();
???????System.out.println("服务启动成功");
???????System.in.read();//阻塞进程
???}
}
server-provider pom.xml 引入dubbo等jar包
<?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>


???<groupId>com.cn.dubbo</groupId>
???<artifactId>dubbo-client</artifactId>
???<version>1.0-SNAPSHOT</version>
???<url>http://www.example.com</url>

???<dependencies>
???????<dependency>
???????????<groupId>com.cn.dubbo</groupId>
???????????<artifactId>server-api</artifactId>
???????????<version>1.0-SNAPSHOT</version>
???????</dependency>

???????<dependency>
???????????<groupId>com.alibaba</groupId>
???????????<artifactId>dubbo</artifactId>
???????????<version>2.5.6</version>
???????</dependency>
???????<dependency>
???????????<groupId>org.apache.zookeeper</groupId>
???????????<artifactId>zookeeper</artifactId>
???????????<version>3.4.10</version>
???????</dependency>
???????<dependency>
???????????<groupId>com.101tec</groupId>
???????????<artifactId>zkclient</artifactId>
???????????<version>0.10</version>
???????</dependency>
???????<dependency>
???????????<groupId>com.caucho</groupId>
???????????<artifactId>hessian</artifactId>
???????????<version>4.0.38</version>
???????</dependency>
???????<dependency>
???????????<groupId>javax.servlet</groupId>
???????????<artifactId>servlet-api</artifactId>
???????????<version>3.0.1</version>
???????</dependency>
???????<dependency>
???????????<groupId>org.mortbay.jetty</groupId>
???????????<artifactId>jetty</artifactId>
???????????<version>6.1.26</version>
???????</dependency>

???</dependencies>

</project>

dubbo-server.xml
<?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">

???<!--提供方信息-->
???<dubbo:application name="dubbo-server" owner="yulong" />
???<!--注册中心-->
???<dubbo:registry address="N/A"/>

???<dubbo:service interface="com.cn.dubbo.IGpHello" ref="gpHelloService"/>

???<bean id="gpHelloService" class="com.cn.dubbo.GpHelloImpl" />



</beans>
3:idea工具mavenprojects 把dubbo-server打成jar包生成到本地,一会客户端引用jar
4:新建dubbo-client工程
5:bubbo-client-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>


???<groupId>com.cn.dubbo</groupId>
???<artifactId>dubbo-client</artifactId>
???<version>1.0-SNAPSHOT</version>
???<url>http://www.example.com</url>

???<dependencies>
???????<dependency>
???????????<groupId>com.cn.dubbo</groupId>
???????????<artifactId>server-api</artifactId>
???????????<version>1.0-SNAPSHOT</version>
???????</dependency>

???????<dependency>
???????????<groupId>com.alibaba</groupId>
???????????<artifactId>dubbo</artifactId>
???????????<version>2.5.6</version>
???????</dependency>
???????<dependency>
???????????<groupId>org.apache.zookeeper</groupId>
???????????<artifactId>zookeeper</artifactId>
???????????<version>3.4.10</version>
???????</dependency>
???????<dependency>
???????????<groupId>com.101tec</groupId>
???????????<artifactId>zkclient</artifactId>
???????????<version>0.10</version>
???????</dependency>
???????<dependency>
???????????<groupId>com.caucho</groupId>
???????????<artifactId>hessian</artifactId>
???????????<version>4.0.38</version>
???????</dependency>
???????<dependency>
???????????<groupId>javax.servlet</groupId>
???????????<artifactId>servlet-api</artifactId>
???????????<version>3.0.1</version>
???????</dependency>
???????<dependency>
???????????<groupId>org.mortbay.jetty</groupId>
???????????<artifactId>jetty</artifactId>
???????????<version>6.1.26</version>
???????</dependency>

???</dependencies>

</project>

6:dubbo-client.xml
<?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">

???<!--提供方信息-->
???<dubbo:application name="dubbo-client" owner="yulong" />
???<!--注册中心-->
???<dubbo:registry address="N/A"/>

???<dubbo:reference id="helloService" ?interface="com.cn.dubbo.IGpHello"
???url="dubbo://127.0.0.1:20880/com.cn.dubbo.IGpHello"
???/>



</beans>

package com.cn.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
???public static void main(String[] args) {

???????ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("dubbo-client.xml");
???????IGpHello iGpHello = (IGpHello) context.getBean("helloService");
???????System.out.println(iGpHello.sayHello("yulong"));
???}
}
控制台:


log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
hello yulong yulong

Process finished with exit code 0


















基于url发布无注册中心dubbo服务demo

原文地址:https://www.cnblogs.com/Yulong123/p/10037099.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved