原创

5.Springcloud的Ribbon组件的集成及实现轮询负载均衡方式

项目地址:

github地址

步骤一:对consumer工程的pom添加坐标

        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

步骤二:在application.yml里面添加对应eureka的代码

eureka:
  client:
    register-with-eureka: false
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

步骤三:对ConfigBean进行新注解@LoadBalanced 获得Rest时加入Ribbon的配置

package com.debuggg.cloud.cfg;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class CfgBean {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

步骤四:主启动类DeptConsumer80_App添加@EnableEurekaClient

package com.debuggg.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class DeptConsumer10001_App {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer10001_App.class,args);
    }
}

步骤五:修改DeptController_Consumer客户端访问类

将REST_URL_PREFIX的地址改为微服务的服务名的地址,正式实现微服务化

package com.atguigu.springcloud.controller;

import java.util.List;

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.RestController;
import org.springframework.web.client.RestTemplate;

import com.atguigu.springcloud.entities.Dept;

@RestController
public class DeptController_Consumer
{
  //private static final String REST_URL_PREFIX = "http://localhost:8001";
  private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";

  @Autowired
  private RestTemplate restTemplate;

  @RequestMapping(value="/consumer/dept/add")
  public boolean add(Dept dept)
  {
   return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class);
  }

  @RequestMapping(value="/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id)
  {
   return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class);
  }

  @SuppressWarnings("unchecked")
  @RequestMapping(value="/consumer/dept/list")
  public List<Dept> list()
  {
   return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
  } 

  //测试@EnableDiscoveryClient,消费端可以调用服务发现
  @RequestMapping(value="/consumer/dept/discovery") 
  public Object discovery()
  {
   return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", Object.class);
  }  

}

步骤六:启动并测试

![file](http://file.debuggg.com/oneblog/article/20200311142020877.png)
正文到此结束
本文目录