关于maven+dubbo+springboot环境的搭建

来源:互联网 发布:怎样查看追加淘宝评价 编辑:程序博客网 时间:2024/06/05 20:11

大概流程是这样的:

项目A有一个接口Ainterface,想一个接口实现类Aimpl;

项目B要用到Aimpl,但是通过dubbo和maven,可以在只知道接口的情况下实现。

A需要将接口上传到maven私服,通过zookeeper注册实现类。

B导入maven的接口类之后,通过注册zookeeper可以调用Aimpl。

具体代码如下:

A项目的接口

public interface TestDubbo {    String testDubbo(String s);}
A的实现类:
public class TestDubboImpl implements TestDubbo {    @Override    public String testDubbo(String s) {        System.out.println("ssss");        return s+"-------"+s;    }}
A的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>    <artifactId>facade</artifactId>    <groupId>com.example.provider</groupId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <distributionManagement>        <repository>            <id>weidai-releases</id>            <url>http://ip:port/nexus/content/repositories/releases</url>        </repository>        <snapshotRepository>            <id>weidai-snapshots</id>            <url>http://ip:port/nexus/content/repositories/snapshots</url>        </snapshotRepository>    </distributionManagement></project>
A的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:application name="demo-provider" />    <!--使用zookeeper注册中心暴露服务地址-->    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />    <!--用dubbo协议在20880端口暴露服务-->    <dubbo:protocol name="dubbo" port="20880" />    <!--自己的测试服务-->    <dubbo:service interface="com.facade.TestDubbo" ref="TestDubboImpl"/>    <bean id="TestDubboImpl" class="server.TestDubboImpl" /></beans>

B的maven   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.example.consumer</groupId><artifactId>democ</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><version>2.5.3</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>com.example.provider</groupId><artifactId>facade</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

B的consumer.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="demo-consumer" />    <!--使用zookeeper注册中心暴露服务地址-->    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />    <!--用dubbo协议在20880端口暴露服务-->    <dubbo:protocol name="dubbo" port="20880" />    <!--自己的测试服务-->    <dubbo:reference interface="com.facade.TestDubbo" id="TestDubbo"/></beans>
B中的引用:
package com.example.demo.controller;import com.facade.TestDubbo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;/** * Created by chenhaitao on 2017/6/14. */@RestControllerpublic class TestController {    @Resource    private TestDubbo testDubbo;    @RequestMapping(value = "no2")    public String test(){        return testDubbo.testDubbo("头尾");    }}

原创粉丝点击