全系列皆是个人根据网上资料学习实现后的个人理解,如有不对不足之处,请指出,勿喷!
ribbon是什么?
ribbon可以帮助我们实现负载均衡的效果。只需使用@LoadBalanced注解即可实现。使用方便快捷。
直接上手:
这次我们使用上一博文的工程,启动服务注册 工程,以及启动两个服务并注册到注册中心。
新建一个ribbon工程:
pom文件如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>wujie_study_day02_eureka_ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wujie_study_day02_eureka_ribbon</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-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>
修改配置文件如下:
server:
port: 8891
spring:
application:
name: eureka_ribbon
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
在启动类上加注解@EnableDiscoveryClient向服务中心注册,并且注入一个restTemplate的bean。通过@LoadBalanced注解开启负载均衡。
package com.springcloud.wujie_study_day02_eureka_ribbon;
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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class WujieStudyDay02EurekaRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(WujieStudyDay02EurekaRibbonApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
service层代码如下:
package com.springcloud.wujie_study_day02_eureka_ribbon.service;
/**
* @Classname ServerService
* @Description TODO
* @Date 2019/5/23 17:08
* @Created by wujie
*/
public interface ServerService {
public String getInfo(String name);
}
实现service层:
package com.springcloud.wujie_study_day02_eureka_ribbon.service.impl;
import com.springcloud.wujie_study_day02_eureka_ribbon.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @Classname ServerServiceImpl
* @Description TODO
* @Date 2019/5/23 17:09
* @Created by wujie
*/
@Service
public class ServerServiceImpl implements ServerService {
@Autowired
private RestTemplate restTemplate;
@Override
public String getInfo(String name) {
return restTemplate.getForObject("http://wujie-study-day01-eureka-client/test?name="+name,String.class);
}
}
controller层实现如下:
package com.springcloud.wujie_study_day02_eureka_ribbon.controller;
import com.springcloud.wujie_study_day02_eureka_ribbon.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Classname RibbonController
* @Description TODO
* @Date 2019/5/23 17:05
* @Created by wujie
*/
@RestController
public class RibbonController {
@Autowired
private ServerService serverService;
@RequestMapping(value = "testRibbon")
public String testRibbon(@RequestParam String name){
return serverService.getInfo(name);
}
}
最后启动这个项目并访问localhost:8891/testRibbon?name=XXX并多次请求出现如下图所示:
我们可以发现端口不一致,证明我们实现成功。