文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

怎么使用Resttemplate和Ribbon调用Eureka实现负载均衡

2023-06-29 16:37

关注

本篇内容介绍了“怎么使用Resttemplate和Ribbon调用Eureka实现负载均衡”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1.服务注册和发现Eureka

可以用作服务治理。

2.首先我们建立一个父子工程

最外层是forezp

其下面建立四个子工程

3.forezp工程相关

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的服务中心:eureka-server相关

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>

application.properties文件

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

需要在启动类添加 @EnableEurekaServer 注解来开启注册服务

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

5.eureka-client和eureka-client1用来提供服务

这两个子工程其实区别不大,只是为了验证实现负载均衡。

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>

新建一个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;  @RestControllerpublic class ApiController {      @Value("${server.port}")    String port;    @GetMapping("/hi")    public String home(@RequestParam String name1){        return "hi "+name1+"i am a port:"+port;    }}

eureka-client 的 apllication.properties配置如下

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

eureka-client2的 apllication.properties配置如下

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

需要在启动类添加注解 @EnableEurekaClient,开启服务提供

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

6.eureka-ribbon-client工程相关

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>

applicaiton.properties配置文件如下

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

新建RibbonConfig类,在这里注入RestTemplate类同时开启负载均衡

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;  @Configurationpublic class RibbonConfig {      @Bean    @LoadBalanced    RestTemplate restTemplate(){        return new RestTemplate();    }}

新建service层,调用eureka-client (eureka-client1)提供的restful接口,其中 eureka-client 为  eureka-client (eureka-client1)注册在 eureka-service上的serviceId信息,如果没有配置serviceId,默认使用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);    }}

新建controller层,调用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;  @RestControllerpublic class RibbonController {    @Autowired    RibbonService ribbonService;      @GetMapping("/hi")    public String hi(@RequestParam(required = false,defaultValue = "forezp")String name){        return ribbonService.hi(name);    }}

同时也需要在启动类添加@EnableEurekaClient注解

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

7.验证

按照顺序启动工程

浏览器访问  http://localhost:8761/

可以看到EUREKA-CLIENT 和 EUREKA-RIBBON-CLIENT已经注册进来了 

怎么使用Resttemplate和Ribbon调用Eureka实现负载均衡

我们访问 http://localhost:8764/hi?name=forezp 两次

可以看到  页面分别返回

hi forezpi am a port:8762

hi forezpi am a port:8763

这样验证了我们实现了负载均衡。

“怎么使用Resttemplate和Ribbon调用Eureka实现负载均衡”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯