SpringCloud-客户端的载荷均衡Ribbon(三)

SpringCloud-客户端的载荷均衡Ribbon(三)

二、Ribbon实战

​一、创设EurekaServer,EurekaClient1,EurekaClient二,在此以前早已说过了Eureka的使用,这里直接上代码:

EurekaServer:

图片 1图片 2

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class,args);
    }

}

ServerApplication.java

图片 3图片 4

<?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.cn</groupId>
  <artifactId>eureka-ribbon-server</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.</groupId>
      <artifactId></artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <!-- 添加spring-boot的maven插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

pom.xml

图片 5图片 6

server.port=8761
#注意:这两个配置eureka默认为true,要改成false,否则会报错,connot connect server
#表示是否将自己注册在EurekaServer上
eureka.client.register-with-eureka=false
#表示是否从EurekaServer获取注册信息
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

application.properties

EurekaClient1:

图片 7图片 8

<?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.cn</groupId>
  <artifactId>eureka-ribbon-client</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <!-- 添加spring-boot的maven插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

pom.xml

图片 9图片 10

server.port=8762
spring.application.name=client-8762
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

application.properties

而在起步类中进入RestTemplate远程调用实例到容器中,并且增加LoadBalanced注明,使RestTemplate具有负载均衡的才具

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

    /** 
     * @Description: 加入@LoadBalanced注解,就可以为RestTemplate加入负载均衡的能力 
     * @Param:
     * @return:  
     * @Author:  
     * @Date: 2018/6/15 
     */ 
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

}

创建Controller,注入RestTemplate、LoadBalancerClient实例:

package com.cn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @program: springcloud-example
 * @description:
 * @author: 
 * @create: 2018-06-15 15:55
 **/
@Controller
public class RibbonController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/loadInstance")
    @ResponseBody
    public String loadInstance() {
        ServiceInstance choose = this.loadBalancerClient.choose("client-87");
        System.out.println(choose.getServiceId()+":"+choose.getHost()+":"+choose.getPort());
        return choose.getServiceId() + ":" + choose.getHost() + ":" + choose.getPort();
    }

}

EurekaClient2:

pom.xml与EurekaClient1中一致

application.xml:

server.port=8763
spring.application.name=client-87
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

图片 11图片 12

package com.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @program: springcloud-example
 * @description:
 * @author: 535504
 * @create: 2018-06-15 16:05
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}

ClientApplication

ClientController.java:

package com.cn.contorller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: springcloud-example
 * @description:
 * @author: 
 * @create: 2018-06-15 16:12
 **/
@Controller
public class ClientController {

    @GetMapping("/getUser")
    @ResponseBody
    public String getUser() {
        System.out.println("获取用户成功");
        return "获取用户成功";
    }

}

2、运营顺序:

  一、依次运营EurekaServer =》 EurekaClient1 =》 EurekaClient二   ;

  2、然后将EurekaClient第22中学的application.properties的server.port=8763改为server.port=8764,再度运营该品种;

  叁、打开EurekaServer的布局页面(

图片 13

  四、大家在地点栏输入

    图片 14

    图片 15

  ⑤、大家在看调整台,如图:

    图片 16

 

迄今,Ribbon已经入门,是还是不是很轻松,然而那只是最简便易行的行使,玖牛一毛尔…学无边无际乎!

 

 

 示例代码:

劳务的意识和消费

三.二.3.application.yml配置与3.1.三布置同样,即维持不改变就能够,最终运营项目就可以(今后以此类型还要富含了Ribbon与Feign的负荷均衡远程调用服务的格局),通过反复造访:
(基于Ribbon实现)、

调用示举个例子下:

一、Ribbon简介

  Ribbon是Netflix发表的负荷均衡器,它促进调节HTTP和TCP客户端的一言一行。为Ribbon配置服务提供者地址列表后,Ribbon就可依照某种负载均衡算法,自动地支援服务消费者去乞请。Ribbon默感觉大家提供了许多的负载均衡算法,举例:轮询,随机等,也可自定义;

Ribbon的GitHub:

  而在SpringCloud中利用Ribbon和Eureka时,Ribbon会自动从EurekaServer中赢得服务提供者地址列表,并依据负载均衡算法。

例:创设服务意识和消费轻松示例:

a.必需是interface,因为@FeignClient评释只好用来interface中,而且很刚毅HelloRemoteService是远程调用,本地不应当落成的,假使精晓原理就更精通这几个接口只是为着生成可供restTemplate调用的U陆风X八L方法而矣;

server.port=**8090**


spring.application.name=consumer-helloservice


   

eureka.client.service-url.defaultZone=http://eurekaserver1:1111/eureka/,http://eurekaserver2:1112/eureka

序言:微服务框架结构,不可防止的留存单个微服务有多少个实例,那么客户端怎么样将请求分摊到三个微服务的实例上呢?这里大家就需求运用负载均衡了

起步后在服务注册主题就足以看看那三个实例服务,如下:

壹.四.在spring boot运行类中增多@EnableEurekaServer就能够,如下代码:


  • pom.xml 文件中供给追加 spring-cloud-starter-ribbon 和 spring-cloud-starter-eureka
    注重,代码如下:

    <?xml version=”1.0″ encoding=”UTF-8″?>

 

//spring boot启动类:package cn.zuowenjun.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@EnableDiscoveryClient@SpringBootApplicationclass EurekaclientconsumerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaclientconsumerApplication.class, args);    }    @LoadBalanced    @Bean    public RestTemplate restTemplate(){        return  new RestTemplate();    }}//controller:package cn.zuowenjun.cloud.controller;import cn.zuowenjun.cloud.service.HelloRemoteService;import cn.zuowenjun.cloud.service.HelloService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @Autowired    private  HelloService helloService;    @RequestMapping("/x")    public Object multiplyForRestTemplate(@RequestParam int a, @RequestParam int b) {       return   helloService.multiply;    }}//HelloService :package cn.zuowenjun.cloud.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class HelloService {    @Autowired    private RestTemplate restTemplate;    @Value("${spring.application.helloServiceProvider}")    private String  helloServiceName;    public Object multiply(int a,int b){        String url="http://"+ helloServiceName +"/multiply/" + a +"/" + b;        return restTemplate.getForObject(url,String.class);    }}
  • 开创应用主类
    ConsumerHelloserviceApplication ,通过 @EanbleDiscoveryClient
    评释将利用注册为Eureka 客户端应用,扩张 @Bean 声明的回来
    RestTemplate 类型的措施:

    @EnableDiscoveryClient

7.
起动rebbon-consumer应用,之后大家就足以在劳务基本面板中旁观了PRADOIBBON-CONSUMELX570服务。

  三.三.编辑controller服务相关代码,在spring
boot运营类上加多@EnableDiscoveryClient申明,具体完整兑今世码如下:(除了@EnableDiscoveryClient阐明,基余代码与普通的spring
MVC项目代码均一致)


  • application.properties
    中,扩展服务注册中的地址配置须要和劳动提供者的等同,不然是力不从心察觉服务的,相同的时间设置该顾客的端口号,代码如下:

作者们亟须透过服务名的措施来进展调用,在劳动治理框架中那几个充裕主要。

View Code

由此Spring Cloud Ribbon
的包裹,我们在微服务架构中使用客户端负载均衡调用非常简单,只须要分为两步:

  1. 始建新的底蕴Spring
    Boot工程来作为劳动消费者,取名ribbon-consumer,参预注重。

     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.dcz</groupId>
     <artifactId>rebbon-consumer</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>jar</packaging>
    
     <name>rebbon-consumer</name>
     <description>Demo project for Spring Boot</description>
    
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.3.8.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>
     </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</artifactId>
         </dependency>
    
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-ribbon</artifactId>
         </dependency>
    
     </dependencies>
    
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-dependencies</artifactId>
                 <version>Brixton.SR5</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>
    

  2.贰.在application.yml文件中增多如下配置(若没有请参见壹.三法创立):留神spring.application.name,那个是服务实例名,注册及服务消费时均需选拔该名称

  • 劳务提供者只须要运维七个劳务实例并注册到1个挂号中央或是四个相关联的服务登记中央

  • 服务消费者直接通过调用被
    @LoadBalanced 表明修饰过的 RestTemplate 来落实面向服务的接口调用。

 八. 在浏览器中经过

FeignClient的运作规律详见:深切领会Feign之源码分析,核心理路是:spring
boot项目运营时检查@EnableFeignClients,若有则扫描被@FeignClient注脚接口并流入到spring
IOC容器中,然后在呼吁被@
FeignCleint标记的接口方法时,会透过JDK动态代理来生成现实的RequesTemplate,RequesTemplate又会生成Request,Request交给Client去处理,最后Client被包裹到LoadBalanceClient类,那个类Ribbon中的LoadBalancerClient同样,后边的负载均衡的管理请求同样。

@RestController


public
class ConsumerController { 

        @Autowired


        private RestTemplate
restTemplate; 

          

        @RequestMapping
(value = "/ribbonconsumer", method = RequestMethod.GET)


        public String ribbonConsumer() {


   

                ResponseEntity&lt;String&gt;
responseEntity = null; 

                try {


                        responseEntity
= 

                                        restTemplate.getForEntity("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/get",



String.class); 

   

                        if
(responseEntity.getStatusCode() == HttpStatus.OK) {


                                return
responseEntity.getBody(); 

                        }


   

                        return "response status " +
responseEntity.getStatusCodeValue();


                }
catch
(UnsupportedEncodingException e) { 

                        e.printStackTrace();


                        return "error";


                }


        }


}

当Ribbon和Eureka同期接纳时,Ribbon的服务实例清单RibbonServerList会被DiscoveryEnableNIEWServerList重写。

  壹.一.由此IDEA来制造三个空的spring
boot项目(类型是:maven-archtype-quickstart,那样最精简,当然假如您采用webapp项目也是足以,只是认为没有须要)。

&lt;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>"&gt;


        &lt;modelVersion&gt;**4.0.0**&lt;/modelVersion&gt; 

   

        &lt;groupId&gt;org.drsoft.consumer&lt;/groupId&gt;


        &lt;artifactId&gt;consumer-helloservice&lt;/artifactId&gt;


        &lt;version&gt;**0.0.1**-SNAPSHOT&lt;/version&gt;


        &lt;packaging&gt;jar&lt;/packaging&gt;


   

        &lt;name&gt;consumer-helloservice&lt;/name&gt;


        &lt;description&gt;Demo
project for Spring Boot&lt;/description&gt;


   

        &lt;parent&gt;


                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;


                &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;


                &lt;version&gt;**1.5.6**.RELEASE&lt;/version&gt;


                &lt;relativePath/&gt;


        &lt;/parent&gt;


   

        &lt;properties&gt;


                &lt;project.build.sourceEncoding&gt;UTF-**8**&lt;/project.build.sourceEncoding&gt;


                &lt;project.reporting.outputEncoding&gt;UTF-**8**&lt;/project.reporting.outputEncoding&gt;


                &lt;java.version&gt;**1.8**&lt;/java.version&gt;


                &lt;spring-cloud.version&gt;Dalston.SR2&lt;/spring-cloud.version&gt;


        &lt;/properties&gt;


   

        &lt;dependencies&gt;


                &lt;dependency&gt;


                        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;


                        &lt;artifactId&gt;spring-cloud-starter-eureka&lt;/artifactId&gt;


                &lt;/dependency&gt;


                &lt;dependency&gt;


                        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;


                        &lt;artifactId&gt;spring-cloud-starter-ribbon&lt;/artifactId&gt;


                &lt;/dependency&gt;


                &lt;dependency&gt;


                        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;


                        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;


                &lt;/dependency&gt;


   

                &lt;dependency&gt;


                        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;


                        &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;


                        &lt;scope&gt;test&lt;/scope&gt;


                &lt;/dependency&gt;


        &lt;/dependencies&gt;


   

        &lt;dependencyManagement&gt; 

                &lt;dependencies&gt;


                        &lt;dependency&gt;


                                &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;


                                &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;


                                &lt;version&gt;${spring-cloud.version}&lt;/version&gt;


                                &lt;type&gt;pom&lt;/type&gt;


                                &lt;scope&gt;import&lt;/scope&gt;


                        &lt;/dependency&gt;


                &lt;/dependencies&gt;


        &lt;/dependencyManagement&gt; 

   

        &lt;build&gt;


                &lt;plugins&gt;


                        &lt;plugin&gt;


                                &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;


                                &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;


                        &lt;/plugin&gt;


                &lt;/plugins&gt;


        &lt;/build&gt;


&lt;/project&gt;

 

  <dependencyManagement>    <dependencies>      <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-dependencies</artifactId>        <version>Greenwich.RELEASE</version>        <type>pom</type>        <scope>import</scope>      </dependency>    </dependencies>  </dependencyManagement>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.11</version>      <scope>test</scope>    </dependency>    <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>    <!--当.yml配置不生效时,应添加snakeyaml依赖,但一般spring-boot-starter中默认有此依赖,非spring boot项目需要添加-->    <dependency>      <groupId>org.yaml</groupId>      <artifactId>snakeyaml</artifactId>      <version>1.23</version>    </dependency>  </dependencies>

Spring
Cloud Ribbon 是一个依照 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix
Ribbon 达成,通过Spring Cloud 的包裹,能够让我们轻便的将面向服务的REST
模板请求自动转换为客户端负载均衡的劳动调用。客户端负载均衡在系统架构中是多少个特别关键的,并且是只可以去推行的原委,因为负载均衡是对系统的高可用、互连网压力的消除和管理本领扩大容积的第三手腕,客户端负载均衡要求经过心跳去维护服务端清单的健康性,那几个要求服务登记大旨合营到位,在Spring
Cloud 完成的服务治理框架中,私下认可会创建针对各种服务治理框架的 Ribbon
自动化整合配置,比方 Eureka 中的
org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration

 

图片 17

  • 开始应用,访问地址
    http://localhost:8090/ribbonconsumer ,就可以看出请求重返音讯        
     

劳务消费者重要产生五个职分——服务的觉察和劳动的消费,服务意识的天职是由Eureka客户端完毕,而服务消费的任务是由Ribbon达成。

图片 18为了前面服务消费者能体验出负载均衡的成效,能够把该项目再以另3个端口(server.port=880二)重新启航海运输作二个实例(IDEA运转三个实例的秘诀请参见:
Configuration–> program
argements中钦命:–server.port=880二就可以,原理与平昔通过命令:java -jar
xxx –server.port=880二附近),那样就能够有多个服务提供者了,要是查看eureka
server主页( currently registered
with Eureka列表中展示出1个服务实例音讯,如下图示:

@SpringBootApplication


public
class ConsumerHelloserviceApplication {


   

        public static void main(String\[\]
args) { 

                SpringApplication.run(ConsumerHelloserviceApplication.class,
args); 

        }


   

        @LoadBalanced


        @Bean


        public RestTemplate
createRestTemplate() { 

                return new RestTemplate();


        }


}

 

View Code

  • 创办
    ConsumerController 类,并落实 /consumer 接口,在类中注入
    RestTemplate
    并由此此类实例来调用服务,在接口中对服务的调用,这里运用的是劳务的名而不是1个实际的地方,服务消费Java代码:

 

  <dependencyManagement>  <dependencies>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-dependencies</artifactId>      <version>Greenwich.RELEASE</version>      <type>pom</type>      <scope>import</scope>    </dependency>  </dependencies></dependencyManagement>  <dependencies>    ... ...其它原有依赖        <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter</artifactId>      <!--<version>2.1.0.RELEASE</version>-->    </dependency>    <dependency>      <groupId>org.springframework.cloud</groupId>      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>      <!--<version>2.1.0.RELEASE</version>-->    </dependency>  </dependencies>
package com.dcz;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2017/5/13.
 */

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    public String helloConsumer(){
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
    }

}

  三.壹.3.在application.yml文件中增多如下配置(若未有请参见一.叁法创制):

 

    3.1.1.在POM
XML中添加spring-cloud-starter-netflix-ribbon依赖,如下:

 

【即:具体微服务项目,注册服务消息,暴光API】,当然也会有异常的大大概还借使serviceconsumer,须要长途调用其余服务**

图片 19

  壹.三.在resouces目录下(若未有请创设,注意设为souces
root目录,方法:右键文件夹->马克 directory as->souces
root)创立application.yml(或application.properties,本文示例全部使用yml),增加如下配置:

  1. 最后在application.properties
    文件中配置Eureka服务基本的地点,并设置贰个不1的端口。

    spring.application.name=ribbon-consumer
    server.port=9000

    eureka.client.serviceUrl.defaultZone=

  一.二增加spring
cloud相关正视,如下所示:(增多了dependencyManagement节点,并陈设spring-cloud-dependencies
pom
import依赖,指标是:便于依赖承继,与parent节点成效周边,增多具体信赖时,若包涵在parent中或pom
import信赖中则无需版本号,能够保险组件的1致性,详见:
pom
import依赖,则增添具体注重时必要钦点version版本号,而且须求注意各依赖组件间的包容性难点,如下边笔者把version注释掉)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图