SpringCould Ribbon负载均衡和Eureka集群

1
2
3
4
本文主要介绍 SpringCould 组件 Ribbon , Eureka 实现负载均衡
其实实现负载均衡方式有很多
比如,Nginx LVS ....等
1
2
3
4
5
概述:
服务消费端 80(端口)
服务提供者 800180028003 (端口)
服务注册中心 700170037003 (端口)

微服务架构 建立maven统一管理项目pom项目(springcolud_parent) 追加pom文件 ,dependencyManagement进行统一dependencies版本管理

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<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>springcolud_parent</groupId>
<artifactId>springcolud_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!--规定木jdk 和字符编码 -->
<properties>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- 进行SpringCloud依赖包的导入处理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud离不开SpringBoot,所以必须要配置此依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>microcloud-api</groupId>
<artifactId>microcloud-api</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>springcolud_parent</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 源代码使用的开发版本 -->
<source>${jdk.version}</source>
<!-- 需要生成的目标class文件的编译版本 -->
<target>${jdk.version}</target>
<encode>${project.build.sourceEncoding}</encode>
</configuration>
</plugin>
<!-- -关于SpringCould 打jar文件 无法运行Application程序 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<module>microcloud-api</module>
<module>microcloud-provider-dept-8001</module>
<module>microcloud-provider-dept-8002</module>
<module>microcloud-consumer-80</module>
<module>microcloud-eureka-7001</module>
<module>microcloud-eureka-7002</module>
<module>microcloud-eureka-7003</module>
<module>microcloud-eureka-7004</module>
</modules>
</project>
## 新建 服务器注册中心项目》》Eureka(7001,7003,7004) 追加pom文件
```python
<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>
<parent>
<groupId>springcolud_parent</groupId>
<artifactId>springcolud_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microcloud-eureka-7004</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>

新建 EurekaApplicationStart 服务器启动 @EnableEurekaServer 代表是EurekaServer端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.jorden.server.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
*
* @ClassName: EurekaApplicationStart
* @Description: eureka服务注册
* @author liwenqiang
* @date 2017125日 下午1:02:32
*
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplicationStart {
public static void main(String[] args) throws Exception {
SpringApplication.run(EurekaApplicationStart.class, args);
}
}

EnableEurekaServer配置

EnableEurekaServer 配置文件 application.yml (eureka-700X.com 为hosts文件127.0.01)
spring.application.name 为 Eureka服务注册中心名字 不可重复 其他的 2个 EnableEurekaServer 都一样 改端口就行
defaultZone 这个就是 Eureka集群配置。注册到其他的Eureka服务注册中心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
port: 7004
spring:
application:
name: eureka-7004.com
## eureak实例定义
eureka:
instance:
## 定义 Eureka 实例所在的主机名称
hostname: eureka-7004.com
client:
service-url:
defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7003.com:7003/eureka
register-with-eureka: false # 当前的微服务不注册到eureka之中
fetch-registry: false # 不通过eureka获取注册信息

服务提供 8001 8002 8003 (端口)

和之前springMCV 一样 提供restful端口
@RestController

配置文件 application.yml 我的orm是mybatis

如果要做负载均衡 application.name ( 8001 8002 8003)必须一样

instance-id 为是此微服务实例id
prefer-ip-address :true 是在服务注册中心是否显示ip
eureka.client.defaultZone 为服务注册中心地址

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
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.jorden.li.domain # 定义所有操作类的别名所在包
mapper-locations: # 所有的mapper映射文件
- classpath:mybatis/mapper/*.xml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 配置当前要使用的数据源的操作类型
driver-class-name: org.gjt.mm.mysql.Driver # 配置MySQL的驱动程序类
url: jdbc:mysql://localhost:3306/mldn8001 # 数据库连接地址
username: root # 数据库用户名
password: 123456 # 数据库连接密码
dbcp2: # 进行数据库连接池的配置
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化提供的连接数
max-total: 5 # 最大的连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
application:
name: microcloud-provider-dept
## # 客户端进行Eureka注册的配置
eureka:
client:
service-url:
defaultZone: http://eureka-7001.com:7001/eureka,http://eureka-7003.com:7003/eureka ,http://eureka-7004.com:7004/eureka
instance:
instance-id: dept-8001.com
prefer-ip-address: true
lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)

服务提供 8002 8003 (端口)的项目都一样 改一个 server.port instance-id 其他的不变

作者的 rest接口

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
35
36
37
38
39
40
41
42
package com.jorden.li.web;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jorden.li.dao.service.IDeptService;
import com.jorden.li.domain.Dept;
/**
*
* @ClassName: DeptRest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author liwenqiang
* @date 2017125日 上午11:20:10
*
*/
@RestController
public class DeptRest {
@Resource
private IDeptService deptService;
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Object get(@PathVariable("id") long id) {
return this.deptService.get(id);
}
@RequestMapping(value = "/dept/add", method = RequestMethod.GET)
public Object add(@RequestBody Dept dept) {
return this.deptService.add(dept);
}
@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
public Object list() {
return this.deptService.list();
}
}

服务提供服务启动 @EnableEurekaClient 必加上

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
package com.jorden.li;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* microcloud-provider-dept-8001
*
* 启动配置类
* @author wqli
*
*/
@SpringBootApplication
@EnableEurekaClient
public class ApplicationStart {
public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationStart.class, args);
}
}

服务提供服务pom文件

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<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>
<parent>
<groupId>springcolud_parent</groupId>
<artifactId>springcolud_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microcloud-provider-dept-8001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>microcloud-api</groupId>
<artifactId>microcloud-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>

消费者 80

application.yml 配置

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 80
## 服务注册
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka-7001.com:7001/eureka,
http://eureka-7003.com:7003/eureka,
http://eureka-7004.com:7004/eureka

注册到Eureka中心

消费者 80 rest 配置 @LoadBalanced 是为负载均衡注解

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
35
package com.jorden.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
*
* @ClassName: RestConfig
* @Description: TODO(这里用一句话描述这个类的作用)
* @author liwenqiang
* @date 2017125日 上午11:13:50
*
*/
@Configuration
public class RestConfig {
/**
*
* @Title: getRestTemplate
* @Description: TODO(负载均衡配置) @LoadBalanced
* @param @return 设定文件
* @return RestTemplate 返回类型
* @throws
*
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}

消费者 80 rest接口调用 MICROCLOUD-PROVIDER-DEPT 为微服务 在eureka 注册的名字

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
35
36
package com.jorden.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.jorden.li.domain.Dept;
/**
*
* @ClassName: ConsumerDeptController
* @Description: TODO(这里用一句话描述这个类的作用)
* @author liwenqiang
* @date 2017125日 上午11:21:23
*
*/
@RestController
public class ConsumerDeptController {
private static String url = "MICROCLOUD-PROVIDER-DEPT";
@Autowired
private RestTemplate restTemplate;
public static final String DEPT_GET_URL = "http://" + url + "/dept/get/";
public static final String DEPT_GETLISGT_URL = "http://" + url + "/dept/list";
@RequestMapping(value = "/consumer/dept/{id}")
public Object getDept(@PathVariable("id") Long id) {
Dept dept = this.restTemplate.getForObject(DEPT_GET_URL + id, Dept.class);
return dept;
}
}

消费者 80 pom文件

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<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>
<parent>
<groupId>springcolud_parent</groupId>
<artifactId>springcolud_parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microcloud-consumer-80</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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-ribbon</artifactId>
</dependency>
<dependency>
<groupId>microcloud-api</groupId>
<artifactId>microcloud-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>

消费者 80 启动 @EnableEurekaClient 注解 不要忘记

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
@EnableEurekaClient
public class ApplicationStart {
public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationStart.class, args);
}
}