Spring Cloud Netflix 创建服务消费者——Ribbon(四)
概述
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的。Spring cloud 有两种服务调用方式,一种是 ribbon + restTemplate,另一种是 feign。
Ribbon+RestTemplate
Ribbon 是一个负载均衡客户端,可以很好的控制 http 和 tcp 的一些行为。
创建服务消费者
完整pom文件
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
| <?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.2.2.RELEASE</version> <relativePath/> </parent> <groupId>com.xzt</groupId> <artifactId>cloud-ribbon-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-ribbon-service</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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.yml
1 2 3 4 5 6 7 8 9 10 11
| spring: application: name: cloud-ribbon-service
server: port: 8764
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
|
Configuration
配置注入 RestTemplate
的 Bean,并通过 @LoadBalanced
注解表明开启负载均衡功能 s
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.xzt.ribbon.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;
@Configuration public class RestTemplateConfiguration { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
|
Service
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
| package com.xzt.ribbon.test.service;
import com.xzt.ribbon.test.beans.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;
@Service public class AdminService {
private Logger logger = LoggerFactory.getLogger(AdminService.class); @Autowired private RestTemplate restTemplate;
public String sayHi(String message) { return restTemplate.getForObject("http://CLOUD-ADMIN-SERVICE/hi?message=" + message, String.class); }
public String postHi(String userName, String password) { User user = new User(); user.setUserName(userName); user.setPassword(password); User res = restTemplate.postForObject("http://CLOUD-ADMIN-SERVICE/req_post", user, User.class); return String.format("%s ,welcome! I am from port %s", userName, res.getPort()); } }
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.xzt.ribbon.test.controller;
import com.xzt.ribbon.test.service.AdminService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
@RestController public class AdminController { @Autowired private AdminService adminService;
@RequestMapping(value = "hi", method = RequestMethod.GET) public String sayHi(String message) { return adminService.sayHi(message); }
@RequestMapping(value = "req_post", method = RequestMethod.GET) public String postHi(String userName, String password) { return adminService.postHi(userName, password); } }
|
在cloud-admin-service
增加以下接口
1 2 3 4 5 6
| @RequestMapping(value = "req_post", method = RequestMethod.POST) public User postHi(@RequestBody User user){ logger.info("userName:{}----password:{}",user.getUserName(),user.getPassword()); user.setPort(port); return user; }
|
测试
GET
访问http://localhost:8764/hi?message=12345
,将出现以下内容
1
| Hi,your message is : 12345 i am from port : 8762
|
POST
访问http://localhost:8764/req_post?userName=mo&password=123456
,将出现以下内容
1
| mo ,welcome! I am from port 8762
|
如果配置多个cloud-admin-service
服务,多次访问此接口,端口号应当随之变化