SpringCloud 服务消费者-rest+ribbon

spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign.

ribbon

ribbon是一个负载均衡客户端,Feign默认集成了ribbon。ribbon已经默认实现了如下配置bean:

(1) IClientConfig ribbonClientConfig: DefaultClientConfigImpl

(2)IRule ribbonRule: ZoneAvoidanceRule

(3)IPing ribbonPing: NoOpPing

(4)ServerList ribbonServerList: ConfigurationBasedServerList

(5)ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

(6)ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

实战

基于前上篇文章,启动eureka-server工程,启动server-hi工程,它的端口为8762,将server-hi的配置文件的端口改为8763,再启动一个server-hi。

1、如何在idea启动多个spring boot工程实例

(1)在idea点击Application的右下角,弹出选项后,点击Edit Configuration.

(2)打开配置后,将默认的Single instance only的钩去掉。

2、建一个服务消费者

重新新建一个spring-boot工程,取名为:service-ribbon。

在它的pom.xml继承父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.forezp</groupId>
    <artifactId>service-ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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


    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

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

 

</project>

在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为service-ribbon,程序端口为8764.

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册,并且向程序的ioc注入一个bean:restTemplate,并通过@LoadBalanced注解表明这个restTemplate开启负载均衡的功能。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {

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

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

}

写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用程序名称代替具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例。

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

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


}

写一个controller,在controller中调用HelloService的方法

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;

    @GetMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.hiService( name );
    }
}

在浏览器上多次访问http://localhost:8764/hi?name=xxx

猜你喜欢

转载自blog.csdn.net/CHS007chs/article/details/83502099
今日推荐