美文网首页
Spring Cloud Ribbon 入门

Spring Cloud Ribbon 入门

作者: 歌哥居士 | 来源:发表于2019-03-30 12:34 被阅读0次

添加依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.baozi</groupId>
    <artifactId>cloud-ribbon</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>ribbon-registration</module>
        <module>ribbon-provider</module>
        <module>ribbon-consumer</module>
    </modules>


    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
<?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>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-ribbon</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>ribbon-registration</artifactId>
   <!--<version>子类没有版本号会使用父类的,和父类统一版本</version>-->


</project>
<?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>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-ribbon</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>ribbon-consumer</artifactId>

</project>
<?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>
   <parent>
      <groupId>org.baozi</groupId>
      <artifactId>cloud-ribbon</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>ribbon-provider</artifactId>

</project>

注册中心

spring.application.name=ribbon-registration
server.port=8081

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class RibbonRegistrationApplication {

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

}

服务提供者

spring.application.name=ribbon-provider
server.port=8082

eureka.client.service-url.defaultZone=http://localhost:8081/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class RibbonProviderApplication {

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

}
import org.springframework.web.bind.annotation.*;

@RestController
public class ProviderController {

    // 传统形式 ------------------

    @GetMapping("/m1")
    private String m1(@RequestParam String id) {
        return id;
    }

    @PostMapping("/m2") // POST形式传参数、对象只能用RequestBody,去接收postForObject的第二个参数
    private String m2(@RequestBody String id) {
        return id;
    }


    // REST形式 ------------------

    @GetMapping("/m3/{id}")
    private String m3(@PathVariable String id) {
        return id;
    }

    @PostMapping("/m4/{id}")
    private String m4(@PathVariable String id) {
        return id;
    }

    @PostMapping("/m5") // 传对象与传统形式一致,RestTemplate本来就是为REST形式设计的
    private String m5(@RequestBody String id) {
        return id;
    }

}

服务消费者

spring.application.name=ribbon-consumer
server.port=8083

eureka.client.service-url.defaultZone=http://localhost:8081/eureka
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;

@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {

   @Bean
   @LoadBalanced
   public RestTemplate restTemplate() {
      return new RestTemplate();
   }

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

}

控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ConsumerController {

    // 无需LoadBalancerClient:http://配置的spring.application.name/路径
    // 例如: restTemplate.getForObject("http://ribbon-provider/provider", String.class);
    @Autowired
    private RestTemplate restTemplate;

    // 传统形式 ------------------

    @GetMapping("/m1")
    private String m1(@RequestParam String id) {
        // 拼url
        String url = MessageFormat.format("http://ribbon-provider/m1?id={0}", id);
        return restTemplate.getForObject(url, String.class);
    }

    @GetMapping("/m2")
    private String m2(@RequestParam String id) {
        return restTemplate.postForObject(
                "http://ribbon-provider/m2", id, String.class);
    }


    // REST形式 ------------------

    @GetMapping("/m3")
    private String m3(@RequestParam String id) {
        // 方式1:可变参数
        return restTemplate.getForObject("http://ribbon-provider/m3/{id}", String.class, id);
        // 方式2:map
//        Map<String, Object> map = new HashMap<>();
//        map.put("id", id);
//        return restTemplate.getForObject("http://ribbon-provider/m2/{id}", String.class, map);
    }

    @GetMapping("/m4")
    private String m4(@RequestParam String id) {
        return restTemplate.postForObject(
                "http://ribbon-provider/m4/{id}", null, String.class, id);
//        Map<String, Object> map = new HashMap<>();
//        map.put("id", id);
//        return restTemplate.postForObject(
//                "http://ribbon-provider/m4/{id}", null, String.class, map);
    }

    @GetMapping("/m5")
    private String m5(@RequestParam String id) {
        return restTemplate.postForObject(
                "http://ribbon-provider/m5", id, String.class);
    }

}

相关文章

网友评论

      本文标题:Spring Cloud Ribbon 入门

      本文链接:https://www.haomeiwen.com/subject/vazcvqtx.html