spring的分散配置

分散配置

a)   引入外部bean来读取外部配置文件这样在<value>${name}</value>可以使用这个占位符来注入值了

[html]  view plain copy
  1. <!--第一种,引入外部bean来读取外部配置文件 -->  
  2.   
  3. <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  4.   
  5.     <!--引入单个文件-->  
  6.   
  7. property name="location">  
  8.   
  9.         <value>person.properties</value>  
  10.   
  11.     </property>  
  12.   
  13. <!--引入多个文件-->  
  14.   
  15.     <property name="locations">  
  16.   
  17.         <list>  
  18.   
  19.             <value>person.properties</value>  
  20.   
  21.         </list>  
  22.   
  23.     </property>  
  24.   
  25. </bean>  


b)   使用spring自带的context工具来读取外部文件

首先需要引入spring的context的命名空间和连接地址 。然后使用

<context:property-placeholderlocation="person.properties"/>来引入文件

c)   定制编辑器后处理类

i.   在配置文件中配置

[html]  view plain copy
  1. <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  2.   
  3.       <property name="customEditors">  
  4.   
  5.          <map>  
  6.   
  7.              <!-- key指向的是:需要转换的类 -->  
  8.   
  9.              <entry key="cn.csdn.service.Address">  
  10.   
  11.                    <!-- value指向的是 实现的编辑器类 -->  
  12.   
  13.                   <bean class="cn.csdn.editor.AddressEditor"/>  
  14.   
  15.              </entry>  
  16.   
  17.          </map>  
  18.   
  19.       </property>  
  20.   
  21.  </bean>  


ii. 在java中首先需要继承PropertyEditorSupport这个类然后实现setAsText方法,代码如下

[java]  view plain copy
  1. publicclass AddressEditor extends PropertyEditorSupport{  
  2.   
  3.    
  4.   
  5.     @Override  
  6.   
  7.     publicvoid setAsText(String text) throws IllegalArgumentException {  
  8.   
  9.         // TODO Auto-generated method stub  
  10.   
  11.         //java实现转换  
  12.   
  13.         if(!"".equals(text)){  
  14.   
  15.              String args[] = text.split("\\.");  
  16.   
  17.              if(args.length>3){  
  18.   
  19.                  Address address = new Address();  
  20.   
  21.                  address.setProvince(args[0]);  
  22.   
  23.                  address.setCity(args[1]);  
  24.   
  25.                  address.setStreet(args[2]);  
  26.   
  27.                  address.setZipCode(args[3]);  
  28.   
  29.                  setValue(address);  
  30.   
  31.              }else{  
  32.   
  33.                  this.setAsText(null);  
  34.   
  35.              }  
  36.   
  37.         }else{  
  38.   
  39.             this.setAsText(null);  
  40.   
  41.         }  
  42.   
  43. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud作为基于Spring Boot的微服务框架,为解决微服务中的通信、服务治理、负载均衡等问题提供了良好的解决方案。其中,Spring Cloud网关是实现微服务架构中请求分发和负载均衡的重要组件。 在Spring Cloud网关中,基于Zuul和Spring Cloud Gateway两种方案,可以实现对外的负载均衡配置。其中,采用Zuul的方式进行负载均衡的实现相对简单,但是功能相对较弱;而采用Spring Cloud Gateway方式实现的负载均衡则更加强大,也得到了更多的应用。 Spring Cloud Gateway是Spring Cloud生态中提供的一个新的网关解决方案,相比于Zuul,它的性能更高、更易扩展,并且还支持WebSocket。 在实现Spring Cloud Gateway的负载均衡功能时,需要在配置文件中指定所使用的负载均衡策略,例如: ``` spring: cloud: gateway: routes: - id: service1 uri: lb://service1 predicates: - Path=/service1/** filters: - StripPrefix=1 lb: client-name: service1 rule-config: RoundRobinLoadBalancer ``` 具体来说,上述配置文件中,路由规则为请求/service1/**会被转发到URI为lb://service1的微服务上,其中lb表示负载均衡策略,client-name表示微服务的名称,rule-config表示所使用的负载均衡算法。 除此之外,还可以通过添加Eureka Server以及Ribbon等依赖来进一步优化Spring Cloud Gateway的负载均衡能力,这样可以将服务注册在Eureka上,然后将IP地址分散在多台机器上,实现更好的负载均衡效果。 综上所述,Spring Cloud网关为微服务架构中的请求分发和负载均衡提供了良好的解决方案,在具体的配置时需要根据实际情况进行操作,以达到最佳的负载均衡效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值