负载均衡(Ribben和Feign)

1、Ribben 负载均衡

 1.导包
   

 <!--客户端负载均衡实现 ribbon-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

 

 2、修改配置类Application类---加@LoadBalanced
    

@SpringBootApplication
    @EnableEurekaClient //开启注册中心客户端
    public class UserApplication {

        /**
         * 使用RestTemplate调用服务者提供的接口
         * RestTemplate交个spring管理
         * @param
         */
        @Bean
        @LoadBalanced //开启负载均衡
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
        
        /
         *  修改负载均衡的算法,也可以不修改,默认轮询
         * @param
         */
        @Bean
        public IRule myRule(){
            //使用随机规则替换掉轮询
            return new RandomRule();
        }
        public static void main(String[] args){
            SpringApplication.run(UserApplication.class);
        }
    }

 

 3、controller层修改了url地址  地址使用的服务提供者的yml配置的name属性值DEPT-PROVIDER
    

@RestController
    public class UserController {
        //注入RestTemplate
        @Autowired
        private RestTemplate restTemplate;

        @RequestMapping("/userDept/{id}")
        public Dept getDeptById(@PathVariable("id") Long id){
            /*
                调用注册中心的提供者的接口
                url:http://+服务提供者的配置文件的name属性值+调用的方法路径
             */
            Dept dept = restTemplate.getForObject("http://DEPT-PROVIDER/dept/" + id, Dept.class);
            System.out.println(dept);
            return dept;
        }
    }

 

 4、注册中心服务提供者的controller层修改---加了端口号的注解,方便访问的时候查看是哪个端口号
    

@RestController  //使用@RestController是因为要传json值
    public class DeptController {

        //注入端口号
        @Value("${server.port}")
        private String port;

        //springCloud是基于restful风格的,所以传参也要这个样子
        @RequestMapping("/dept/{id}")
        public Dept getDept(@PathVariable("id") Long id){
            /*
            dept作为服务的提供者,要向外暴露接口,返回的是一个dept对象,
             服务的消费者调用接口获取,获取到的是dept对象,所以需要把dept对象单独提出去
             再以jar包的方式依赖进行
             */
            //模拟数据库返回数据

            return new Dept(id,"销售部"+port);
        }
    }

 

  

2 Feign 负载均衡 

1、导包
    

<!--feign的支持-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

 

 2、主配置类---家注解@EnableFeignClients
    

@SpringBootApplication
    @EnableEurekaClient //开启注册中心客户端
    @EnableFeignClients
    public class EmployeeApplication {
        public static void main(String[] args){
            SpringApplication.run(EmployeeApplication.class);
        }
    }

 

 3、添加feign层
    

@FeignClient("DEPT-PROVIDER") //与服务提供者yml配置的name属性值一致
    public interface DeptFeignClient {
        /**
         *  方法路径和参数,方法名必须和服务提供着的一致
         * @param id
         * @return
         */
        @RequestMapping("/dept/${id}")
        public Dept getDept(@PathVariable("id") Long id);
    }

 

 4、controller层
   

 @RestController
    public class EmployeeController {
        /**
         * 注入feign的接口
         */
        @Autowired
        private DeptFeignClient deptFeignClient;

        @RequestMapping("/userDept/{id}")
        public Dept getDeptById(@PathVariable("id") Long id){
            return deptFeignClient.getDept(id);
        }
    }

 

Feign的使用方式2

  1.准备连接接口   

/**
 *  
 *  @date 2019/8/12 16:24
 *  @Description:
*/
@FeignClient(name = "name-service")//与服务提供者yml配置的name属性值一致,直接写在调用服务方
public interface CounterService {

    /**
     * 
     * @param path 路径
     * @return
   * /counter:被调用服务的的controller的name
   * /get/counter/by/path 方法的路径

     */
    @RequestMapping(value = "/counter/get/by/path",method = RequestMethod.POST)
    ApiResult<String> getCounterByPath(@RequestParam("path") String path);

    @RequestMapping(value = "/counter/get/macro/iden/counter",method = RequestMethod.GET)
    ApiResult<String> getMacroIdenCounter();

    @RequestMapping(value = "/counter/set/value/by/path",method = RequestMethod.POST)
    ApiResult<Boolean> setCounterByPath(@RequestParam("path") String path, @RequestParam("value") Long value);
}

 

 2.调用服务的控制层接口@Controller

@RequestMapping(value = "/counter",produces = {"application/json;charset=utf-8"})
public class CounterController {


    @Autowired
    private CounterCreateServiceImpl counterCreateService;


    /**
     * 根据路径获取计数值
     * @param path 计数值锁路径
     * @return
     */
    @RequestMapping(value = "/get/by/path",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult<String> getCounterByPath(@RequestParam("path") String path){
        String code = "";
        try{
            code = this.counterCreateService.getPreCountByCounterPath(path);
        }catch (Exception e){
            return new ApiResult<>(e);
        }
        return new ApiResult<>(code);
    }
}

3.调用方调用接口
@Service
public class testImpl(){
  @Autowired
  private CounterService counterService;
  //直接注入使用
  
  public Boolean insertMicroGroup(String name, String memo, String create_userid, Integer sort) {
    
    counterService.getCounterByPath("path参数的值");

  }

}

 
posted @ 2019-08-25 15:55  涂小二  阅读(1122)  评论(0编辑  收藏  举报