二 spring cloud 服务消费Ribbon

参考 https://blog.csdn.net/forezp/article/details/81040925

ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为

在一篇的基础上继续做demo

1 将 service-hi 模块通过设置进行多实例运行这样就相当于一个简单的集群了

 idea启动多个实例
点击倒三角符号选中 Edit configuration
右上角 single instance only 去掉勾 启动
修改端口和工程名 启动
这样就可以产生两个实例了

2在原来的项目下新建模块service-rabbon

3 修改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>com.cloudtest</groupId>
    <artifactId>eureka</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <modules>
        <module>eureka-server</module>
        <module>service-hi</module>
    </modules>

    <!--添加项目依赖-->
    <dependencies>
        <!-- jaxb模块引用 java9+ 上需要自己手动建立依赖 - start -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

        <!-- jaxb模块引用 - end -->

        <!--test测试依赖-->
        <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>Finchley.RELEASE</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>

    <!-- 项目参数(之前的pom都没有配置  是否可以不用配置)
    <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>Finchley.RELEASE</spring-cloud.version>
    </properties>
    -->



</project>

4 修改 appcation.yml

server:
  port: 8764
spring:
  application:
    name: service-ribbon

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

5 主类配置

@EnableDiscoveryClient向服务中心注册;

注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能

//@EnableDiscoveryClient向服务中心注册
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced //注解表明这个restRemplate开启负载均衡的功能。
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

6 编写一个测试类HelloService

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name){
        return restTemplate.getForObject( "http://SERVICE-HI/hi?name="+name,String.class);
    }

}

7 编写一个测试类HelloController

@RestController
public class HelloCotroller {

    @Autowired
    HelloService helloService;

    @GetMapping("/hi")
    public String hi(@RequestParam(required=false) String name){
//        List<String> list = new ArrayList<String>();
//        for(int i = 0 ; i < 10 ; i++){
//         String result = helloService.hiService(name);
//         list.add(result);
//        }
//        return list.toString();
        return helloService.hiService(name);
    }
}

在浏览器上多次访问http://localhost:8764/hi?name=forezp,浏览器交替显示不同的端口

这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=”+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值