ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Ribbon负载均衡

2021-03-05 12:32:35  阅读:162  来源: 互联网

标签:Customer 负载 return String id 均衡 public Ribbon RequestMapping


步骤:
1、导入依赖
2、加入注解
3、通过服务名+路径访问

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    return new RestTemplate();
}
--------------------------------------------------------
@GetMapping("/customer")
public String customer(){						//SEARCH是eureka里面服务的名称
    String result = restTemplate.getForObject("http://SEARCH/search", String.class);

    //4. 返回
    return result;
}

负载均衡策略方式
1、注解方式

@Bean
public IRule robbinRule(){
    return new RandomRule();
}

2、配置文件配置

# 指定具体服务的负载均衡策略
SEARCH:      # 编写服务名称
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule  # 具体负载均衡使用的类

Feign代替RestTemplate访问服务
1、导入依赖
2、加入注解
3、创建接口 通过接口访问
4.调用接口的方法 直接访问

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

启动类注解 @EnableFeignClients
创建接口,接口的方法和路径全部和另一个服务的controller一样

@FeignClient("SEARCH")   // 指定服务名称
public interface SearchClient {
    
    // value -> 目标服务的请求路径,method -> 映射请求方式
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    String search();

	//FeignClient的接口方法 有参数的时候参数需要加入注解
    @RequestMapping("/search/{id}")
    public Customer findById(@PathVariable(value = "id") Integer id);

    @RequestMapping("/getCustomer")
    public Customer getCustomer(@RequestParam("id") Integer id,@RequestParam("name") String name);

    //这个方法参数前没有加任何东西,默认就一个@RequestBody注解,所以另一个服务的参数要加@RequestBody
    @RequestMapping("/add")
    public Customer add(Customer customer);

}

另一个服务的controller

@Value("${server.port}")
    private String port;

    @RequestMapping("/search/show")
    public String show(){
        System.out.println("show");
        return "search"+port;
    }

    @RequestMapping("/search/{id}")
    public Customer findById(@PathVariable Integer id){

        return new Customer(id,"zhangsan",19);
    }

    @RequestMapping("/getCustomer")
    public Customer getCustomer(Integer id, String name){
        return new Customer(id,name,30);
    }

    @RequestMapping("/add")
    public Customer add(@RequestBody Customer customer){
        return customer;
    }

测试


@Autowired
    private EurekaClient eurekaClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FeignClient feignClient;

    @RequestMapping("/client/test")
    public Customer test(){
//EurekaClient 和restTemplate方式访问
       // InstanceInfo info = eurekaClient.getNextServerFromEureka("Search", false);
       // String homePageUrl = info.getHomePageUrl();

       // System.out.println(homePageUrl);

//        String result = restTemplate.getForObject(homePageUrl + "/search/show", String.class);
        String result = restTemplate.getForObject("http://Search/search/show", String.class);

        System.out.println(result);
        return new Customer(1,"lee",30);
    }
-------------------------------------------

@RequestMapping("/client/test")
    public String test(){


        return feignClient.show();

    }

    @RequestMapping("/client/test2/{id}")
    public Customer test2(@PathVariable Integer id){


        return feignClient.findById(id);

    }
    @RequestMapping("/client/test3")
    public Customer test3(Integer id,String name){


        return feignClient.getCustomer(id,name);

    }
    @RequestMapping("/client/test4")
    public Customer test4(Customer customer){

        System.out.println(customer);
        return feignClient.add(customer);

    }

标签:Customer,负载,return,String,id,均衡,public,Ribbon,RequestMapping
来源: https://blog.csdn.net/weixin_45174508/article/details/114389627

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有