interface
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d88206a36e33cc56ec20e22c109cd330.png)
User
package com.gizzard.model;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
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;
}
}
Userservice
package com.gizzard.service;
import com.gizzard.model.User;
public interface UserService {
User queryUserById(Integer id,String username);
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gizzard</groupId>
<artifactId>006-zk-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
Userservice-provider
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d137044fcd8449457a0d510cd8fb6b24.png)
UserServiceImpl
package com.gizzard.service.impl;
import com.gizzard.model.User;
import com.gizzard.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public User queryUserById(Integer id,String username) {
User user = new User();
user.setId(id);
user.setUsername(username);
return user;
}
}
dubbo-zk-userservice-provider.xml
<?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://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">
<dubbo:application name="007-zk-userservice-provider"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:service interface="com.gizzard.service.UserService" ref="userServiceImpl"/>
<bean id="userServiceImpl" class="com.gizzard.service.impl.UserServiceImpl"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo-zk-userservice-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
consumer
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/02f3d26c2dde44d3df77cd6f24cea864.png)
UserController
package com.gizzard.web;
import com.gizzard.model.User;
import com.gizzard.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/userDetail")
public String userDetail(Model model, Integer id, String username){
User user = userService.queryUserById(id, username);
model.addAttribute("user",user);
return "userDetail";
}
}
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<context:component-scan base-package="com.gizzard.web"/>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
dubbo-zk-consumer.xml
<?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="008-zk-consumer"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:reference id="userService"
interface="com.gizzard.service.UserService"
url="dubbo://localhost:20880"
/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:dubbo-zk-consumer.xml,
classpath:applicationContext.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
userDetail.jsp
<%--
Created by IntelliJ IDEA.
User: JiZhen
Date: 2021/11/15
Time: 9:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户详情</title>
</head>
<body>
<h3>用户详情</h3>
<hr>
<h3>用户标识:${user.id}</h3>
<h3>用户姓名:${user.username}</h3>
</body>
</html>