Load Balancing (Ribben and Feign)

1, Ribben load balancing

 1. Package guide
    <! - client load balancing implementation Ribbon ->
    <dependency>
        <the groupId> org.springframework.cloud </ the groupId>
        <the artifactId> Starter-Spring-Cloud-Ribbon </ the artifactId>
    </ dependency>

 2, modify the configuration class Application class --- plus @LoadBalanced
    @SpringBootApplication
    @EnableEurekaClient // open registry client
    public class UserApplication {

        / **
         * Use RestTemplate call the service provided by the interface
         * RestTemplate pay a spring management
         * @param
         * /
        @Bean
        @LoadBalanced // open load balancing
        public RestTemplate getRestTemplate () {
            return new new RestTemplate ();
        }
        
        /
         * modify load balancing algorithm, may not be modified, default polling
         * @param
         * /
        @Bean
        public IRule myrule ( ) {
            // replace random polling rules
            return new new RandomRule ();
        }
        public static void main(String[] args){
            SpringApplication.run(UserApplication.class);
        }
    }

 3, controller layer to modify the value of the name attribute DEPT-PROVIDER yml address of the service provider url addresses configured
    @RestController
    public class the UserController {
        // injection RestTemplate
        @Autowired
        Private RestTemplate RestTemplate;

        @RequestMapping ( "/ userDept / {ID} ")
        public Dept getDeptById (@PathVariable (" the above mentioned id ") Long the above mentioned id) {
            / *
                provider of call registry interface
                url: http: // + method path name attribute value service provider configuration file + called
             * /
            Dept's Dept = restTemplate.getForObject ( "HTTP: // the DEPT-the PROVIDER / Dept /" + ID, Dept.class);
            System.out.println (Dept);
            return Dept;
        }
    }

 4, registry service provider controller layer modified --- add annotations port number, when easy access to see which port number
    @RestController // use @RestController because they want to pass json values
    public class DeptController {

        // injection port number
        @Value ( "server.port $ {}")
        Private String port;

        // springCloud is based restful style, so mass participation should be like this
        @ RequestMapping ( "/ the dept / {the above mentioned id}")
        public Dept getDept ( @PathVariable ( "the above mentioned id") Long the above mentioned id) {
            / *
            dept as a provider of services, to be exposed outside the interface, returns an Object dept,
             customer service call interface to get, get to that dept objects, we need to the dept objects proposed to separate
             again dependent manner jar package for
             * /
            // return the analog data database

            return new dept (id, "sales" Port +);
        }
    }

  

2 Feign Load Balancing  

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

 2, the main configuration --- home based annotation @EnableFeignClients
    @SpringBootApplication
    @EnableEurekaClient // Open the registry client
    @EnableFeignClients
    public class EmployeeApplication {
        public static void main (String [] args) {
            SpringApplication.run (EmployeeApplication.class);
        }
    }

 3, was added feign layer
    @FeignClient ( "DEPT-PROVIDER") // name attribute value with the service provider configuration coincides yml
    public interface DeptFeignClient {
        / **
         * Method and path parameters, and the method name to be consistent with the service provider
         * ID @param
         * @return
         * /
        @RequestMapping ( "/ Dept / $ {ID}")
        public Dept's 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);
        }
    }

Guess you like

Origin www.cnblogs.com/tuxiaoer/p/11408156.html