Dubbo + ZooKeeper 入门快速开发练习详解,linux系统架构师报名

Dubbo + ZooKeeper 入门快速开发练习详解,linux系统架构师报名,第1张

Dubbo + ZooKeeper 入门快速开发练习详解,linux系统架构师报名

org.springframework

spring-tx

${spring.webmvc.version}

org.springframework

spring-jdbc

${spring.webmvc.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

junit

junit

4.12

test

3. 创建公共子模块

pom.xml

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

dubbo_parent

com.lichun

1.0-SNAPSHOT

dubbo_common

jar

User.java

package com.lichun.pojo;

import java.io.Serializable;

public class User implements Serializable {

private Integer id;

private String username;

private Integer age;

@Override

public String toString() {

return “User{” +

“id=” + id +

“, username=’” + username + ‘’’ +

“, age=” + age +

‘}’;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

4. 创建接口子模块

此模块,主要存放业务接口的定义,它是服务消费者模块和服务提供者模块的公共依赖模块

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

dubbo_parent

com.lichun

1.0-SNAPSHOT

4.0.0

dubbo_interface

jar

com.lichun

dubbo_common

1.0-SNAPSHOT

UserService.java

package com.lichun.service;

import com.lichun.pojo.User;

public interface UserService {

User findById(Integer id);

}

5. 创建服务提供者模块

只要能启动spring的容器(加载spring的配置文件)就可以启动项目,因此有三种启动项目方式:

  • ClassPathXmlApplication

  • 监听器

  • DispatcherServlet

注册中心验证:

注意:

  • 消费者与提供者应用名称不能相同

  • 如果有多个服务提供者,名称不能相同,通信端口也不能相同

  • 只有服务提供者才会配置服务发布的协议,默认是dubbo协议,端口号是20880

pom.xml

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/xsd/maven-4.0.0.xsd">

dubbo_parent

com.lichun

1.0-SNAPSHOT

4.0.0

dubbo_provider

war

com.lichun

dubbo_interface

1.0-SNAPSHOT

org.springframework

spring-webmvc

com.alibaba

dubbo

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

pendency>

org.apache.zookeeper

zookeeper

org.apache.curator

curator-framework

org.apache.curator

curator-recipes

org.mybatis

mybatis

org.mybatis

mybatis-spring

com.alibaba

druid

mysql

mysql-connector-java

org.springframework

spring-tx

org.springframework

spring-jdbc

org.slf4j

slf4j-log4j12

UserDao.java

package com.lichun.dao;

import com.lichun.pojo.User;

import org.apache.ibatis.annotations.Select;

public interface UserDao {

@Select(“select * from t_user where id = #{id}”)

User findById(Integer id);

}

UserServiceImpl.java

package com.lichun.service.impl;

import com.lichun.dao.UserDao;

import com.lichun.pojo.User;

import com.lichun.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

public class UserServiceImpl implements UserService {

@Autowired

UserDao userDao;

@Override

public User findById(Integer id) {

User user = userDao.findById(id);

return user;

}

}

MyApplicationContext.java

package com.lichun;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class MyApplicationContext {

public static void main(String[] args) throws IOException {

new ClassPathXmlApplicationContext(“classpath:spring-provider.xml”);

System.in.read();

}

}

log4j.properties

direct log messages to stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target=System.err

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

set log levels - for more verbose logging change ‘info’ to ‘debug’

log4j.rootLogger=debug, stdout

spring-dao.xml

  • 配置数据源

  • 配置sqlSessionFactory对象

  • 扫描dao包,创建dao接口的动态代理对象

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">

spring-service.xml

  • 配置事务(事务管理器,注解事务)

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:tx=“http://www.springframework.org/schema/tx”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd”>

spring-provider.xml

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:dubbo=“http://dubbo.apache.org/schema/dubbo”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd”>

web.xml

xmlns=“http://java.sun.com/xml/ns/javaee”

xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”

version=“2.5”>

contextConfigLocation

classpath:spring-provider.xml

org.springframework.web.context.ContextLoaderListener

6. 创建服务消费者模块

pom.xml

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

dubbo_parent

com.lichun

1.0-SNAPSHOT

4.0.0

dubbo_consumer

war

com.lichun

dubbo_interface

1.0-SNAPSHOT

org.springframework

spring-webmvc

com.fasterxml.jackson.core

jackson-core

2.9.0

com.fasterxml.jackson.core

jackson-databind

2.9.0

com.fasterxml.jackson.core

jackson-annotations

2.9.0

javax.servlet

servlet-api

2.5

provided

com.alibaba

dubbo

org.apache.zookeeper

zookeeper

org.apache.curator

curator-framework

org.apache.curator

curator-recipes

org.slf4j

slf4j-log4j12

UserController.java

package com.lichun.controller;

import com.lichun.pojo.User;

import com.lichun.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

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

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping("/user")

public class UserController {

@Autowired

private UserService userService;

@RequestMapping("/findById")

public User findById(Integer id) {

// springmvc默认使用jaskson来解析java对象为json字符串,注意引入jaskson的依赖

return userService.findById(id);

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5619005.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存