spring cloud开发Greenwich版:服务调用feign(七)

在实际应用中,spring cloud通常feign组件与服务提供者进行通信,feign有着更友好的api调用方式
我们改造微服务’ralab-client’

  • Add the Maven spring-cloud-config-server denpendency
  • Add @EnableFeignClients to the application class
  • Add FeignClient Interface communicate to microservice ‘ralab-nlp’
  • Add invoke Interface to RestController NlpResource
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    <?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 https://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.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ralab</groupId>
    <artifactId>ralab-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ralab-client</name>
    <description>Demo project for Spring Boot</description>

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

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

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--添加依赖,获取配置服务器的相关配置信息-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</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>

RalabClientApplication文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.ralab.ralabclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableFeignClients
public class RalabClientApplication {

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

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

}

EurekaFeignService文件

1
2
3
4
5
6
7
8
9
10
11
12
package com.ralab.ralabclient.services;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "nlp-service")
public interface EurekaFeignService {

@RequestMapping(value = "nlp/api")
String nlpService();
}

然后在NlpResource调用上面的feign接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ralab.ralabclient.resources;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.ralab.ralabclient.services.EurekaFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/nlp")
@RefreshScope
public class NlpResource {

@Autowired
private RestTemplate restTemplate;

@Autowired
private EurekaFeignService eurekaFeignService;

@Value("${example.property}")
private String exampleProperty;

@RequestMapping("/config")
public String home() {
return "exampleProperty:" + exampleProperty;
}

@GetMapping("/service")
@HystrixCommand(fallbackMethod = "getFallbackIndex")
public String index()
{
//String result = restTemplate.getForObject("http://localhost:8090/nlp/api", String.class);
String result = restTemplate.getForObject("http://nlp-service/nlp/api", String.class);
return result;
}

@GetMapping("/feign")
@HystrixCommand(fallbackMethod = "getFallbackIndex")
public String feignservice()
{
String result = eurekaFeignService.nlpService();
return result;
}

private String getFallbackIndex()
{
//String result = restTemplate.getForObject("http://localhost:8090/nlp/api", String.class);

return "不好意思,没有获取到服务端信息";
}
}

运行RalabClientApplication,打开http://localhost:8091/nlp/feign

坚持原创技术分享,您的支持是我前进的动力!