公司微服务子服务架构分析
开一篇博客来更新自己对公司项目架构的学习与理解。项目架构主要涉及到以下几点:
ssm,dubbo,zookerper,redis。
持续更新中……
一 SSM框架搭建
首先是ssm框架的搭建,想必大家已经非常熟悉了,这里就不给出代码了,贴一个我经常用的一个模板项目文件,配置文件十分简短,只包含了最基本的ssm整合配置。
二 整合ssm+dubbo+zookerper
首先之前做的只是一个单体的服务,接下的要做的dubbo是分布式服务治理方案,如果不了解dubbo的可以先看看dubbo官网,这里贴出的是dubbo官网中文文档,里面的说明非常详细。对于分布式服务,我们首先将项目改成多模块(不了解maven多模块的项目构建的朋友可以先去了解一下)。
将原项目改造成父项目,创建两个子模块sdz-customer和sdz-provider,sdz-provider作为一个子服务(也就是通俗说的“微服务”的一个单体)对外提供一些接口,供其他子服务调用。如图所示:
provider业务代码
对于服务提供者来说,需要实现的是:实现一个接口,并且将这个接口申明到dubbo服务中,然后将dubbo服务注册到zookerper中,关于zookerper的详细可以去zookerper的官网了解一下,这里只是做功能性的使用,就不做多余的说明了。
demoApi.java(申明的接口)
public interface demoApi {
String demoMethod(String name);
}
demoApiImpl.java(作为提供者对接口的实现)
import com.demo.dubbo.api.demoApi;
import org.springframework.stereotype.Service;
@Service("demoService")
public class demoApiImpl implements demoApi {
@Override
public String demoMethod(String name) {
return "hello"+name;
}
}
配置文件:dubbo-demo-provider(配置文件加载进项目)
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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="demo-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<bean id="demoService" class="com.demo.ssm.service.dubbo.demoApiImpl"/>
<dubbo:service interface="com.demo.dubbo.api.demoApi" ref="demoService"/>
</beans>
customer业务代码
demoApi.java(申明的远程服务的接口,等会调用接口,其实对于服务者和消费者没有必要定义两次,可以抽象出一个接口包即可)
public interface demoApi {
String demoMethod(String name);
}
demoController在消费者的controller远程调用服务者的接口
@Controller
@RequestMapping("/test1")
public class demoCotroller {
@Autowired
private demoApi demoService;
@RequestMapping("1")
@ResponseBody
public String TestApi(){
return demoService.demoMethod("asdasdasd");
}
}
dubbo-consumer.xml消费者dubbo配置文件
<?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="dubbo_consumer" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.demo.dubbo.api.demoApi" />
</beans>
项目整体测试
第一步:首先启动zookerper作为注册中心
第二步:启动提供者服务
这边是采用maven插件jetty作为应用服务器
第三步:启动消费者服务
注意两个服务端口不要一样了
第四步:接口的远程调用
访问消费者controller,controller会调用接口,接口的实现是由提供者提供的,至此我们完成了接口的远程调用。至此完成了RPC的使用。