使用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
4.0.0
org.dcms.cif
dcms-cif-business
0.0.1-SNAPSHOT
jar
dcms-cif-business
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.3.3.RELEASE
UTF-8
1.8
junit
junit
4.11
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-redis
org.springframework.boot
spring-boot-starter-web
com.alibaba
druid
1.0.13
mysql
mysql-connector-java
5.1.38
org.mybatis
mybatis
3.2.8
org.mybatis
mybatis-spring
1.2.2
com.alibaba
dubbo
2.5.3
org.dcms.cif
dcms-cif-service
0.0.1-SNAPSHOT
com.101tec
zkclient
0.4
org.springframework.boot
spring-boot-maven-plugin
application.yml
server:
port: 18080
logging:
config: classpath:logback.xml
path: D:\workspace\dcms-web-restful
file: log.log
dubbo-cif-providers.xml
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("[email protected]");
return new Response(vo);
}
}
创建MAVEN项目:dcms-cif-restful
4.0.0
org.dcms.cif
dcms-web-restful
0.0.1-SNAPSHOT
jar
dcms-web-restful
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.3.3.RELEASE
UTF-8
1.8
junit
junit
4.11
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-jdbc
org.dcms.cif
dcms-cif-service
0.0.1-SNAPSHOT
com.alibaba
dubbo
2.5.3
com.101tec
zkclient
0.4
org.springframework.boot
spring-boot-maven-plugin
application.yam
server:
port: 8081
context-path: /
dubbo-cif-consumer.xml
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:
本人不善于表达,大伙凑合看