目录
Ribbon是什么?
Ribbon能干嘛?
-
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-eurekaartifactId>
- <version>1.4.7.RELEASEversion>
- dependency>
- # Eureka配置
- eureka:
- client:
- register-with-eureka: false # 不向 Eureka注册自己
- service-url: # 从三个注册中心中随机取一个去访问
- defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- @SpringBootApplication
- @EnableEurekaClient //开启Eureka 客户端
- public class DeptConsumer_80 {
- public static void main(String[] args) {
- SpringApplication.run(DeptConsumer_80.class,args);
- }
- }
- @Configuration
- public class ConfigBean {//@Configuration -- spring applicationContext.xml
-
- @Bean
- @LoadBalanced //配置负载均衡实现RestTemplate
- public RestTemplate getRestTemplate(){
- return new RestTemplate();
- }
-
- }
- // Ribbon:我们这里的地址,应该是一个变量,通过服务名来访问
- //private static final String REST_URL_PREFIX = "http://localhost:8001";
- private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
修改完后,就不用关心ip地址和端口号了
环境搭建
- CREATE DATABASE `db02`;
- use `db02`;
- CREATE TABLE `dept` (
- `deptno` bigint(20) NOT NULL AUTO_INCREMENT,
- `dname` varchar(60) DEFAULT NULL,
- `db_source` varchar(60) DEFAULT NULL,
- PRIMARY KEY (`deptno`) USING BTREE
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
- 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());
- CREATE DATABASE `db03`;
- use `db03`;
- CREATE TABLE `dept` (
- `deptno` bigint(20) NOT NULL AUTO_INCREMENT,
- `dname` varchar(60) DEFAULT NULL,
- `db_source` varchar(60) DEFAULT NULL,
- PRIMARY KEY (`deptno`) USING BTREE
- ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
- 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());
pom依赖
- <dependencies>
-
- <dependency>
- <groupId>org.examplegroupId>
- <artifactId>springcloud-apiartifactId>
- <version>1.0-SNAPSHOTversion>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- dependency>
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-coreartifactId>
- dependency>