Ribbon单机配置与集群实战
1. 修改microservicecloud-consumer-dept-80工程
1.1 pom文件增加内容
1 2 3 4 5 6 7 8 9 10 11 12 13
| <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>
|
1.2 修改application.yml 追加eureka的服务注册地址
1 2 3 4 5 6
| eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
|
1.3 RestTemplate配置
对ConfigBean进行新注解@LoadBalanced 获得Rest时加入Ribbon的配置
1 2 3 4 5
| @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); }
|
1.4 主启动类DeptConsumer80_App添加@EnableEurekaClient
1 2 3 4 5 6 7
| @SpringBootApplication @EnableEurekaClient public class ConsumerDept80Application { public static void main(String[] args) { SpringApplication.run(ConsumerDept80Application.class, args); } }
|
1.5 修改DeptController_Consumer客户端访问类
将原来由url访问改为通过微服务名称访问
1 2
| private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
|
Ribbon和Eureka整合后Consumer可以直接调用服务而不用再关心地址和端口号
1.6 单机验证
先启动Eureka集群,然后启动provider-8001注册到Eureka集群中,最后启动consumer-80,后访问下面url
1 2 3
| http://localhost/consumer/dept/get/1 http://localhost/consumer/dept/list http://localhost/consumer/dept/add?dname=大数据部
|
正常返回数据,即表明consumer配置正确
2. Ribbon集群
2.1 架构说明
Ribbon在工作时分成两步
- 第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server。
- 第二步再根据用户指定的策略,在从server取到的服务注册列表中选择一个地址。
其中Ribbon提供了多种策略:比如轮询、随机和根据响应时间加权。
2.2 新建工程
根据provider-8001工程分别复制出provider-8002和provider-8003两个工程,修改相应的配置
2.3 新建数据库
8002数据库脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| DROP DATABASE IF EXISTS cloudDB02; CREATE DATABASE cloudDB02 CHARACTER SET UTF8; USE cloudDB02; CREATE TABLE dept ( deptno BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, dname VARCHAR (60), db_source VARCHAR (60) ); INSERT INTO dept(dname,db_source) VALUES ( '开发部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '人事部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '财务部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '市场部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '运维部' ,DATABASE()); SELECT * FROM dept;
|
8003数据库脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| DROP DATABASE IF EXISTS cloudDB03; CREATE DATABASE cloudDB03 CHARACTER SET UTF8; USE cloudDB03; CREATE TABLE dept ( deptno BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, dname VARCHAR (60), db_source VARCHAR (60) ); INSERT INTO dept(dname,db_source) VALUES ( '开发部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '人事部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '财务部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '市场部' ,DATABASE()); INSERT INTO dept(dname,db_source) VALUES ( '运维部' ,DATABASE()); SELECT * FROM dept;
|
2.4 修改两个工程的yml文件
8002 yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| server: port: 8002 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml type-aliases-package: com.hellodev.entities mapper-locations: - classpath:mybatis/mapper/**/*.xml spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/cloudDB02 username: root password: 1234 dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: microservicecloud-dept8002 prefer-ip-address: true info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: ${project.artifactId} build.version: ${project.version}
|
8003 yml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| server: port: 8003 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml type-aliases-package: com.hellodev.entities mapper-locations: - classpath:mybatis/mapper/**/*.xml spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/cloudDB03 username: root password: 1234 dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200 eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: microservicecloud-dept8003 prefer-ip-address: true info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: ${project.artifactId} build.version: ${project.version}
|
2.5 集群验证
首先启动Eureka集群,集群启动成功后再启动三个provider工程
最后启动consumer工程,并访问接口验证
资源获取
公众号回复 : Ribbon实战 获取本节代码
公众号回复 : SpringCloud思维导图
Tips
欢迎收藏和转发,感谢你的支持!(๑•̀ㅂ•́)و✧
欢迎关注我:后端小哥,专注后端开发,希望和你一起进步!