springcloud ribbon Finchley 版本,自定义算法
引用上一个项目,在原有的基础上进行更改,添加springcloud的内荣。
eureka-server 和上一个springcloud eureka的一样,没有改动。
添加cloud-api
package com.zhaowb.springcloud.cloudapi.entities; import java.io.Serializable; public class Dept implements Serializable { private Long deptno; // 主键
private String dname; // 部门名称
private String db_source;// 来自那个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不同数据库 public Dept() {
} public Dept(String dname) {
super();
this.dname = dname;
} public Long getDeptno() {
return deptno;
} public void setDeptno(Long deptno) {
this.deptno = deptno;
} public String getDname() {
return dname;
} public void setDname(String dname) {
this.dname = dname;
} public String getDb_source() {
return db_source;
} public void setDb_source(String db_source) {
this.db_source = db_source;
} @Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", db_source='" + db_source + '\'' +
'}';
}
}
只需要添加这个实体类,在下边使用 。
更改 server-client ,更改为 eureka-hi
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.zhawb.springcloud</groupId>
<artifactId>eureka-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-hi</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.example</groupId>
<artifactId>cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.zhaowb.springcloud</groupId>
<artifactId>cloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-eureka-client</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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: 8762
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.zhaowb.springcloud.cloudapi.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:/mybatis/mapper/**/*.xml # mapper映射文件 spring:
application:
name: eureka-client-hi
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称
username: root
password: 308539393
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: eureka-client-hi
prefer-ip-address: true
info:
app.name: eureka-hi
company.name: www.zhaowb.com
build.artifactId: $project.artifactId$ # 这样写在idea 中运行出来不显示版本号,直接显示 $project.artifactId$ 字符串,可以自己修改
build.version: $project.version$
配置mybatis
目录结构如图:mybatis.cfg.xml ,今开启二级缓存
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings>
<setting name="cacheEnabled" value="true"/><!-- 二级缓存开启 -->
</settings> </configuration>
deptMapper.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="com.zhawb.springcloud.eurekahi.dao.DeptDao"> <select id="findById" resultType="Dept" parameterType="Long">
select deptno,dname,db_source from dept where deptno=#{deptno};
</select>
<select id="findAll" resultType="Dept">
select deptno,dname,db_source from dept;
</select>
<insert id="addDept" parameterType="Dept">
INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE());
</insert> </mapper>
DeptController.java
package com.zhawb.springcloud.eurekahi.controller; import com.zhaowb.springcloud.cloudapi.entities.Dept;
import com.zhawb.springcloud.eurekahi.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
public class DeptController {
@Autowired
private DeptService service;
@Autowired
private DiscoveryClient client; @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(@RequestBody Dept dept) {
return service.add(dept);
} @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") Long id) {
return service.get(id);
} @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
public List<Dept> list() {
return service.list();
} // @Autowired
// private DiscoveryClient client;
@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
public Object discovery() {
List<String> list = client.getServices();
System.out.println("**********" + list); List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
for (ServiceInstance element : srvList) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return this.client;
} }
DeptDao.java
package com.zhawb.springcloud.eurekahi.dao; import com.zhaowb.springcloud.cloudapi.entities.Dept;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper
public interface DeptDao { boolean addDept(Dept dept); Dept findById(Long id); List<Dept> findAll();
}
DeptService.java
package com.zhawb.springcloud.eurekahi.service; import com.zhaowb.springcloud.cloudapi.entities.Dept; import java.util.List; public interface DeptService { boolean add(Dept dept); Dept get(Long id); List<Dept> list();
}
DeptServiceImpl.java
package com.zhawb.springcloud.eurekahi.service.impl; import com.zhaowb.springcloud.cloudapi.entities.Dept;
import com.zhawb.springcloud.eurekahi.dao.DeptDao;
import com.zhawb.springcloud.eurekahi.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptDao dao; @Override
public boolean add(Dept dept) {
return dao.addDept(dept);
} @Override
public Dept get(Long id) {
return dao.findById(id);
} @Override
public List<Dept> list() {
return dao.findAll();
} }
启动类EurekaHiApplication.java
package com.zhawb.springcloud.eurekahi; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
public class EurekaHiApplication { public static void main(String[] args) {
SpringApplication.run(EurekaHiApplication.class, args);
}
}
为了使用ribbon的负载均衡,将上面的eureka-hi 复制两份,分别为eureka-hi2,eureka-hi3
server:
port: 8764
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.zhaowb.springcloud.cloudapi.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:/mybatis/mapper/**/*.xml # mapper映射文件 spring:
application:
name: eureka-client-hi
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/cloudDB02 # 数据库名称
username: root
password: 308539393
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: eureka-client-hi2
prefer-ip-address: true
info:
app.name: eureka-hi3
company.name: www.zhaowb.com
build.artifactId: $project.artifactId$
build.version: $project.version$
修改端口号,数据库地址,
spring:
application:
name: eureka-client-hi
这个一定不要修改,保持三个一致。
eureka-hi3
server:
port: 8764
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.zhaowb.springcloud.cloudapi.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:/mybatis/mapper/**/*.xml # mapper映射文件 spring:
application:
name: eureka-client-hi
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/cloudDB03 # 数据库名称
username: root
password: 308539393
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间 eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: eureka-client-hi3
prefer-ip-address: true
info:
app.name: eureka-hi3
company.name: www.zhaowb.com
build.artifactId: $project.artifactId$
build.version: $project.version$
添加一个消费者,cloud-consumer,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.zhaowb.springcloud</groupId>
<artifactId>cloud-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>cloud-consumer</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.example</groupId>
<artifactId>cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.zhaowb.springcloud</groupId>
<artifactId>cloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
主要是添加,eureka-client 和ribbon的jar
配置 application.yml
server:
port: 8080 eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:8761/eureka/ spring:
application:
name: cloud-consumer
启动类CloudConsumerApplication.java
package com.zhaowb.springcloud.cloudconsumer; import com.zhaowb.springcloud.cloudconsumer.config.MyRibbonRule;
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.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class CloudConsumerApplication { public static void main(String[] args) {
SpringApplication.run(CloudConsumerApplication.class, args);
} @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} /**
* 使用ribbon 的随机算法
* @return
*/
// @Bean
// public IRule myrule(){
// return new RandomRule();
// } }
ribbon 默认的是轮询算法,如果想要用随机算法,可以使用下边那个。
DeptConsumerController.java
package com.zhaowb.springcloud.cloudconsumer.controller; import com.zhaowb.springcloud.cloudapi.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
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
public class DeptConsumerController { //eureka-client-hi
private static final String REST_URL_PREFIX = "http://EUREKA-CLIENT-HI"; @Autowired
private RestTemplate restTemplate; @RequestMapping(value = "/consumer/dept/list")
public List<Dept> list() {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
} }
EUREKA-CLIENT-HI 是在eureka server 注册的 application 的名字。
现在就可以在浏览器输入,http://localhost:8080/consumer/dept/list 访问,当看到"db_source":"clouddb02"字段改变,就说明成功了。
自定义ribbon 算法:MyRibbonRule.java 依然是轮训算法,不过更改为了每5次更改一次。
package com.zhaowb.springcloud.cloudconsumer.config; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server; import java.util.List; public class MyRibbonRule extends AbstractLoadBalancerRule {
// total = 0 // 当total==5以后,我们指针才能往下走,
// index = 0 // 当前对外提供服务的服务器地址,
// total需要重新置为零,但是已经达到过一个5次,我们的index = 1 private int total = 0; // 总共被调用的次数,目前要求每台被调用5次
private int currentIndex = 0; // 当前提供服务的机器号 public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
Server server = null; while (server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers(); int serverCount = allList.size();
if (serverCount == 0) {
/*
* No servers. End regardless of pass, because subsequent passes only get more
* restrictive.
*/
return null;
} // int index = rand.nextInt(serverCount);// java.util.Random().nextInt(3);
// server = upList.get(index); // private int total = 0; // 总共被调用的次数,目前要求每台被调用5次
// private int currentIndex = 0; // 当前提供服务的机器号
if (total < 5) {
server = upList.get(currentIndex);
total++;
} else {
total = 0;
currentIndex++;
if (currentIndex >= upList.size()) {
currentIndex = 0;
}
} if (server == null) {
/*
* The only time this should happen is if the server list were somehow trimmed.
* This is a transient condition. Retry after yielding.
*/
Thread.yield();
continue;
} if (server.isAlive()) {
return (server);
} // Shouldn't actually happen.. but must be transient or a bug.
server = null;
Thread.yield();
}
return server;
} @Override
public Server choose(Object key) {
return choose(getLoadBalancer(), key);
} @Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
// TODO Auto-generated method stub }
}
在启动类上添加 ,@RibbonClient(value = "EUREKA-CLIENT-HI", configuration = MyRibbonRule.class)
就可以使用自定义算法了,value 是使用在那个微服务上 ,configuration 是自定义的类。
当访问的时候看到刷新5次,db_source"字段 更改一次,说明自定义的算法成功了。
代码在码云上:https://gitee.com/JiShiMoWang/cloud-demo
springcloud ribbon Finchley 版本,自定义算法的更多相关文章
- SpringCloud(Finchley版本)中Zull过滤器ResponseBoby返回中文乱码解决方案
Spring Cloud带有"Cloud"的字样,但它并不是云计算解决方案,而是在Spring Boot基础上构建的,用于快速构建分布式系统的通用模式的工具集.使用Spring C ...
- spring-cloud: eureka之:ribbon负载均衡自定义配置(二)
spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...
- SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
一.spring cloud简介 鉴于<史上最简单的Spring Cloud教程>很受读者欢迎,再次我特意升级了一下版本,目前支持的版本为Spring Boot版本2.0.3.RELEAS ...
- 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/ 本文出自方志朋的博客 一.spring ...
- SpringCloud教程二:Ribbon(Finchley版)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- SpringCloud学习(一)服务的注册与发现Eureka(Finchley版本)
创建服务注册中心 在这里,我还是采用Eureka作为服务注册与发现的组件. 首先创建一个空项目 首先创建一个空项目,再创建一个maven项目,首先创建一个主Maven工程,在其pom文件引入依赖,sp ...
- SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)
在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管 ...
- springcloud Eureka Finchley.RELEASE 版本
创建一个父项目cloud-demo pom.xml <?xml version="1.0" encoding="UTF-8"?> <proje ...
- 客户端实现负载均衡:springCloud Ribbon的使用
Netfilx发布的负载均衡器,是一个基于http.tcp的客户端负载均衡工具,具有控制http.tcp客户端的行为,为ribbon配置服务提供者的地址后,ribbon就 可以经过springClou ...
随机推荐
- js设置当前窗口为最上层窗口
实际使用iframe框架时会遇到当前窗口页面不能位于最上层窗口的情况,或者是防止自己的网站被别人放在他们的iframe中,这时就需要设置: <script language="java ...
- NX二次开发-UFUN点构造器UF_UI_point_construct
#include <uf.h> #include <uf_ui.h> UF_initialize(); //点构造器 char sCue[] = "点构造器" ...
- error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.【转载】
文章出处https://blog.csdn.net/qq_38721302/article/details/82850292 今天编写C++程序在使用头文件#include<cstring> ...
- hive简述
显示表头,当前终端有效 set hive.cli.print.header=true; 查看表结构 desc table; 详细的表结构 desc formatted table; 删除表 drop ...
- 关于double的输入输出
double定义的变量输入的时候一定要%lf输入,要是%f输入的话,得到的结果会是0 float输入的时候是%f 但是在输出的时候%lf和%f都可以输出 建议使用double类型时,用%lf输入,%f ...
- Windows系统下安装MySQL 8.0.11数据库
MySQL数据库是常用的数据库之一,而且该数据库开源免费,所以很多公司在使用.本文记录如何在Windows系统下安装MySQL数据库,本次安装的版本号为8.0.11,这个版本是当前的最新版本,据宣传, ...
- jdbc_mysql----函数助手参数化
第一个值填写的是文件路径 第二个值填写的是从第几个开始执行,从0开始
- iOS组件化开发-发布私有库
远程索引库 将远程索引库添关联到本地 pod repo 查看本地已关联仓库源 pod repo add 本地索引库名称 远程索引库仓库地址 pod repo update 索引库名称 pod repo ...
- JS事件 加载事件(onload)注意:1. 加载页面时,触发onload事件,事件写在<body>标签内。 2. 此节的加载页面,可理解为打开一个新页面时。
加载事件(onload) 事件会在页面加载完成后,立即发生,同时执行被调用的程序. 注意:1. 加载页面时,触发onload事件,事件写在<body>标签内. 2. 此节的加载页面,可理解 ...
- 火狐浏览器缓存导致JS已经改变的ID没改变
问题主要就是火狐浏览器缓存. 比如,自己写一个JS,如下: $(document).ready(function () { $("#bigRoom").live("cli ...