【SpringCloud】SpringCloud基础教程(7)
前言:
本文内容:Config:服务端连接Git配置、Zuul:客户端连接服务端远程访问、远程配置实战测试
推荐免费SpringCloud基础教程视频:【狂神说Java】SpringCloud最新教程IDEA版_哔哩哔哩_bilibili
SpringCloud笔记代码下载地址:
蓝奏云:下载地址 密码:joker
百度云:下载地址 提取码:04eh
Config:服务端连接Git配置
-
新建Module为
springcloud-config-server-3344
-
在pom文件中引入依赖
1
2
3
4
5
6
7
8
9
10
11
12<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> -
在
resources
下新建application.yaml
1
2
3
4
5
6
7
8
9
10
11server:
port: 3344
spring:
application:
name: springcloud-config-server
#远程连接
cloud:
config:
server:
git:
uri: https://gitee.com/jokerdig/springcloud-config.git -
新建
com.jokerdig.springcloud
包,在包下新建ConfigServer_3344
主启动类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17package com.jokerdig.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author Joker大雄
* @data 2022/8/10 - 10:45
**/
// 开启ConfigServer
public class ConfigServer_3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigServer_3344.class,args);
}
} -
启动
ConfigServer_3344
,运行测试http://localhost:3344/application-dev.yaml
1
2
3
4
5spring:
application:
name: springcloud-config-dev
profiles:
active: dev
Config:客户端连接服务端远程访问
-
为
Gitee
提交config-client.yaml
(提交步骤参考上一篇)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
27spring:
profiles:
active: dev
server:
port: 3355
# spring开发配置
spring:
profiles: dev
application:
name: springcloud-client-dept
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
server:
port: 3356
# spring测试配置
spring:
profiles: test
application:
name: springcloud-client-dept
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ -
新建Module为
springcloud-config-client-3355
-
在pom中引入依赖
1
2
3
4
5
6
7
8
9
10
11
12<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> -
在
resources
下新建bootstrap.yaml
和application.yaml
bootstrap
1
2
3
4
5
6
7
8# bootstrap 系统级别的配置
spring:
cloud:
config:
uri: http://localhost:3344
name: config-client # 需要读取的资源名称
profile: dev
label: masterapplication
1
2
3
4# application用户级别的配置
spring:
application:
name: springcloud-config-client-3355 -
新建包
com.jokerdig.springcloud
,在包下新建ConfigClient_3355
主启动类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.jokerdig.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Joker大雄
* @data 2022/8/10 - 11:19
**/
public class ConfigClient_3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_3355.class,args);
}
} -
新建
com.jokerdig.springcloud.controller
包,在包下新建ConfigClientController
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
27package com.jokerdig.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Joker大雄
* @data 2022/8/10 - 11:29
**/
public class ConfigClientController {
private String applicationName;
private String eurekaServer;
private String port;
public String getConfig(){
return "applicationName:"+applicationName+
"eurekaServer:"+eurekaServer+
"port:"+port;
}
} -
启动3344、3355,运行测试
1
2
3applicationName:springcloud-client-dept
eurekaServer:http://eureka7001.com:7001/eureka/
port:3355
Config:远程配置实战测试
把之前项目的配置从本地改为远端
-
为
Gitee
提交config-dept.yaml
和config-eureka.yaml
config-dept
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
67spring:
profiles:
active: dev
server:
port: 8001
# mybatis配置
mybatis:
type-aliases-package: com.jokerdig.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
# spring开发配置
spring:
profiles: dev
application:
name: springcloud-config-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?useSSL=true&serverTimezone=UTC
username: root
password: 123456
# Eureka配置
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001
prefer-ip-address: true
# info信息配置
info:
app.name: jokerdig-springcloud
company.name: jokerdig.com
server:
port: 8001
# mybatis配置
mybatis:
type-aliases-package: com.jokerdig.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
# spring开发配置
spring:
profiles: test
application:
name: springcloud-config-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db02?useSSL=true&serverTimezone=UTC
username: root
password: 123456
# Eureka配置
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001
prefer-ip-address: true
# info信息配置
info:
app.name: jokerdig-springcloud
company.name: jokerdig.comconfig-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
37
38
39
40
41
42
43spring:
profiles:
active:
server:
port: 7001
# spring配置
spring:
profiles: dev
application:
name: springcloud-config-eureka
# eureka配置
eureka:
instance:
hostname: eureka7001.com #服务端实例名称
client:
register-with-eureka: false # 是否向eureka注册中心注册自己
fetch-registry: false # 如果为false 则表示自己为注册中心
service-url:
# http://${eureka.instance.hostname}:${server.port}/eureka/
# 集群 需要取关联别人
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server:
port: 7001
# spring配置
spring:
profiles: test
application:
name: springcloud-config-eureka
# eureka配置
eureka:
instance:
hostname: eureka7001.com #服务端实例名称
client:
register-with-eureka: false # 是否向eureka注册中心注册自己
fetch-registry: false # 如果为false 则表示自己为注册中心
service-url:
# http://${eureka.instance.hostname}:${server.port}/eureka/
# 集群 需要取关联别人
defaultZone: -
新建Module为
springcloud-config-eureka-7001
复制
springcloud-eureka-7001
内容到这个Module修改主启动类名字为
ConfigEurekaServer_7001
-
在
pom
中引入依赖(原本复制的依赖不变)1
2
3
4<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> -
在
resources
下新建bootstrap.yaml
并重新配置application.yaml
bootstrap
1
2
3
4
5
6
7spring:
cloud:
config:
name: config-eureka
label: master
profile: dev
uri: http://localhost:3344application
1
2
3spring:
application:
name: springcloud-config-eureka-7001 -
运行3344、
ConfigEurekaServer_7001
,进行测试http://localhost:7001/ 可以访问说明配置ok
-
新建Module为
springcloud-config-dept-8001
复制
springcloud-provider-dept-8001
内容到这个Module修改主启动类名字为
ConfigDeptProvider_8001
-
在
pom
中引入依赖(原本复制的依赖不变)1
2
3
4<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> -
在
resources
下新建bootstrap.yaml
并重新配置application.yaml
bootstrap
1
2
3
4
5
6
7spring:
cloud:
config:
name: config-dept
label: master
profile: dev
uri: http://localhost:3344application
1
2
3
4
5
6
7
8
9
10spring:
application:
name: springcloud-config-dept-8001
9. 运行3344、`ConfigEurekaServer_7001`、`ConfigDeptProvider_8001`,进行测试
http://localhost:8001/dept/get/1
```java
{"deptno":1,"dname":"开发部","db_source":"db01"}