基于 Zookeeper、Dubbo 构建互联网分布式基础架构(2)|学习笔记

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
简介: 快速学习基于 Zookeeper、Dubbo 构建互联网分布式基础架构(2)

开发者学堂课程【基于 Zookeeper、Dubbo 构建互联网分布式基础架构互联网基础架构演进(3)】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/635/detail/10163


基于 Zookeeper、Dubbo 构建互联网分布式基础架构(2)

 

目录:

一. TestService

二. testweb

三. Testcontroller

四. Testserviceimpl

五. Apllicationcontext.xml

六. web 配置文件

七. 引入 dubbo

 

一.testService

Create by jackiechan on 2018/3/21/下午8:21

Publice interface testservice

此方法在此处没有任何意义,纯粹是为了演示拆分

@param name

@return

String getdata(string name);

注:做 controller 时一定需要 testservice 但在输入时却发现没有,原因是因为没有依赖,依赖要在本地仓库里寻找如果没有则再远程服务包寻找。

将 service 添加为依赖首先要放在本地仓库数据包里,添加成功后进行查看

 

二、testweb 包

xsi:schemaLocat ion="http://maven.apache.org/P0H/4.0.0http:maven,apache.org/maven-v4_0_0

<parent><artifac tId>testduboodemo</artifactId

<groupId>com.qianfeng</groupId>

<version>1.0</version>

</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>testweb</artifactId>

<name>testweb</name>

<urbhttp://maven.apache.org</url

<packaging>war</packaging>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

cscope>provided</scope»

</dependency>

</dependencies>

 

三.testcontroller

import com.qianfeng.testduboo.TestService;

import org.springframework,web.bind.annotation.RequestMapping;

import org.springframework,wcb.bind.annotation.Restcontroller;
import javax,annotation,Resource;
Created by jackiechan on 2018/3/21/下午8:16
@RestController
public class TestController
//这里声明一个service,service最终会变成一个 web 项目,也就是无法直接依赖,此处仍然需要声明对象.

//解决方式,我们将 service 的接口声明和实现进行拆分,将 service 创建为一个项目,jar包格式的项目.

//会用到service项目,serviceimpl 实现中也用到了这个接口
@Resource
private TestService service;
@Reques tMapping("/getData")
public string getData(String name)
string data = service.getData(name);
return


四.testserviceimpl

import com.qianfeng.testduboo.TestService;

import org.spring framework.stereotype.Service;

@Service("testServiceImpl")

public class TestServiceImplt implements TestService

@override
public string getData(String name )
return "你传递的数据是"+name ;

 

五.Apllicationcontext.xml

<?xml version-"1.0"encoding="UTF-8"7>

<beansxmlns="http://www,springframework,org/schema/beans"

xmlns:xsi-"http://www.w3,org/2001/XMLSchema-instance"

xmlns:aop="http:(www.springframework.org/schema/aop"

xm Lns:dubbo-"http://code.alibabatech.com/schema/dubbo"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation-"

<!扫描注解-->

<context:component-scanbase-package ="com,gianfeng"/>
<!--使用dubbo的另外一个原因,可以和spring无缝整合-->
<!--目的:
找到注册中心,告诉它我是谁,我的端口是多少
<!--配置别名,目的是在后台可以看到这个服务的别名,好区分到底是谁,这个参数可以随便写,但是最好是通俗易懂-->dubbo:application name="test1"/>
<!--2配置注册中心
address 注册中心的地址,protocol 指的是注册中心的协议格式<dubbo:registry.address="192.168.3.224:2181" .protocol="zookeeper"/>

interface 代表的就是类型
ref代表的就是到底具体发布哪个服务 timeout 连接超时时间
<dubbo:service interface="com.gianfeng.testduboo.TestService"ref="* imeoute"6000" />

</beans>


六.web 配置文件

<web-app xmlns-"http://xmlns.jcp.org/xml/ns/javaee"

xmlns:xsi="http:1/www3.org/2001/XMLSchema-instance"

xSi:schemaLocation-"http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_31.xsd"

version-"3.1"

metadata-complete-"true",

<context-param>

<param-name>contextConfigLocaton</param-name>

<param-value>claspath:ApplicationContext.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener

</web-app>

 

七.引入 dubbo

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.9</version>
<exclusions>
<exclusion>
<groupId>org,springframework</groupId»<artifactId>spring</artifactId>
</exclusion>

相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
2天前
|
负载均衡 Java API
使用Spring Cloud构建Java微服务架构
使用Spring Cloud构建Java微服务架构
|
2天前
|
SQL 存储 运维
网易游戏如何基于阿里云瑶池数据库 SelectDB 内核 Apache Doris 构建全新湖仓一体架构
随着网易游戏品类及产品的快速发展,游戏数据分析场景面临着越来越多的挑战,为了保证系统性能和 SLA,要求引入新的组件来解决特定业务场景问题。为此,网易游戏引入 Apache Doris 构建了全新的湖仓一体架构。经过不断地扩张,目前已发展至十余集群、为内部上百个项目提供了稳定可靠的数据服务、日均查询量数百万次,整体查询性能得到 10-20 倍提升。
网易游戏如何基于阿里云瑶池数据库 SelectDB 内核 Apache Doris 构建全新湖仓一体架构
|
1天前
|
存储 前端开发 JavaScript
构建高性能返利App的技术架构解析
构建高性能返利App的技术架构解析
|
2天前
|
Kubernetes Java 微服务
使用Spring Boot构建微服务架构
使用Spring Boot构建微服务架构
|
2天前
|
缓存 NoSQL 数据库
分布式系统面试全集通第一篇(dubbo+redis+zookeeper----分布式+CAP+BASE+分布式事务+分布式锁)
分布式系统面试全集通第一篇(dubbo+redis+zookeeper----分布式+CAP+BASE+分布式事务+分布式锁)
8 0
|
2天前
|
设计模式 监控 安全
一文搞懂:zookeeper实现分布式锁安全用法
一文搞懂:zookeeper实现分布式锁安全用法
|
2天前
|
监控 安全 Java
构建Java微服务架构的实用指南
构建Java微服务架构的实用指南
|
2天前
|
负载均衡 监控 Java
构建高可用Java微服务架构的秘籍
构建高可用Java微服务架构的秘籍
|
2天前
|
Kubernetes Java 微服务
使用Spring Boot构建微服务架构
使用Spring Boot构建微服务架构
|
4天前
|
消息中间件 监控 Java
使用Java构建微服务架构的最佳实践
使用Java构建微服务架构的最佳实践