发表于: 2020-06-18 23:29:58

1 1725


今天完成的事情:


项目服务器运行完毕


服务端  



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

   <bean id="accountServiceImpl" class="com.ptteng.service.serviceImpl.accountServiceImpl"></bean>
   <bean id="companyServiceImpl" class="com.ptteng.service.serviceImpl.companyServiceImpl"></bean>
   <bean id="professionServiceImpl" class="com.ptteng.service.serviceImpl.professionServiceImpl"></bean>
   <bean id="studentServiceImpl" class="com.ptteng.service.serviceImpl.studentServiceImpl"></bean>


   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="accountA" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="accountServiceImpl" />
       <property name="serviceName" value="accountA"></property>
       <property name="serviceInterface" value="com.ptteng.service.accountService"></property>
       <property name="registryPort" value="8888"></property>
   </bean>

   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="companyA" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="companyServiceImpl" />
       <property name="serviceName" value="companyA"></property>
       <property name="serviceInterface" value="com.ptteng.service.companyService"></property>
       <property name="registryPort" value="8888"></property>
   </bean>

   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="professionA" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="professionServiceImpl" />
       <property name="serviceName" value="professionA"></property>
       <property name="serviceInterface" value="com.ptteng.service.professionService"></property>
       <property name="registryPort" value="8888"></property>
   </bean>


   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="studentA" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="studentServiceImpl" />
       <property name="serviceName" value="studentA"></property>
       <property name="serviceInterface" value="com.ptteng.service.studentService"></property>
       <property name="registryPort" value="8888"></property>
   </bean>

<!--    ========================bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb========================-->


   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="accountB" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="accountServiceImpl" />
       <property name="serviceName" value="accountB"></property>
       <property name="serviceInterface" value="com.ptteng.service.accountService"></property>
       <property name="registryPort" value="8887"></property>
   </bean>


   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="companyB" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="companyServiceImpl" />
       <property name="serviceName" value="companyB"></property>
       <property name="serviceInterface" value="com.ptteng.service.companyService"></property>
       <property name="registryPort" value="8887"></property>
   </bean>

   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="professionB" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="professionServiceImpl" />
       <property name="serviceName" value="professionB"></property>
       <property name="serviceInterface" value="com.ptteng.service.professionService"></property>
       <property name="registryPort" value="8887"></property>
   </bean>

   <!-- 将一个类发布为一个RMI服务 -->
   <bean id="studentB" class="org.springframework.remoting.rmi.RmiServiceExporter">
       <property name="service" ref="studentServiceImpl" />
       <property name="serviceName" value="studentB"></property>
       <property name="serviceInterface" value="com.ptteng.service.studentService"></property>
       <property name="registryPort" value="8887"></property>
   </bean>


</beans>


SpringServer服务启动端

package com.ptteng.Server;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringServer {

   public static void main(String[] args) {

       ApplicationContext applicationContext= new ClassPathXmlApplicationContext("spring/SpringServer.xml","spring/applicationContext.xml");
//        ApplicationContext applicationContexta= new ClassPathXmlApplicationContext("spring/SpringServerB.xml");
       System.out.println("远程服务已启动");

   }
}


打包Jar在服务器运行


这是个大坑   试了很多方式  才成功



1.直接package   没有打包进依赖    no attribute错误


2.maven -assembly-plugin打包     no main class 错误


3. idea   artifacts   添加jar 打包     spring配置头冲突的错误


3. maven -shade-plugin打包     

(可以解决配置头冲突问题)

我看师兄都是用这个,有成功的也有失败的。   


我是失败了  打包过程中怎么也打包不进去依赖


最后找到这大哥的文章  解决了问题

https://www.cnblogs.com/leekeggs/p/10276672.html


还需要添加这个才行:




pom  最终的plugin配置

<plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
         <version>1.4</version>
         <executions>
             <execution>
                 <phase>package</phase>
                 <goals>
                     <goal>shade</goal>
                 </goals>
                 <configuration>

                     <transformers>
                         <transformer
                                 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                             <resource>META-INF/spring.handlers</resource>
                         </transformer>
                         <transformer
                                 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                             <mainClass>com.ptteng.Server.SpringServer</mainClass>
                         </transformer>
                         <transformer
                                 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                             <resource>META-INF/spring.schemas</resource>
                         </transformer>
                     </transformers>
                 </configuration>
             </execution>
         </executions>
     </plugin>

 </plugins>

</pluginManagement>

 <plugins>
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
     </plugin>
 </plugins>


mvn clean installl  打包

放到服务器运行jar

启动成功!




客户端:



客户端也要配置实体类   和 接口


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

   <bean id="studentClientA"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.studentService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8888/studentA"></property>
   </bean>

   <bean id="companyClientA"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.companyService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8888/companyA"></property>
   </bean>
   <bean id="professionClientA"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.professionService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8888/professionA"></property>
   </bean>
   <bean id="accountClientA"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.accountService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8888/accountA"></property>
   </bean>

   <!--==============   clienB  ===========================-->


   <bean id="studentClientB"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.studentService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8887/studentB"></property>
   </bean>

   <bean id="companyClientB"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.companyService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8887/companyB"></property>
   </bean>
   <bean id="professionClientB"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.professionService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8887/professionB"></property>
   </bean>
   <bean id="accountClientB"  class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
       <property name="serviceInterface" value="com.ptteng.service.accountService"></property>
       <property name="serviceUrl" value="rmi://127.0.0.1:8887/accountB"></property>
   </bean>

</beans>






在controller层启动客户端发送请求  并且调用服务段的servise接口


因为有俩服务 

所以通过生成 0  1的随机数进行随机访问

决定访问A端口  还是B端口

int index = (intMath.floor((Math.random() (2)));

       logger.info("当前随机数是"+index+"开始执行下一步");


然后:

如果A端口访问出错 访问B  

 如果B端口访问出错 访问A


这样即使断掉一个服务  

也能正常访问。



其中一个controller文件 

studentController.calss

package com.ptteng.controller;

import com.ptteng.enity.student;
import com.ptteng.service.studentService;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class studentController {


   Logger logger = Logger.getLogger(studentController.class);

   //记载客户端文件
   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/springClient.xml");

   @ResponseBody
   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public List<student> allStudent() {

       int index = (int) Math.floor((Math.random() * (2)));

       logger.info("当前随机数是"+index+"开始执行下一步");

       if (index == 0) {

           try {
               logger.info("获取A服务接口");
               studentService studentA = (studentService) applicationContext.getBean("studentClientA");
               List<student> list = studentA.allStudent();
               logger.info("A服务查询的数据为" + list);
               return list;

           } catch (Exception e) {

               logger.info("A出错,现在访问B服务接口");
               studentService studentB = (studentService) applicationContext.getBean("studentClientB");
               List<student> list = studentB.allStudent();
               System.out.println("B服务查询的数据为" + list);
               return list;

           }

       } else {

           try {
               logger.info("获取B服务接口");
               studentService studentB = (studentService) applicationContext.getBean("studentClientB");
               List<student> list = studentB.allStudent();
               logger.info("B服务查询的数据为" + list);
               return list;

           } catch (Exception e) {

               logger.info("B出错,现在访问A服务接口");
               studentService studentA = (studentService) applicationContext.getBean("studentClientA");
               List<student> list = studentA.allStudent();
               System.out.println("A服务查询的数据为" + list);
               return list;

           }

       }

   }
}





然后打包war   部署到tomcat



关于这个要求



把war包部署到

tomcat  

resin


然后nginx 配置负载均衡  



jmete跑个200


tomcat日志   在运行




resin日志  也在运行  



运行过程中,访问的服务端接口也是随机的


明天计划的事情:


深度思考  

下个任务



返回列表 返回列表
评论

    分享到