分布式项目分解SSM(1spring+springMVC+mybatis+maven+zookeepker+dubbo综合练习)关键代码实现

目录查看代码:

项目要求:

两种实现方式(注解注册、注解发现)(配置xml文件注册、 配置xml文件发现)

消费者(一):

发布者项目结构:

发布者的项目依赖:

其他的都和之前的MVC项目结构一致,唯一不同的是serviceImpl实现类:

application-mapper.xml

application-provider.xml文件

application-service.xml文件配置

实现类的java代码部分(发布、service.impl实现类)

Service实现类:

发布代码:

 

消费者:

pom依赖

resources配置文件:

application-consumer.xml文件

springmvc.xml文件

web.xml文件

Controller层文件

发布者(二)使用注解的方式发布service实现类

dubbo-provider.properties配置文件:

applicationContext-mybatis.xml配置文件

Service实现类

发布代码:

消费者(二)

dubbo-consumer.properties配置文件

springmvc配置文件

controller层文件

ConsumerConfig配置文件

EmpController层(注解就用Reference的注解)

 

注解实现文件的发现,与之前注解启动发现服务不同的是需要main方法来加载容器,我们这里把类放在了controller层中,这样spring容器启动会扫描controller层,久会扫描到该类的注解,创建对象读取数据等等一系列的配置


 

项目要求:


两种实现方式(注解注册、注解发现)(配置xml文件注册、 配置xml文件发现)

我们使用

消费者(一):

发布者项目结构:

发布者的项目依赖:

<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>
  <parent>
    <groupId>com.bjsxt.emp</groupId>
    <artifactId>test-emp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>com.bjsxt.emp-rpc</groupId>
  <artifactId>test-emp-rpc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  	<dependencies>
		<!-- mysql依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.47</version>
		</dependency>
		<!-- mybatis依赖 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>
		<!-- mybatis-spring依赖 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>2.0.1</version>
		</dependency>
		<!-- pagehelper依赖 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>4.0.3</version>
		</dependency>

		<!-- druid依赖 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.18</version>
		</dependency>
		<!-- spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<!-- spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<!-- spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<!-- spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<!-- spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<!-- spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		
		<!-- dubbo全部依赖 -->
		
				<!-- 添加dubbo的依赖 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.5</version>
		</dependency>
		 
		<dependency>
			<groupId>com.alibaba.spring</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>1.0.2</version>
		</dependency>

		 <dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>4.1.0</version>
			 <!-- 先排除zookeeper -->
		   <exclusions>
		    	<exclusion>
		    		<groupId>org.apache.zookeeper</groupId>
		    		<artifactId>zookeeper</artifactId>
		    	</exclusion>
		    </exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>4.1.0</version>
			
		</dependency> 
		<!-- 添加zookeeper3.4.x版本 -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.8</version>
		</dependency> 
		
		</dependencies>
</project>

其他的都和之前的MVC项目结构一致,唯一不同的是serviceImpl实现类:

application-mapper.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"              
    xsi:schemaLocation="                                               
            http://www.springframework.org/schema/beans    
            http://www.springframework.org/schema/beans/spring-beans.xsd    
          ">
           
     <!-- 实例化datasource数据库连接池 -->
     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     	<property  name="driverClassName" value="com.mysql.jdbc.Driver"></property>
     	<property name="url" value="jdbc:mysql://192.168.25.10:3306/cas"></property>
     	<property name="username" value="root"></property>
     	<property name="password" value="root"></property>
     </bean>   
     
     <!-- 实例化SqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     	<!-- 输入数据库连接池 -->
     	<property name="dataSource" ref="dataSource"></property>
     	
     </bean>    
     
     <!-- 扫描Mapper接口,产生代理对象 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="com.bjsxt.mapper"></property>
     	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>
     
</beans>

application-provider.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"   
	xmlns:context="http://www.springframework.org/schema/context"  
    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 
            http://www.springframework.org/schema/context    
            http://www.springframework.org/schema/context/spring-context.xsd
            http://code.alibabatech.com/schema/dubbo
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd      
          ">
          
          
    
      
    
     <!-- 发布远程服务 -->
     
     <!-- 1 给发布的远程服务器名字 -->
     <dubbo:application name="emp-provider"></dubbo:application>
     <!-- 2 指定服务的注册中心 -->
     <dubbo:registry protocol="zookeeper" 
     	address="192.168.25.101:2181,192.168.25.101:2182,192.168.25.101:2183" 
     	client="curator"/>
     <!-- 3 指定远程服务的访问协议和端口 -->
     <dubbo:protocol name="dubbo" port="20880" />
     <!-- 
     	4 声明需要暴露的服务对应的接口 
     	 interface 需要暴露的服务对应的接口的完全限定名
     	 ref 引用实现类的对象
     -->
     <dubbo:service interface="com.bjsxt.service.EmpService" ref="empServiceImpl" />
     
     
     
     
</beans>

application-service.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
    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
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context.xsd 
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd 
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd    
          ">
          
     <!-- 扫描业务对象 -->
     <context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan> 
	 
	<!-- 实例化事务管理器对象 -->
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 声明事务切面 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="load*" read-only="true"/>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="save*"/>
			<tx:method name="update*"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<!-- 进行aop的织入 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.bjsxt.service.impl.*.*(..))" id="pc"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
	</aop:config>
</beans>

实现类的java代码部分(发布、service.impl实现类)

Service实现类:

package com.bjsxt.service.impl;

import java.util.List;

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

//import com.alibaba.dubbo.config.annotation.Service;
import com.bjsxt.mapper.EmpMapper;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;

@Service
public class EmpServiceImpl implements EmpService {

	@Autowired
	private EmpMapper empMapper;
	@Override
	public List<Emp> loadEmpListService(Emp emp) {
		// TODO Auto-generated method stub
		return empMapper.loadEmpListMapper(emp);
	}

	@Override
	public void deleteEmpService(Integer id) {
		// TODO Auto-generated method stub
		empMapper.deleteEmpMapper(id);
	}

}

发布代码:

package com.bjsxt.app;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RpcApp {

	
	public static void main(String[] args) {
		/****
		 * 加载spring容器暴露服务
		 */
		ClassPathXmlApplicationContext 
			ac=new ClassPathXmlApplicationContext("application-mapper.xml",
					"application-service.xml","application-provider.xml");
		
		ac.start();
		
		System.out.println("========暴露服务========");
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

这样就可以把代码发布到dubbo集群中

 

消费者:


pom依赖

<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>
	<parent>
		<groupId>com.bjsxt.emp</groupId>
		<artifactId>test-emp</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.bjsxt.emp.manager</groupId>
	<artifactId>test-emp-manager</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<!-- =============================== -->
	<dependencies>
		<!-- spring依赖 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>
		<!-- jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- jsp -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
			<scope>provided</scope>
		</dependency>
		<!-- servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.4</version>
		</dependency>

		<!-- 添加jackson依赖 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.9.0</version>
		</dependency>


		<!--============== dubbo全部依赖================ -->
		<!-- 添加dubbo的依赖 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.6.5</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba.spring</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>1.0.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-framework</artifactId>
			<version>4.1.0</version>
			<!-- 先排除zookeeper -->
			<exclusions>
				<exclusion>
					<groupId>org.apache.zookeeper</groupId>
					<artifactId>zookeeper</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.apache.curator</groupId>
			<artifactId>curator-recipes</artifactId>
			<version>4.1.0</version>

		</dependency>
		<!-- 添加zookeeper3.4.x版本 -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.8</version>
		</dependency>

	</dependencies>
	<!-- 声明需要的插件 -->
	<build>
		<plugins>
			<!-- 添加tomcat插件 -->
			<plugin>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<groupId>org.apache.tomcat.maven</groupId>
				<version>2.2</version>
				<configuration>
					<port>8080</port>
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

resources配置文件:

application-consumer.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"    
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"   
	xmlns:context="http://www.springframework.org/schema/context"  
    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 
            http://www.springframework.org/schema/context    
            http://www.springframework.org/schema/context/spring-context.xsd
            http://code.alibabatech.com/schema/dubbo
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd      
          ">
      
      <!-- 1 给消费端服务起名字 -->
      <dubbo:application name="emp-consumer"></dubbo:application>
      
       <!-- 2 指定服务的注册中心 -->
      <dubbo:registry protocol="zookeeper" 
     	address="192.168.25.101:2181,192.168.25.101:2182,192.168.25.101:2183" 
     	client="curator"/>
     	
      <!-- 3 获得远程服务代理对象,代码对象在spring容器保存
      	interface 指定代理对象实现的接口
      	id 用来引用代理对象
       -->
     <!--  <dubbo:reference interface="com.bjsxt.service.EmpService"
      	 id="empServiceProxy"></dubbo:reference> -->
     <!--  <dubbo:annotation package="com.bjsxt.controller"/> -->
      
      
     
     
</beans>

springmvc.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" 
    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://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 
            http://code.alibabatech.com/schema/dubbo
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd 
          ">
       <!-- 扫描controller对象 --> 
       <context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
       <!-- 配置注解驱动 -->
       <mvc:annotation-driven></mvc:annotation-driven>
       <!-- 配置静态资源映射 -->
       <!-- <mvc:resources location="/xxx/" mapping="/xxx/**"></mvc:resources> -->
       <!-- 配置视图解析器 --> 
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       		<property name="prefix" value="/WEB-INF/jsp/"></property>
       		<property name="suffix" value=".jsp"></property>
       </bean>
       <!-- 配置文件上传解析器 -->
       <dubbo:annotation package="com.bjsxt.controller"/>
       <!-- 注册拦截器 --> 
</beans>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
  <display-name>emp-manager</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
  
  <!-- 加载spring容器(spring的xml文件) -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:application-*.xml</param-value>
  </context-param>
  <!-- 注册监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置前端控制器,servlet -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 配置过滤器,处理post请求的乱码 -->
  <filter>
  	<filter-name>charset</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>charset</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Controller层文件

package com.bjsxt.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;

@Controller
public class EmpController {

	/*@Autowired(如果配置文件用第一种就用该注解)
	private EmpService empService;
	*/@Reference
	private EmpService empService;
	
	
	/***
	 * 处理员工信息的查询请求
	 */
	@RequestMapping("/loadEmpList")
	public String loadEmpList(Emp emp,Model model) {
		List<Emp> result = empService.loadEmpListService(emp);
		model.addAttribute("result", result);
		return "list";
	}
	/***
	 * 处理员工信息删除请求
	 */
	@RequestMapping("/deleteEmp")
	public String deleteEmp(Integer id) {
		empService.deleteEmpService(id);
		return "redirect:/loadEmpList";
	}
}

 

 



发布者(二)使用注解的方式发布service实现类


只有在service实现类的时候会有差距。让我们先看一下配置文件

dubbo-provider.properties配置文件:

#给发布的远程服务起名字
dubbo.application.name=dubbo-provider
#指定注册中心访问地址
dubbo.registry.address=zookeeper://192.168.220.10:2181
#指定暴露的服务的协议
dubbo.protocol.name=dubbo
#指定暴露的服务的端口
dubbo.protocol.port=20880

 


applicationContext-mybatis.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">


       <!--【A】连接数据库获得数据源-->

       <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">

           <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

           <property name="url" value="jdbc:mysql://localhost:3306/test"></property>

           <property name="username" value="root"></property>

           <property name="password" value="root"></property>

       </bean>

      <!--【B】获得session工厂-->

       <bean id="factroy" class="org.mybatis.spring.SqlSessionFactoryBean">

           <property name="dataSource" ref="ds"></property>

           <property name="typeAliasesPackage" value="com.bjsxt.pojo"></property>
       </bean>


      <!--【C】扫描mapper文件-->

       <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">

           <property name="sqlSessionFactoryBeanName" value="factroy"></property>

           <property name="basePackage" value="com.bjsxt.mapper"></property>
       </bean>

</beans>

Service实现类

package com.bjsxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjsxt.mapper.EmpMapper;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;

/**
 * 雇员业务层实现类
 * @author Administrator
 *
 */
@Service
public class EmpServiceImpl implements EmpService {
	
	@Autowired
	EmpMapper empMapper;
 
	/**
	 * 查询指定条件的雇员
	 */
	@Override
	public List<Emp> select4Condition(Emp emp) {
		
		return empMapper.select4Condition(emp);
	}
	
	/**
	 * 删除id为x的员工
	 */
	@Override
	public int delete(Integer id) {
		return empMapper.delete(id);
	}

}

发布代码:

package com.bjsxt.provider;

import java.io.IOException;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

public class EmpProvider {

	public static void main(String[] args) {
		// 加载spring容器
		AnnotationConfigApplicationContext 
			context = new AnnotationConfigApplicationContext(Configer.class);

		// 启动spring容器
		context.start();

		System.out.println("==============完成服务的发布=============");
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Configuration // 实例化静态内部类所在的类
	@EnableDubbo(scanBasePackages = "com.bjsxt.service.impl")
	@PropertySource(value = "classpath:/dubbo-provider.properties")
	@ImportResource(value= {"applicationContext-mybatis.xml"})
	//指定需要扫描Spring组件所在的包
	@ComponentScan(value = {"com.bjsxt.controller"})
	static class Configer {

	}

}

 

 

消费者(二)

dubbo-consumer.properties配置文件

#给发布的远程服务起名字
dubbo.application.name=emp-consumer
#指定注册中心访问地址
dubbo.registry.address=zookeeper://192.168.25.101:2181

springmvc配置文件

<?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" 
    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://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 
            http://code.alibabatech.com/schema/dubbo
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd 
          ">
       <!-- 扫描controller对象 --> 
       <context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
       <!-- 配置注解驱动 -->
       <mvc:annotation-driven></mvc:annotation-driven>
       <!-- 配置静态资源映射 -->
       <!-- <mvc:resources location="/xxx/" mapping="/xxx/**"></mvc:resources> -->
       <!-- 配置视图解析器 --> 
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       		<property name="prefix" value="/WEB-INF/jsp/"></property>
       		<property name="suffix" value=".jsp"></property>
       </bean>
       <!-- 配置文件上传解析器 -->
     <!--   <dubbo:annotation package="com.bjsxt.controller"/> -->
       <!-- 注册拦截器 --> 
</beans>

 

controller层文件

第一个是用来加载静态代码块配置dubbo的,第二个就是controller层的代码

ConsumerConfig配置文件

package com.bjsxt.controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@Configuration //将静态内部类实例化
//加载dubbo-consumer.properties
@PropertySource("classpath:/dubbo-consumer.properties")
//指定暴露的服务接口对应的包
@EnableDubbo(scanBasePackages = "com.bjsxt.service")
//指定需要扫描Spring组件所在的包
//@ComponentScan(value = {"com.bjsxt.controller"})
public class ConsumerConfig {

	public ConsumerConfig() {
		System.out.println("=========================");
	}
}

EmpController层(注解就用Reference的注解)

package com.bjsxt.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjsxt.pojo.Emp;
import com.bjsxt.service.EmpService;

@Controller
public class EmpController {

	/*@Autowired
	private EmpService empService;
	*/@Reference
	private EmpService empService;
	
	
	/***
	 * 处理员工信息的查询请求
	 */
	@RequestMapping("/loadEmpList")
	public String loadEmpList(Emp emp,Model model) {
		List<Emp> result = empService.loadEmpListService(emp);
		model.addAttribute("result", result);
		return "list";
	}
	/***
	 * 处理员工信息删除请求
	 */
	@RequestMapping("/deleteEmp")
	public String deleteEmp(Integer id) {
		empService.deleteEmpService(id);
		return "redirect:/loadEmpList";
	}
}

 

注解实现文件的发现,与之前注解启动发现服务不同的是需要main方法来加载容器,我们这里把类放在了controller层中,这样spring容器启动会扫描controller层,久会扫描到该类的注解,创建对象读取数据等等一系列的配置

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章