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)需要的依赖:
<?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版本需要单独的引用。
<!--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配置文件:
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)启动类:
@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)示例:
注册中心:
可以看到 sc-saturn 已经成功注册到 consul中了。另外,上面的consul ui界面发生了变化,这是1.2版本做出的优化,相较以前的ui界面,已经好看很多了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Springboot升级到2.7.2结合nacos遇到的坑及解决
这篇文章主要介绍了Springboot升级到2.7.2结合nacos遇到的坑及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-06-06java虚拟机钩子关闭函数addShutdownHook的操作
这篇文章主要介绍了java虚拟机钩子关闭函数addShutdownHook的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-02-02SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程
这篇文章主要介绍了SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02MybatisPlus更新为null的字段及自定义sql注入
mybatis-plus在执行更新操作,当更新字段为空字符串或者null的则不会执行更新,本文主要介绍了MybatisPlus更新为null的字段及自定义sql注入,感兴趣的可以了解一下2024-05-05
最新评论