您的位置:首页 > 运维架构 > 网站架构

java后端系统架构之服务治理篇:dubbo的实验

2015-03-30 09:13 681 查看
本篇实验dubbo的去中心化服务治理能力

dubbo介绍

服务治理开源项目,具备服务自动伸缩能力。当有部分dubbo服务实例不可用时,其通过注册中心(本实验是zookeeper注册中心),将不可用的服务在客户端调用层删除。

实验主机

localhost dubbo 服务实例
localhost casdemo web示例,允当dubbo服务消费者
localhost zookeeper实例

zookeeper启动

./zkServer.sh start

可以配置多个zookeeper实例

服务端

发布demoService.sayHello()服务
</pre></div><div>DubboProviderMain</div><div><pre name="code" class="java">package casdemo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboProviderMain {
/**
* @Title main
* @Description TODO
* @Author weizhi2018
* @param args
* @throws
*/

public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"remote-provider.xml"});
context.start();

System.out.println("Press any key to exit.");
System.in.read();
}
}


运行JAVA main函数

remote-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="casdemo"  />

<!-- 使用multicast广播注册中心暴露服务地址
<dubbo:registry address="multicast://224.5.6.7:1234" />-->

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<dubbo:registry protocol="zookeeper" address="192.168.161.73:21818"/>

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="casdemo.DemoService" ref="demoService" />

<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="casdemo.DemoServiceImpl" />

</beans>


客户端

客户端为web工程

webconsumer.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"> 
<!--注册中心地址配置-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:dubbo.properties</value>
</list>
</property>
</bean>

<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<!-- 应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。 -->
<dubbo:application name="consumer-of-casdemo"/>

<!-- 使用zookeeper注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.161.73:21818" />

<!-- 使用multicast广播注册中心暴露发现服务地址
<dubbo:registry address="multicast://224.5.6.7:1234" /> -->

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="casdemo.DemoService" />

</beans>


Login

调用dubbo服务
demoService.sayHello()
</pre></div><div><pre name="code" class="java">package casdemo;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
* Servlet implementation class Login
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;

//@Autowired
DemoService demoService;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {

super();
// TODO Auto-generated constructor stub
}
private ApplicationContext applicationContext;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
session.invalidate();
response.sendRedirect("login.jsp");
}
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generatedmethod stub
super.init(config);
applicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
demoService=(DemoService)applicationContext.getBean("demoService");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
String username=request.getParameter("username");
System.out.println(demoService.sayHello("testman"));
session.setAttribute("user",username);
response.sendRedirect("usr/index.jsp");
}

}


WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>casdemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring-servlet.xml,classpath*:webconsumer.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>CheckLoginFilter</filter-name>
<filter-class>casdemo.CheckLoginFilter</filter-class>
</filter>
<listener>
<listener-class>casdemo.DebugSessionListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>casdemo.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>CheckLoginFilter</filter-name>
<url-pattern>/usr/*</url-pattern>
</filter-mapping>
</web-app>


WEB-INF\spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true">
<context:component-scan base-package="casdemo">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="20971520" />
</bean>

<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>image/jpeg</value>
<value>image/png</value>
<value>image/svg+xml</value>
<value>application/pdf</value>
</list>
</property>
</bean>

</beans>


测试

工程地址 :https://code.csdn.net/wh0426/msmdemo/tree/master
访问 http://localhost:8080/casdemo/login.jsp 登录成功后,会在控制台日志中打印Hello testman 。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐