前一篇实现了服务注册中心的搭建,并提供服务注册到注册中心上。在之前的基础上,实现服务消费。
一、相关介绍
1、RestTemplate工具
2、@LoadBalanced注解:开启客户端负载均衡
二、ribbon示例:
先启动eureka-service注册中心,再将eureka-client修改端口号为2222、2223分别启动两次,可以看到服务提供者注册了两个端口2222、2223,
下面实现ribbon的负载均衡消费:
1、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><groupId>com.demo.cloud</groupId><artifactId>myspringcloud-eureka-ribbon</artifactId><version>1.0-SNAPSHOT</version><!-- springBoot --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Edgware.RELEASE</spring-cloud.version></properties><dependencies><!--eureka-server--><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>com.demo.cloud.api</groupId><artifactId>myspringcloud-api</artifactId><version>1.0-SNAPSHOT</version><scope>system</scope><systemPath>D:/my-demo-code/codecloud/myspringcloud-api/target/my-api.jar</systemPath></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId><version>3.2.1</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.5</version></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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories>
</project>
2、application配置文件:
server.port=3333
server.context-path=/myTest
spring.application.name=my-test
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
3、启动类:
package com.demo.cloud;
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 MyConsumerApplication {@Bean@LoadBalancedRestTemplate restTemplate(){return new RestTemplate();}public static void main(String args[]){SpringApplication.run(MyConsumerApplication.class,args);}
}
4、controller:
package com.demo.cloud.controller;import com.demo.cloud.dto.AuthorityDTO;
import com.demo.cloud.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;@RequestMapping("/user")
@RestController
public class UserApiController {@Autowiredprivate RestTemplate restTemplate;//注意这里服务提供者的名称与注册中心上保持一致大写了,与代码中名称一致小写也可以private static final String REST_URL_PREFIX="http://MY-SERVICE/myService";@RequestMapping("/findByUserName")public UserDTO findByUserName(String username){HashMap<String,String> paramMap = new HashMap<>();paramMap.put("username",username);UserDTO userDTO = restTemplate.getForObject(REST_URL_PREFIX+"/user/findByUserName?username={username}",UserDTO.class,paramMap);return userDTO;}@RequestMapping("/getAllUsers")public List<UserDTO> getAllUsers(){List<UserDTO> userDTOS = restTemplate.getForObject(REST_URL_PREFIX+"/user/getAllUsers",List.class);return userDTOS;}@RequestMapping("/addUser")public void addUser1(@RequestBody UserDTO userDTO){restTemplate.postForEntity(REST_URL_PREFIX+"/user/addUser",userDTO,Boolean.class);}
}
启动后刷新注册中心,看到消费者也注册上去了
控制台可以看到ribbon输出了当前客户端维护的my-service服务提供者的情况,包括各个实例的请求总数、上一次连接的信息、总的请求失败数量等等
测试:
多次访问查看服务提供者日志,可以看到两个节点都有访问