SpringAli服务消费

微服务改变着我们的项目结构,影响深远

Spring Cloud Alibaba 服务消费

  1. 服务消费(原始方式)
  2. 服务消费(使用Feign)

服务消费(原始方式)

显示的使用 LoadBalanceClient 和 RestTemplate 结合的方式来访问。

创建新的moudle模块(消费者),pom.xml如下

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
<?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">
<parent>
<artifactId>cloud-dependencies</artifactId>
<groupId>com.wwj</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>nacos-consumer</artifactId>

<packaging>jar</packaging>

<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->

<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.wwj.consumer.ConsumerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

安装标准构建启动类

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}

}

构建一个ConsumerConfiguration配置类,注入RestTemplate对象

1
2
3
4
5
6
7
@Configuration
public class ConsumerConfiguration {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

编写controller

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
package com.wwj.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
//注入启动客户端对象
@Autowired
private LoadBalancerClient loadBalancerClient;
//注入RestTemplate模板
@Autowired
private RestTemplate restTemplate;
//可以用来获取当前应用名称
@Value("${spring.application.name}")
private String appName;


@GetMapping(value = "/echo/app/name")
public String echo() {
//使用 LoadBalanceClient 和 RestTemplate 结合的方式来访问
//选择服务名字
ServiceInstance serviceInstance = loadBalancerClient.choose("provider");
//拼接restful请求
String url = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
return restTemplate.getForObject(url, String.class);
}
}

构建应用程序启动文件 application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring:
application:
name: consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

server:
port: 9091

management:
endpoints:
web:
exposure:
include: "*"

结果如下

IMAGE

端点检查 http://localhost:9091/actuator/nacos-discovery

IMAGE

服务消费(使用Feign)

概论

Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,Nacos 也很好的兼容了 Feign,默认实现了负载均衡的效果

  1. 使用接口很容易抽象理解
  2. 集成Ribbon达到负载均衡

构建新的moudle,pom.xml内容如下

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
<?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">
<parent>
<artifactId>cloud-dependencies</artifactId>
<groupId>com.wwj</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>nacos-consumer-feign</artifactId>
<packaging>jar</packaging>

<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->

<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.wwj.consumer.feign.ConsumerFeignApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

构建启动类

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApplication.class, args);
}
}

通过feign调用远程服务

1
2
3
4
5
6
7
8
9
10
11
/**
* 1.通过@FeignClient伪造一个http客户端请求已经注册的服务
* 2.接口中的方法需要去匹配已经注册的服务方里面的请求
*/

@FeignClient(value = "nacos-provider")
public interface ProviderService {

@GetMapping(value = "/echo/{message}")
String echo(@PathVariable String message);
}

controller注入对应的服务

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class ProviderController {

@Autowired
private ProviderService providerService;

@GetMapping("echo")
public String echo() {
return providerService.echo("Feign Client");
}
}

全局配置文件声明如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring:
application:
name: consumer-feign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

server:
port: 9092

management:
endpoints:
web:
exposure:
include: "*"

结果如下

IMAGE

测试负载均衡

  1. 服务端启动多个示例
  2. 多次访问localhost:9092/echo

IMAGE

IMAGE