Dubbo
序列化-二进制、网络通信-Socket
搭建无注册中心Dubbo
创建两个工程ConsumerProject和ProviderProject,都导入dubbo,spring-context,spring-webmvc包
public interface Provider {
String provide();
}
@Component("providerImpl")
public class ProviderImpl implements Provider {
public String provide() {
return "Hello World";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<dubbo:application name="Provider"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.dubbo.service.Provider"
ref="providerImpl"
registry="N/A"/>
<context:component-scan base-package="com"/>
</beans>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo_config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
@Controller
public class Consumer {
@Autowired
Provider provider;
@RequestMapping("/consume")
@ResponseBody
public String consume() {
return provider.provide();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<dubbo:application name="Consumer"/>
<dubbo:reference id="provider"
interface="com.dubbo.service.Provider"
url="dubbo://localhost:20880"
registry="N/A"/>
<context:component-scan base-package="com"/>
<mvc:annotation-driven/>
</beans>
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:duubo_config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
搭建有接口模块Dubbo
创建interface,provider,consumer三个工程
public interface Service {
String getMsg();
User getUser(Integer id);
}
public class User implements Serializable {
private String name;
private Integer age;
private String sex;
}
在provider中添加依赖dubbo,spring-context,spring-webmvc,添加interface工程的依赖
@Component
public class ServiceImpl implements Service {
public String getMsg() {
return "Hello World";
}
public User getUser(Integer id) {
return user;
}
}
<beans ...>
<dubbo:application name="Provider"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dbuuo:service interface="com.dubbo.service.Service"
ref="serviceImpl"
registry="N/A"/>
<context:component-scan base-pack="com"/>
</beans>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo_config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在consumer中添加依赖dubbo,spring-context,spring-webmvc,添加interface工程的依赖
@RestController
public class Controller {
@Autowired
Service service;
@RequestMapping("/hello")
public String hello() {
return service.getMsg();
}
@RequestMapping("/user")
public String getUser(@RequestParam("id") Integer id) {
return service.getUser(id);
}
}
<beans>
<dubbo:application name="Consumer"/>
<dubbo:reference id="service"
interface="com.dubbo.service.Service"
url="dubbo://localhost:20880"
registry="N/A"/>
<context:component-scan base-pack="com"/>
<mvc:annotation-driven/>
</beans>
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:duubo_config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
搭建有注册中心Dubbo
启动zookeeper服务/注册中心
? https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
启动dubbo-ops服务/监控中心
? https://github.com/apache/dubbo-admin/tree/master
注册中心的存储结构
dubbo => service => provider&consumer => url
创建interface,provider,consumer工程
public interface Service {
String getMsg();
User getUser(Integer id);
}
public class User implements Serializable {
private String name;
private Integer age;
private String sex;
}
provider中添加依赖dubbo,spring-webmvc,spring-context,curator-framework,引入interface工程依赖
@Component
public class ServiceImpl implement Service {
public String getMsg() {
reutrn "hello world";
}
public User getUser(Integer id) {
return user;
}
}
<beans>
<dubbo:application name="Provider"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:service interface="com.dubbo.service.Service" ref="serviceImpl"/>
<context:component-scan base-package="com"/>
</beans>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo_config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在consumer中添加依赖dubbo,spring-context,spring-webmvc,添加interface工程的依赖
@RestController
public class Controller {
@Autowired
Service service;
@RequestMapping("/hello")
public String hello() {
return service.getMsg();
}
@RequestMapping("/user")
public String getUser(@RequestParam("id") Integer id) {
return service.getUser(id);
}
}
<beans>
<dubbo:application name="Consumer"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:reference id="service" interface="com.dubbo.service.Service"/>
<context:component-scan base-package="com"/>
<mvc:annotation-driven/>
</beans>
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:duubo_config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置原则
搭建有版本号Dubbo
创建interface,provider,consumer工程
public interface Service {
String getMsg();
User getUser(Integer id);
}
public class User implements Serializable {
private String name;
private Integer age;
private String sex;
}
provider中添加依赖dubbo,spring-webmvc,spring-context,curator-framework,引入interface工程依赖
@Component
public class ServiceImpl implement Service {
public String getMsg() {
reutrn "hello world";
}
public User getUser(Integer id) {
return user;
}
}
@Component
public class NewServiceImpl implement Service {
public String getMsg() {
reutrn "hello world";
}
public User getUser(Integer id) {
return user;
}
}
<beans>
<dubbo:application name="Provider"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:service interface="com.dubbo.service.Service" ref="serviceImpl" version="1.0"/>
<dubbo:service interface="com.dubbo.service.Service" ref="newServiceImpl" version="2.0"/>
<context:component-scan base-package="com"/>
</beans>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo_config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在consumer中添加依赖dubbo,spring-context,spring-webmvc,添加interface工程的依赖
@RestController
public class Controller {
@Autowired
Service service;
@Autowired
Service newService;
@RequestMapping("/hello")
public String hello() {
return service.getMsg();
}
@RequestMapping("/user")
public String getUser(@RequestParam("id") Integer id) {
return service.getUser(id);
}
}
<beans>
<dubbo:application name="Consumer"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:reference id="service" interface="com.dubbo.service.Service" version="1.0"/>
<dubbo:reference id="newService" interface="com.dubbo.service.Service" version="2.0"/>
<context:component-scan base-package="com"/>
<mvc:annotation-driven/>
</beans>
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:duubo_config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
|