app开发定制公司微服务核心三大组件(nacos,feign,ribbon)

: 

服务注册:Nacos Clientapp开发定制公司会通过发送RESTapp开发定制公司请求的方式向Nacos Serverapp开发定制公司注册自己的服务,app开发定制公司提供自身的元数据,比如ip地址、app开发定制公司端口等信息。
Nacos Server接收到注册请求后,就会把这些元数据信息存储在一个双层的内存Map中。
服务心跳:在服务注册后,Nacos Client会维护一个定时心跳来持续通知Nacos Server,说明服务一直处于可用状态,防止被剔除。默认5s发送一次心跳。
服务同步:Nacos Server集群之间会互相同步服务实例,用来保证服务信息的一致性。
服务发现:服务消费者(Nacos Client)在调用服务提供者的服务时,会发送一个REST请求给Nacos Server,获取上面注册的服务清单,并且缓存在Nacos Client本地,同时会在Nacos Client本地开启一个定时任务定时拉取服务端最新的注册表信息更新到本地缓存
服务健康检查:Nacos Server会开启一个定时任务用来检查注册服务实例的健康情况,对于超过15s没有收到客户端心跳的实例会将它的healthy属性置为false(客户端服务发现时不会发现),如果某个实例超过30秒没有收到心跳,直接剔除该实例(被剔除的实例如果恢复发送心跳则会重新注册)

的作用

从拉取服务,对http客户端封装,根据负载均衡策略对服务调用进行负载均衡

自定义负载均衡策略可以实现:

1、金丝雀发布(灰度发布),要在nacos服务元数据配置版本号,在调用时过滤掉不同版本的服务即可

2、同集群调用,根据nacos中的clusterName进行过滤

注意:不同的服务可以设置不同的负载均衡策略

feign的作用

服务调用,维护管理rest调用地址,内置拦截器对http请求可以进行拦截

feign-ribbon-feign相关联的源码调用链

定义Feign客户端

  1. @FeignClient(name = "product", contextId = "ds-mgr-ds-base"
  2. , configuration = {DefaultFallback.class})
  3. public interface TestService {
  4. @RequestMapping(value = "/product/service", method = RequestMethod.GET)
  5. public String product();
  6. }

其中product服务有三个实例:

在启动类中对product方法进行调用:

  1. @SpringBootApplication
  2. @EnableFeignClients
  3. public class NacosConfigApplication {
  4. public static void main(String[] args) {
  5. ApplicationContext ac = SpringApplication.run(NacosConfigApplication.class, args);
  6. TestService testService = ac.getBean(TestService.class);
  7. System.out.println(testService.product());
  8. }
  9. }

 首先TestService是通过factoryBean生成的bean,其实是一个代理对象

  1. class FeignClientFactoryBean
  2. implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {
  3. ..........................................
  4. @Override
  5. public Object getObject() throws Exception {
  6. return getTarget();
  7. }
  8. /**
  9. * @param <T> the target type of the Feign client
  10. * @return a {@link Feign} client created with the specified data and the context
  11. * information
  12. */
  13. <T> T getTarget() {
  14. FeignContext context = this.applicationContext.getBean(FeignContext.class);
  15. Feign.Builder builder = feign(context);
  16. if (!StringUtils.hasText(this.url)) {
  17. if (!this.name.startsWith("http")) {
  18. this.url = "http://" + this.name;
  19. }
  20. else {
  21. this.url = this.name;
  22. }
  23. this.url += cleanPath();
  24. return (T) loadBalance(builder, context,
  25. new HardCodedTarget<>(this.type, this.name, this.url));
  26. }
  27. if (StringUtils.hasText(this.url) && !this.url.startsWith("http")) {
  28. this.url = "http://" + this.url;
  29. }
  30. String url = this.url + cleanPath();
  31. Client client = getOptional(context, Client.class);
  32. if (client != null) {
  33. if (client instanceof LoadBalancerFeignClient) {
  34. // not load balancing because we have a url,
  35. // but ribbon is on the classpath, so unwrap
  36. client = ((LoadBalancerFeignClient) client).getDelegate();
  37. }
  38. builder.client(client);
  39. }
  40. Targeter targeter = get(context, Targeter.class);
  41. return (T) targeter.target(this, builder, context,
  42. new HardCodedTarget<>(this.type, this.name, url));
  43. }
  44. ................................................................................
  45. }

此时 TestService testService = ac.getBean(TestService.class);已经获取了代理对象,在执行testService.product()中,会执行以下invoke方法

  1. static class FeignInvocationHandler implements InvocationHandler {
  2. .......................................
  3. return dispatch.get(method).invoke(args);
  4. }

 点击invoke方法进去SynchronousMethodHandler中执行invoke方法

  1. @Override
  2. public Object invoke(Object[] argv) throws Throwable {
  3. RequestTemplate template = buildTemplateFromArgs.create(argv);
  4. Retryer retryer = this.retryer.clone();
  5. while (true) {
  6. ..............
  7. return executeAndDecode(template);
  8. ..................
  9. }
  10. }

 在执行executeAndDecode方法的时候,会调用client去执行rest调用,这个client此时是LoadBalancerFeignClient

  1. Object executeAndDecode(RequestTemplate template) throws Throwable {
  2. ...............................
  3. response = client.execute(request, options);
  4. ...................................
  5. }

这个类就是feign和ribbon的桥梁 

  1. public class LoadBalancerFeignClient implements Client {
  2. .............................
  3. @Override
  4. public Response execute(Request request, Request.Options options) throws IOException {
  5. try {
  6. URI asUri = URI.create(request.url());
  7. String clientName = asUri.getHost();
  8. URI uriWithoutHost = cleanUrl(request.url(), clientName);
  9. FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(
  10. this.delegate, request, uriWithoutHost);
  11. IClientConfig requestConfig = getClientConfig(options, clientName);
  12. return lbClient(clientName)
  13. .executeWithLoadBalancer(ribbonRequest, requestConfig).toResponse();
  14. }
  15. catch (ClientException e) {
  16. IOException io = findIOException(e);
  17. if (io != null) {
  18. throw io;
  19. }
  20. throw new RuntimeException(e);
  21. }
  22. }
  23. IClientConfig getClientConfig(Request.Options options, String clientName) {
  24. IClientConfig requestConfig;
  25. if (options == DEFAULT_OPTIONS) {
  26. requestConfig = this.clientFactory.getClientConfig(clientName);
  27. }
  28. else {
  29. requestConfig = new FeignOptionsClientConfig(options);
  30. }
  31. return requestConfig;
  32. }
  33. ....................................
  34. }

requestConfig = this.clientFactory.getClientConfig(clientName);

  1. @Override
  2. public <C> C getInstance(String name, Class<C> type) {
  3. C instance = super.getInstance(name, type);
  4. if (instance != null) {
  5. return instance;
  6. }
  7. IClientConfig config = getInstance(name, IClientConfig.class);
  8. return instantiateWithConfig(getContext(name), type, config);
  9. }

C instance = super.getInstance(name, type); 

  1. public <T> T getInstance(String name, Class<T> type) {
  2. AnnotationConfigApplicationContext context = getContext(name);
  3. if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
  4. type).length > 0) {
  5. return context.getBean(type);
  6. }
  7. return null;
  8. }
  1. protected AnnotationConfigApplicationContext getContext(String name) {
  2. if (!this.contexts.containsKey(name)) {
  3. synchronized (this.contexts) {
  4. if (!this.contexts.containsKey(name)) {
  5. this.contexts.put(name, createContext(name));
  6. }
  7. }
  8. }
  9. return this.contexts.get(name);
  10. }
  1. protected AnnotationConfigApplicationContext createContext(String name) {
  2. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  3. if (this.configurations.containsKey(name)) {
  4. for (Class<?> configuration : this.configurations.get(name)
  5. .getConfiguration()) {
  6. context.register(configuration);
  7. }
  8. }
  9. for (Map.Entry<String, C> entry : this.configurations.entrySet()) {
  10. if (entry.getKey().startsWith("default.")) {
  11. for (Class<?> configuration : entry.getValue().getConfiguration()) {
  12. context.register(configuration);
  13. }
  14. }
  15. }
  16. context.register(PropertyPlaceholderAutoConfiguration.class,
  17. this.defaultConfigType);
  18. context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(
  19. this.propertySourceName,
  20. Collections.<String, Object>singletonMap(this.propertyName, name)));
  21. if (this.parent != null) {
  22. // Uses Environment from parent as well as beans
  23. context.setParent(this.parent);
  24. // jdk11 issue
  25. // https://github.com/spring-cloud/spring-cloud-netflix/issues/3101
  26. context.setClassLoader(this.parent.getClassLoader());
  27. }
  28. context.setDisplayName(generateDisplayName(name));
  29. context.refresh();
  30. return context;
  31. }

我觉得这个方法是ribbion中最核心的方法,就是为每一个服务创建一个ioc容器上下文,这样的做的目的就是让每一个服务都能够自定义自己的负载均衡策略,还有调用不同注册中心的服务列表

他会把RibbonClientConfiguration这个配置类加载进容器

 在RibbonClientConfiguration类中有两个核心的bean

  1. @Bean
  2. @ConditionalOnMissingBean
  3. public IRule ribbonRule(IClientConfig config) {
  4. if (this.propertiesFactory.isSet(IRule.class, name)) {
  5. return this.propertiesFactory.get(IRule.class, config, name);
  6. }
  7. ZoneAvoidanceRule rule = new ZoneAvoidanceRule();
  8. rule.initWithNiwsConfig(config);
  9. return rule;
  10. }
  11. @Bean
  12. @ConditionalOnMissingBean
  13. public ILoadBalancer ribbonLoadBalancer(IClientConfig config,
  14. ServerList<Server> serverList, ServerListFilter<Server> serverListFilter,
  15. IRule rule, IPing ping, ServerListUpdater serverListUpdater) {
  16. if (this.propertiesFactory.isSet(ILoadBalancer.class, name)) {
  17. return this.propertiesFactory.get(ILoadBalancer.class, config, name);
  18. }
  19. return new ZoneAwareLoadBalancer<>(config, rule, ping, serverList,
  20. serverListFilter, serverListUpdater);
  21. }

ribbonRule是配置负载均衡策略的,ribbonLoadBalancer主要是去nocas中获取服务列表的

接下来看ribbonLoadBalancer方法:

  1. public ZoneAwareLoadBalancer(IClientConfig clientConfig, IRule rule,
  2. IPing ping, ServerList<T> serverList, ServerListFilter<T> filter,
  3. ServerListUpdater serverListUpdater) {
  4. super(clientConfig, rule, ping, serverList, filter, serverListUpdater);
  5. }
  1. public DynamicServerListLoadBalancer(IClientConfig clientConfig, IRule rule, IPing ping,
  2. ServerList<T> serverList, ServerListFilter<T> filter,
  3. ServerListUpdater serverListUpdater) {
  4. super(clientConfig, rule, ping);
  5. this.serverListImpl = serverList;
  6. this.filter = filter;
  7. this.serverListUpdater = serverListUpdater;
  8. if (filter instanceof AbstractServerListFilter) {
  9. ((AbstractServerListFilter) filter).setLoadBalancerStats(getLoadBalancerStats());
  10. }
  11. restOfInit(clientConfig);
  12. }
  1. void restOfInit(IClientConfig clientConfig) {
  2. boolean primeConnection = this.isEnablePrimingConnections();
  3. // turn this off to avoid duplicated asynchronous priming done in BaseLoadBalancer.setServerList()
  4. this.setEnablePrimingConnections(false);
  5. enableAndInitLearnNewServersFeature();
  6. updateListOfServers();
  7. if (primeConnection && this.getPrimeConnections() != null) {
  8. this.getPrimeConnections()
  9. .primeConnections(getReachableServers());
  10. }
  11. this.setEnablePrimingConnections(primeConnection);
  12. LOGGER.info("DynamicServerListLoadBalancer for client {} initialized: {}", clientConfig.getClientName(), this.toString());
  13. }
  1. @VisibleForTesting
  2. public void updateListOfServers() {
  3. List<T> servers = new ArrayList<T>();
  4. if (serverListImpl != null) {
  5. servers = serverListImpl.getUpdatedListOfServers();
  6. LOGGER.debug("List of Servers for {} obtained from Discovery client: {}",
  7. getIdentifier(), servers);
  8. if (filter != null) {
  9. servers = filter.getFilteredListOfServers(servers);
  10. LOGGER.debug("Filtered List of Servers for {} obtained from Discovery client: {}",
  11. getIdentifier(), servers);
  12. }
  13. }
  14. updateAllServerList(servers);
  15. }

  1. @Override
  2. public List<NacosServer> getUpdatedListOfServers() {
  3. return getServers();
  4. }

接下来就是去获取nacos服务,则程序中可以注入ILoadBalancer获取服务列表

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发