springcloud实战之Euraka

开发工具

  • jdk1.8
  • maven 3.6.2
  • IntelliJ IDEA
  • springCloud Finchley.RELEASE
  • springBoot 2.0.3

java

project 依赖

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
	<?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.qn</groupId>
<artifactId>springcloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<!--子项目-->
<modules>
<module>consumer-order</module>
<module>provider-user</module>
<module>appEureka</module>
<module>consumer-order-ribbon</module>
</modules>

<!--参数-->
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<!--springBoot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>

<!--springCloud版本序列配置-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!--Spring Cloud 基础-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<!--Spring Cloud 服务注册组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--此处的依赖是SpringBoot2.0以后专用的,如果您使用的SpringBoot版本低于2.0请使用spring-cloud-starter-eureka-server-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<!--spring boot 2.0.3版本解决方案:spring-cloud-starter-feign-->
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<!--Spring Boot 执行器组件-->
<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>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

eureka 客户端依赖、启动类、配置

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>appEureka</artifactId>

</project>

启动类

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaServer//开启Eurekaserver
public class Eureka{
public static void main(String[]args){
SpringApplication.run(Eureka.class,args);
}
}

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 端口号
server:
port: 9999
# 服务注册相关配置
eureka:
# 服务实例主机名
instance:
hostname: localhost
# 服务提供者配置
client:
# 不进行注册(当服务注册中心是单点而非高可用时的配置方式)
registerWithEureka: false
# 不获取注册信息(当服务注册中心是单点而非高可用时的配置方式)
fetchRegistry: false
# 服务注册中心地址
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

消费者依赖、启动类、配置、bean、业务类

依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>consumer-order</artifactId>

</project>
启动类
1
2
3
4
5
6
7
8
9
10
11
@SpringBootApplication
@EnableEurekaClient
public class ConsumerApp{
public static void main(String[]args){
SpringApplication.run(ConsumerApp.class,args);
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
port:8900
spring:
application:
name:consumer-order
eureka:
instance:
hostname: localhost
# 发呆时间,即服务失效时间(缺省为90s),就是超过15秒没有续约就会从注册表中剔除
lease-expiration-duration-in-seconds: 15
# 心跳时间,即服务续约间隔时间(缺省为30s)
lease-renewal-interval-in-seconds: 5
client:
service-url:
defaultZone:http://localhost:9999/eureka
bean
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
public class User{
private Long id;
private Date date;

public User(){

}

public User(Long id){
this.id=id;
this.date=newDate();
}


public Longget Id(){
return id;
}

public void setId(Long id){
this.id=id;
}

public Date getDate(){
return date;
}

public void setDate(Datedate){
this.date=date;
}
}
业务类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@RestController
public class OrderController{
@Autowired
private RestTemplate restTemplate;

@Autowired
private EurekaClient eurekaClient;

@GetMapping("/order/{id}")
public Userget Order(@PathVariableLongid){
InstanceInfo nextServerFromEureka=eurekaClient.getNextServerFromEureka("PROVIDER-USER",false);
Stringurl=nextServerFromEureka.getHomePageUrl();
Useruser=restTemplate.getForObject(url+"/user/"+id,User.class);
return user;
}
}

提供者依赖、配置、启动类、bean、业务类

依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>provider-user</artifactId>

</project>
配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
port:7900#自定义boot项目访问端口
spring:
application:
name:provider-user
eureka:
instance:
hostname: localhost
# 发呆时间,即服务失效时间(缺省为90s),就是超过15秒没有续约就会从注册表中剔除
lease-expiration-duration-in-seconds: 15
# 心跳时间,即服务续约间隔时间(缺省为30s)
lease-renewal-interval-in-seconds: 5
client:
service-url:
defaultZone:http://localhost:9999/eureka
启动类
1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaClient//启用eureka客户端
public class ProviderApp{
public static void main(String[]args){
SpringApplication.run(ProviderApp.class,args);
}
}
bean
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
public class User{
private Long id;
private Date date;

public User(){

}

public User(Long id){
this.id=id;
this.date=newDate();
}


public Longget Id(){
return id;
}

public void setId(Long id){
this.id=id;
}

public Date getDate(){
return date;
}

public void setDate(Datedate){
this.date=date;
}
}
业务类
1
2
3
4
5
6
7
8
9
10
@RestController
public class UserController{
@Autowired
private EurekaClient eurekaClient;

@GetMapping("/user/{id}")
publicUsergetUser(@PathVariableLongid){
return new User(id);
}
}

测试

  • 启动 eureka、消费者、提供者,访问 eureka 客户端
    springcloud实战之eureka(二)_2019-11-14-10-26-57.png
  • 消费者通过 eureka 访问提供者
    springcloud实战之eureka(二)_2019-11-14-10-31-56.png
文章目录
  1. 1. 开发工具
  2. 2. java
    1. 2.1. project 依赖
    2. 2.2. eureka 客户端依赖、启动类、配置
      1. 2.2.0.1. 依赖
      2. 2.2.0.2. 启动类
      3. 2.2.0.3. 配置
      4. 2.2.0.4. 消费者依赖、启动类、配置、bean、业务类
        1. 2.2.0.4.1. 依赖
        2. 2.2.0.4.2. 启动类
        3. 2.2.0.4.3. 配置
        4. 2.2.0.4.4. bean
        5. 2.2.0.4.5. 业务类
      5. 2.2.0.5. 提供者依赖、配置、启动类、bean、业务类
        1. 2.2.0.5.1. 依赖
        2. 2.2.0.5.2. 配置
        3. 2.2.0.5.3. 启动类
        4. 2.2.0.5.4. bean
        5. 2.2.0.5.5. 业务类
  • 3. 测试
  • |