關於maven+dubbo+springboot環境的搭建

大概流程是這樣的:

項目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.
 */
@RestController
public class TestController {
    @Resource
    private TestDubbo testDubbo;

    @RequestMapping(value = "no2")
    public String test(){
        return testDubbo.testDubbo("頭尾");
    }
}

發佈了80 篇原創文章 · 獲贊 25 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章