最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解

 更新時間:2021年04月08日 09:16:16   作者:追極  
這篇文章主要介紹了SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.打開官網(wǎng)稍微學(xué)習(xí)一下,了解一下spring cloud是個什么東西,大概有哪些組件等

https://spring.io/projects/spring-cloud

https://docs.spring.io/spring-cloud-netflix/docs/current/reference/html/

2.新建項目

打開網(wǎng)址:https://start.spring.io/

選擇需要引入的組件,然后下載下來即可

3.更改項目結(jié)構(gòu)

為了測試的方便,需將項目結(jié)構(gòu)更改為多模塊的項目。

步驟如下:

(1) 依次新建子模塊register-center/provider/consumer,刪除父模塊中多余的src、target等文件夾

(2) 修改父模塊的pom文件:僅保留<dependencyManagement>配置節(jié),<dependencies>配置節(jié)全部注釋掉,因為可在子模塊按需添加依賴。

(3) 修改register-center的pom中的依賴配置

<dependencies>
        <!-- Eureka注冊中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

(4) 修改provider和consumer的pom中依賴配置

<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>
    </dependencies>

4.新建相應(yīng)的測試類和配置文件

4.1 register-center模塊

啟動類

package com.hdwang.springcloudtest.registercenter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Application {

    /**
     * 運行點對點模式(集群模式)時,通過添加VM參數(shù)啟動不同的注冊中心節(jié)點實例
     * 例如:-Dspring.profiles.active=peer2
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

yml配置

spring:
  application:
    #應(yīng)用名
    name: register-center
  freemarker:
    template-loader-path: classpath:/templates/
    prefer-file-system-access: false
  #激活的配置,可運行時添加參數(shù)進行修改 -Dspring.profiles.active=peer2
  profiles:
    active: peer1

# #Eureka獨立模式配置,僅有一個注冊中心節(jié)點
#server:
#  port: 8090
#eureka:
#  instance:
#    hostname: localhost
#  client:
#    #僅僅作為注冊中心,既不提供服務(wù)也不訂閱服務(wù)
#    registerWithEureka: false
#    fetchRegistry: false
#    #注冊中心地址
#    serviceUrl:
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


# Eureka點對點模式,保證注冊中心高可用,注冊的實例信息會在點與點之間相互同步
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/

---
#每個注冊中心節(jié)點不同的配置
spring:
  profiles: peer1
server:
  port: 8091
eureka:
  instance:
    #在本機hosts中配置即可
    hostname: peer1.com

---
spring:
  profiles: peer2
server:
  port: 8092
eureka:
  instance:
    hostname: peer2.com

---
spring:
  profiles: peer3
server:
  port: 8093
eureka:
  instance:
    hostname: peer3.com

4.2 provider模塊

啟動類

package com.hdwang.springcloudtest.provider;

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

@SpringBootApplication
public class Application {

    /**
     * 運行時添加VM參數(shù): -Dserver.port=8082可以啟動多個provider實例
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

服務(wù)注冊類

package com.hdwang.springcloudtest.provider.restservice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 注冊的服務(wù)
 */
@RestController
public class RestService {
    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(RestService.class);

    @RequestMapping("/sayHello")
    public String sayHello(String name) {
        LOG.info("sayHello was called");
        return "hello, " + name;
    }
}

yml配置

spring:
  application:
    #應(yīng)用名,也是eureka的服務(wù)名
    name: provider
  freemarker:
    template-loader-path: classpath:/templates/
    prefer-file-system-access: false

server:
  #運行時,添加參數(shù)-Dserver.port=8082運行新的provider實例
  port: 8081

eureka:
  client:
    #注冊中心地址
    serviceUrl:
      #注冊中心獨立模式
      #defaultZone: http://localhost:8090/eureka/
      #注冊中心點對點模式
      defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/

4.3 consumer配置

啟動類

package com.hdwang.springcloudtest.consumer;

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

@SpringBootApplication
public class Application {

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

}

服務(wù)調(diào)用測試類

package com.hdwang.springcloudtest.consumer.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * 測試
 */
@RestController
public class TestController {

    /**
     * 使用服務(wù)名才能負(fù)載均衡,不能使用直接使用地址
     */
    private static final String REST_URL_PREFIX = "http://provider";

    @Autowired
    private EurekaClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/testGet")
    public String testGet() {
        ResponseEntity<String> res = restTemplate.getForEntity(REST_URL_PREFIX + "/sayHello?name={1}", String.class, getName());
        return res.getBody();
    }

    @GetMapping("/testPost")
    public String testPost() {
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
        params.add("name", getName());

//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity< MultiValueMap<String, Object>> request = new HttpEntity<>(params, null);
        ResponseEntity<String> res = restTemplate.postForEntity(REST_URL_PREFIX + "/sayHello", request, String.class);
        return res.getBody();
    }

    private String getName() {
        List<String> greetings = Arrays.asList("Bob", "Alice", "Jack");
        Random rand = new Random();
        int randomNum = rand.nextInt(greetings.size());
        return greetings.get(randomNum);
    }
}

RestTemplate負(fù)責(zé)均衡配置類

package com.hdwang.springcloudtest.consumer.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    /**
     * 構(gòu)建有負(fù)載均衡功能的RestTemplate實例對象
     *
     * @return RestTemplate實例對象
     */
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

yml配置

spring:
  application:
    #應(yīng)用名,也是Eureka的服務(wù)名
    name: cosumer

server:
  port: 8088

eureka:
  client:
    #注冊中心地址
    serviceUrl:
      #注冊中心獨立模式
      #defaultZone: http://localhost:8090/eureka/
      #注冊中心點對點模式
      defaultZone: http://peer1.com:8091/eureka/,http://peer2.com:8092/eureka/,http://peer3.com:8093/eureka/

5.運行測試

5.1本機hosts配置

127.0.0.1 peer1.com
127.0.0.1 peer2.com
127.0.0.1 peer3.com

5.2 編輯運行配置

三個注冊中心節(jié)點運行配置

兩個服務(wù)提供者的運行配置

5.3 運行程序

(1) 啟動注冊中心

依次啟動RegisterCenter1->RegisterCenter2->RegisterCenter3,啟動成功后,可訪問http://localhost:8091/ 或http://localhost:8092/ 或http://localhost:8093/ 查看是否啟動成功

(2)啟動服務(wù)提供者provider

依次啟動Provider1->Provider2, 隨便訪問一個注冊中心地址首頁即可查看狀態(tài),如下圖

(3) 啟動消費者cosumer

(4) 在瀏覽器中進行測試

測試地址:http://localhost:8088/testPost / http://localhost:8088/testGet

(5) 在Provider1/Provider2的控制臺中可以看到輸出結(jié)果

2021-04-07 15:26:56.043  INFO 8796 --- [nio-8081-exec-1] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:58.860  INFO 8796 --- [nio-8081-exec-2] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.535  INFO 8796 --- [nio-8081-exec-3] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.925  INFO 8796 --- [nio-8081-exec-4] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.266  INFO 8796 --- [nio-8081-exec-5] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.663  INFO 8796 --- [nio-8081-exec-6] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.938  INFO 8796 --- [nio-8081-exec-7] c.h.s.provider.restservice.RestService   : sayHello was called

2021-04-07 15:26:58.602  INFO 17828 --- [nio-8082-exec-1] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.194  INFO 17828 --- [nio-8082-exec-2] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:26:59.737  INFO 17828 --- [nio-8082-exec-3] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.109  INFO 17828 --- [nio-8082-exec-4] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.414  INFO 17828 --- [nio-8082-exec-5] c.h.s.provider.restservice.RestService   : sayHello was called
2021-04-07 15:27:00.815  INFO 17828 --- [nio-8082-exec-6] c.h.s.provider.restservice.RestService   : sayHello was called

恭喜!至此,Spring Clound 微服務(wù)集群框架您已經(jīng)搭建成功!

附錄

github地址:https://github.com/hdwang123/springcloudtest

參考文章:

https://www.zhihu.com/question/283286745/answer/763040709

https://www.cnblogs.com/qdhxhz/p/9357502.html

https://www.cnblogs.com/cjsblog/p/8005766.html

https://blog.csdn.net/weixin_44448094/article/details/88535475

到此這篇關(guān)于SpringCloud搭建netflix-eureka微服務(wù)集群的過程詳解的文章就介紹到這了,更多相關(guān)SpringCloud搭建netflix-eureka集群內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java泛型繼承原理與用法詳解

    Java泛型繼承原理與用法詳解

    這篇文章主要介紹了Java泛型繼承原理與用法,結(jié)合實例形式分析了java泛型繼承的相關(guān)原理與實現(xiàn)技巧,需要的朋友可以參考下
    2019-07-07
  • Java的關(guān)鍵字之transient詳解

    Java的關(guān)鍵字之transient詳解

    這篇文章主要介紹了Java的關(guān)鍵字之transient詳解,在Java編程中,transient是一個關(guān)鍵字,通常用于修飾變量,它的主要作用是用于指示JVM在對象序列化時忽略指定變量,從而避免數(shù)據(jù)泄露的安全問題,需要的朋友可以參考下
    2023-09-09
  • 如何在Spring中自定義scope的方法示例

    如何在Spring中自定義scope的方法示例

    這篇文章主要介紹了如何在Spring中自定義scope的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • Java 添加、讀取和刪除 Excel 批注的操作代碼

    Java 添加、讀取和刪除 Excel 批注的操作代碼

    這篇文章主要介紹了Java 添加、讀取和刪除 Excel 批注的操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot實現(xiàn)國密通信的流程步驟

    SpringBoot實現(xiàn)國密通信的流程步驟

    這篇文章主要介紹了SpringBoot實現(xiàn)國密通信的流程步驟,我們需要完成以下步驟:生成支持國密的證書,配置兩個 Spring Boot 項目,使用國密證書實現(xiàn) HTTPS和驗證通信是否成功,通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-11-11
  • SpringBoot實現(xiàn)抽獎算法的示例代碼

    SpringBoot實現(xiàn)抽獎算法的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何通過SpringBoot實現(xiàn)抽獎算法,文中的示例代碼簡潔易懂,具有一定的參考價值,感興趣的小伙伴可以了解一下
    2023-06-06
  • gradle和maven打包時排除application.properties問題

    gradle和maven打包時排除application.properties問題

    文章主要介紹了Gradle、Maven(用于構(gòu)建JAR包)和Maven(用于構(gòu)建WAR包),文章基于個人經(jīng)驗,為讀者提供了參考,并鼓勵大家支持腳本之家
    2024-12-12
  • java:?無效的目標(biāo)發(fā)行版:?20問題解決辦法

    java:?無效的目標(biāo)發(fā)行版:?20問題解決辦法

    這篇文章主要給大家介紹了關(guān)于java:?無效的目標(biāo)發(fā)行版:?20問題的解決辦法,出現(xiàn)這個問題大多數(shù)是因為自己安裝的jdk版本和你在運行項目時的所選的jdk版本不一致所導(dǎo)致的,需要的朋友可以參考下
    2024-03-03
  • SpringBoot2零基礎(chǔ)到精通之自動配置底層分析及小技巧

    SpringBoot2零基礎(chǔ)到精通之自動配置底層分析及小技巧

    SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時也是簡化Spring的一種快速開發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)自動配置的底層分析與一些開發(fā)中的小技巧
    2022-03-03
  • eclipse導(dǎo)入工程報錯問題項目或者文件有紅叉的解決方案

    eclipse導(dǎo)入工程報錯問題項目或者文件有紅叉的解決方案

    這篇文章主要介紹了eclipse導(dǎo)入工程報錯問題項目或者文件有紅叉的解決方案,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05

最新評論

浦东新区| 威海市| 崇阳县| 府谷县| 屯门区| 武冈市| 孙吴县| 宜章县| 克山县| 察隅县| 仲巴县| 长垣县| 田林县| 玉门市| 宣城市| 富民县| 辽阳市| 嘉鱼县| 汉中市| 根河市| 涟水县| 纳雍县| 锦州市| 赤水市| 丹江口市| 白水县| 逊克县| 朝阳县| 石楼县| 迁西县| 喜德县| 天柱县| 江口县| 霞浦县| 隆子县| 东安县| 太仆寺旗| 留坝县| 西盟| 应城市| 金阳县|