• 玩转SpringCloud(F版本) 二.服务消费者(1)ribbon+restTemplate


    上一篇博客有人问我,Springcloud系列会不会连载 ,大家可以看到我的标签分类里已经开设了SpringCloud专题,所以当然会连载啦,本人最近也是买了本书在学习SpringCloud微服务框架,知识会随时分享的!!!!!!!!!!!!!!!!!!!!!

    二.服务消费者

    在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign

    本片博客以上一篇博客 玩转SpringCloud 一.服务的注册与发现(Eureka) 的项目为基础   https://www.cnblogs.com/lsy131479/p/9613755.html  

    本片博客将讲解ribbon+restTemplate模式,下一篇讲解feign模式

    1. ribbon+restTemplate

    ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为

    启动demo1 工程;启动demo2工程,它的端口为8762;将demo2的配置文件的端口改为8763,并启动,会发现:demo2demo1 注册了2个实例,这就相当于一个小的集群

    启动之前先将demo2的启动设置单例关掉

    项目启动后并且关掉单例启动后,改变demo2的端口号

    再次启动demo2,查看注册中心的服务http://localhost:8761

     会发现:demo2在demo1 注册了2个实例,这就相当于一个小的集群。

    建一个服务消费者

    项目架构:

    重新新建一个spring-boot工程,取名为:demo3;

    引入主项目,以及相关jar包:

    <parent>
       <groupId>com.fsdm</groupId>
       <artifactId>SpringCloud_test1</artifactId>
       <version>1.0-SNAPSHOT</version>
    </parent>
    
    <dependencies>
       <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
       </dependency>
    </dependencies>

    yml配置:

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8764
    spring:
      application:
      #工程名称
        name: service-ribbon

    在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

    @EnableEurekaClient
    @EnableDiscoveryClient
    @SpringBootApplication
    public class Demo3Application {
    
       public static void main(String[] args) {
          SpringApplication.run(Demo3Application.class, args);
       }
    
    
       @Bean
       @LoadBalanced
       RestTemplate restTemplate() {
          return new RestTemplate();
       }
    }

    注解解析:

     

    @EnableDiscoveryClient

    1. 基于spring-cloud-commons,并且在classpath中实现。

    2. 就是如果选用的注册中心是eureka推荐@EnableEurekaClient,如果是其他的注册中心推荐使用@EnableDiscoveryClient,如果classpath中添加了eureka,则它们的作用是一样的。

     

     

    @Bean

    1、Java面向对象,对象有方法和属性,那么就需要对象实例来调用方法和属性(即实例化);

    2、凡是有方法或属性的类都需要实例化,这样才能具象化去使用这些方法和属性;

    3、规律:凡是子类及带有方法或属性的类都要加上注册BeanSpring IoC的注解

     

    4、Bean理解为类的代理或代言人(实际上确实是通过反射、代理来实现的),这样它就能代表类拥有该拥有的东西了

    5、我们都在微博上@过某某,对方会优先看到这条信息,并给你反馈,那么在Spring中,你标识一个@符号,那么Spring就会来看看,并且从这里拿到一个Bean或者给出一个Bean

     

    @LoadBalanced

    1. 表明这个restRemplate开启负载均衡的功能。

    2. 实现负载均衡

    写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名.

    public class HelloService {
    
        @Autowired
        RestTemplate restTemplate;
    
        public String hiService(String name) {
            return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
        }
    }

    写一个controller,在controller中用调用HelloService 的方法

    @RestController
    public class HelloControler {
    
        @Autowired
        HelloService helloService;
    
        @GetMapping(value = "/hi")
        public String hi(@RequestParam String name) {
            return helloService.hiService( name );
        }
    
    }

    启动demo3工程

    运行工程情况:

    (demo2双启动)

    在浏览器上多次访问http://localhost:8764/hi?name=forezp,浏览器交替执行显示

    这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

    此时的架构:(网摘)

      · 一个服务注册中心,eureka server,端口为8761

      · service-hi工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册

      · sercvice-ribbon端口为8764,向服务注册中心注册

      · sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;

                                        未完,待续。。。

  • 相关阅读:
    小程序canvas生成海报-新旧接口
    vue网页小程序实现七牛云图片文件上传以及原生组件video显示不出问题
    【文化课】 一篇魔改英语理解
    python萌新笔记
    版本控制(Version control)
    开源许可证(License)
    agc004c
    python日期时间、时间戳互相转换
    拓展django-haystack全文检索的样式和搜索频率限制
    常用JS代码
  • 原文地址:https://www.cnblogs.com/lsy131479/p/9625479.html
Copyright © 2020-2023  润新知