国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon

這篇具有很好參考價值的文章主要介紹了SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

?壹、零基礎(chǔ)

一、微服務(wù)架構(gòu)零基礎(chǔ)理論入門

SpringCloud=分布式微服務(wù)架構(gòu)的一站式解決方案,是多種微服務(wù)架構(gòu)落地技術(shù)的集合體,俗稱微服務(wù)全家桶。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

二、從2.2.x和H版開始說起

springboot版本選擇:

git源碼地址:https://github.com/spring-projects/spring-boot/releases/

SpringBoot2.0新特性:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Release

springcloud版本選擇:

git源碼地址:https://github.com/spring-projects/spring-cloud

官網(wǎng):https://spring.io/projects/spring-cloud

官網(wǎng)看cloud版本

springcloud和springboot之間依賴關(guān)系:https://spring.io/projects/spring-cloud#overview

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

三、關(guān)于Cloud各種組件的停更/升級/替換

服務(wù)注冊中序:Eureka(掛)。zookeeper。Consul。Nacos(百萬級推薦)。

服務(wù)調(diào)用1:Ribbon。LoadBalancer。

服務(wù)調(diào)用2:Feign(掛)。OpenFeign。

服務(wù)降級:Hystrix(掛)。resilience4j。sentienl(推薦)。

服務(wù)網(wǎng)關(guān):Zuul(掛)。Zuul2(未出)。gateway(推薦)。

服務(wù)配置:Config(掛)。Nacos(推薦)。

服務(wù)總線:Bus(掛)。Nacos(推薦)。

參考資料:

1. Spring Cloud:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/

2. Spring Cloud中文文檔:https://www.bookstack.cn/read/spring-cloud-docs/docs-index.md

3. Spring Boot:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/

四、微服務(wù)架構(gòu)編碼構(gòu)建

4.1 引入依賴

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

<!-- 統(tǒng)一管理jar包版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>
<!-- 子模塊繼承之后,提供作用:鎖定版本+子modlue不用寫groupId和version  -->
    <dependencyManagement>
        <dependencies>
            <!--spring boot 2.2.2-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--spring cloud alibaba 2.1.0.RELEASE-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
                <optional>true</optional>
            </dependency>
        </dependencies>
    </dependencyManagement>
<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.2.1.RELEASE</version>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

DependencyManagement和Dependencies的區(qū)別:

<DependencyManagement>是父類定義的坐標(biāo)版本號,用在父工程,類似于規(guī)范將全工程的版本統(tǒng)一,只負(fù)責(zé)定義,而不負(fù)責(zé)引入實現(xiàn)。真正引入依賴的是子類<dependencies>定義的版本號。如果子類沒指定版本號,默認(rèn)是用父類的。如果子類指定了版本號,則版本號用子類指定的(如果不在子項目中聲明依賴,不會從父項目中繼承。只要在子項目中寫了該依賴項,并且沒有指定具體的版本,才會從父項目中繼承該項,并且version和scope都讀取自父pom)。

maven中跳過單元測試:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

4.2 支付模塊構(gòu)建

1.建module:右鍵項目新建Module

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

2.改POM

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>
    <!--mysql-connector-java-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--jdbc-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </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>

3. 寫YML

在cloud-provider-payment8001的resources下創(chuàng)建application.yml文件,注意要變成綠色樹葉:

server:
  port: 8081

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&usrSSL=false
    username: root
    password: 111111

mybatis:
  mapper-locations: classpath:mapper/*.xml #在mapper包下創(chuàng)建.xml配置文件
  type-aliases-package: com.atguigu.springcloud.entities #entities下創(chuàng)建實體類

4. 主啟動

在src/main/java/com/atguigu/springcloud包下創(chuàng)建PaymentMain8001類,加上@SpringBootApplication作為主啟動類:

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

5. 業(yè)務(wù)類

5.1 建表SQL

CREATE TABLE `payment`(
    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
    `serial` varchar(200) DEFAULT '',
    PRIMARY KEY(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

5.2 entities實體類

在java/com/atguigu/springcloud下創(chuàng)建entities包,在其下創(chuàng)建Payment類:

主實體Payment代碼:

public class Payment implements Serializable {
    private Long id;
    private String serial;
    public Long getId() {return id;}
    public void setId(Long id) {this.id = id;}
    public String getSerial() {return serial;}
    public void setSerial(String serial) {this.serial = serial;}
    public Payment(Long id, String serial) {this.id = id;this.serial = serial;}
    public Payment() {}
}

Json封裝體CommonResult

在entities包下創(chuàng)建CommonResult類:

public class CommonResult<T> {
    private Integer code;
    private String message;
    private T data;
    public  CommonResult(Integer code,String message){this(code,message,null);}
    public CommonResult(Integer code, String message, T data) {this.code = code;this.message = message;this.data = data;}
    public CommonResult() {}
    public Integer getCode() {return code;}
    public void setCode(Integer code) {this.code = code;}
    public String getMessage() {return message;}
    public void setMessage(String message) {this.message = message;}
    public T getData() {return data;}
    public void setData(T data) {this.data = data;}
}

1. @Data是一個Lombok注解,用于自動生成Java類的常見方法,如getter、setter、toString、equals和hashCode方法。

2. this(code, message, null)表示調(diào)用當(dāng)前類的另一個構(gòu)造函數(shù)CommonResult(Integer code, String message, Object data),并傳遞參數(shù)codemessagenull。

3. @AllArgsConstructor是Lombok注解之一,用于自動生成一個包含所有參數(shù)的構(gòu)造函數(shù)。

5.3 dao

創(chuàng)建dao包,在其下創(chuàng)建PaymentDao接口,記得寫@Mapper注解:

@Mapper
public interface PaymentDao {
    public int create(Payment payment);
    public Payment getPaymentById(@Param("id") Long id);
}

mybatis的映射文件:

在resources下創(chuàng)建mapper包,創(chuàng)建PaymentMapper.xml映射文件:

1. parameterType是用來指定傳入SQL語句的參數(shù)類型。

2. resultMap用來指定查詢結(jié)果映射到哪個Java對象上。

3. column是數(shù)據(jù)庫字段,property是java對象屬性。

4.keyProperty:用于指定自動生成的主鍵值要被設(shè)置到Java實體類的哪個屬性上。

resultMap映射屬性,用于防止命名不規(guī)范造成錯誤:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.springcloud.dao.PaymentDao">
    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        insert into payment(serial) values(#{serial});
    </insert>
    <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        select * from payment where id=#{id};
    </select>
</mapper>

5.4 service

在springcloud下創(chuàng)建service包,創(chuàng)建PaymentService接口:

public interface PaymentService{
    public int create(Payment payment);
    public Payment getPatmentById(@Param("id") Long id);
}

1. @Param注解的作用是給方法參數(shù)起別名,@Param注解的作用是給方法參數(shù)起別名。@Param("id")表示給方法參數(shù)id起別名為"id"。這樣在SQL映射文件中可以使用#{id}來引用這個參數(shù)。

創(chuàng)建PaymentServiceImpl實現(xiàn)類,記得寫@Service注解

1. @Resource注解是用來標(biāo)記一個需要依賴注入的對象。可以根據(jù)名稱進(jìn)行依賴注入,也可以根據(jù)類型進(jìn)行依賴注入。

@Service
public class PaymentServiceImpl implements PaymentService {
    @Resource
    private PaymentDao paymentDao;

    public int create(Payment payment) {
        return paymentDao.create(payment);
    }
    public Payment getPatmentById(Long id) {
        return paymentDao.getPaymentById(id);
    }
}

5.5 controller

在springcloud下創(chuàng)建controller包,然后創(chuàng)建PaymentController類:

1. @GetMapping注解用于將HTTP的GET請求映射到相應(yīng)的處理方法上。

2.?@PathVariable注解用于將URL路徑中的變量值綁定到方法參數(shù)上。

@RestController
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @PostMapping(value="/payment/create")
    public CommonResult create(Payment payment){
        System.out.println(payment.getSerial());
        int result = paymentService.create(payment);
        if(result>0){
            return new CommonResult(200,"插入數(shù)據(jù)庫成功",result);
        }else{
            return new CommonResult(444,"插入數(shù)據(jù)庫失敗",null);
        }
    }
    @GetMapping(value="/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id)
    {
        Payment payment = paymentService.getPatmentById(id);
        if(payment != null){
            return new CommonResult(200,"查詢成功",payment);
        }else{
            return new CommonResult(444,"沒有對應(yīng)記錄,查詢ID:"+id,null);
        }
    }
}

5.6 測試

啟動啟動類,在地址欄輸入localhost:8001/payment/get/31

瀏覽器不適合發(fā)post請求,因此去postman發(fā)請求,id是自增的,只需要發(fā)送serial即可:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

4.3 熱部署

Devtools熱部署,代碼改動,自動重啟。

第1步:在cloud-provider-payment8001添加依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

?第2步:父工程cloud2020添加插件

 <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.2.1.RELEASE</version>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

第3步:開啟自動編譯權(quán)限?

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

第4步:更新值,開啟熱注冊

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

輸入快捷鍵:ctrl + shift + alt + /

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)?SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

第5步:重啟

(失敗)測試:同樣是PaymentMain8001啟動,然后進(jìn)入到cloud-provider-payment8001下的controller,更改CommonResult為CommonResult<Payment>,會實時刷新(失敗)。

4.4 消費(fèi)者訂單模塊

1. 建module

名字:Cloud-consumer-order80,src下有main和test,main下有java和resource

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

2. 改pom

web、acuator是圖形化的監(jiān)控展現(xiàn),要先加mybatis的驅(qū)動運(yùn)行,之后可以刪除mybatis驅(qū)動,不然.yml會變不成綠葉:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </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>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
</dependencies>

3. 寫YML

在resources下建application.yml,設(shè)置端口80

server:
  port: 81

4. 主啟動

在java下創(chuàng)建com.atguigu.springcloud.OrderMain80類

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

5. 業(yè)務(wù)類

5.1 entities復(fù)制payment8001的

RestTemplate提供了多種便捷訪問遠(yuǎn)程Http服務(wù)的方法,是一種簡單便捷的訪問restful服務(wù)模板類,是Spring提供的用于訪問Rest服務(wù)的客戶端模板工具集。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.2 創(chuàng)建springcloud下的config包,在包下創(chuàng)建ApplicationContextConfig類:

@Configuration
public class ApplicationContextConfig{
    @Bean
    public RestTemplate getRestTemp(){
        return new RestTemplate();
    }
}

5.3 在controller包下創(chuàng)建OrderController類:

@RestController
@Slf4j
public class OrderController{
    public static final String PAYMENT_URL = "http://localhost:8081";
    @Resource
    private RestTemplate restTemplate;
    @PostMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}

?SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)?SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

但此時調(diào)用create方法會出現(xiàn)插入值為空的情況,記得要在cloud-provider-payment8001的controller下的PaymentController類中加入@RequestBody注解:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

@RequestBody注解用于將HTTP請求的請求體部分映射到方法的參數(shù)上。

新版的Dashboard叫作services

4.5 重構(gòu)

新建一個cloud-api-commons模塊,作為通用封裝類:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

在模塊下引入依賴:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.1.0</version>
    </dependency>
<dependencies>

在模塊的java目錄下建com.atguigu.springcloud.entities包,直接將entities粘貼在下面:

點擊右邊的Maven,選擇刷新出現(xiàn)cloud-api-commons,進(jìn)入Lifecycle點擊clean點擊install即可。

刪除掉原先的entities包

將下面依賴粘貼在刪掉entities包的模塊之下:


<dependency>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>${project.version}</version>
</dependency>

貳、初級

五、Eureka服務(wù)注冊與發(fā)現(xiàn)

7001、7002模擬的是Eureka服務(wù)器,81模擬消費(fèi)者,8081和8082模擬生產(chǎn)者。

5.1 基礎(chǔ)知識

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.2 服務(wù)端安裝

1. 建module

ArtifactId:cloud-eureka-server7001,com.atguigu.springcloud

2. 改POM

<dependencies>
<!--eureka-server-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</deoendency>
<dependency>
    <groupId>org.atguigu.springcloud</groupId>
    <artifactId>cloud-api-common</artifactId>
    <version>${project.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</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>

3. 寫YML

在resources下創(chuàng)建application.yml

server:
 port: 7001
eureka:
    instance:
      hostname: localhost #eureka服務(wù)端實例名稱
    client:
        #false表示不向注冊中心注冊自己
        register-with-eureka: false
        #false表示自己端就是注冊中心,我的職責(zé)就是維護(hù)服務(wù)實例,不需要檢索服務(wù)
        fetch-registry: false
        service-url:
        #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊服務(wù)都需要依賴這個地址
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
        

4. 主啟動

創(chuàng)建EurekaMain7001作為啟動類:

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001{
    public static void main(String[] args){
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

如果成功輸入localhost:7001后出現(xiàn)下面字樣:?

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.3 支付微服務(wù)8081入駐進(jìn)eurekaServer

在8081的Pom中引入:

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

在application.yml文件中增加如下代碼:

eureka:
  client:
    #表示是否將自己注冊進(jìn)Eurekaserver默認(rèn)為true
    register-with-eureka: true
    #是否從EurekaServer抓取已有的注冊信息默認(rèn)為true。單節(jié)點無所謂,集群必須設(shè)置為true才能配合ribbon使用負(fù)載均衡。
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

在啟動類添加注解:@EnableEurekaClient

如果成功,Instances區(qū)域會出現(xiàn)CLOUD-PAYMENT-SERVICE這一新的Instances:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.4 訂單微服務(wù)81入駐進(jìn)eurekaServer

在Pom中加入如下代碼:

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

在YML中加入如下代碼:

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

?在啟動類中加入如下代碼:@EnableEurekaClient

如果成功出現(xiàn)如下2個:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

register-with-eureka為false表示不讓入駐,為true表示讓入駐。

5.5 集群原理說明

Eureka Server:服務(wù)注冊,將服務(wù)信息注冊進(jìn)服務(wù)中心。服務(wù)發(fā)現(xiàn),從注冊中心上獲取服務(wù)信息。實質(zhì),存key服務(wù)名 取 value調(diào)用地址。

先啟動eureka注冊中心。啟動服務(wù)提供者payment支付服務(wù)。支付服務(wù)啟動后會把自身信息(如服務(wù)地址以別名方式注冊進(jìn)eureka)。消費(fèi)者order服務(wù)在需要調(diào)用接口時,使用服務(wù)別名去注冊中心獲取實際的RPC遠(yuǎn)程調(diào)用地址。消費(fèi)者或者調(diào)用地址后,底層實際是利用HttpClient技術(shù)實現(xiàn)遠(yuǎn)程調(diào)用。消費(fèi)者獲取服務(wù)地址后會緩存在本地jvm內(nèi)存中,默認(rèn)每隔30秒更新一次服務(wù)調(diào)用地址。

Eureka具有高可用特點,可搭建Eureka注冊中心集群,實現(xiàn)負(fù)載均衡+容錯故障。避免單點故障。互相注冊,相互守望。

5.6 集群環(huán)境構(gòu)建

建Module:cloud-eureka-server7002

Pom:直接復(fù)制7001

YML:建application.yml

主啟動類:EurekaMain7002,用7001的注解

修改配置:C:\Windows\System32\drivers\etc路徑下的hosts文件。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

修改7001的.yml,修改hostname和defaultZone:

server:
  port: 7001
eureka:
  instance:
    hostname: eureka7001.com #eureka服務(wù)端實例名稱
  client:
    #false表示不向注冊中心注冊自己
    register-with-eureka: false
    #false表示自己端就是注冊中心,我的職責(zé)就是維護(hù)服務(wù)實例,不需要檢索服務(wù)
    fetch-registry: false
    service-url:
      #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊服務(wù)都需要依賴這個地址
      defaultZone: http://eureka7002.com:7002/eureka/

修改7002的.yml文件:

server:
  port: 7002
eureka:
  instance:
    hostname: eureka7002.com #eureka服務(wù)端實例名稱
  client:
    #false表示不向注冊中心注冊自己
    register-with-eureka: false
    #false表示自己端就是注冊中心,我的職責(zé)就是維護(hù)服務(wù)實例,不需要檢索服務(wù)
    fetch-registry: false
    service-url:
      #設(shè)置與Eureka Server交互的地址查詢服務(wù)和注冊服務(wù)都需要依賴這個地址
      defaultZone: http://eureka7001.com:7001/eureka/

輸入eureka7002.com:7002==127.0.0.1:7002和eureka7001.com:7001==127.0.0.1能見到eureka首頁:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)輸入

locahost:7002能指向7001,locahost:7001能指向7002算是成功:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.7 訂單支付兩微服務(wù)注冊進(jìn)Eureka集群

YML:在8081的eureka下添加:

defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

?YML:在81的eureka下添加:

eureka:
    client:
        register-with-eureka: true
        fetchRegistry: true
        service-url:
                defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

啟動:

先要啟動EurekaServer,7001/7002服務(wù)?

再啟動服務(wù)提供者provider,8081

再啟動消費(fèi)者,81

輸入http://localhost:81/consumer/payment/get/31,如果查詢成功即可

測試:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

eureka7001.com:7001可以看到81和8081注冊進(jìn)入

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

eureka7002.com:7002同樣也可以看到

5.8 支付微服務(wù)集群配置

建Module:創(chuàng)建cloud-provider-payment8082

改Pom:把8081粘貼入8082

寫YML:把8081的粘貼入8082,改一個端口號

主啟動:建PaymentMain8082把8081的啟動類復(fù)制過來,把resource下的mapper包粘貼到8082,把com包直接粘貼到8082。

改controller:修改8001和8002加入如下內(nèi)容:

@Value("${server.port}")
private String serverPort;
return new CommonResult(200,"插入數(shù)據(jù)庫成功,serverPort"+serverPort,result);
return new CommonResult(200,"查詢成功,serverPort"+serverPort,payment);

啟動順序:7001、7002、8081、8082、81

測試:eureka7001.com:7001,eureka7002.com:7002,localhost:81/consumer/payment/get/31

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

發(fā)現(xiàn)bug:訂單服務(wù)訪問地址是寫死的。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

此時會報錯,因為IDEA不知道是這個地址下的哪臺服務(wù)器提供服務(wù)。

使用@LoadBalanced注解賦予RestTemplate負(fù)載均衡的能力,注解寫在config包的ApplicationContextConfig下:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

此時訪問localhost:81/consumer/payment/get/31,端口號會在8081和8082之間來回切換:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.9 actuator微服務(wù)信息完善

加上instance: instance-id和prefer-ip-address: true(訪問路徑顯示ip地址)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.10 服務(wù)發(fā)現(xiàn)Discovery

在8081的controller的PaymentController類上修改:

@Slf4j
@RestController
public class PaymentController {
    @Resource
    private DiscoveryClient discoveryClient;
    @GetMapping(value="/payment/discovery")
    public Object discovery()
    {
        List<String> services = discoveryClient.getServices();
        for(String element : services){
            log.info("*****element: "+element);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for(ServiceInstance instance : instances){
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return instances;
    }
}

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5. 11 Eureka自我保護(hù)理論知識

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

概括:某時刻某一個微服務(wù)不可用了,Eureka不會立刻清理,依舊會對該微服務(wù)的信息進(jìn)行保存。

為了防止EurekaClient可以正常運(yùn)行,但是與EurekaServer網(wǎng)絡(luò)不通情況下,EurekaServer不會立刻將EurekaClient服務(wù)器剔除。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

5.12 禁止自我保護(hù)

在7001的yml文件中的eureka下,加下列代碼:

  server:
    #關(guān)閉自我保護(hù)機(jī)制,保證不可用服務(wù)被及時踢除
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

測試:先看8081是否存在,關(guān)閉8081,看卡是否立刻刪除。

Eureka停更說明

六、Zookeeper服務(wù)注冊與發(fā)現(xiàn)

6.1 Zookeeper的安裝

下載zookeeper-3.4.9,下面是安裝地址

https://archive.apache.org/dist/zookeeper/

在Linux虛擬機(jī)中,創(chuàng)建/opt/module目錄,把zookeeper的gz壓縮包放到該目錄下。

然后解壓該壓縮包到該目錄:

tar -zxvf zookeeper-3.4.10.tar.gz -C /opt/module/

然后修改配置:

將/opt/module/zookeeper-3.4.10/conf這個路徑下的zoo.sample.cfg修改為zoo.cfg:

mv zoo_sample.cfg zoo.cfg

在/opt/module/zookeeper-3.4.10目錄創(chuàng)建zkData文件夾:

mkdir zkData

打開zoo.cfg文件,修改dataDir路徑:

vim zoo.cfg

修改如下內(nèi)容:

dataDir=/opt/module/zookeeper-3.4.10/zkData

啟動Zookeeper服務(wù)

輸入:sudo /opt/module/zookeeper-3.4.9/bin/zkServer.sh start

進(jìn)入Zookeeper

輸入:sudo /opt/module/zookeeper-3.4.9/bin/zkCli.sh

退出Zookeeper?

輸入:quit

關(guān)閉Zookeeper服務(wù):

sudo /opt/module/zookeeper-3.4.9/bin/zkServer.sh stop

6.2 項目環(huán)境配置

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

在/opt/module/zookeeper-3.4.9/bin目錄下輸入:

sudo systemctl stop firewalld 關(guān)閉防火墻。

sudo systemctl status firewalld檢查防火墻是否關(guān)閉。

確保虛擬機(jī)能和windows ping通

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

建module:cloud-provider-payment8004

pom:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>

YML:?

server:
  port: 8004
#服務(wù)別名 — — — — 注冊zookeeper到注冊中心名稱
spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: 192.168.182.157:2181

啟動類:

com.atguigu.springcloud.PaymentMain8004?

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8004.class,args);
    }
}

服務(wù)類:

springcloud下建controller包,PaymentController

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    @RequestMapping(value="/payment/zk")
    public String paymentzk(){
        return "springcloud with zookeeper: "+serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

然后啟動Zookeeper服務(wù),進(jìn)入zookeeper

輸入:sudo /opt/module/zookeeper-3.4.9/bin/zkServer.sh start

輸入:sudo /opt/module/zookeeper-3.4.9/bin/zkCli.sh

輸入:get /zookeeper

版本沖突問題:

<dependency>
<!--添加zookeeper3.4.9版本-->
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.9</version>
<!--先排除自帶的zookeeper3.5.3-->
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </exclusion>
        </exclusions>
</dependency>

然后點擊啟動。

測試localhost:8004/payment/zk,如果每次刷新后面一串?dāng)?shù)字會變則成功:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

把json串賦值到tool.lu/json看得到信息則成功:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

6.3 臨時還是持久節(jié)點

用的是臨時的,關(guān)閉8004一段時間,zookeeper會踢掉。重新啟動,流水號會改變。

6.4 訂單服務(wù)注冊進(jìn)zookeeper

module:cloud-consumerzk-order81。

POM:復(fù)制8004,見上節(jié)。

YML:

server:
  port: 81
#服務(wù)別名 — — — — 注冊zookeeper到注冊中心名稱
spring:
  application:
    name: cloud-consumer-order
  cloud:
  #注冊到zookeeper地址
    zookeeper:
      connect-string= 192.168.182.157:2181

啟動類:

@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain81 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZKMain81.class,args);
    }
}

config:

@Configuration
public class ApplicationContextConfig {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

controller:

@RestController
@Slf4j
public class OrderZKController {
    public static final String INVOKE_URL="http://cloud-provider-payment";
    @Resource
    private RestTemplate restTemplate;
    @GetMapping(value="/consumer/payment/zk")
    public String paymentInfo(){
        String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
        return result;
    }
}

測試:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

七、Consul服務(wù)注冊與發(fā)現(xiàn)

7.1 Consul簡介

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

7.2 安裝并運(yùn)行Consul

下載地址:

https://developer.hashicorp.com/consul/downloads

?SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

點擊地址欄,輸入cmd,輸入consul --version,再輸入consul agent -dev

輸入http://localhost:8500會進(jìn)入界面,看到勾勾:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

7.3 服務(wù)提供者注冊進(jìn)Consul

module:cloud-providerconsul-payment8006

pom:

<dependency>
? ? ?<groupId>org.springframework.cloud</groupId>
? ? ?<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

yml:

server:
  port: 8006
  
spring:
  application:
    name: consul-provider-payment

#consul注冊中心地址
cloud:
  consul:
    host: localhost
    port: 8500
    discovery:
      service-name: ${spring.application.name}

啟動類:

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8006.class,args);
    }
}

業(yè)務(wù)類:復(fù)制8004,稍加修改

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    @RequestMapping(value="/payment/consul")
    public String paymentzk(){
        return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

測試類:啟動

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

7.4 服務(wù)消費(fèi)者注冊進(jìn)Consul

module: cloud-consumerconsul-order81

pom:

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</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>

yml:

server:
  port: 81

spring:
  application:
    name: cloud-consumer-order

#consul注冊中心地址
cloud:
  consul:
    host: localhost
    port: 8500
    discovery:
      service-name: ${spring.application.name}

主啟動類:

@SpringBootApplication
@EnableDiscoveryClient //該注解用于向使用consul或者zookeeper作為注冊中心時注冊服務(wù)
public class OrderConsulMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderConsulMain80.class,args);
    }
}

配置Bean:

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

Controller:

@RestController
@Slf4j
public class OrderConsulController {
    public static final String INVOKE_URL = "http://consul-provider-payment";
    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/payment/consul")
    public String paymentInfo(){
        String result = restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
        return result;
    }
}

驗證測試:啟動

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

訪問測試地址:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

7.5 三個注冊中心異同點

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

CAP:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

AP是Eureka,是保證一致性。

?不同網(wǎng)絡(luò)分區(qū)數(shù)據(jù)同步失敗,為了保證可用性,允許可以返回舊值,所以是違背了一致性要求。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

CP是Zookeeper和Consul,是數(shù)據(jù)要一致,不一致就掛報錯。

八、Ribbon負(fù)載均衡服務(wù)調(diào)用

8.1 Ribbon入門介紹

先恢復(fù)環(huán)境EurekaMain7001,EurekaMain7002,PaymentMain8081,PaymentMain8083,OrderMain81。

輸入eureka7001.com:7001

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

基本介紹:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

能做什么:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

集中式:?

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

進(jìn)程內(nèi):?

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

8.2 Ribbon的負(fù)載均衡和Rest調(diào)用

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

此時刷新網(wǎng)頁發(fā)現(xiàn)已經(jīng)可以實現(xiàn)輪詢,卻并未引入Ribbon,實際的原因是Eureka中已經(jīng)自帶了Ribbon。

在cloud-consumer-order81的OrderController中加入如下代碼:

@GetMapping("/consumer/payment/getForEntity/{id}")
public CommonResult<Payment> getPayment2(@PathVariable("id") Long id)
{
    ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    if(entity.getStatusCode().is2xxSuccessful()){
        return entity.getBody();
    }else{
        return new CommonResult<>(444,"操作失敗");
    }
}

輸入:localhost:81/consumer/payment/getForEntity/31

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

8.3 Ribbon默認(rèn)自帶的負(fù)載規(guī)則

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

8.4 Ribbon負(fù)載規(guī)則替換

ribbon替換規(guī)則要求不能把配置放在Main包上級一層的包以下,就是要在包外面自己創(chuàng)建一個。

在atguigu下面創(chuàng)建一個包叫作myrule。

在myrule下面創(chuàng)建MySelfRule類。

@Configuration
public class MySelfRule {
    @Bean
    public IRule myRule(){
        return new RandomRule();
    }
}

?在OrderMain81上添加@RibbonClient注解:

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name="CLOUD-PAYMENT-SERVICE",configuration= MySelfRule.class)
public class OrderMain81 {
    public static void main(String[] args){
        SpringApplication.run(OrderMain81.class,args);
    }
}

?輸入:localhost:81/consumer/payment/getForEntity/31,此時的訪問是隨機(jī)的

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

8.5 Ribbon默認(rèn)負(fù)載輪詢算法原理

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

8.6 RoundRobinRule源碼分析

快捷鍵:alt+ctrl+b查看源碼

8.7 Ribbon之手寫輪詢算法

1.注釋掉cloud-consumer-order81配置類里的@LoadBalance

2.在springcloud下新建lb包,新建接口LoadBalancer

public interface LoadBalancer {
    ServiceInstance instances(List<ServiceInstance> serviceInstances);
}

3.在lb包下實現(xiàn)類:MyLB

@Component
public class MyLB implements  LoadBalancer{
    private AtomicInteger atomicInteger = new AtomicInteger(0);
    public final int getAndIncrement(){
        int current;
        int next;
        do{
            current = this.atomicInteger.get();
            next = current >= 2147483647 ? 0:current+1;
        }while(!this.atomicInteger.compareAndSet(current,next));
        System.out.println("*****第幾次訪問,次數(shù)next:"+next);
        return next;
    }
    @Override
    public ServiceInstance instances(List<ServiceInstance> serviceInstances){
        int index = getAndIncrement() % serviceInstances.size();
        return serviceInstances.get(index);
    }
}

在controlle包下的OrderControlle寫入如下代碼:

@GetMapping(value="/consumer/payment/lb")
public String getPaymentLB(){
    List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
    if(instances == null || instances.size() <= 0){
        return null;
    }
    ServiceInstance serviceInstance = loadBalancer.instances(instances);
    URI uri = serviceInstance.getUri();
    System.out.println(uri);
    System.out.println(uri+"/payment/lb");
    return restTemplate.getForObject(uri+"/payment/lb",String.class);
}
@Resource
private LoadBalancer loadBalancer;
@Resource
private DiscoveryClient discoveryClient;

在8081和8083的PaymentController中寫入如下代碼:

@GetMapping(value="/payment/lb")
    public String getPaymentLB(){
        return serverPort;
}

輸入地址:localhost:81/consumer/payment/lb,效果是8081和8083交替出現(xiàn)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

九、OpenFeign服務(wù)接口調(diào)用

9.1 OpenFeig是什么??

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

9.2 OpenFeign服務(wù)調(diào)用?

接口+注解:微服務(wù)調(diào)用接口(提供方和調(diào)用方相吻合的接口)+@FeignClient

module:cloud-consumer-feign-order81

pom:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.atguigu.springcloud</groupId>
        <artifactId>cloud-api-common</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>

yml:

server:
  port: 81
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

主啟動:

com.atguigu.springcloud.OrderFeignMain81添加@EnableFeignClients

@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain81 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain81.class,args);
    }
}

業(yè)務(wù)類:

在springcloud下添加service包,然后添加PaymentFeignService接口。

@Configuration
@FeignClient(value="CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping(value="/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}

controller下創(chuàng)建OrerFeignController

@RestController
@Slf4j
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;
    @GetMapping(value="/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        return paymentFeignService.getPaymentById(id);
    }
}

測試:localhost:81/consumer/payment/get/31,出現(xiàn)8081和8083交替結(jié)果。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

總結(jié):Feign是一個聲明式的Web服務(wù)客戶端,它使得編寫HTTP客戶端變得更簡單。我們只需要創(chuàng)建一個接口并在接口上添加注解,F(xiàn)eign就會自動提供實現(xiàn),無需手動創(chuàng)建URL、發(fā)送HTTP請求、處理HTTP響應(yīng)等。

該客戶端會將請求發(fā)送到名為CLOUD-PAYMENT-SERVICE的服務(wù)。

9.3 OpenFeign超時控制

在8081的controller下的PaymentController添加如下代碼:

@GetMapping(value="/payment/feign/timeout")
public String paymentFeignTimeout(){
    try{
        TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return serverPort;
}

在81的service包下的PaymentFeignService接口下添加如下代碼:

@GetMapping(value="/payment/feign/timeout")
public String paymentFeignTimeout();

在81的controller包下

@GetMapping(value="/consumer/payment/feign/timeout")
public String paymentFeignTimeout(){
//客戶端默認(rèn)等1秒
return paymentFeignService.paymentFeignTimeout();
}

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

測試:

啟動7001,8001

localhost:8081/payment/feign/timeout,正常訪問耗時秒

localhost:81/consumer/payment/feign/timeout時間超時

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

解決:

server:
  port: 81
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

#設(shè)置feign客戶端超時時間(OpenFeign默認(rèn)支持ribbon)
ribbon:
#指的是建立連接所用的時間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩段連接所用的時間
  ReadTimeout: 5000
#指的是建立連接后從服務(wù)器讀取到可用資源所用的時間
  ConnectTimeout: 5000

再測試成功。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

9.4 OpenFeign日志增強(qiáng)

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

在cloud-consumer-feign-order80下的springcloud下創(chuàng)建config,配置日志bean:

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLoggerlevel()
    {
        return Logger.Level.FULL;
    }
}

?在YML文件中添加如下代碼:

logging:
  level:
    com.atguigu.springcloud.service.PaymentFeignService: debug

測試:啟動Openfeign81。localhost/consumer/payment/get/31。

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)

在控制臺看調(diào)用結(jié)果:

SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon,筆記,eureka,zookeeper,consul,ribbon,微服務(wù),學(xué)習(xí),1024程序員節(jié)文章來源地址http://www.zghlxwxcb.cn/news/detail-718891.html

到了這里,關(guān)于SpringCloud學(xué)習(xí)筆記(上):服務(wù)注冊與發(fā)現(xiàn):Eureka、Zookeeper、Consul+負(fù)載均衡服務(wù)調(diào)用:Ribbon的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 什么是SpringCloud Eureka服務(wù)注冊與發(fā)現(xiàn)

    什么是SpringCloud Eureka服務(wù)注冊與發(fā)現(xiàn)

    ??前言 本篇博文是關(guān)于SpringCloud Eureka 介紹,希望你能夠喜歡 ??個人主頁:晨犀主頁 ??個人簡介:大家好,我是晨犀,希望我的文章可以幫助到大家,您的滿意是我的動力???? ??歡迎大家:這里是CSDN,我總結(jié)知識的地方,歡迎來到我的博客,感謝大家的觀看?? 如果文

    2024年02月09日
    瀏覽(34)
  • 【SpringCloud微服務(wù)】- Eureka服務(wù)注冊與服務(wù)發(fā)現(xiàn)Discovery

    【SpringCloud微服務(wù)】- Eureka服務(wù)注冊與服務(wù)發(fā)現(xiàn)Discovery

    Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個基于REST的服務(wù),主要用于定位運(yùn)行在AWS域中的中間層服務(wù),以達(dá)到負(fù)載均衡和中間層服務(wù)故障轉(zhuǎn)移的目的。 SpringCloud將它集成在其子項目spring-cloud-netflix中,以實現(xiàn)SpringCloud的服務(wù)發(fā)現(xiàn)功能。 Eureka包含兩個組件: Eureka Server 和 E

    2024年02月03日
    瀏覽(25)
  • SpringCloud_Eureka服務(wù)的注冊與發(fā)現(xiàn)

    SpringCloud_Eureka服務(wù)的注冊與發(fā)現(xiàn)

    分布式:分布式解決的是系統(tǒng)性能問題: 即 解決系統(tǒng)部署上單點的問題 ,盡量讓組成系統(tǒng)的子系統(tǒng)分散在不同的機(jī)器上進(jìn)而提高系統(tǒng)的吞吐能力。 即多個相同的子系統(tǒng)在不同的服務(wù)器上。 分布式系統(tǒng)架構(gòu)存在問題通過服務(wù)治理解決。 微服務(wù):將系統(tǒng)的業(yè)務(wù)功能劃分為極小

    2023年04月22日
    瀏覽(15)
  • SpringCloud 教程 | 第一篇: 服務(wù)的注冊與發(fā)現(xiàn)(Eureka)

    SpringCloud 教程 | 第一篇: 服務(wù)的注冊與發(fā)現(xiàn)(Eureka)

    一、spring cloud簡介 spring cloud 為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)的一些工具,包括配置管理、服務(wù)發(fā)現(xiàn)、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分布式會話等等。它運(yùn)行環(huán)境簡單,可以在開發(fā)人員的電腦上跑。另外說明spring cloud是基于springboot的,所以需

    2024年02月11日
    瀏覽(20)
  • 【SpringCloud】二、服務(wù)注冊發(fā)現(xiàn)Eureka與負(fù)載均衡Ribbon

    【SpringCloud】二、服務(wù)注冊發(fā)現(xiàn)Eureka與負(fù)載均衡Ribbon

    服務(wù)提供者:一次業(yè)務(wù)中,被其它微服務(wù)調(diào)用的服務(wù)。(提供接口給其它微服務(wù)) 服務(wù)消費(fèi)者:一次業(yè)務(wù)中,調(diào)用其它微服務(wù)的服務(wù)。(調(diào)用其它微服務(wù)提供的接口) 很明顯,這是一個相對的概念。 上一篇中,遠(yuǎn)程調(diào)用時,url參數(shù)是寫死在代碼中的,而不同的測試、生產(chǎn)、

    2024年02月06日
    瀏覽(23)
  • 微服務(wù)-服務(wù)的注冊與發(fā)現(xiàn)(Consul、zookeeper、etcd、eureka、Nacos)

    一. 對比常用的注冊中心 Consul、zookeeper、etcd、eureka、Nacos Feature Consul Zookeeper Etcd Eureka Nacos 服務(wù)健康檢查 ?服務(wù)狀態(tài),內(nèi)存,硬盤等 ?(弱)長連接,keepalive ?連接心跳 ?可配支持 傳輸層?(PING?或?TCP)和應(yīng)用層?(如?HTTP、MySQL、用戶自定義)的健康檢查 多數(shù)據(jù)中心 ?支持 ?— ?

    2023年04月19日
    瀏覽(28)
  • 服務(wù)注冊與發(fā)現(xiàn)Eureka、Zookeeper、Consul 三個注冊中心的異同點(CAP理論)

    服務(wù)注冊與發(fā)現(xiàn)Eureka、Zookeeper、Consul 三個注冊中心的異同點(CAP理論)

    Eureka Eureka是由Netflix開源的一個服務(wù)注冊和發(fā)現(xiàn)組件,它主要用于構(gòu)建高可用、分布式系統(tǒng)的基礎(chǔ)設(shè)施中。Eureka的服務(wù)器端被稱為Eureka Server,客戶端則是那些需要注冊的服務(wù)。Eureka具有以下特點: 高可用性 :Eureka支持多節(jié)點部署,能夠在節(jié)點故障時自動進(jìn)行故障轉(zhuǎn)移。 自我

    2024年04月22日
    瀏覽(31)
  • 基于SpringCloud的微服務(wù)架構(gòu)學(xué)習(xí)筆記(2)注冊中心Eureka和負(fù)載均衡Ribbon

    基于SpringCloud的微服務(wù)架構(gòu)學(xué)習(xí)筆記(2)注冊中心Eureka和負(fù)載均衡Ribbon

    1.7.1 遠(yuǎn)程調(diào)用的問題 地址信息獲取 : 服務(wù)消費(fèi)者 如何獲取 服務(wù)提供者 的 地址信息 (不能每次都寫死): URL:http://localhost:8081/user/\\\"+order.getUserId() 多選一 :如果有多個服務(wù)提供者,消費(fèi)者如何進(jìn)行選擇 監(jiān)測健康狀態(tài) :消費(fèi)者如何獲知提供者的健康狀態(tài) 1.7.2 eureka原理 地址

    2024年02月13日
    瀏覽(18)
  • Springcloud筆記(2)-Eureka服務(wù)注冊中心

    Springcloud筆記(2)-Eureka服務(wù)注冊中心

    Eureka作為一個微服務(wù)的治理中心,它是一個服務(wù)應(yīng)用,可以接收其他服務(wù)的注冊,也可以發(fā)現(xiàn)和治理服務(wù)實例。 服務(wù)治理中心是微服務(wù)(分布式)架構(gòu)中最基礎(chǔ)和最核心的功能組件,它主要對各個服務(wù)實例進(jìn)行管理,包括 服務(wù)注冊和服務(wù)發(fā)現(xiàn) 等 本文參考:springcloud教程 --

    2024年02月05日
    瀏覽(56)
  • SpringCloud學(xué)習(xí)筆記(二)_Eureka注冊中心

    SpringCloud學(xué)習(xí)筆記(二)_Eureka注冊中心

    一、Eureka簡介 Eureka是一項基于REST(代表性狀態(tài)轉(zhuǎn)移)的服務(wù),主要在AWS云中用于定位服務(wù),以實現(xiàn)負(fù)載均衡和中間層服務(wù)器的故障轉(zhuǎn)移。我們稱此服務(wù)為Eureka Server。Eureka還帶有一個基于Java的客戶端組件Eureka Client,它使與服務(wù)的交互更加容易??蛻舳诉€具有一個內(nèi)置的負(fù)載

    2024年02月11日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包