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界面,已经好看很多了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)