项目名称为spring-cloud-ribbon
1、使用IDEA工具创建一个SpringBoot项目
勾选Web、Eureka Discovery和Ribbon,从而自动在pom.xml中引入Ribbon的包
![](https://i-blog.csdnimg.cn/blog_migrate/3bed510dc4a50c91b48ee1c63f8e9069.png)
2、创建完之后,项目结构如下:
![](https://i-blog.csdnimg.cn/blog_migrate/df26db29632f8e1788aade2c363e674f.png)
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.0http://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.2.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.etc</groupId>
<artifactId>spring-cloud-eureka-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-eureka-ribbon</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.BUILD-SNAPSHOT</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入eureka-client的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入ribbon的依赖 -->
<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>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
3、删除application.properties文件,新建application.yml文件如下:
#指定注册中心的地址
eureka:
client:
service-url:
defaultZone:
http://localhost:8761/eureka/
#指定服务的端口号
server:
port:
8763
#指定服务的名称
spring:
application:
name:
service-ribbon
4、启动类上添加注解,并且启动负载均衡
package com.etc.springcloudribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableEurekaClient//向注册中心注册自己
@SpringBootApplication
public class SpringCloudRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudRibbonApplication.class, args);
}
@Bean
@LoadBalanced //表明这个restTemplate开启负载均衡功能
RestTemplate restTemplate(){
return new RestTemplate();
}
}
5、新建一个HelloService类,使用负载均衡调用服务的接口
package com.etc.springcloudribbon.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
/**
* 通过之前注入ioc容器的restTemplate来消费SERVICE-HELLO服务的"/hello"接口
* 启动:service-ribbon 工程,当我们访问http://localhost:8764/hi?name=ribbon,浏览器显示
* hello ribbon,i am from port:8762
* 此时关闭 service-hello 工程,当我们再访问http://localhost:8764/hello?name=ribbon,浏览器会显示
* hello ,ribbon,orry,error!
* 这就说明当 service-hello 工程不可用的时候,service-ribbon调用 service-hello的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。
* @param name
* @return
*/
@HystrixCommand(fallbackMethod = "helloError")//创建熔断器的功能,并指定了fallbackMethod熔断方法
public String hello(String name){
return restTemplate.getForObject("http://SERVICE-HELLO/hello?name="+name,String.class);
}
public String helloError(String name){
return "hi,"+name+",sorry,error!";
}
}
6、新建一个HelloController类,调用HelloService的方法
package com.etc.springcloudribbon.controller;
import com.etc.springcloudribbon.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping(value = "/hello")
public String hello(@RequestParam String name){
return helloService.hello(name);
}
}
7、重新访问注册中心,可以看到已经注册在上面
![](https://i-blog.csdnimg.cn/blog_migrate/8c121b88c6f6ca0b8d94185c7716b6ee.png)
8、页面访问方法如下:
![](https://i-blog.csdnimg.cn/blog_migrate/03ee6e5766ac46ba737a08b9e94a2d96.png)