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
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

    <groupId>test.ygy</groupId>
    <artifactId>ribbon-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ribbon-consumer</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值