高可用ribbon负载均衡业务整合

 

 

高可用ribbon负载均衡业务整合

 

创建2个工程

 

工程1

 

application.properties

server.port=8081
spring.application.name=youfanproducttype
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

 

 

 

工程2

 

application.properties

server.port=8011
spring.application.name=youfanproducttype
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

 

产品类型控制器

package com.youfan.control;


import com.youfan.model.ProductType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by youfan on 2018/6/6 0006.
 */
@RestController
public class ProducttypoutControl {
    /**
     * 注入RestTemplate
     */
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/listproducttype",method = RequestMethod.GET)
    public List<ProductType> listproducttype(){
        String url="http://YOUFANPRODUCTTYPE/listproducttype";
        List<ProductType> list = restTemplate.getForObject(url,List.class);
        return list;
    }

}

说明:

1)通过YOUFANPRODUCTTYPE可以访问对应的实例服务;

 

 

 

 


==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合Ribbon可以实现客户端的负载均衡。下面是整合步骤: 1. 添加依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> ``` 2. 创建Ribbon配置类:创建一个配置类,用于配置Ribbon的相关属性。可以使用`@Configuration`注解进行标注,示例代码如下: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.ribbon.RibbonClient; @Configuration @RibbonClient(name = "serviceName") // 服务名 public class RibbonConfig { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } } ``` 3. 创建服务调用类:创建一个用于调用服务的类,并注入`RestTemplate`对象。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class ServiceInvoker { @Autowired private RestTemplate restTemplate; public String invokeService() { String serviceUrl = "http://serviceName/path"; // 服务名和路径 return restTemplate.getForObject(serviceUrl, String.class); } } ``` 4. 启用Ribbon:在Spring Boot的启动类上添加`@EnableDiscoveryClient`注解,示例代码如下: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 以上就是Spring Boot整合Ribbon的基本步骤。通过配置Ribbon,可以实现服务的负载均衡,提高系统的性能和可用性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值