spring cloud服务调用

spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign。
Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了ribbon, 只要使用@FeignClient时,ribbon就会自动使用。

Ribbon+RestTemplate实现服务调用

1.创建服务注册中心

前面章节已经介绍过,如不熟悉请查看

2.创建服务,并注册带注册中心

将http://blog.csdn.net/forwujinwei/article/details/72800571创建的应用复制一份,修改application.properties中的端口号为9001
**注意:在这里我踩了一个坑,请不要将spring.application.name=eureka.client.01,在后续的调用中会报错,就是因为.01,请不要使用还有数字的注册方式,只要保证两个应用的spring.application.name相同就可实现负载均衡测试,在eureka主页面就可以看到是否注册成功。**

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.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>spring-cloud-servername>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.3.RELEASEversion>
        <relativePath/> 
    parent> 
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.7java.version>
        <spring-cloud.version>Camden.SR4spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eurekaartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
           <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-ribbonartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement> 

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

application.properties

server.port=9090
spring.application.name=eureka.client.02   #别这么命名,会死的莫名其妙,在这里不调用,我就不做修改了
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/

入口类:

package com.example;

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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudClientDiscoveryApplication {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
         RestTemplate template = new RestTemplate();
            SimpleClientHttpRequestFactory factory = (SimpleClientHttpRequestFactory) template.getRequestFactory();
            factory.setConnectTimeout(3000);
            factory.setReadTimeout(3000);
            return template;
    }

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

调用类,为方便测试使用controller类
package com.example.controller;

import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.example.Consten;

@RestController
public class DiscoveryController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBanlancerClient;
@RequestMapping(“/hi”)
public String hi(){
return restTemplate.getForObject(“http://eureka.client/person/get“, String.class);
}
}
补充下服务类:

package com.example.controller;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/person")
public class PersonController {
     @Value("${server.port}")
      String port;
    @RequestMapping(value="/get",method=RequestMethod.GET)
    public String getPerson1(){
        return port;//方便观测负载
    }
}

4.访问验证负载均衡

访问:http://localhost:9090/hi
多次访问会发现ribbon默认是使用轮询的方式交替调用

5总结

到这里spring cloud在开发中的主干内容已经联通,也就是第一阶段基本已经结束,接下来研究断路器hystrix/zuul,至于feign服务的调用将不在这里阐述,生产中不常用。


想了解更多java相关技术,请关注公众号“JavaEE那些事”

扫描下面二维码,更多技术资料等你来拿
spring cloud服务调用_第1张图片

你可能感兴趣的:(spring-cloud)