SpringBoot+Maven+Dubbo+Zookeeper+CentOS7 项目搭建与发布到阿里云服务器

      由于最近项目要用到dubbo+zookeeper。所以就在网上找了一些例子来看,试着自己搭建了一个项目,并放到了服务器上,大家有兴趣可以访问。这篇文章主要记录项目搭建以及部署过程中踩过的坑,希望能帮到你。。。

项目结构如下:

项目目录结构

项目是idea新建一个简单springboot项目,然后在这个项目基础上新建两个module项目:

springboot-dubbo-provider项目:

pom文件:

<?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.wty.DubboWty</groupId>
	<artifactId>dubboclient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>dubboclient</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.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-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--dubbo-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.4.10</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
			<exclusions>
				<exclusion>
					<artifactId>slf4j-log4j12</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>








	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
			</plugin>

		</plugins>

	</build>


</project>

注:这里有个坑,就是最后打包的时候,必须加上

<configuration>
    <classifier>exec</classifier>
</configuration>

,如果不加,本地跑是没有问题的,但是打包发布服务的时候就有问题了,具体什么意思,可以看这篇文章:

        https://blog.csdn.net/guduyishuai/article/details/60968728

 

接口类:

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

实现类:

@Service
public class SayHelloImpl implements SayHello{
    @Override
    public String sayHello(String name) {
        System.out.println("service 被调用了");
        return "Hello "+name;
    }
}

项目resources目录下新建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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- 配置dubbo -->
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="taotao-manager-service" />

    <!-- 使用multicast广播注册中心暴露服务地址 不安全,不稳定 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
    <!--直连-->
    <!--<dubbo:registry address="N/A" />-->
<dubbo:registry protocol="zookeeper" address="你部署的zookeeper服务器的ip:2181" check="false" subscribe="false"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.wty.dubbo.springbootdubboprovider.Service.SayHello"
                   ref="sayHello" />
    <!-- 具体服务接口的实现 -->
    <bean id="sayHello" class="com.wty.dubbo.springbootdubboprovider.Service.ServiceImpl.SayHelloImpl" />
</beans>

启动类加上注解

@ImportResource(value = {"classpath:dubbo-provider.xml"})扫描配置文件:
@SpringBootApplication
@ImportResource(value = {"classpath:dubbo-provider.xml"})
public class SpringbootDubboProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootDubboProviderApplication.class, args);
	}
}

 

 

springboot-dubbo-consumer项目:

pom文件:

<?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.wty.dubbo</groupId>
	<artifactId>springboot-dubbo-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot-dubbo-consumer</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.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-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
        <dependency>
            <groupId>com.wty.dubbo</groupId>
            <artifactId>springboot-dubbo-provider</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
		<!--<dependency>-->
			<!--<groupId>com.wty.dubbo</groupId>-->
			<!--<artifactId>springboot-dubbo-provider</artifactId>-->
			<!--<version>0.0.1-SNAPSHOT</version>-->
		<!--</dependency>-->




		<!--dubbo-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.4.10</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
			<exclusions>
				<exclusion>
					<artifactId>slf4j-log4j12</artifactId>
					<groupId>org.slf4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.github.sgroschupf</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.1</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
SayHelloController类:
@RestController
public class SayHelloController {
    @Autowired
    SayService sayService;

    @RequestMapping("/hello")
    public String say(@RequestParam("name") String name) {
        System.out.println("name:" +name);
        return sayService.say(name);
    }


}
SayService类:
@Component
public class SayService  {

    @Resource
   private SayHello sayHello;

    public String say (String name) {
        System.out.println("服务层"+name);
       String s= sayHello.sayHello(name);
        return s;
    }

}

注:这里有个坑,就是@Reference注解千万不能用,用了就报错。后来改用@Resource就不报错了。

具体两者之间差异自行百度。

项目resources目录下新建dubbo-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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">



    <!-- 配置dubbo服务 -->
    <dubbo:application name="taotao-manager-web" />

    <!-- 使用广播 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
    <dubbo:registry protocol="zookeeper" address="zookeeper服务器的地址:2181"  check="false"/>

    <!-- 声明要调用的服务,timeout是设置连接超时最长时间,如果不设置,默认一秒超时,重试3次 -->
    <dubbo:reference interface="com.wty.dubbo.springbootdubboprovider.Service.SayHello"
                     id="sayHello" timeout="1000000" />
</beans>

启动类:

@SpringBootApplication
@ImportResource("classpath:dubbo-consumer.xml")
public class SpringbootDubboConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootDubboConsumerApplication.class, args);
	}
}

注:因为是两个项目同时启动,所以要改一下项目启动端口号。这个很简单,在项目application.properties里面加上一行代码:

server.port=8012 就可以了。建议两个项目都改一下,不要和其他项目冲突。

现在项目基本搭建完成,只要你配置的zookeeper服务器正常可以访问,那么这个项目就肯定没有问题。启动的时候先启动provider,再启动consumer就行。

 

接下来说一下打包部署的事,打包的时候provider项目要install到本地仓库中去,因为加了<configuration> <classifier>exec</classifier> </configuration> 代码,所以会在本地仓库生成两个jar包,一个是可依赖的,一个是可执行的。

部署的时候部署可依赖jar包就可以了。然后package consumer项目的时候provider项目必须时启动状态,要不然打包也是不成功的。把这两个jar包打包成功后就可以上传服务器启动了。这里还要注意一点就是启动的时候不要java -jar 这样启动一个再启动另一个的话第一个项目会停掉,建议使用后台启动命令

nohup java -jar wty-0.0.1-SNAPSHOT.jar > wty.log  2>&1 &    后台启动 日志打印到wty.log文件中 返回进程号  

这个时候就可以访问了。。。这是我的项目,可以访问的:

http://wxxcxxx.top:8012/hello?name="你的名字”

代码上传到github欢迎讨论:

https://github.com/yiTian66/dubboclient

©️2020 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值