zoukankan      html  css  js  c++  java
  • SpringCloudCommons模块

    本文介绍SpringCloud的另一个基础模块 Spring Cloud Commons模块 。只要在项目的pom文件中引入了 spring-cloud-starter 依赖包 ,就可以保证 spring-cloud-commons 的jar被引入。

    Spring Cloud Commons模块设计的目的,Spring Cloud Commons模块是为了对微服务中的服务注册与发现、负载均衡、熔断器等功能提供一个抽象层代码,这个抽象层与具体的实现无关。这样这些功能具体的实现上可以采用不同的技术去实现,并可以做到在使用时灵活的更换。

    下面是一些常用的抽象点:

    1. @EnableDiscoveryClient
      该注解是用来在META-INF/spring.factorie文件中查找DiscoveryClient接口的实现类,并以Bean的形式加载到Spring的IoC容器中。在使用的时候会把这个注解加在SpringBoot的main类上。但这个注解在目前springCloud的Greenwich版本上已经不再需要了(也就是可有可无),只要引入具体的DiscoveryClient接口的jar依赖就可以,因为具体实现包上会通过自动配置类进行设置。

    2. ServiceRegistry接口
      这个接口提供注册Registration与撤销Registration的注册的方法。这里的Registration是一个标记接口,用来描述一个服务实例,具体包含关于实例的信息,比如它的主机名和端口等信息。

    3. 让Spring RestTemplate具备负载均衡功能
      在创建RestTemplate的Bean时使用@LoadBalanced注解,就可以自动配置为使用ribbon。如下面的示例所示:

    @Configuration
    public class MyConfiguration {
        @LoadBalanced
        @Bean
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
    public class MyClass {
        @Autowired
        private RestTemplate restTemplate;
    
        public String doOtherStuff() {        
            //注意:代码中的url要使用服务名,而不是主机名
            String results = restTemplate.getForObject("http://stores/stores", String.class);
            return results;
        }
    }
    

    原文链接 https://www.cnblogs.com/hzhuxin/p/10562295.html

  • 相关阅读:
    git命令小汇总和github
    有关版本控制--SVN
    ng-做一个简单的通讯录--学习使用路由和HTTP
    ng-辅助操作
    ng-router
    ng-http
    ng-指令
    ng-组件
    ng-核心特性(模型概念)
    ng--tolist说明
  • 原文地址:https://www.cnblogs.com/gotodsp/p/15605082.html
Copyright © 2011-2022 走看看