中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Cloud 服務發布與調用

發布時間:2020-07-13 22:31:53 來源:網絡 閱讀:660 作者:zhuwensheng 欄目:開發技術
1. 發布服務
  1. 引入相關依賴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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zws.cloud</groupId>
    <artifactId>Eurka-Producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Eurka-Producer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Eureka 客戶端依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. 配置Eureka Server地址 application.yml
server:
  port: 8080

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: Eureka-Producer
  1. 發布一個服務,其實就是一個Http接口,如下:
package com.zws.cloud.producer;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wensh.zhu
 * @Date 2019/2/16 19:33
 */
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

啟動 
package com.zws.cloud.producer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EurekaProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaProducerApplication.class, args);
    }

}
2. 調用服務
  1. 依賴配置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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zws.cloud</groupId>
    <artifactId>Eurka-Producer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Eurka-Producer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- Ribbon用于客戶端負載均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <!-- Feign用于服務發現與調用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. 啟用Ribbon負載均衡以及Feign接口注入,application.yml
server:
  port: 8088

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: Eureka-Consumer
  1. 定義Feign客戶端以及熔斷回調
package com.zws.cloud.consumer;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @Author wensh.zhu
 * @Date 2019/2/16 15:14
 */
@FeignClient(value = "Eureka-Producer", fallbackFactory = HelloClientFallbackFactory.class)
public interface HelloClient {

    @GetMapping("/hello")
    String hello();
}

package com.zws.cloud.consumer;

import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @Author wensh.zhu
 * @Date 2019/2/16 16:12
 */
@Component
public class HelloClientFallbackFactory implements FallbackFactory<String>, HelloClient {
    @Override
    public String create(Throwable throwable) {
        return this.hello();
    }

    @Override
    public String hello() {
        return "";
    }
}

@FeignClient(value = "Eureka-Producer", fallbackFactory = HelloClientFallbackFactory.class)其中Eurka-Producer為服務提供方的實例名稱(spring.application.name)

  1. 啟用Feign接口以及調用
package com.zws.cloud.EurkaProducer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@EnableFeignClients
@SpringBootApplication
public class EurkaProducerApplication {

    @Resource
    private HelloFeign helloFeign;

    public static void main(String[] args) {
        SpringApplication.run(EurkaProducerApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello() {
        return helloFeign.hello();
    }

}
3. 總結

服務發布主要涉及如下三個方面:

  1. 配置Eureka Server地址:
    ## 多個以逗號隔開
    eureka:
    client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/
  2. 引入Eureka Client依賴
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  3. 定義普通的Http接口

服務調用

  1. 引入三個依賴:
<!-- Eureka客戶端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<!-- Ribbon用于負載均衡 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

<!-- Feign用于服務發布與調用 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. 配置Eureka Server:
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka/
  1. 定義FeignClient接口以及失敗的回調
  2. 啟動類添加@EnableFeignClients注解
@EnableFeignClients會把第三步定義的接口注入Spring上下文中 ,然后使用的時候直接注入即可。
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

西藏| 昭通市| 岢岚县| 普兰县| 陈巴尔虎旗| 孟州市| 齐齐哈尔市| 西吉县| 田东县| 承德县| 台江县| 五寨县| 荃湾区| 肇庆市| 蒲城县| 宁化县| 安化县| 如东县| 栾川县| 枣庄市| 丹江口市| 上思县| 建始县| 博罗县| 长顺县| 苗栗县| 黑水县| 城固县| 凤庆县| 邹平县| 青阳县| 康定县| 安西县| 双桥区| 乐陵市| 嘉义市| 开封市| 仁布县| 宁安市| 安顺市| 新竹县|