您现在的位置是:首页 > 学无止境 > Spring Cloud

Spring cloud入门基础(3)客户端Ribbon实现

贾继涛 2019-01-22 2207人围观

1.首先看下我整理的项目目录结构如下

 png

2. 创建File->New->Directory创建文件夹hello-spring-cl-sweb-ribbon目录结构类似如下

png

3.pom.xml


<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>
    <parent>
        <groupId>com.jjt.cloud</groupId>
        <artifactId>hello-spring-cl-den</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cl-den/pom.xml</relativePath>
    </parent>
    <artifactId>hello-spring-cl-sweb-ribbon</artifactId>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <!--<artifactId>spring-cloud-starter-eureka</artifactId>-->
        </dependency>
        <!-- 引入ribbon 依赖 ,用来实现负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>      
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jjt.hello.hello.SwebRibbonApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

4.java启动main


package com.jjt.hello.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
public class SwebRibbonApplication {
    //@Bean 应用在方法上,用来将方法返回值设为为bean
    @Bean
    @LoadBalanced  //@LoadBalanced实现负载均衡
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(SwebRibbonApplication.class, args);
    }
}


5.服务类调用接口

package com.jjt.hello.hello.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloConsumerService {
    @Autowired
    private RestTemplate restTemplate;
    public String getHelloContent() {
        return restTemplate.getForObject("http://hello-spring-cl-service/hi?msg=1111111111111", String.class);
    }  
}


6.controller类



package com.jjt.hello.hello.controller;
import com.jjt.hello.hello.Service.HelloConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
    @Autowired
    private HelloConsumerService helloConsumerService;
    @RequestMapping("/hello-consumer")
    public String helloConsumer() {
        return helloConsumerService.getHelloContent();
    }
}

7.application.yml文件


spring:
    application:
        name: hello-spring-cl-sumer
server:
    port: 8764
eureka:
    client:
        serviceUrl:
            defaultZone: http://127.0.0.1:8761/eureka/


8.启动四个项目


9访问负载均衡 http://127.0.0.1:8764/hello-consumer?msg=111111111111




我的代码公开在码云 其中包括pom中 父依赖



https://gitee.com/jiajitao/springCloudFirst



您的支持