java编写简单的dubbo提供者

Wesley13
• 阅读 495

1.先建一个项目

java编写简单的dubbo提供者

pom.xml文件

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.guilf</groupId>
  5     <artifactId>com-guilf-provider-service</artifactId>
  6     <version>0.0.1-SNAPSHOT</version>
  7     
  8     <properties>
  9         <slf4j.version>1.6.6</slf4j.version>
 10         <log4j.version>1.2.9</log4j.version>
 11     </properties>
 12 
 13     <dependencies>
 14     <dependency>
 15             <groupId>com.guilf</groupId>
 16             <artifactId>com.guilf.api</artifactId>
 17             <version>0.0.1-SNAPSHOT</version>
 18         </dependency>
 19         <!-- dubbo所需要的包 -->
 20         <dependency>
 21             <groupId>com.alibaba</groupId>
 22             <artifactId>dubbo</artifactId>
 23             <version>2.5.3</version>
 24         </dependency>
 25 
 26         <dependency>
 27             <groupId>org.apache.zookeeper</groupId>
 28             <artifactId>zookeeper</artifactId>
 29             <version>3.4.5</version>
 30         </dependency>
 31 
 32         <dependency>
 33             <groupId>com.github.sgroschupf</groupId>
 34             <artifactId>zkclient</artifactId>
 35             <version>0.1</version>
 36         </dependency>
 37         
 38         <!-- log start -->
 39         <dependency>
 40             <groupId>log4j</groupId>
 41             <artifactId>log4j</artifactId>
 42             <version>${log4j.version}</version>
 43         </dependency>
 44         <dependency>
 45             <groupId>org.slf4j</groupId>
 46             <artifactId>slf4j-api</artifactId>
 47             <version>${slf4j.version}</version>
 48         </dependency>
 49         <dependency>
 50             <groupId>org.slf4j</groupId>
 51             <artifactId>slf4j-log4j12</artifactId>
 52             <version>${slf4j.version}</version>
 53         </dependency>
 54     </dependencies>
 55     <!-- maven打包用的 -->
 56     <build>
 57         <plugins>
 58             <plugin>
 59                 <groupId>org.apache.maven.plugins</groupId>
 60                 <artifactId>maven-compiler-plugin</artifactId>
 61                 <configuration>
 62                     <source>1.5</source>
 63                     <target>1.5</target>
 64                 </configuration>
 65             </plugin>
 66 
 67             <plugin>
 68                 <groupId>org.apache.maven.plugins</groupId>
 69                 <artifactId>maven-source-plugin</artifactId>
 70                 <version>2.1.2</version>
 71                 <executions>
 72                     <execution>
 73                         <id>attach-sources</id>
 74                         <phase>verify</phase>
 75                         <goals>
 76                             <goal>jar-no-fork</goal>
 77                         </goals>
 78                     </execution>
 79                 </executions>
 80             </plugin>
 81 
 82             <plugin>
 83                 <groupId>org.apache.maven.plugins</groupId>
 84                 <artifactId>maven-shade-plugin</artifactId>
 85                 <version>2.3</version>
 86                 <executions>
 87                     <execution>
 88                         <phase>package</phase>
 89                         <goals>
 90                             <goal>shade</goal>
 91                         </goals>
 92                         <configuration>
 93                             <transformers>
 94                                 <transformer
 95                                     implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
 96                                     <mainClass>com.guilf.service.impl.TestMain</mainClass>
 97                                 </transformer>
 98                                 <transformer
 99                                     implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
100                                     <resource>META-INF/spring.handlers</resource>
101                                 </transformer>
102                                 <transformer
103                                     implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
104                                     <resource>META-INF/spring.schemas</resource>
105                                 </transformer>
106                             </transformers>
107                         </configuration>
108                     </execution>
109                 </executions>
110             </plugin>
111         </plugins>
112     </build>
113 
114 </project>

dubbo-provider.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管理页面比较清晰是哪个应用暴露出来的 -->
   <dubbo:application name="dubbo_provider"/>
   <dubbo:protocol name="dubbo" port="20880"/>
   <!-- 使用zookeeper注册中心暴露服务地址 -->  
   <dubbo:registry address="zookeeper://192.168.88.134:2181?backup=192.168.88.134:2182,192.168.88.134:2183" check="false"/>
  <!-- 要暴露的服务接口 -->  
  <dubbo:service  interface="com.guilf.service.api.GuilfDubboServiceApi" ref="guilfDubboServiceApi"/>
   <!--具体实现该接口的 bean-->
    <bean id="guilfDubboServiceApi" class="com.guilf.service.impl.GuilfDubboServiceImpl"/>
</beans>

GuilfDubboServiceImpl.java

package com.guilf.service.impl;

import com.guilf.service.api.GuilfDubboServiceApi;

public class GuilfDubboServiceImpl implements GuilfDubboServiceApi
{
    public String findUser(String name, String password) {
        return "======name:"+name+"=====password:"+password;
    }
}

 GuilfDubboServiceApi.java

package com.guilf.service.api;

public interface GuilfDubboServiceApi
{
    public String findUser(String name, String password);
}

 TestMain.java

package com.guilf.service.impl;
import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

    public static void main(String[] args) {
        // 加载dubbo-provider.xml,
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(    
                new String[]{"classpath:conf/dubbo-provider.xml"});
        // 启动
        context.start();  
        System.out.println("Press any key to exit.");    
        try {
            // 按任意键退出
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

 启动

java编写简单的dubbo提供者

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
执键写春秋 执键写春秋
3年前
基于Maven工程下的MyBatis框架+MySQL+连接池的数据查询操作
具体操作项目结构引入项目依赖pom.xml<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
springboot2.1整合mybatis
1:添加依赖<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Spring boot 配置异步处理执行器
示例如下:1、新建Maven项目 asyncexecutor2、pom.xml<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:sch
Stella981 Stella981
3年前
Mybatis自动生成实体类
Maven自动生成实体类需要的jar包一、pom.xml中1<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:schemaLocation"http://maven.a
Stella981 Stella981
3年前
SpringBoot2.1.6 + Shiro1.4.1 + Thymeleaf + Jpa整合练习
  首先,添加maven依赖,完整的pom文件如下:1<?xmlversion"1.0"encoding"UTF8"?2<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema
Stella981 Stella981
3年前
Maven系列
<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:schemaLocation"http://maven.apache.org/POM/4.0.0http://ma
Stella981 Stella981
3年前
Spring Boot 集成log4j2日志
一、pom文件配置<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:schemaLocation"http://maven.apache.org/POM/4.0.0h