版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_23490433/article/details/89185544
在微服务架构中,应用被拆分为一个独立的服务,服务与服务之间的通讯是基于httpRestful
springCloud中有2种服务调用方式:一种是ribbon+restTemplate,一种是feign
本章讲解如何使用ribbon+restTemplate调用服务,以及实现负载均衡
第一步、创建一个maven工程,工程名为eureka-ribbon,结构图如下:
第二步、修改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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cn</groupId>
<artifactId>eureka-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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-eureka-client</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第三步、修改application.yml文件,内容如下:
server:
port: 8503
spring:
application:
name: service-ribbon
eureka:
client:
service-url:
defaultZone: http://localhost:8501/eureka/
第四步、新建包com.cn.service,然后在该包下新建类RibbonService
package com.cn.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* 写一个测试服务类,通过之前注入到IOC容器中的restTemplate来消费
* eureka-client项目中的service-client1服务的/hello接口
* 本示例:直接用程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例
* 根据服务实例在请求的时候会用具体的url替换掉服务名
* */
@Service
public class RibbonService {
@Autowired
RestTemplate restTemplate;
public String ribbonService(String name){
return restTemplate.getForObject("http://SERVICE-CLIENT1/hello?name="+name,String.class);
}
}
第五步、新建包com.cn.controller,然后在该包下新建类RibbonController
package com.cn.controller;
import com.cn.service.RibbonService;
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;
@RestController
public class RibbonController {
@Autowired
RibbonService ribbonService;
@RequestMapping("/hi")
public String test(@RequestParam String name){
return ribbonService.ribbonService(name);
}
}
第六步、修改启动类,代码如下:
package com.cn.eurekaribbon;
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.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //通过该注解向服务中心注册
@ComponentScan({"com.cn.service","com.cn.controller"})
public class EurekaRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}
@Bean //向ioc容器中注入一个bean
@LoadBalanced //表明这个restTemplate开启了负载均衡的功能
RestTemplate restTemplate(){
return new RestTemplate();
}
}
第七步、测试
修改eureka-client类的配置文件application.yml的端口号为8504和8506,8502端口启动一次启动类、修改端口8504然后启动一次启动类、修改端口8506然后启动一次启动类,如下
先启动eureka-server注册中心,再逐个启动eureka-client,再启动eureka-ribbon类,然后,打开浏览器测试,如下:
再次刷新,显示如下:
每次刷新,调用的eureka-client不同,说明实现负载均衡成功