• 第三章、注册中心-Zookeeper(实例1)


    1.新建Model不使用maven框架

    2.User类

    1. package com.it.dubbo.entity;
    2. import java.io.Serializable;
    3. public class User implements Serializable {
    4. private Integer id;
    5. private String name;
    6. @Override
    7. public String toString() {
    8. return "User{" +
    9. "id=" + id +
    10. ", name='" + name + '\'' +
    11. '}';
    12. }
    13. public Integer getId() {
    14. return id;
    15. }
    16. public void setId(Integer id) {
    17. this.id = id;
    18. }
    19. public String getName() {
    20. return name;
    21. }
    22. public void setName(String name) {
    23. this.name = name;
    24. }
    25. }

    3.UserService

    1. package com.it.dubbo.service;
    2. import com.it.dubbo.entity.User;
    3. public interface UserService {
    4. /**
    5. * 根据用户标识获取用户的信息
    6. * @param id
    7. * @return
    8. */
    9. User queryUserById(Integer id,String name);
    10. }

    1.新建model使用maven的archetype-webapp的框架

    2.pom.xml文件

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <groupId>com.it.dubbogroupId>
    5. <artifactId>007-zk-userservice-providerartifactId>
    6. <version>1.0-SNAPSHOTversion>
    7. <packaging>warpackaging>
    8. <dependencies>
    9. <dependency>
    10. <groupId>org.springframeworkgroupId>
    11. <artifactId>spring-contextartifactId>
    12. <version>4.3.16.RELEASEversion>
    13. dependency>
    14. <dependency>
    15. <groupId>org.springframeworkgroupId>
    16. <artifactId>spring-webmvcartifactId>
    17. <version>4.3.16.RELEASEversion>
    18. dependency>
    19. <dependency>
    20. <groupId>com.alibabagroupId>
    21. <artifactId>dubboartifactId>
    22. <version>2.6.2version>
    23. dependency>
    24. <dependency>
    25. <groupId>com.it.dubbogroupId>
    26. <artifactId>006-zk-interfaceartifactId>
    27. <version>1.0-SNAPSHOTversion>
    28. dependency>
    29. <dependency>
    30. <groupId>org.apache.curatorgroupId>
    31. <artifactId>curator-frameworkartifactId>
    32. <version>4.1.0version>
    33. dependency>
    34. dependencies>
    35. <build>
    36. <plugins>
    37. <plugin>
    38. <artifactId>maven-compiler-pluginartifactId>
    39. <version>3.1version>
    40. <configuration>
    41. <source>1.8source>
    42. <target>1.8target>
    43. configuration>
    44. plugin>
    45. plugins>
    46. build>
    47. project>

    3.UserServiceImpl类

    1. package com.it.dubbo.service.impl;
    2. import com.it.dubbo.entity.User;
    3. import com.it.dubbo.service.UserService;
    4. import java.util.UUID;
    5. public class UserServiceImpl implements UserService {
    6. @Override
    7. public User queryUserById(Integer id, String name) {
    8. User user = new User();
    9. user.setId(id);
    10. user.setName(name);
    11. return user;
    12. }
    13. }

    4.dubbo-zk-userservice-provider.xml配置文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    3. 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">
    4. <dubbo:application name="007-zk-userservice-provider"/>
    5. <dubbo:protocol name="dubbo" port="20880"/>
    6. <dubbo:registry address="zookeeper://localhost:2181"/>
    7. <dubbo:service interface="com.it.dubbo.service.UserService" ref="userServiceImpl"/>
    8. <bean id="userServiceImpl" class="com.it.dubbo.service.impl.UserServiceImpl"/>
    9. beans>

    5.web.xml配置文件

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    4. version="4.0">
    5. <context-param>
    6. <param-name>contextConfigLocationparam-name>
    7. <param-value>classpath:dubbo-zk-userservice-provider.xmlparam-value>
    8. context-param>
    9. <listener>
    10. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    11. listener>
    12. web-app>

    1.新建Model使用archetype-webapp框架

    2.pom.xml文件

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0modelVersion>
    4. <groupId>com.it.dubbogroupId>
    5. <artifactId>008-zk-consumerartifactId>
    6. <version>1.0-SNAPSHOTversion>
    7. <packaging>warpackaging>
    8. <dependencies>
    9. <dependency>
    10. <groupId>org.springframeworkgroupId>
    11. <artifactId>spring-contextartifactId>
    12. <version>4.3.16.RELEASEversion>
    13. dependency>
    14. <dependency>
    15. <groupId>org.springframeworkgroupId>
    16. <artifactId>spring-webmvcartifactId>
    17. <version>4.3.16.RELEASEversion>
    18. dependency>
    19. <dependency>
    20. <groupId>com.alibabagroupId>
    21. <artifactId>dubboartifactId>
    22. <version>2.6.2version>
    23. dependency>
    24. <dependency>
    25. <groupId>com.it.dubbogroupId>
    26. <artifactId>006-zk-interfaceartifactId>
    27. <version>1.0-SNAPSHOTversion>
    28. dependency>
    29. <dependency>
    30. <groupId>org.apache.curatorgroupId>
    31. <artifactId>curator-frameworkartifactId>
    32. <version>4.1.0version>
    33. dependency>
    34. dependencies>
    35. <build>
    36. <plugins>
    37. <plugin>
    38. <artifactId>maven-compiler-pluginartifactId>
    39. <version>3.1version>
    40. <configuration>
    41. <source>1.8source>
    42. <target>1.8target>
    43. configuration>
    44. plugin>
    45. plugins>
    46. build>
    47. project>

    3.UserController类

    1. package com.it.dubbo.web;
    2. import com.it.dubbo.entity.User;
    3. import com.it.dubbo.service.UserService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.ui.Model;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. @Controller
    9. public class UserController {
    10. @Autowired
    11. private UserService userService;
    12. @RequestMapping(value = "/userDetail")
    13. public String userDetail(Integer id,String name, Model model){
    14. User user = userService.queryUserById(id, name);
    15. model.addAttribute("user",user);
    16. return "userDetail";
    17. }
    18. }

    4.applicationContext.xml配置文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:mvc="http://www.springframework.org/schema/mvc"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    6. <context:component-scan base-package="com.it.dubbo.web"/>
    7. <mvc:annotation-driven/>
    8. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    9. <property name="prefix" value="/"/>
    10. <property name="suffix" value=".jsp"/>
    11. bean>
    12. beans>

    5.dubbo-zk-consumer.xml文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
    3. 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">
    4. <dubbo:application name="008-zk-consumer"/>
    5. <dubbo:registry address="zookeeper://localhost:2181"/>
    6. <dubbo:reference id="userService" interface="com.it.dubbo.service.UserService"/>
    7. beans>

     6.web.xml文件

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    4. version="4.0">
    5. <servlet>
    6. <servlet-name>dispatcherServletservlet-name>
    7. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    8. <init-param>
    9. <param-name>contextConfigLocationparam-name>
    10. <param-value>classpath:applicationContext.xml,classpath:dubbo-zk-consumer.xmlparam-value>
    11. init-param>
    12. servlet>
    13. <servlet-mapping>
    14. <servlet-name>dispatcherServletservlet-name>
    15. <url-pattern>/url-pattern>
    16. servlet-mapping>
    17. web-app>

    7.userDetail.jsp文件

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: lijianglin
    4. Date: 2022/8/13
    5. Time: 8:33
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>用户详情title>
    12. head>
    13. <body>
    14. <div>用户编号:${user.id}div>
    15. <div>用户姓名:${user.name}div>
    16. body>
    17. html>

    服务器配置

    功能测试:

    1. 先启动注册中心
    2. 再启动 tomcat 服务器
    3. 访问 008-zk-consumer的userDetail.jsp

     

  • 相关阅读:
    matlab画自定义函数
    左值和右值
    Apollo7.0系统概述
    CCF CSP认证 历年题目自练Day36
    [Spring] Spring5——IOC 简介(二)
    正则系列之断言Assertions
    【算法04】二分法常见题型
    基于Springboot+Vue实现高校疫情防控系统
    [山东科技大学OJ]1487 Problem B: 编写函数:字符串的查找字符 之二 (Append Code)
    RocksDB基本架构与原理详解
  • 原文地址:https://blog.csdn.net/weixin_59334478/article/details/126314655