spring cloud服务调用

来源:互联网 发布:在淘宝上买到假海之蓝 编辑:程序博客网 时间:2024/06/05 19:29

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
<?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.example</groupId>    <artifactId>server</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>spring-cloud-server</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.3.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>     <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.7</java.version>        <spring-cloud.version>Camden.SR4</spring-cloud.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-eureka</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>           <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-ribbon</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-devtools</artifactId>        </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>

application.properties

server.port=9090spring.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@EnableDiscoveryClientpublic 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服务的调用将不在这里阐述,生产中不常用。