springboot-cloud-2-ribbon

    github地址:https://github.com/github-ygy/spring_cloud_test.git

ribbon-consumer

    依据springboot-cloud-2-eureka 快速启动ribbon消费
     前提:
     eureka-server : 注册中心分片1(也是提供者),注册自己的helloworld服务
     eureka-server-slave:注册中心分片2(也是服务提供者),注册自己的helloworld服务
     eureka-client  : 服务提供者,注册自己的getClient服务

pom


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>test.ygygroupId>
    <artifactId>ribbon-consumerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>ribbon-consumername>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.6.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Dalston.SR2spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-ribbonartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>


project>

appliction.properties

#####defaultZone 为 eureka分片其中之1的地址
spring.application.name=ribbon-consumer
server.port= 8001
eureka.client.serviceUrl.defaultZone=http://pear1:9998/eureka

Application

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {


    @Bean
    @LoadBalanced     //负载均衡
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }
}

controller

/**
 * Created by guoyao on 2017/8/8.
 */
@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate ;

    @RequestMapping("/consumerServer")
    public String consumerServer() {
         //此方法为eureka—server , eureka-server-slave 提供
        return  restTemplate.getForEntity("http://SERVER/helloworld?key=ygy",String.class).getBody();
    }

    @RequestMapping("/consumerClient")
    public String consumerClient() {
         //此方法为eureka—client提供
        return  restTemplate.getForEntity("http://client/getClient?key=client",String.class).getBody();
    }
}

输出

//localhost:8001/
// 调用consumerServer 2次,consumerClient一次
eureka—serverthis is server master ! 
eureka—server-salve:  this is server slave !
eureka-clientthis is eureka client server 

自定义ribbon负载

/**
 * Created by guoyao on 2017/8/10.
 */
@Configuration    // 包目录必须在@ComponentScan扫描不到的目录
public class CustomerRibbonConfig {

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

application

@SpringBootApplication
@RibbonClient(name = "client",configuration =CustomerRibbonConfig.class)
@EnableDiscoveryClient
public class RibbonConsumerApplication {

    @Bean
    @LoadBalanced     //负载均衡
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }
}

你可能感兴趣的:(springboot)