# 细粒度配置自定义
- java代码配置
- 用配置属性配置
- 最佳实践总结
Ribbon支持非常灵活的配置,在实际项目中用的做多的就是配置它的负载均衡规则,例如:默认的ZoneAvoidanceRule满足不了我们业务上的需求,我们可以把这个规则改成随机的,Ribbon支持细粒度的配置,也就是说:假设内容中心,同时调用两个微服务,用户中心和微服务A,Ribbon可以实现让内容中心调用用中心的时候使用随机规则,而调用微服务A的时候,使用**ZoneAvoidanceRule **
# 导航
# 6-6细粒度配置自定义01-java代码方式
- 创建一个ribbonconfiguration,该ribbonconfiguration包一定要在启动类包之外,然后在该包下创建RibbonConfiguration类
package com.itmuch.ribbonconfiguration;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
/**
* @ClassName:RibbonConfiguration
* @Description:为什么这个包和类一定要在启动类之外?下面有解释
* @author: Torey
*/
@Configuration
public class RibbonConfiguration {
@Bean
public IRule ribbonRule(){
//RandomRule 随机选择一个Server
return new RandomRule();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 创建一个包,configuration,在该包下创建:UserCenterRibbonConfiguration类
package com.itmuch.contentcenter.configuration;
import com.itmuch.ribbonconfiguration.RibbonConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName:UserCenterRibbonConfiguration
* @Description:
* @author: Torey
*/
@Configuration
@RibbonClient(name = "user-center",configuration = RibbonConfiguration.class)
public class UserCenterRibbonConfiguration {
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 6-7 细粒度配置自定义02-父子上下文
# ribbonconfiguration包为什么一定要在启动类包之外?
为什么这个包和类一定要在启动类之外? 这个类有一个@Configuration注解,而@Configuration注解里面有一个@Component注解, 也就是说@Configuration其实也是一种特殊的@Component,我们再看启动类,启动类上有 一个@SpringBootApplication注解,@SpringBootApplication也是一个复合注解, 它包含了很多注解,其中有个一个@ComponentScan,@ComponentScan注解是用来扫描 Component的,也就是说:@Component、@Controller、@Repository、@Service等等这些 Component注解,都会被扫描到,包括@Configuration,也会被扫描到,那么默认扫描的 位置是哪里呢?是扫描当前启动类所在的包,以及启动类所在包下面的所有的子包,那么从这里 我们可以分析出:Ribbon的配置类,一定不能被启动类扫描到。
# 为啥不能扫描到呢?
答:因为Spring的上下文,其实是一个树状的上下文,在我们的应用里,ApplicationContext, 也就是@SpringBootApplication所扫描的上下文,叫主上下文,而 Ribbon呢,也有一个 上下文,是一个子上下文,那么父子上下文扫描的包一旦重叠,就会导致各种各样的奇葩问题, 如果父子上下文发生重叠:就会导致事务不生效 等很多奇葩问题: Ribbon的父子上下文发生重叠,导致的问题如下: Customconfiguration class必须是econfiguration类,但请注意它不在主应用程序 上下文的aecomponentscan中。否则,它将由所有eribbonclients共享。如果您使用ecomponentscan (或espringbootapplication),则需要采取措施避免将其包含在内 (例如,您可以将其放入单独的、不重叠的包中,或在ecomponentscan中明确指定要扫描的包)
# 6-8 细粒度配置自定义03-配置属性方式
# 6-9细粒度配置自定义03-两种方式对比
配置方式 | 优点 | 缺点 |
---|---|---|
代码配置 | 基于代码,更加灵活 | 有小坑(父子上下文),线上修改得重新打包、发布 |
属性配置 | 易上手 配置更加直观 线上修改无序重新打包、发布 优先级更高 | 极端场景下没有代码配置方式灵活 |
# 6-10 细粒度配置自定义04-最佳实践
- 尽量使用属性配置,属性方式实现不了的情况下在考虑用代码配置
- 在同一个微服务内尽量保持单一性,比如统一使用属性配置,不要两种方式混用,增加定位代码的复杂性
# 6-11 全局配置
//实现了全局配置
@RibbonClients(defaultConfiguration= RibbonConfiguration.class)
2
package com.itmuch.contentcenter.configuration;
import com.itmuch.ribbonconfiguration.RibbonConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName:UserCenterRibbonConfiguration
* @Description:
* @author: Torey
*/
@Configuration
//@RibbonClient(name = "user-center",configuration = RibbonConfiguration.class)
//实现了全局配置
@RibbonClients(defaultConfiguration= RibbonConfiguration.class)
public class UserCenterRibbonConfiguration {
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 6-12 支持的配置项
# 6-13 Ribbon的饥饿加载
默认情况下Ribbon是懒加载的,也就是第一次请求的时候,才会去加载,所以第一次加载特别的慢,可以开启饥饿加载
ribbon:
eager-load:
# 开启饥饿加载
enabled: true
# 为哪些微服务实现饥饿加载,多个用逗号分隔
clients: user-center
2
3
4
5
6
# 6-18 现有架构存在的问题
- 来了一个新员工,我不知道这个URL是做什么的,也就是 代码不可读
- 复杂的URL难以维护: 如果URL非常复杂,有很多参数,每个测试时什么意思,也都不知道
- 难以响应需求的变化,比如:如果请求参数经常变,维护这个参数超痛苦
- 编程体验不统一,URL参数没有提示
package com.itmuch.contentcenter.service.content;
import com.itmuch.contentcenter.dao.share.ShareMapper;
import com.itmuch.contentcenter.domain.dto.content.ShareDTO;
import com.itmuch.contentcenter.domain.dto.user.UserDTO;
import com.itmuch.contentcenter.domain.entity.share.Share;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.naming.ldap.PagedResultsControl;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
/**
* @ClassName:ShareService
* @Description:
* @author: Torey
*/
@Slf4j
@Service
public class ShareService {
@Autowired
private ShareMapper shareMapper;
@Autowired
private RestTemplate restTemplate;
public ShareDTO findById(Integer id){
Share share = shareMapper.selectByPrimaryKey(id);
Integer userId = share.getUserId();
//当restTemplate去请求的时候,Ribbon会自动的把user-center这个名称转换成用户中心在Nacos上的地址,
//并且根据负载均衡算法,返回一个实例给restTemplate去请求
//现有系统存在的问题
// 1. 来了一个新员工,我不知道这个URL是做什么的,也就是 代码不可读
// 2. 复杂的URL难以维护: 如果URL非常复杂,有很多参数,每个测试时什么意思,也都不知道
// 3. 难以响应需求的变化,比如:如果请求参数经常变,维护这个参数超痛苦
// 4. 编程体验不统一,URL参数没有提示
UserDTO userDTO= restTemplate.getForObject("http://user-center/users/{userId}",
UserDTO.class,
userId);
// 消息的装配
ShareDTO shareDTO=new ShareDTO();
BeanUtils.copyProperties(share,shareDTO);
shareDTO.setWxNickname(userDTO.getWxNickname());
return shareDTO;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56