1. Architecture Description
Ribbon is divided into two steps during operation
first step is to select EurekaServer, it is preferable to select a less loaded server within the same region.
The second step then according to a user-specified policy, select from a registration server to get the list of services address.
Wherein Ribbon provides a variety of strategies: such as polling, random and weighted according to the response time.
2. Reference microservicecloud-provider-dept-8001, two new, named 8002,8003
3. New 8002/8003 database, each micro-services are connected respective databases
8002SQL script
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;
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;
8003SQL script
4. Modify the respective YML 8002/8003
8002YML
server:
port: 8002
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml #mybatis所在路径
type-aliases-package: com.atguigu.springcloud.entities #entity别名类
mapper-locations:
- classpath:mybatis/mapper/**/*.xml #mapper映射文件
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: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
Client: Client # eureka registered into the list of services in the
Service-url:
defaultzone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003 .com: 7003 / Eureka /
instance:
instance-the above mentioned id: microservicecloud-dept8002 # custom service name information
prefer-ip-address: true # access path to display the IP address
info:
the app.name: atguigu-microservicecloud
company.name: the WWW .atguigu.com
build.artifactId: $ project.artifactId $
build.version: $ project.version $
8003YML
server:
port: 8003
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml #mybatis所在路径
type-aliases-package: com.atguigu.springcloud.entities #entity别名类
mapper-locations:
- classpath:mybatis/mapper/**/*.xml #mapper映射文件
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: 123456
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
eureka:
Client: Client # eureka registered into the list of services in the
Service-url:
defaultzone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003 .com: 7003 / Eureka /
instance:
instance-the above mentioned id: microservicecloud-dept8003 # custom service name information
prefer-ip-address: true # access path to display the IP address
info:
the app.name: atguigu-microservicecloud
company.name: the WWW .atguigu.com
build.artifactId: $ project.artifactId $
build.version: $ project.version $
Remarks:
port
Database Links
External exposure unified service instance name
5. Start cluster configuration region 3 eureka
6. Start 3 Dept micro service starts and each test passed
7. Start microservicecloud-consumer-dept-80
8. The client load balancing is completed by Ribbo and access services on micro-step Dept
http://localhost/consumer/dept/list
Observe the database name to see the return of, different, load balancing implementation
9. summary
Ribbon is actually a soft load balancing of client components,
he can request and other required client used in combination, and eureka combination is just an example of them.