欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 11:50:34
概述: spring boot 2.0相对于之前的版本,变化还是很大的。首先对jdk的版本要求已经不能低于1.8,其次依赖的spring的版本也是最新版本5.0,并集成了...

概述:

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)示例:

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界面,已经好看很多了。

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