亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何使用Resttemplate和Ribbon調(diào)用Eureka實(shí)現(xiàn)負(fù)載均衡

 更新時(shí)間:2022年03月26日 10:40:58   作者:追月亮的猴子  
這篇文章主要介紹了如何使用Resttemplate和Ribbon調(diào)用Eureka實(shí)現(xiàn)負(fù)載均衡,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

1.服務(wù)注冊(cè)和發(fā)現(xiàn)Eureka

可以用作服務(wù)治理。

2.首先我們建立一個(gè)父子工程

最外層是forezp

其下面建立四個(gè)子工程

  • eureka-server
  • eureka-client
  • eureka-client1
  • eureka-ribbon-client

3.forezp工程相關(guān)

1.forezp pom文件如下

<?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.5.RELEASE</version>
? ? ? <relativePath/> <!-- lookup parent from repository -->
? ?</parent>
? ?<groupId>com.example</groupId>
? ?<artifactId>forezp</artifactId>
? ?<version>0.0.1-SNAPSHOT</version>
? ?<name>forezp</name>
? ?<description>Demo project for Spring Boot</description>?
?
? ?<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>Dalston.SR1</spring-cloud.version>
? ?</properties>?
?
? ?<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>?
?
? ?<modules>
? ? ? <module>euraka-client</module>
? ? ? <module>euraka-server</module>
? ? ? <module>eureka-ribbon-client</module>
? ? ? <module>euraka-client2</module>
? ?</modules>?
?
? ?<!--<build>-->
? ? ? <!--<plugins>-->
? ? ? ? ?<!--<plugin>-->
? ? ? ? ? ? <!--<groupId>org.springframework.boot</groupId>-->
? ? ? ? ? ? <!--<artifactId>spring-boot-maven-plugin</artifactId>-->
? ? ? ? ?<!--</plugin>-->
? ? ? <!--</plugins>-->
? ?<!--</build>-->?
?
</project>

4.Eureka的服務(wù)中心:eureka-server相關(guān)

1.pom文件

<?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>com.example</groupId>
? ? ? <artifactId>forezp</artifactId>
? ? ? <version>0.0.1-SNAPSHOT</version>
? ? ? <relativePath/> <!-- lookup parent from repository -->
? ?</parent>
? ?<groupId>com.example</groupId>
? ?<artifactId>euraka-server</artifactId>
? ?<version>0.0.1-SNAPSHOT</version>
? ?<name>euraka-server</name>
? ?<description>Demo project for Spring Boot</description>?
?
? ?<properties>
? ? ? <java.version>1.8</java.version>
? ? ? <spring-cloud.version>Greenwich.SR1</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>

2.application.properties文件

server.port=8761?
eureka.instance.hostname=localhost
?
#防止自己注冊(cè)自己
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#eureka的注冊(cè)地址
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka/}

3.需要在啟動(dòng)類添加 @EnableEurekaServer 注解來開啟注冊(cè)服務(wù)

package com.example.eurakaserver;??
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;?
?
@SpringBootApplication
@EnableEurekaServer
public class EurakaServerApplication {??
? ?public static void main(String[] args) {
? ? ? SpringApplication.run(EurakaServerApplication.class, args);
? ?}??
}

5.eureka-client和eureka-client1用來提供服務(wù)

這兩個(gè)子工程其實(shí)區(qū)別不大,只是為了驗(yàn)證實(shí)現(xiàn)負(fù)載均衡。

1.pom文件

<?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>com.example</groupId>
?? ??? ?<artifactId>forezp</artifactId>
?? ??? ?<version>0.0.1-SNAPSHOT</version>
?? ??? ?<relativePath/> <!-- lookup parent from repository -->
?? ?</parent>
?? ?<groupId>com.example</groupId>
?? ?<artifactId>euraka-client2</artifactId>
?? ?<version>0.0.1-SNAPSHOT</version>
?? ?<name>euraka-client2</name>
?? ?<description>Demo project for Spring Boot</description>
?
?? ?<properties>
?? ??? ?<java.version>1.8</java.version>
?? ??? ?<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
?? ?</properties>
?
?? ?<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>
?
?? ??? ?<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>

2.新建一個(gè)ApiController類

package com.example.eurakaclient2;??
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;?
?
@RestController
public class ApiController {?
?
? ? @Value("${server.port}")
? ? String port;
? ? @GetMapping("/hi")
? ? public String home(@RequestParam String name1){
? ? ? ? return "hi "+name1+"i am a port:"+port;
? ? }
}

3.eureka-client 的 apllication.properties配置如下

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8762
spring.application.name=eureka-client

4.eureka-client2的 apllication.properties配置如下

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=8763
spring.application.name=eureka-client

5.需要在啟動(dòng)類添加注解 @EnableEurekaClient,開啟服務(wù)提供

package com.example.eurakaclient2;??
import org.springframework.boot.SpringApplication;
? ? ? import org.springframework.boot.autoconfigure.SpringBootApplication;
? ? ? import org.springframework.cloud.netflix.eureka.EnableEurekaClient;?
?
@SpringBootApplication
@EnableEurekaClient
public class EurakaClient2Application {??
? ?public static void main(String[] args) {
? ? ? SpringApplication.run(EurakaClient2Application.class, args);
? ?}?
}

6.eureka-ribbon-client工程相關(guān)

1.pom文件

<?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>com.example</groupId>
? ? ? <artifactId>forezp</artifactId>
? ? ? <version>0.0.1-SNAPSHOT</version>
? ? ? <relativePath/> <!-- lookup parent from repository -->
? ?</parent>
? ?<groupId>com.example</groupId>
? ?<artifactId>eureka-ribbon-client</artifactId>
? ?<version>0.0.1-SNAPSHOT</version>
? ?<name>eureka-ribbon-client</name>
? ?<description>Demo project for Spring Boot</description>?
?
? ?<properties>
? ? ? <java.version>1.8</java.version>
? ? ? <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
? ?</properties>?
?
? ?<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-ribbon</artifactId>
? ? ? </dependency>?
?
? ? ? <dependency>
? ? ? ? ?<groupId>org.springframework.cloud</groupId>
? ? ? ? ?<artifactId>spring-cloud-starter-netflix-eureka-client</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>?
?
</project>

2.applicaiton.properties配置文件如下

spring.application.name=eureka-ribbon-client
server.port=8764
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

3.新建RibbonConfig類,在這里注入RestTemplate類同時(shí)開啟負(fù)載均衡

package com.example.eurekaribbonclient;??
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 RibbonConfig {?
?
? ? @Bean
? ? @LoadBalanced
? ? RestTemplate restTemplate(){
? ? ? ? return new RestTemplate();
? ? }
}

4.新建service層,調(diào)用eureka-client (eureka-client1)提供的restful接口,其中 eureka-client 為  eureka-client (eureka-client1)注冊(cè)在 eureka-service上的serviceId信息,如果沒有配置serviceId,默認(rèn)使用spring.application.name

package com.example.eurekaribbonclient;??
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;??
?
? ? @Service
? ? public class RibbonService {
? ? ? ? @Autowired
? ? RestTemplate restTemplate;
? ? public String hi(String name){??
? ? ? ? return restTemplate.getForObject("http://eureka-client/hi?name1="+name,String.class);
? ? }
}

5.新建controller層,調(diào)用RibbonService為我們提供的方法

package com.example.eurekaribbonclient;??
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;?
?
@RestController
public class RibbonController {
? ? @Autowired
? ? RibbonService ribbonService;?
?
? ? @GetMapping("/hi")
? ? public String hi(@RequestParam(required = false,defaultValue = "forezp")String name){
? ? ? ? return ribbonService.hi(name);
? ? }
}

6.同時(shí)也需要在啟動(dòng)類添加@EnableEurekaClient注解

package com.example.eurekaribbonclient;??
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;?
?
@SpringBootApplication
@EnableEurekaClient
public class EurekaRibbonClientApplication {?
?
? ?public static void main(String[] args) {
? ? ? SpringApplication.run(EurekaRibbonClientApplication.class, args);
? ?}??
}

7.驗(yàn)證

1.按照順序啟動(dòng)工程

2.瀏覽器訪問  http://localhost:8761/

可以看到EUREKA-CLIENT 和 EUREKA-RIBBON-CLIENT已經(jīng)注冊(cè)進(jìn)來了 

3.我們?cè)L問 http://localhost:8764/hi?name=forezp 兩次

可以看到  頁(yè)面分別返回

hi forezpi am a port:8762

hi forezpi am a port:8763

這樣驗(yàn)證了我們實(shí)現(xiàn)了負(fù)載均衡。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java泛型枚舉Annotation接口詳細(xì)解讀與Eclipse發(fā)展

    Java泛型枚舉Annotation接口詳細(xì)解讀與Eclipse發(fā)展

    這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • mybatis查詢實(shí)現(xiàn)返回List<Map>類型數(shù)據(jù)操作

    mybatis查詢實(shí)現(xiàn)返回List<Map>類型數(shù)據(jù)操作

    這篇文章主要介紹了mybatis查詢實(shí)現(xiàn)返回List<Map>類型數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • javaweb用戶注銷后點(diǎn)擊瀏覽器返回刷新頁(yè)面重復(fù)登錄問題的解決方法

    javaweb用戶注銷后點(diǎn)擊瀏覽器返回刷新頁(yè)面重復(fù)登錄問題的解決方法

    這篇文章主要為大家詳細(xì)介紹了javaweb用戶注銷后點(diǎn)擊瀏覽器返回刷新頁(yè)面重復(fù)登錄問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • java二路歸并排序示例分享

    java二路歸并排序示例分享

    這篇文章主要介紹了java二路歸并排序示例,需要的朋友可以參考下
    2014-02-02
  • Java檢測(cè)死鎖案例

    Java檢測(cè)死鎖案例

    這篇文章主要介紹了Java檢測(cè)死鎖案例,本文列舉了導(dǎo)致死鎖的程序,通過使用jconsole工具進(jìn)行檢測(cè)等,講述了避免死鎖的方法,需要的朋友可以參考下
    2021-07-07
  • SpringBoot實(shí)現(xiàn)接口等冪次校驗(yàn)的示例代碼

    SpringBoot實(shí)現(xiàn)接口等冪次校驗(yàn)的示例代碼

    本文主要介紹了SpringBoot實(shí)現(xiàn)接口等冪次校驗(yàn)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java使用Optional實(shí)現(xiàn)優(yōu)雅避免空指針異常

    Java使用Optional實(shí)現(xiàn)優(yōu)雅避免空指針異常

    空指針異常(NullPointerException)可以說是Java程序員最容易遇到的問題了。為了解決這個(gè)問題,Java?8?版本中推出了?Optional?類,本文就來講講如何使用Optional實(shí)現(xiàn)優(yōu)雅避免空指針異常吧
    2023-03-03
  • tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例

    tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例

    這篇文章主要為大家介紹了tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • java后端如何獲取完整url的代碼

    java后端如何獲取完整url的代碼

    這篇文章主要介紹了java后端如何獲取完整url的代碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java?WebSocket?服務(wù)端實(shí)現(xiàn)代碼

    java?WebSocket?服務(wù)端實(shí)現(xiàn)代碼

    WebSocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議。它實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工(full-duplex)通信——允許服務(wù)器主動(dòng)發(fā)送信息給客戶端,這篇文章主要介紹了java?WebSocket?服務(wù)端代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02

最新評(píng)論