基于Dubbo实现远程服务交互

本文介绍了如何利用Dubbo和ZooKeeper进行远程服务交互。Dubbo作为服务治理框架,提供了多种协议支持,如HTTP、HESSIAN、RMI等,并实现了软负载均衡和监控中心。通过ZooKeeper作为服务注册中心,实现服务提供方和消费方的透明调用。此外,使用SpringBoot简化项目构建,内嵌TOMCAT容器。项目包括POJO对象、服务接口定义、服务接口实现及消费端应用,实现了RESTful API的响应。
摘要由CSDN通过智能技术生成

使用Dubbo进行远程服务交互,Dubbo本身支持很多协议HTPP、HESSION、RMI 等等,由于Dubbo已经封装了协议、序列化、通信,并提供了软负载均衡,监控中心.服务注册中心,无论是Consumer、Proveder都不需要关注细节,只需要简单的配置完成了消费端和提供端透明化的调用。

使用如下组件:

ZooKeeper: (服务注册中心),服务提供方将服务发布到注册中心,只是将服务的地址名称暴露出来,服务消费方订阅服务提供方的名称,就能实现透明化的调用、

DUBBO:服务治理框架。

SpringBoot:约定大于配置,提供基于Spring快速构建应用,SpringBoot并不是新的技术,只是在原有Spring上提供了很多默认的配置,加快构建的速度.内嵌TOMCAT等容器


构建项目:


dcms-cif-base:POJO对象提供了服务端和消费端使用

dcms-cif-service:服务接口提供了服务端和消费端使用

dcms-cif-restful:消费端,接受http请求,返回JSON,实现resutful

dcms-cif-business:服务接口实现,提供服务端

服务接口:

创建MAVEN项目:dcms-cif-service,只是简单的接口

package com.dcms.mcif.service;

import com.dcms.mcif.domain.Response;

public interface UserService {

	Response<?> findUserById(String id);

}
创建MAVEN项目:dcms-cif-base

package com.dcms.mcif.domain;

public class UserVO implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String id;
	private String name;
	private int age;
	private String email;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
创建MAVEN项目:dcms-cif-business



<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>org.dcms.cif</groupId>
	<artifactId>dcms-cif-business</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>dcms-cif-business</name>
	<url>http://maven.apache.org</url>


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.13</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.8</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.dcms.cif</groupId>
			<artifactId>dcms-cif-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.4</version>
		</dependency>

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
application.yml

server:
      port: 18080
logging:
      config: classpath:logback.xml
      path: D:\workspace\dcms-web-restful
      file: log.log
dubbo-cif-providers.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<bean id="userServiceImp" class="com.dcms.cif.serviceImp.UserServiceImp" />

	<!-- -->
	<dubbo:application name="cif-providers" />

	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>

	<dubbo:protocol name="dubbo" port="28888"></dubbo:protocol>
	
	<dubbo:service interface="com.dcms.mcif.service.UserService" ref="userServiceImp"></dubbo:service>

</beans>
Application
package com.dcms.cif;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan
@ImportResource(value = "classpath:dubbo-cif-providers.xml")
public class Application {

	public static void main(String[] args) {

		SpringApplication.run(Application.class, args);

	}
}
Service实现:

package com.dcms.cif.serviceImp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dcms.mcif.domain.Response;
import com.dcms.mcif.domain.UserVO;
import com.dcms.mcif.service.UserService;

public class UserServiceImp implements UserService {

	public static final Logger logger = LoggerFactory
			.getLogger(UserServiceImp.class);

	public Response<?> findUserById(String id) {

		UserVO vo = new UserVO();
		vo.setId(id);
		vo.setName("lisi");
		vo.setAge(27);
		vo.setEmail("dubbo@qq.com");

		return new Response<UserVO>(vo);
	}
}
创建MAVEN项目:dcms-cif-restful


<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>org.dcms.cif</groupId>
	<artifactId>dcms-web-restful</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>dcms-web-restful</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
		<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>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.dcms.cif</groupId>
			<artifactId>dcms-cif-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.4</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
application.yam

server:
    port: 8081
    context-path: /
dubbo-cif-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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


	<dubbo:application name="cif-consumer" />

	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

	<dubbo:reference id="userService" interface="com.dcms.mcif.service.UserService" />

</beans>
Application

package com.dcms.cif;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
@ComponentScan
@ImportResource(value = { "classpath:dubbo-cif-consumer.xml" })
public class Application {

	public static void main(String[] args) {

		SpringApplication.run(Application.class, args);
	}
}
UserController

package com.dcms.cif.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dcms.mcif.domain.Response;
import com.dcms.mcif.service.UserService;

@RestController
@RequestMapping(value = "/cif")
public class UserController {
	
	public static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService service ;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Response<?> getUser(@PathVariable("id") String id) {
	
		logger.info(UserController.class.getName() + "  [getUser] invoke......");
		
		return service.findUserById(id);
	}
}
启动zk:




首先启动服务端:



调用成功。。。。。。。。。。。。。。。。。

本人不善于表达,大伙凑合看




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值