spring cloud ribbon 服务发现(三)

  ribbon 是一个基于 http 和 tcp 的客户端负载均衡器

1.准备三个项目 a.之前的注册中心 b。注册中心测试的demo项目  3.新建一个ribbon项目

2.新建hmq-ribbon-server 复制 demo项目即可

3.新增ribbon的依赖

 

<?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>

   <groupId>com.heimeiqiu</groupId>
   <artifactId>hmq-ribbon-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>

   <name>hmq-ribbon-server</name>
   <description>hmq spring boot test demo</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <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>Edgware.RELEASE</spring-cloud.version>
   </properties>

   <dependencies>

      <!--服务配置-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <!--<scope>provided</scope>-->
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <!--spring mvc 配置-->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>4.3.7.RELEASE</version>
      </dependency>
      <!--数据连接配置-->
      <!-- 连接池 -->
   <!--   <dependency>
         <groupId>com.jolbox</groupId>
         <artifactId>bonecp-spring</artifactId>
         <version>0.8.0.RELEASE</version>
      </dependency>-->
      <!--增加eureka-server的依赖-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <!--服务端<dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
      </dependency>-->

      <!--增加 ribbon 依赖 start-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-ribbon</artifactId>
      </dependency>
      <!--增加 ribbon 依赖 end-->


      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>1.3.0</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.35</version>
   </dependency>
   <!-- alibaba的druid数据库连接池 -->
   <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.11</version>
   </dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
   </dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
   </dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-joda</artifactId>
   </dependency>
   <dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-parameter-names</artifactId>
   </dependency>
   <!-- 分页插件 -->
   <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.1.2</version>
   </dependency>
   <!-- alibaba的druid数据库连接池 -->
   <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.0</version>
   </dependency>
      <!--整合freemark-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>
 

      <!-- 单元测试 -->
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <scope>test</scope>
      </dependency>
     
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aspects</artifactId>
      </dependency>
     


      <!-- httpclient -->
      <dependency>
         <groupId>org.apache.httpcomponents</groupId>
         <artifactId>httpclient</artifactId>
      </dependency>



      <dependency>
         <groupId>com.github.sgroschupf</groupId>
         <artifactId>zkclient</artifactId>
         <version>0.1</version>
      </dependency>
      <dependency>
         <groupId>org.apache.tomcat.embed</groupId>
         <artifactId>tomcat-embed-jasper</artifactId>
         <scope>provided</scope>
      </dependency>

      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.1.43</version>
      </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.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
               <encoding>UTF-8</encoding>
            </configuration>
         </plugin>
         <!-- java编译插件 -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
               <encoding>UTF-8</encoding>
            </configuration>
         </plugin>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
      <pluginManagement>
         <plugins>
            <!-- 配置Tomcat插件 -->
            <plugin>
               <groupId>org.apache.tomcat.maven</groupId>
               <artifactId>tomcat8-maven-plugin</artifactId>
               <version>2.2</version>
            </plugin>
         </plugins>
      </pluginManagement>
   </build>


</project>
4.创建主类

    在主类 HmqDemoServerApplication 添加注解

@EnableDiscoveryClient 将应用注册为Eureka服务
并在该类中创建RestTemplate 的spring Bean实例 并添加 @LoadBalanced 注解开启客户端的负载均衡
package com.heimeiqiu;

import com.heimeiqiu.config.DataSourceConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.client.RestTemplate;

import javax.sql.DataSource;

@Configuration
@SpringBootApplication  //开启自动化注解
@ComponentScan(basePackages = "com.heimeiqiu")
@MapperScan("com.heimeiqiu.dao.mapper")
@EnableDiscoveryClient//激活Enureka中的DisconveryClient实现(自动话配置,创建DisconveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例才能实现上述Controller中对服务的输出 )
//@PropertySource(value = { "classpath:jdbc.properties" }, ignoreResourceNotFound = true) //引入文件 读取配置
public class HmqDemoServerApplication   {

   /**
    * 在该主类中创建爱你RestTemplate的Spring Bean 实例,并通过 @LoadBalanced 注解开启客户端负载均衡
    * @return
     */
   @Bean
   @LoadBalanced
   RestTemplate restTemplate(){
      return new RestTemplate();
   }

   //启动应用入口
   public static void main(String[] args) {
      SpringApplication.run(HmqDemoServerApplication.class, args);
   }
}
5.创建一个ConbsumerController 类 调用 客户接口 com.heimeiqiu.controller.ConsumerController

package com.heimeiqiu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by wxl on 2017/12/18.
 */
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    
    @RequestMapping(value = "/ribbon-server",method= RequestMethod.GET)
public String helloConsumer(){ return restTemplate.getForEntity( "http://hmq-demo-server",String. class).getBody(); }}6.修改application.properties 配置

##selevt 配置
#端口
server.port=9001
##Eureka配置
#配置服务名称
spring.application.name=hmq-ribbon-server
eureka.instance.hostname=127.0.0.1
#配置访问路径
#注册中心默认端口就是8761,也可通过下面的方式定义其他端口
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8761/eureka/

7.访问接口查看,断点调试会更清楚其调用过程

http://localhost:9001/ribbon-server

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RibbonSpring Cloud中基于Netflix Ribbon实现的客户端负载均衡和服务调用工具。它是Spring Cloud对Netflix Ribbon的二次封装,通过它可以将面向服务的REST模板(RestTemplate)请求转换为客户端负载均衡的服务调用。RibbonSpring Cloud体系中最核心、最重要的组件之一,几乎存在于每一个使用Spring Cloud构建的微服务中。[3] 在Spring Cloud中使用Ribbon进行负载均衡的配置有多种方式。一种方式是通过在RestTemplate上添加@LoadBalanced注解来实现消费方的负载均衡。另一种方式是在启动类上使用@RibbonClient注解,并指定要调用的服务和自定义的负载均衡规则类。例如,在Spring Boot启动类上加上@RibbonClient注解,指定要调用的服务为"SPRINGCLOUDDEPT",并使用自定义的负载均衡规则类MyRibbonRule。[1][2] 负载均衡在系统中起到了重要的作用,它可以将用户的请求平摊分配到多个服务器上运行,以达到扩展服务器带宽、增强数据处理能力、增加吞吐量、提高网络的可用性和灵活性的目的。在Spring Cloud中,Ribbon实现了负载均衡的功能,可以帮助开发者实现高并发和扩展性要求较高的系统。[3] 总结来说,Spring Cloud Ribbon是一套基于Netflix Ribbon实现的客户端负载均衡和服务调用工具,它是Spring Cloud体系中最核心、最重要的组件之一。通过在RestTemplate上添加@LoadBalanced注解或使用@RibbonClient注解并指定负载均衡规则类,可以实现消费方的负载均衡。负载均衡在系统中起到了重要的作用,可以提高系统的性能和可用性。[1][2][3]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值