服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)

SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)

2021-05-28 13:16张书康 Java教程

这篇文章主要介绍了SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

概述:

spring boot 2.0相对于之前的版本,变化还是很大的。首先对jdk的版本要求已经不能低于1.8,其次依赖的spring的版本也是最新版本5.0,并集成了功能强大的webflux等。

springcloud finchley 版本的升级也带来了全新组件:spring cloud function 和 spring cloud gateway ,前者致力于函数式编程模块的整合,后者则是网关netflix zuul 的替换组件。

1)需要的依赖:

?
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?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.iyb</groupid>
 <artifactid>saturn</artifactid>
 <version>0.0.1-snapshot</version>
 <packaging>jar</packaging>
 
 <name>sc-saturn</name>
 <description>sc-saturn for spring boot</description>
 
 <parent>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-parent</artifactid>
  <version>2.0.1.release</version>
 </parent>
 
 <properties>
  <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
  <java.version>1.8</java.version>
  <mapper.version>3.4.0</mapper.version>
  <pagehelper.version>4.1.6</pagehelper.version>
  <fastjson.version>1.2.13</fastjson.version>
  <jackson.version>2.8.5</jackson.version>
  <druid.version>1.0.27</druid.version>
  <mysql.version>5.1.40</mysql.version>
  <commons-fileupload.version>1.3.1</commons-fileupload.version>
 </properties>
 
 
 <dependencies>
 
  <!--starter begin-->
  <!--spring cloud starter-->
 
  <dependency>
   <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-starter-consul-discovery</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-openfeign</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-starter-netflix-hystrix</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-starter-netflix-zuul</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-starter-netflix-ribbon</artifactid>
  </dependency>
   <dependency>
   <groupid>org.springframework.cloud</groupid>
   <artifactid>spring-cloud-starter-sleuth</artifactid>
  </dependency>
 
 
  <!--tomcat starter-->
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-tomcat</artifactid>
   <scope>provided</scope>
  </dependency>
  <dependency>
   <groupid>org.apache.tomcat.embed</groupid>
   <artifactid>tomcat-embed-jasper</artifactid>
   <scope>compile</scope>
  </dependency>
   <!--actuator starter-->
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-actuator</artifactid>
  </dependency>
 
 
  <!--spring boot starter-->
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter</artifactid>
  </dependency>
 
  <!--spring starter-->
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-jdbc</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-aop</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-data-redis</artifactid>
  </dependency>
  <dependency>
   <groupid>org.springframework.boot</groupid>
   <artifactid>spring-boot-starter-web</artifactid>
  </dependency>
 
  <!--fastjon-->
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>fastjson</artifactid>
   <version>${fastjson.version}</version>
  </dependency>
 
  <dependency>
   <groupid>io.github.openfeign</groupid>
   <artifactid>feign-gson</artifactid>
   <version>9.3.1</version>
  </dependency>
 
  <!--lombok-->
  <dependency>
   <groupid>org.projectlombok</groupid>
   <artifactid>lombok</artifactid>
  </dependency>
 
  <dependency>
   <groupid>log4j</groupid>
   <artifactid>log4j</artifactid>
   <version>1.2.17</version>
  </dependency>
 
 
  <dependency>
   <groupid>commons-beanutils</groupid>
   <artifactid>commons-beanutils</artifactid>
  </dependency>
  <dependency>
   <groupid>commons-collections</groupid>
   <artifactid>commons-collections</artifactid>
  </dependency>
  <dependency>
   <groupid>org.apache.commons</groupid>
   <artifactid>commons-lang3</artifactid>
   <version>3.4</version>
  </dependency>
  <dependency>
   <groupid>com.netflix.feign</groupid>
   <artifactid>feign-gson</artifactid>
   <version>8.15.1</version>
  </dependency>
 
 </dependencies>
 
 
 
 // spring cloud finchley.release
 <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>
 
</project>

需要注意的地方:

部分依赖做了改变:

 

旧版本 finchley 版本
spring-cloud-starter-eureka-client spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-eureka-server spring-cloud-starter-netflix-eureka-server
spring-cloud-starter-hystrix spring-cloud-starter-netflix-hystrix
spring-cloud-starter-hystrix-dashboard spring-cloud-starter-netflix-hystrix-dashboard
spring-cloud-starter-ribbon spring-cloud-starter-netflix-ribbon
spring-cloud-starter-turbine spring-cloud-starter-netflix-turbine
spring-cloud-starter-zuul spring-cloud-starter-netflix-zuul
spring-cloud-starter-feign spring-cloud-starter-openfeign

 

新版本的 springcloud 上面的依赖都发生了变化,依赖中都引入了 netflix,或许是为了声明吧(组件的实现底层并非spring)。

log4j依赖:

在spring cloud之前的版本中,日志只需要引入lombok即可,但是finchley版本需要单独的引用。

?
1
2
3
4
5
6
7
8
9
10
11
<!--lombok-->
  <dependency>
   <groupid>org.projectlombok</groupid>
   <artifactid>lombok</artifactid>
  </dependency>
 
  <dependency>
   <groupid>log4j</groupid>
   <artifactid>log4j</artifactid>
   <version>1.2.17</version>
  </dependency>

2)集成consul配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
spring:
 application:
 name: sc-saturn
 profiles:
 active: dev
 
 cloud:
 config:
#  uri: http://localhost:9999
  discovery:
  enabled: false
  serviceid: sc-earth-config-server
  label: master
  profile: pre
 consul:
  host: http://localhost:8500
  port: 80
  discovery:
   prefer-ip-address: true
   #这里是重写健康检查的endpoint,默认为 /health,重写是为了节省不必要的网络传输,只需要http 200状态码即可
   health-check-path: /ok/health
   heartbeat:
   enabled: false

3)启动类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@springbootapplication
@enablefeignclients
@enablezuulserver
@componentscan(basepackages={"com.iyb.ak"})
@enablediscoveryclient
@slf4j
public class saturnapplication implements commandlinerunner{
 
 public static void main(string[] args) {
  springapplication.run(saturnapplication.class, args);
 }
 
 
 @override
 public void run(string... args) throws exception {
  log.info(">>>>>>>>>>>>>>> sc-saturn 启动完成<<<<<<<<<<<<<");
 }
}

4)示例:

SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)

注册中心:

SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)

可以看到 sc-saturn 已经成功注册到 consul中了。另外,上面的consul ui界面发生了变化,这是1.2版本做出的优化,相较以前的ui界面,已经好看很多了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/woshilijiuyi/article/details/80969407

延伸 · 阅读

精彩推荐