bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

springboot2.0和springcloudFinchley版項目的搭建示例

小編給大家分享一下springboot2.0和springcloud Finchley版項目的搭建示例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

賽罕網站制作公司哪家好,找成都創新互聯!從網頁設計、網站建設、微信開發、APP開發、響應式網站開發等網站項目制作,到程序開發,運營維護。成都創新互聯公司2013年成立到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選成都創新互聯

前段時間spring boot 2.0發布了,與之對應的spring cloud Finchley版本也隨之而來了,兩者之間的關系和版本對應詳見我這邊文章:spring boot和spring cloud對應的版本關系

項目地址: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服務,本地訪問 http://localhost:5000/ 即可看到注冊中心內容。

服務消費者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版項目的搭建示例

在瀏覽器訪問 http://localhost:5100/order/getOrderPort 調用order-service的接口

springboot2.0和springcloud Finchley版項目的搭建示例

 在瀏覽器訪問 http://localhost:5200/user/getOrderPort 通過user-service使用feign聲明式調用order-service的接口

springboot2.0和springcloud Finchley版項目的搭建示例

關閉order-service,再次調用就會發現熔斷機制起了作用

springboot2.0和springcloud Finchley版項目的搭建示例

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

看完了這篇文章,相信你對“springboot2.0和springcloud Finchley版項目的搭建示例”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!

當前名稱:springboot2.0和springcloudFinchley版項目的搭建示例
分享URL:http://vcdvsql.cn/article44/gdggee.html

成都網站建設公司_創新互聯,為您提供網站導航網站制作網站營銷商城網站微信公眾號Google

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都定制網站網頁設計