Web service是什么呢?
答案:Web service 是建立可互操作的分布式应用程序的新平台.
Web service 平台是一套标准,它定义了应用程序如何在Web上实现互操作性.
什么是SOAP?
答案:简单对象访问协议(SOAP)是一种轻量的丶简单的丶基于XML的协议,
它被设计成在Web上交换结构化的和固话的信息.
SOAP包括以下3个部分.
OAP封装:它定义了一个框架,该框架描述了消息中的内容是什么,谁应当处理它,以及它是可选的还是必需的.
SOAP编码规则:它定义了一种序列化的机制,用于交换应用程序所定义的数据类型的实例.
SOAP RPC表示:它定义了用于表示远程过程调用和应答的协定.
Web service的特点
答案:Web service 通过HTTP POST方式接受客户的请求.
Web service 与客户端之间一般使用SOAP协议传输XML数据.
它本身就是为了跨平台或跨语言而设计的.
Web service的框架
CXF
发布服务
1.导入CXF相关jar包
<
dependency
>
<
groupId
>
org.apache.cxf
</
groupId
>
<
artifactId
>
cxf
</
artifactId
>
<
version
>
2.7.7
</
version
>
<
type
>
pom
</
type
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.cxf
</
groupId
>
<
artifactId
>
cxf-rt-frontend-jaxws
</
artifactId
>
<
version
>
2.7.7
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.cxf
</
groupId
>
<
artifactId
>
cxf-rt-transports-http
</
artifactId
>
<
version
>
2.7.7
</
version
>
</
dependency
>
2.配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置CXF框架提供的
Servlet
-->
<servlet>
<servlet-name>
cxf
</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
cxf
</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
3.在类路径下提供applicationContext-cxf.xml
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource=
"classpath:META-INF/cxf/cxf.xml"
/>
<import resource=
"classpath:META-INF/cxf/cxf-extension-soap.xml"
/>
<import resource=
"classpath:META-INF/cxf/cxf-servlet.xml"
/>
</beans>
4.开发一个接口和实现类
接口:
@WebService
public interface ICustomerService {
public List<Customer> findAll();
}
实现类:
@Transactional
public class CustomerServiceImpl implements ICustomerService {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Customer> findAll() {
String sql = "select * from t_customer";
List<Customer> list = jdbcTemplate.query(sql, new RowMapper<Customer>(){
public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
int id = rs.getInt("id");//根据字段名称从结果集中获取对应的值
String name = rs.getString("name");
String station = rs.getString("station");
String telephone = rs.getString("telephone");
String address = rs.getString("address");
String decidedzone_id = rs.getString("decidedzone_id");
return new Customer(id, name, station, telephone, address, decidedzone_id);
}
});
return list;
}
}
5.修改配置applicationContext-cxf.xml
<!-- 配置数据源 -->
<bean id=
"dataSource"
class=
"org.springframework.jdbc.datasource.DriverManagerDataSource"
>
<property name=
"driverClassName"
value=
"com.mysql.jdbc.Driver"
/>
<property name=
"url"
value=
"jdbc:mysql:///crm_heima32"
/>
<property name=
"username"
value=
"root"
/>
<property name=
"password"
value=
"root"
/>
</bean>
<!-- 事务管理器 -->
<bean id=
"txManager"
class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager"
>
<property name=
"dataSource"
ref=
"dataSource"
/>
</bean>
<!-- 支持事务注解 -->
<tx:annotation-driven transaction-manager=
"txManager"
/>
<bean id=
"jdbcTemplate"
class=
"org.springframework.jdbc.core.JdbcTemplate"
>
<property name=
"dataSource"
ref=
"dataSource"
/>
</bean>
<bean id=
"customerService"
class=
"com.itheima.crm.service.CustomerServiceImpl"
>
<property name=
"jdbcTemplate"
ref=
"jdbcTemplate"
/>
</bean>
<!-- 注册服务 -->
<jaxws:server id=
"myService"
address=
"/customer"
>
<jaxws:serviceBean>
<ref bean=
"customerService"
/>
</jaxws:serviceBean>
</jaxws:server>
客户端调用服务(导包)
<
dependency
>
<
groupId
>
org.apache.cxf
</
groupId
>
<
artifactId
>
cxf
</
artifactId
>
<
version
>
2.7.7
</
version
>
<
type
>
pom
</
type
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.cxf
</
groupId
>
<
artifactId
>
cxf-rt-frontend-jaxws
</
artifactId
>
<
version
>
2.7.7
</
version
>
</
dependency
>
<
dependency
>
<
groupId
>
org.apache.cxf
</
groupId
>
<
artifactId
>
cxf-rt-transports-http
</
artifactId
>
<
version
>
2.7.7
</
version
>
</
dependency
>
1.进入CXF源码包bin目录下.
2.使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件
apache-cxf-2.4.2\bin>wsdl2java -d .
-p
项目名(例如:com.heima.client)
路径(例如:http://192.168.115.87:8080/crm_heima/service/
customer
?wsdl)
或
3.将接口文件复制到项目中
4.提供spring配置文件,注册客户端代理对象
<jaxws:client id=
"crmClient"
serviceClass=
"com.itheima.crm.ICustomerService"
5.通过注解方式将代理对象注入给Action
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {
@Autowired
private IUserService userService;
注意:服务端扩展了新的方法,需要客户端重新生成客户端代码
Dubbo
发布服务和消费者
1.
导入Dubbo相关jar包
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
dubbo
</artifactId>
<version>
${dubbo.version}
</version>
</dependency>
或
<!--
dubbo
相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>
dubbo
</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>
netty
</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>
zookeeper
</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>
zkclient
</artifactId>
</dependency>
2.发布:
在类路径下提供applicationContext-dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<
beans
xmlns
=
"
http://www.springframework.org/schema/beans
"
<!-- 提供方应用信息,用于计算依赖关系 -->
<
dubbo:
application
name
=
"
hello-world-app
"
/>
<!-- 使用multicast广播注册中心暴露服务地址 -->
<
dubbo:
registry
protocol
="
zookeper
"
address
=
"
224.5.6.7:2181
"
/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<
dubbo:
protocol
name
=
"
dubbo
"
port
=
"
20880
"
/>
<!-- 声明需要暴露的服务接口 -->
<
dubbo:
service
interface
=
"
com.alibaba.dubbo.demo.DemoService
"
ref
=
"
demoService
"
/>
<!-- 和本地bean一样实现服务 -->
<
bean
id
=
"
demoService
"
class
=
"
com.alibaba.dubbo.demo.provider.DemoServiceImpl
"
/>
</
beans
>
2.消费:
在类路径下提供applicationContext-dubbo.xml
<?xml version="1.0" encoding="UTF-8"?>
<
beans
xmlns
=
"
http://www.springframework.org/schema/beans
"
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
=
"
consumer-of-helloworld-app
"
/>
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<
dubbo:
registry
protocol
="
zookeper
"
address
=
"
224.5.6.7:2181
"
/>
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<
dubbo:
reference
id
=
"
demoService
"
interface
=
"
com.alibaba.dubbo.demo.DemoService
"
timeout
="
6000
"
/>
</
beans
>
使用Dubbo 建议使用zookeeper
zookeeper安装
安装步骤:
第一步:安装
jdk
第二步:把
zookeeper
的压缩包上传到
linux
系统。
第三步:解压缩压缩包
tar -zxvf zookeeper-3.4.6.tar.gz
第四步:进入
zookeeper-3.4.6
目录,创建
data
文件夹。
第五步:把
zoo_sample.cfg
改名为
zoo.cfg
[root@localhost conf]# mv zoo_sample.cfg zoo.cfg
第六步:修改
data
属性:
dataDir=/root/zookeeper-3.4.6/data
第七步:启动
zookeeper
[root@localhost bin]# ./zkServer.sh start
关闭:
[root@localhost bin]# ./zkServer.sh stop
查看状态:
[root@localhost bin]# ./zkServer.sh status
zookeeper集群搭建
安装步骤:
第一步:需要安装
jdk
环境。
第二步:把
zookeeper
的压缩包上传到服务器。
第三步:解压缩。
第四步:把
zookeeper
复制三份。
[root@localhost ~]# mkdir /usr/local/solr-cloud
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper01
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper02
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper03
第五步:在每个
zookeeper
目录下创建一个
data
目录。
第六步:在
data
目录下创建一个
myid
文件,文件名就叫做“
myid
”。内容就是每个实例的
id
。例如
1
、
2
、
3
[root@localhost data]# echo 1 >> myid
[root@localhost data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr 7 18:23 myid
[root@localhost data]# cat myid
1
第七步:修改配置文件。把
conf
目录下的
zoo_sample.cfg
文件改名为
zoo.cfg
server.1=192.168.25.154:2881:3881
server.2=192.168.25.154:2882:3882
server.3=192.168.25.154:2883:3883
|
第八步:启动每个
zookeeper
实例。
启动
bin/zkServer.sh start
或者使用p处理
vim start-all.sh
cd zookeeper01/bin
./zkServer.sh start
cd ../../
cd zookeeper02/bin
./zkServer.sh start
cd ../../
cd zookeeper03/bin
./zkServer.sh start
cd ../../
chmod u+x start-all.sh
查看
zookeeper
的状态:
bin/zkServer.sh status