接着上一篇我们了解了Ribbon的客户端的负载均衡器
接下来我看下如何使用做到负载均衡,话不多说直接上开始吧
我们创建一个客户端的消费者微服务工程: microservicecloud-consumer-dept-80
怎么创建包和启动类我这里就不讲了
下面我们就直接上代码了需要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">
<parent>
<artifactId>microservicecloud</artifactId>
<groupId>com.atguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-consumer-dept-80</artifactId>
<description>部门微服务消费者</description>
<dependencies>
<dependency><!-- 自己定义的api -->
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Ribbon相关 -->
<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,可能这个devtools不会起作用,即应用不会restart-->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
关于其他的依赖我这里不多做介绍,关键这里是引入ribbon客户端的负载均衡依赖
yml配置文件:
server:
port: 80
eureka:
client:
register-with-eureka: false #自己不能注册自己
service-url:
#defaultZone: http://localhost:7001/eureka/ #单机版
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #eureka集群
java代码结构:
对ConfigBean配置类加@LoadBalanced新注解,获得RestTemplate调用微服务的对象,RestTemplate对象给加入ribbon负载均衡
package com.atguigu.springcloud.cfgbeans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
主启动类添加新注解@EnableEurekaClient:
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
//在启动该微服务的时候就能去加载我们的自定义Ribbon配置类,从而使配置生效
//@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
//@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
public class DeptConsumer80_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_App.class, args);
}
}
修改客户端的访问类(还记得之前讲的微服务的生产提供者吗
由此我们需要将这个访问微服务的地址配置成:
private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
):
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/consumer")
public class DeptController_Consumer {
//private static final String REST_URL_PREFIX = "http://localhost:8001";
private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
/**
* 使用restTemplate访问restful接口非常的简单粗暴无脑。 (url, requestMap,
* ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
*/
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/dept/add")
public boolean add(Dept dept) {
return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
}
@RequestMapping(value = "/dept/get/{id}")
public Dept get(@PathVariable("id") Long id) {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
}
//@RequestMapping(value = "/dept/get/{id}", method= RequestMethod.GET)
//@GetMapping("/dept/get/{id}")
/*public Dept getDeptById(@RequestParam(value="id",required = false,defaultValue = "1") Long id) {
Dept dept=restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/"+id , Dept.class);
return dept;
}*/
@SuppressWarnings("unchecked")
@RequestMapping(value = "/dept/list")
public List<Dept> list()
{
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
}
// 测试@EnableDiscoveryClient,消费端可以调用服务发现
@RequestMapping(value = "/dept/discovery")
public Object discovery(){
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
}
}
这样就完成了负载均衡代码的搭建,下面我就来测试一下吧
1,首先启动eureka集群,如果不懂eureka集群请参考学习这篇文章,这里就不多说了
2,启动微服务提供者,如果不懂微服务提供者实现请参考https://blog.csdn.net/ywl470812087/article/details/102535582
3,启动客户端消费者
访问:http://localhost/consumer/dept/get/1
访问eureka注册中心:http://eureka7003.com:7003/ 和 http://eureka7002.com:7002/ 以及 http://eureka7001.com:7001/
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/ywl470812087/article/details/102636250
内容来源于网络,如有侵权,请联系作者删除!