springcloud(十四)、ribbon负载均衡策略应用案例
一、eureka-server服务中心项目不再创建
二、eureka-common-empdept公共组件项目不再掩饰
三、创建eureka-client-provider-empdept-one提供者项目
3.1 结构如下
pom.xml文件内容如下:
<dependencies>
<dependency>
<groupId>cn.kgc</groupId>
<artifactId>eureka-common-empdept</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
pom.xml
3.2 DeptMapper.java
package cn.kgc.mapper; import cn.kgc.vo.Dept;
import org.apache.ibatis.annotations.Select; import java.util.List; /**
* Created by Administrator on 2019/8/29.
*/
public interface DeptMapper {
@Select("select * from dept")
List<Dept> optionData();
}
DeptMapper.java
3.3EmpMapper.java
package cn.kgc.mapper; import cn.kgc.vo.Emp;
import org.apache.ibatis.annotations.Select; import java.util.List;
import java.util.Map; /**
* Created by Administrator on 2019/8/29.
*/
public interface EmpMapper {
List<Map<String,Object>> showData(Emp emp);
}
EmpMapper.java
3.4EmpMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.kgc.mapper.EmpMapper">
<select id="showData" resultType="map" parameterType="emp">
select d.dname,d.loc,e.* from emp e,dept d where e.deptno=d.deptno
<if test="empno!=null">
and e.empno=#{empno}
</if>
<if test="deptno!=null and deptno!=-1">
and e.deptno=#{deptno}
</if>
</select>
</mapper>
EmpMapper.xml
3.5DeptService.java
package cn.kgc.service; import cn.kgc.vo.Dept;
import org.apache.ibatis.annotations.Select; import java.util.List; /**
* Created by Administrator on 2019/8/29.
*/
public interface DeptService {
List<Dept> optionData();
}
DeptService.java
3.6 DeptServiceImpl.java
package cn.kgc.service; import cn.kgc.mapper.DeptMapper;
import cn.kgc.vo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class DeptServiceImpl implements DeptService{ @Autowired
private DeptMapper deptMapper; public List<Dept> optionData() {
return deptMapper.optionData();
}
}
DeptServiceImpl.java
3.7 EmpService.java
package cn.kgc.service; import cn.kgc.vo.Emp; import java.util.List;
import java.util.Map; /**
* Created by Administrator on 2019/8/29.
*/
public interface EmpService {
List<Map<String,Object>> showData(Emp emp);
}
EmpService.java
3.8 EmpServiceImpl.java
package cn.kgc.service; import cn.kgc.mapper.EmpMapper;
import cn.kgc.vo.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import java.util.List;
import java.util.Map; @Service
@Transactional
public class EmpServiceImpl implements EmpService{
@Autowired
private EmpMapper empMapper; public List<Map<String, Object>> showData(Emp emp) {
return empMapper.showData(emp);
}
}
EmpServiceImpl.java
3.9 CenterController,java
package cn.kgc.controller; import cn.kgc.mapper.DeptMapper;
import cn.kgc.mapper.EmpMapper;
import cn.kgc.service.DeptService;
import cn.kgc.service.EmpService;
import cn.kgc.vo.Dept;
import cn.kgc.vo.Emp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import java.util.List;
import java.util.Map; @RestController
public class CenterController { @Autowired
private EmpService empService; @Autowired
private DeptService deptService; private Logger logger= LoggerFactory.getLogger(CenterController.class); @RequestMapping("/ribbon.do")
public String ribbonTest(@RequestParam("count") Integer count){
logger.info("provider>>> "+count+" <<<eureka-client-provider-one-8762");
return "provider的ribbon测试";
} @RequestMapping("/option.do")
public List<Dept> optionData() {
return deptService.optionData();
} @RequestMapping("/data.do")
public List<Map<String, Object>> showData(@RequestBody Emp emp) {
return empService.showData(emp);
}
}
CenterController.java
3.10 启动类的设置
package cn.kgc; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @MapperScan("cn.kgc.mapper")
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientProviderEmpdeptOneApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientProviderEmpdeptOneApplication.class, args);
} }
启动类
3.11 编写属性文件
spring.application.name=provider-empdept server.port=8762 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.117.145:3306/kh66
spring.datasource.username=root
spring.datasource.password=ok mybatis.type-aliases-package=cn.kgc.vo mybatis.mapper-locations=classpath:mapper/*.xml
application.properties
3.12 启动eureka-server、eureka-client-provider-empdept-one 查看结果
四、创建eureka-client-provider-empdept-two提供者项目,所有的代码模块和eureka-client-provider-empdept-one一模一样,只是端口号不一样即可
五、创建eureka-client-consumer-empdept-p-one消费者项目,
5.1 结构如下
5.2 编写 EmpDeptProviderFeign.java
package cn.kgc.fegin; import cn.kgc.vo.Dept;
import cn.kgc.vo.Emp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import java.util.List;
import java.util.Map; @FeignClient("provider-empdept")
public interface EmpDeptProviderFeign { @RequestMapping("/ribbon.do")
public String ribbonTest(@RequestParam("count") Integer count); @RequestMapping("/option.do")
public String optionData(); @RequestMapping("/data.do")
public String showData(@RequestBody Emp emp);
}
EmpDeptProviderFeign.java
5.3 CenterController.java
package cn.kgc.fegin; import cn.kgc.vo.Dept;
import cn.kgc.vo.Emp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import java.util.List;
import java.util.Map; @FeignClient("provider-empdept")
public interface EmpDeptProviderFeign { @RequestMapping("/ribbon.do")
public String ribbonTest(@RequestParam("count") Integer count); @RequestMapping("/option.do")
public String optionData(); @RequestMapping("/data.do")
public String showData(@RequestBody Emp emp);
}
CenterController.java
5.4 application.properties属性文件编写
spring.application.name=consumer-empdept-p-one server.port=8764 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
application.properties
5.5 编写启动类
package cn.kgc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientConsumerEmpdeptPOneApplication { public static void main(String[] args) {
SpringApplication.run(EurekaClientConsumerEmpdeptPOneApplication.class, args);
} }
启动类
5.6 启动项目顺序为:eureka-server、eureka-client-provider-empdept-one、eureka-client-provider-empdept-two、eureka-client-consumer-empdept-p-one
因为没有设置负载均衡策略,因此他会按照默认方式既 轮询策略进行调用,
5.7 再次选择配置ribbon负载均衡策略为随机分配,再看效果,属性文件设置如下
spring.application.name=consumer-empdept-p-one server.port=8764 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ #随机分配策略
provider-empdept.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
application.properties
5.8 再次按照刚才的启动顺序启动,再来查看
可以挨个把负载均衡策略试一下
springcloud(十四)、ribbon负载均衡策略应用案例的更多相关文章
- Ribbon负载均衡策略与自定义配置new
Ribbon负载均衡策略 配置 对调用的某个服务启用某种负载策略 1)通过配置文件配置 hello: ribbon: NFLoadBalancerRuleClassName:com.netflix.l ...
- Ribbon负载均衡策略与自定义配置
Ribbon负载均衡策略 配置 对调用的某个服务启用某种负载策略 1)通过配置文件配置 hello: ribbon: NFLoadBalancerRuleClassName:com.netflix.l ...
- SpringCloud之Ribbon负载均衡策略
Spring Cloud 微服务架构学习记录与示例 一.认识Ribbon 首先咱们需要认识下负载均衡,一般分为服务器端负载和客户端负载均衡. 服务器端负载均衡:比如Nginx.F5,请求达到服务器后由 ...
- 四. Ribbon负载均衡服务调用
1. 概述 1.1 Ribbon是什么 SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端,是负载均衡的工具. Ribbon是Netflix发布的开源项目,主要功能 ...
- Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制
上一篇文章单独介绍了Ribbon框架的使用,及其如何实现客户端对服务访问的负载均衡,但只是单独从Ribbon框架实现,没有涉及spring cloud.本文着力介绍Ribbon的负载均衡机制,下一篇文 ...
- SpringCloud学习(4)——Ribbon负载均衡
Ribbon概述 SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具. 简单的说, Ribbon是Netflix发布的开源项目, 主要功能是提供客户端软 ...
- 最适合新手入门的SpringCloud教程 6—Ribbon负载均衡「F版本」
SpringCloud版本:Finchley.SR2 SpringBoot版本:2.0.3.RELEASE 源码地址:https://gitee.com/bingqilinpeishenme/Java ...
- Ribbon负载均衡策略配置
在这里吐槽一句:网上很多文章真是神坑,你不看还好,看了只会问题越来越多,就连之前的问题都没有解决!!! 不多说了,Ribbon作为后端负载均衡器,比Nginx更注重的是请求分发而不是承担并发,可以直接 ...
- spring cloud中通过配置文件自定义Ribbon负载均衡策略
一.Ribbon中的负载均衡策略 1.Ribbon中支持的负载均衡策略 AvailabilityFilteringRule:过滤掉那些因为一直连接失败的被标记为circuit tripped的后端se ...
随机推荐
- luoguP1134 阶乘问题 [数论]
题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001, ...
- 【Codeforces Round #429 (Div. 2) B】 Godsend
[Link]:http://codeforces.com/contest/841/problem/B [Description] 两个人轮流对一个数组玩游戏,第一个人可以把连续的一段为奇数的拿走,第二 ...
- JQuery简单实用的模板引擎
1.在html界面声明模板(注意type类型) <script id="tmplInvokeProvider" type="text/x-jquery-tmpl&q ...
- GDI+在Delphi程序的应用 Photoshop色相饱和度明度功能
本文用GDI+实现Photoshop色相/饱和度/明度功能,参照我的其它有关GDI+在 Delphi程序的应用的文章,代码也可供TBitmap使用. 有些人不喜欢,或者不太懂Delphi的BASM代码 ...
- elementui tree 节点过滤加载对应子节点
/官网例子 <el-input placeholder="输入关键字进行过滤" v-model="filterText"> </el-inpu ...
- LeetCode 620. Not Boring Movies (有趣的电影)
题目标签: 题目给了我们一个 cinema 表格, 让我们找出 不无聊的电影,并且id 是奇数的,降序排列. 比较直接简单的,具体看code. Java Solution: Runtime: 149 ...
- LeetCode 744. Find Smallest Letter Greater Than Target (寻找比目标字母大的最小字母)
题目标签:Binary Search 题目给了我们一组字母,让我们找出比 target 大的最小的那个字母. 利用 binary search,如果mid 比 target 小,或者等于,那么移到右半 ...
- CodeForces-1244C-The Football Season-思维
The football season has just ended in Berland. According to the rules of Berland football, each matc ...
- USACO 2007 “March Gold” Ranking the Cows
题目链接:https://www.luogu.org/problemnew/show/P2881 题目链接:https://vjudge.net/problem/POJ-3275 题目大意 给定标号为 ...
- linux就该这么学--资料整理--持续更新
基础命令 服务管理 systemctl redhat7 systemctl start foo.service 启动服务 systemctl restart foo.service 重启服务 syst ...