发表于: 2019-10-14 18:40:52
1 898
今天做了什么
server打jar包运行:
得在pom中加入插件:
<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>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!--这一句很重要,指定程序的入口-->
<mainClass>com.server.MajorServer</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
部署两台Service,在Web中随机访问一台Service:
方法一.使用cluster4spring.jar ; 当调用一个服务失败时,它可以切换到服务列表中的另一个服务端.
<bean id="majorRMI"
class="org.softamis.cluster4spring.rmi.RmiUrlListProxyFactoryBean" parent="rmiClientSetting">
<!--调用服务接口-->
<property name="serviceInterface" value="com.service.MajorService"/>
<!--远程对象地址list ,如果第一个无法使用,会抛异常,然后使用第二个去调用远程对象方法返回结果-->
<property name="serviceURLs" >
<list>
<value>rmi://localhost:1099/majorService</value>
<value>rmi://localhost:1199/majorService</value>
</list>
</property>
<!-- 调用时选择哪个url的策略 -->
<property name="endpointSelectionPolicy">
<bean class="org.softamis.cluster4spring.support.invocation.InTurnEndpointSelectionPolicy"/>
</property>
</bean>
方法二.使用基础的if else, 通过生成随机数来实现随机切换服务端.
public class SwitchService {
private static final Logger log = LogManager.getLogger(SwitchService.class);
private static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
public static UserService getUSerService(){
UserService userService = null;
Random random = new Random();
if (random.nextInt(2)==1){
try{
log.error("get connection to server1");
userService = (UserService) context.getBean("majorRMI1");
} catch (Exception e){
log.error("connect to server1 failed.turn to server2");
userService = (UserService) context.getBean("majorRMI2");
}
}else {
try{
log.error("get connection to server2");
userService = (UserService) context.getBean("majorRMI2");
} catch (Exception e){
log.error("connect to server2 failed.turn to server1");
userService = (UserService) context.getBean("majorRMI1");
}
}
return userService;
}
}
收获
问题
运行客户端时报错:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.softamis.cluster4spring.rmi.RmiUrlListProxyFactoryBean] for bean with name 'majorRMI' defined in class path resource [spring-config-client.xml]; nested exception is java.lang.ClassNotFoundException: org.softamis.cluster4spring.rmi.RmiUrlListProxyFactoryBean
找不到[spring-config-client.xml]中的类"org.softamis.cluster4spring.rmi.RmiUrlListProxyFactoryBean".
不清楚出错原因.
明天计划
评论