1. 程式人生 > >spring cloud服務呼叫

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
<?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=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.訪問驗證負載均衡

5總結

到這裡spring cloud在開發中的主幹內容已經聯通,也就是第一階段基本已經結束,接下來研究斷路器hystrix/zuul,至於feign服務的呼叫將不在這裡闡述,生產中不常用。

想了解更多java相關技術,請關注公眾號“JavaEE那些事”

掃描下面二維碼,更多技術資料等你來拿
這裡寫圖片描述