Ribbon负载均衡

步骤:
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);

    }
上一篇:线程同步的几种方式


下一篇:2017年上半年软考真题 程序员 下午试卷 案例【含答案解析】