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

springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

程序员文章站 2024-02-22 20:27:34
前段时间spring boot 2.0发布了,与之对应的spring cloud finchley版本也随之而来了,两者之间的关系和版本对应详见我这边文章: 项目地址:s...

前段时间spring boot 2.0发布了,与之对应的spring cloud finchley版本也随之而来了,两者之间的关系和版本对应详见我这边文章:

项目地址:spring-cloud-demo

spring boot 1.x和spring cloud dalston和edgware版本搭建的微服务项目现在已经很流行了,现在很多企业都已经在用了,这里就不多说了。

使用版本说明:

  • spring boot 2.0.x
  • spring cloud finchley.release
  • jdk 1.8
  • maven 3.9

eureka 注册中心

spring cloud finchley在支持spring 2.0时修改了eureka的jar包,把之前netflix系列的jar引入的时候都加上了netflix

<dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid>
</dependency>

新建一个eureka-service注册中心服务,pom.xml 文件内容如下:

<?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.sunvalley</groupid>
  <artifactid>eureka-service</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>eureka-service</name>
  <description>demo project for spring boot</description>
 
  <parent>
   <groupid>com.sunvalley.springcloud</groupid>
   <artifactid>spring-cloud-demo</artifactid>
   <version>0.0.1-snapshot</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>
   <spring-cloud.version>finchley.release</spring-cloud.version>
  </properties>
 
  <dependencies>
   <dependency>
     <groupid>org.springframework.cloud</groupid>
     <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid>
   </dependency>
 
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-test</artifactid>
     <scope>test</scope>
   </dependency>
  </dependencies>
 
  <dependencymanagement>
    <dependencies>
      <dependency>
        <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-dependencies</artifactid>
      <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencymanagement>
 
  <build>
   <plugins>
     <plugin>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-maven-plugin</artifactid>
     </plugin>
   </plugins>
  </build>
 
</project>

eureka-service服务的配置文件,application.yml

spring:
 application:
  name: eureka-service
 
server:
 port: 5000
 
eureka:
 instance:
  hostname: localhost
 client:
  registerwitheureka: false
  fetchregistry: false
  serviceurl:
   defaultzone: http://${eureka.instance.hostname}:${server.port}/eureka/

eureka-service服务的配置文件,eurekaserviceapplication

@enableeurekaserver
@springbootapplication
public class eurekaserviceapplication {
 
  public static void main(string[] args) {
   springapplication.run(eurekaserviceapplication.class, args);
  }
}

使用idea启动eureka-service服务,本地访问 即可看到注册中心内容。

服务消费者consumer和提供者provider

实际工作中大多数一个服务既是其它服务的消费者又有可能是服务的提供者,所以我们也就不用刻意的取区分开。

新建一个order-service服务,pom.xml文件

注意:除了eureka-client,openfeign等jar包外,记得引入spring-boot-starter-web不然会出现启动报错。

<?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.sunvalley</groupid>
  <artifactid>order-service</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>order-service</name>
  <description>demo project for spring cloud</description>
 
  <parent>
   <groupid>com.sunvalley.springcloud</groupid>
   <artifactid>spring-cloud-demo</artifactid>
   <version>0.0.1-snapshot</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>
   <spring-cloud.version>finchley.release</spring-cloud.version>
  </properties>
 
  <dependencies>
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter</artifactid>
   </dependency>
 
   <!-- eureka 里面已经包含 ribbon 了, 所以不用单独添加, ribbon依赖, 点击依赖就去看就知道了 -->
   <dependency>
     <groupid>org.springframework.cloud</groupid>
     <artifactid>spring-cloud-starter-netflix-eureka-client</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.boot</groupid>
     <artifactid>spring-boot-starter-web</artifactid>
   </dependency>
 
   <dependency>
     <groupid>org.projectlombok</groupid>
     <artifactid>lombok</artifactid>
     <optional>true</optional>
   </dependency>
 
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-test</artifactid>
     <scope>test</scope>
   </dependency>
  </dependencies>
 
  <dependencymanagement>
   <dependencies>
     <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-dependencies</artifactid>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
     </dependency>
   </dependencies>
  </dependencymanagement>
 
  <build>
   <plugins>
     <plugin>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-maven-plugin</artifactid>
     </plugin>
   </plugins>
  </build>
 
  <repositories>
   <repository>
     <id>spring-snapshots</id>
     <name>spring snapshots</name>
     <url>https://repo.spring.io/snapshot</url>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
   </repository>
   <repository>
     <id>spring-milestones</id>
     <name>spring milestones</name>
     <url>https://repo.spring.io/milestone</url>
     <snapshots>
      <enabled>false</enabled>
     </snapshots>
   </repository>
  </repositories>
 
  <pluginrepositories>
   <pluginrepository>
     <id>spring-snapshots</id>
     <name>spring snapshots</name>
     <url>https://repo.spring.io/snapshot</url>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
   </pluginrepository>
   <pluginrepository>
     <id>spring-milestones</id>
     <name>spring milestones</name>
     <url>https://repo.spring.io/milestone</url>
     <snapshots>
      <enabled>false</enabled>
     </snapshots>
   </pluginrepository>
  </pluginrepositories>
 
 
</project>

order-service服务的配置文件,application.yml

spring:
 application:
  name: order-service
 
server:
 port: 5100
 
eureka:
 client:
  service-url:
   defaultzone: http://localhost:5000/eureka/
 
 
feign:
 hystrix:
  enabled: true

order-service服务的启动类,orderserviceapplication

@enablecircuitbreaker
@enablefeignclients
@enableeurekaclient
@springbootapplication
public class orderserviceapplication {
 
  public static void main(string[] args) {
   springapplication.run(orderserviceapplication.class, args);
  }
}

order-service服务的接口,ordercontroller

@restcontroller
@requestmapping("/order")
public class ordercontroller {
  @value("${server.port}")
  private string port;
 
  /**
   * 获取服务端口号
   * @return
   */
  @getmapping("/getorderport")
  public string getorderport() {
    return "order-service port:" + port;
  }
}

新建一个user-service服务,pom.xml文件跟order-service一样的这里就不列出来了

user-service服务的配置文件,application.yml

spring:
 application:
  name: user-service
 
server:
 port: 5200
 
eureka:
 client:
  service-url:
   defaultzone: http://localhost:5000/eureka/
 
feign:
 hystrix:
  enabled: true

user-service服务的启动类也跟order-service一样的

在user-service工程目录下新建一个package,再新建一个orderremote接口使用feign调用order-service的方法

@feignclient(value = "order-service", fallback = orderremotehystrix.class)
public interface orderremote {
  @getmapping("/order/getorderport")
  string getorderport();
}

熔断机制提示类

@component
public class orderremotehystrix implements orderremote {
  @override
  public string getorderport() {
    return "order service 调用失败!";
  }
} 

user-service服务的接口,usercontroller

@restcontroller
@requestmapping("/user")
public class usercontroller {
  @autowired
  orderremote orderremote;
  @value("${server.port}")
  string port;
 
  /**
   * 获取用户服务的端口
   * @return
   */
  @getmapping("/getuserport")
  public string getuserport() {
    return "user-service port:" + port;
  }
 
  /**
   * 获取订单服务的端口
   * @return
   */
  @getmapping("/getorderport")
  public string getorderport() {
    return "user-order-service port:" + orderremote.getorderport();
  }
}

测试,分别启动eureka-service,order-service,user-service就可以在注册中心看到服务

springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

在浏览器访问 http://localhost:5100/order/getorderport 调用order-service的接口

springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

 在浏览器访问 http://localhost:5200/user/getorderport 通过user-service使用feign声明式调用order-service的接口

springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

关闭order-service,再次调用就会发现熔断机制起了作用

springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

spring cloud gateway

spring cloud gateway是spring官方基于spring 5.0,spring boot 2.0和project reactor等技术开发的网关,spring cloud gateway旨在为微服务架构提供一种简单而有效的统一的api路由管理方式。spring cloud gateway作为spring cloud生态系中的网关,目标是替代netflix zuul,其不仅提供统一的路由方式,并且基于filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

新建一个gateway-service服务,pom.xml文件如下

<?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.sunvalley</groupid>
  <artifactid>gateway-service</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>gateway-service</name>
  <description>demo project for spring cloud</description>
 
  <parent>
   <groupid>com.sunvalley.springcloud</groupid>
   <artifactid>spring-cloud-demo</artifactid>
   <version>0.0.1-snapshot</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>
   <spring-cloud.version>finchley.release</spring-cloud.version>
  </properties>
 
  <dependencies>
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-actuator</artifactid>
   </dependency>
   <dependency>
     <groupid>org.springframework.cloud</groupid>
     <artifactid>spring-cloud-starter-gateway</artifactid>
   </dependency>
   <dependency>
     <groupid>org.springframework.cloud</groupid>
     <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
   </dependency>
   <dependency>
     <groupid>org.springframework.cloud</groupid>
     <artifactid>spring-cloud-starter-netflix-hystrix</artifactid>
   </dependency>
 
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-devtools</artifactid>
     <scope>runtime</scope>
   </dependency>
   <dependency>
     <groupid>org.projectlombok</groupid>
     <artifactid>lombok</artifactid>
     <optional>true</optional>
   </dependency>
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-test</artifactid>
     <scope>test</scope>
   </dependency>
  </dependencies>
 
  <dependencymanagement>
   <dependencies>
     <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-dependencies</artifactid>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
     </dependency>
   </dependencies>
  </dependencymanagement>
 
  <build>
   <plugins>
     <plugin>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-maven-plugin</artifactid>
     </plugin>
   </plugins>
  </build>
 
 
</project>

gateway-service的配置文件,application.yml

spring:
 application:
  name: gateway-service
 cloud:    # spring cloud gateway 路由配置方式
  gateway:
   discovery:   #是否与服务发现组件进行结合,通过 serviceid(必须设置成大写) 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceid 创建路由的功能。
    locator:   #路由访问方式:http://gateway_host:gateway_port/大写的serviceid/**,其中微服务应用名默认大写访问。
     enabled: true
   routes:
   - id: 163           #网关路由到网易官网
    uri: http://www.163.com/
    predicates:
     - path=/163/**
#   - id: order-service      #网关路由到订单服务order-service
#    uri: lb://order-service
#    predicates:
#     - path=/order-service/**
#   - id: user-service      #网关路由到用户服务user-service
#    uri: lb://user-service
#    predicates:
#     - pach=/user-service/**
 
server:
 port: 5001
 
 
logging:
 level:
  org.springframework.cloud.gateway: trace
  org.springframework.http.server.reactive: debug
  org.springframework.web.reactive: debug
  reactor.ipc.netty: debug
 
 
eureka:
 client:
  service-url:
   defaultzone: http://localhost:5000/eureka/
 
feign:
 hystrix:
  enabled: true

gateway-service的启动类,application

package com.sunvalley.demo;
 
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.gateway.route.routelocator;
import org.springframework.cloud.gateway.route.builder.routelocatorbuilder;
import org.springframework.context.annotation.bean;
 
@springbootapplication
public class gatewayserviceapplication {
 
  /**
  * spring cloud gateway 配置方式之一,启动主程序配置,还有一种是配置文件配置
  * @param builder
  * @return
  */
  @bean
  public routelocator customroutelocator(routelocatorbuilder builder) {
   return builder.routes()
      .route(r -> r.path("/qq/**")
           .and()
           .uri("http://www.qq.com/"))
      .build();
  }
 
  public static void main(string[] args) {
   springapplication.run(gatewayserviceapplication.class, args);
  }
}

通过上面我们可以看到,gateway网关路由配置有两种方式:

1.通过@bean自定义routelocator,在启动主类application中配置

2.在配置文件yml中配置

这两种方式都可以实现网关路由是等价的,但是通常项目开发中会使用配置文件yml方式。

运行测试:

访问 http://localhost:8080/qq,路由转发到 http://www.qq.com
访问http://localhost:8080/163, 路由转发到 http://www.163.com

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