Ribbon是一个客户端的负载均衡器,它提供对HTTP和TCP客户端的访问控制;
结合上篇笔记中的示例:eurekaserver作为注册中心,servicehi作为服务提供者(eureka client);
下面直接创建一个项目 service-ribbon,基于 Ribbon 作为负载均衡,来消费servicehi提供的服务。
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.springcloud.lcl</groupId>
<artifactId>service-ribbon</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-ribbon</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</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>
</project>
注意此处使用的spring-boot、spring-cloud版本必须兼容;
2. 项目目录及代码
ServiceRibbonApplication 启动类:
package com.springcloud.lcl;
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;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication
{
public static void main( String[] args )
{
SpringApplication.run(ServiceRibbonApplication.class,args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
api定义类HelloControler:
package com.springcloud.lcl.controller;
import com.springcloud.lcl.service.HelloService;
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;
/**
* @author lcl
* @version 0.1
* @date 2018/10/15
**/
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
service层HelloService类:
package com.springcloud.lcl.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @author lcl
* @version 0.1
* @date 2018/10/15
**/
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name){
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
}
3. 配置文件application.properties(也可用yml方式) ;
spring.application.name=service-ribbon
server.port=8764
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
4. 启动及验证
启动eurekaserver,再依次启动servicehi,service-ribbon;其中servicehi服务可修改端口号后再起一个实例(例:端口8763)
访问eureka http://localhost:8761/ 可看到如下,预期的几个服务已启动并注册;
访问service-ribbon提供的服务: http://localhost:8764/hi?name=name1
再次请求
会看到两个servicehi服务提供者会轮番处理请求;