• 【SpringCloud】第二篇: 服务消费者(rest+ribbon)


    前言:

    必需学会SpringBoot基础知识

    简介:

    spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。

    工具:

    JDK8

    apache-maven-3.5.2

    IntelliJ IDEA 2017.3 x64

    ribbon 简介

    Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies. —–摘自官网

    ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。

    一、准备工作

    本篇是基于第一篇为基础, 首先, 启动 eureka-server:8761 和 eureka-client 项目的dev:8762, pro:8763 分别启动两个实例. 临时搭建一个小集群, 然后访问 127.0.0.1:8761 查看 eureka-client 两个实例是否启动了.

    二、新建服务消费者 (eureka-ribbon)

    2.1 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.lwc</groupId>
    	<artifactId>eureka-ribbon</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>eureka-ribbon</name>
    	<description>Demo project for Spring Boot</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.10.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    		<spring-cloud.version>Edgware.SR2</spring-cloud.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-eureka-server</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-ribbon</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>${spring-cloud.version}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    
    </project>

    2.2  通过 application.yml 跳转 application-dev.yml 配置程序名, 端口 和指定注册中心 如下:

    spring:
      application:
          name: eureka-ribbon
      profiles:
        active: ${@profileActiv@:dev}
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8764
    

    2.3  通过@EnableDiscoveryClient向服务中心注册, 并且利用ioc注入@Bean, 然后通过@LoadBalanced表明restTemplate()开启负载均衡功能; 如下:

    package com.lwc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @author Eddie
     */
    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaRibbonApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaRibbonApplication.class, args);
        }
    }
    package com.lwc.template;
    
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author eddie.lee
     * @Package com.lwc.template
     * @ClassName RestConfig
     * @description
     * @date created in 2018-03-26 19:54
     * @modified by
     */
    @Component
    public class RestConfig {
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    
    }

    2.4 创建服务层测试使用, ribbonService() 里面url使用了eureka-client服务器名称代替127.0.0.1:8762; 如下:

    package com.lwc.service;
    
    /**
     * @author eddie.lee
     * @Package com.lwc.service
     * @ClassName RibbonService
     * @description
     * @date created in 2018-03-26 19:55
     * @modified by
     */
    public interface RibbonService {
    
        String ribbonService(String name);
    
    }
    package com.lwc.service.impl;
    
    import com.lwc.service.RibbonService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author eddie.lee
     * @Package com.lwc.service.impl
     * @ClassName RibbonServiceImpl
     * @description
     * @date created in 2018-03-26 19:57
     * @modified by
     */
    @Service
    public class RibbonServiceImpl implements RibbonService {
    
        @Autowired
        private RestTemplate restTemplate;
    
        @Override
        public String ribbonService(String name) {
            final String url = "http://eureka-client/eureka/client?name=" + name;
            return restTemplate.getForObject(url, String.class);
        }
    
    }

    2.5 创建 controller 作为调用测试服务层使用; 如下:

    package com.lwc.controller;
    
    import com.lwc.service.RibbonService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author eddie.lee
     * @Package com.lwc.controller
     * @ClassName RibbonControler
     * @description 服务消费者
     * @date created in 2018-03-26 20:02
     * @modified by
     */
    @RestController
    @RequestMapping("/eureka")
    public class RibbonControler {
    
        @Autowired
        private RibbonService ribbonService;
    
        @GetMapping("/ribbon")
        public String Ribbon(@RequestParam String name) {
            return ribbonService.ribbonService(name);
        }
    
    }

    新手解析: ribbon相当于 请求ribbon的时候会随机访问eureka-client:8762 和 eureka-client:8763,

                  为什么? 如果一台eureka-client:8762挂了, 还有eureka-client:8763, 保证服务正常使用;

    eureka-ribbon
    http://localhost:8764/eureka/ribbon?name=eddie

    
    

    标签

    2-1

    源码下载
    https://github.com/eddie-code/SpringCloudDemo#springclouddemo

  • 相关阅读:
    Visitor Pattern (Visitor设计模式)
    JS DatePicker: jscalendar1.0___v1.51 (JS日期选择器)
    转:Mediator模式
    转: Debug with IIS Express
    101 LINQ Samples
    关于进程与线程:About Processes and Threads
    转:PureMVC示例 CafeTownsend
    Haskell Editor:Leksah & EclipseFP The Haskell plugin for Eclipse
    Haskell、C#、Java、C 程序执行效率比较 :Write your first parallel Haskell program
    SCJP: java exerciese 1
  • 原文地址:https://www.cnblogs.com/EddieBlog/p/8655851.html
Copyright © 2020-2023  润新知