Springcloud学习(四)Springcloud客户端作为消费者自动负载均衡调用service

Springcloud客户端作为消费者自动负载均衡调用service

  • 服务提供者即生产者是支持在用户量较大的情况下,可能会挂掉,因此多节点支持成为必然。
  • 本文描述客户端调用服务时,如果多个生产者将使用ribbon负载均衡交替调用。
  • 代码地址https://github.com/Jacwo/eureka-server-customer 欢迎start 

开始

  1. 使用idea新建一个maven项目 可以使用spring Initializr
  2. 替换pom.xml
    <?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.yyl</groupId>
        <artifactId>eureka-server-customer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka-server-customer</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
                <version>RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
                <version>RELEASE</version>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    

     

  3. 编写启动类
    package com.yyl.customer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author yangyuanliang
     */
    @EnableDiscoveryClient
    @SpringBootApplication
    public class EurekaServerCustomerApplication {
        @Bean
        @LoadBalanced
        RestTemplate restTemplate(){
            return new RestTemplate();
        }
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerCustomerApplication.class, args);
        }
    
    }
    

     

  4. 测试controller
    package com.yyl.customer.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * author:yangyuanliang Date:2019-08-14 Time:17:39
     **/
    @RestController
    public class CustomerController {
        @Autowired
        private RestTemplate restTemplate;
    
        @RequestMapping(value = "/customer",method = RequestMethod.GET)
        public String helloCustomer(){
            return restTemplate.getForEntity("http://hello-service/hello",String.class).getBody();
        }
    }
    

     

  5. 编写application.properties
    server.port=8888
    spring.application.name=customer
    eureka.client.service-url.defaultZone=http://localhost:8672/eureka/,http://localhost:8673/eureka/
    

     

  6. 改造https://mp.csdn.net/postedit/99455466 服务提供者代码或者下载
    git clone https://github.com/Jacwo/eureka-server-provider.git
  7. 多节点启动生产者即服务提供者
    mvn package
    //节点1
    java -jar target/eureka-server-provider-0.0.1-SNAPSHOT.jar
    //节点2
    java -jar target/eureka-server-provider-0.0.1-SNAPSHOT.jar --spring.profiles.active=p1
    

     

  8. 启动消费者 mvn spring-boot:run 
  9. 开始测试 http://localhost:8888/customer 多次刷新查看生产者日志
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章