相关博文
Spring Cloud 微服务架构搭建(一)— 介绍
Spring Cloud 微服务架构搭建(二)— 统一依赖管理
Spring Cloud 微服务架构搭建(三)— Eureka 服务注册与发现中心
Spring Cloud 微服务架构搭建(四)— 服务提供者
Spring Cloud 微服务架构搭建(五)— Ribbon+RestTemplate 服务负载均衡调用
Spring Cloud 微服务架构搭建(六)— Feign 声明式服务调用
Spring Cloud 微服务架构搭建(七)— Hystrix 熔断器及仪表盘监控
Spring Cloud 微服务架构搭建(八)— Zuul 路由网关及服务过滤
Spring Cloud 微服务架构搭建(九)— Config 分布式配置中心
Spring Cloud 微服务架构搭建(十)— Zipkin 服务链路追踪
Spring Cloud 微服务架构搭建(完结篇)— Spring Boot Admin 服务监控
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 Http Restful 的。Spring Cloud 有两种服务调用方式,一种是 Ribbon + RestTemplate ,另一种是 Feign
Ribbon 是一个负载均衡客户端,可以很好的控制 HTTP 和 TCP 的一些行为。
创建目录 hello-spring-cloud-web-admin-ribbon
,并在该目录下创建 pom.xml
主要添加 spring-cloud-starter-netflix-eureka-server
和 spring-cloud-starter-netflix-ribbon
依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-ribbonartifactId>
dependency>
完整 pom.xml
如下
<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.0modelVersion>
<parent>
<groupId>com.antoniopenggroupId>
<artifactId>hello-spring-cloud-dependenciesartifactId>
<version>1.0.0-SNAPSHOTversion>
<relativePath>../hello-spring-cloud-dependencies/pom.xmlrelativePath>
parent>
<artifactId>hello-spring-cloud-web-admin-ribbonartifactId>
<packaging>jarpackaging>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-ribbonartifactId>
dependency>
<dependency>
<groupId>net.sourceforge.nekohtmlgroupId>
<artifactId>nekohtmlartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<mainClass>com.antoniopeng.hello.spring.cloud.web.admin.ribbon.WebAdminRibbonApplicationmainClass>
configuration>
plugin>
plugins>
build>
project>
spring:
application:
name: hello-spring-cloud-web-admin-ribbon
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8764
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
通过 @EnableDiscoveryClient
注解开启发现服务功能
@SpringBootApplication
@EnableDiscoveryClient
public class WebAdminRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(WebAdminRibbonApplication.class, args);
}
}
配置注入 RestTemplate 的 Bean,并通过 @LoadBalanced
注解表明开启负载均衡功能
@Configuration
public class RestTemplateConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
在这里我们直接用的程序名替代了具体的 URL 地址,在 Ribbon 中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的 URL 替换掉服务名,代码如下:
@Service
public class AdminService {
@Autowired
private RestTemplate restTemplate;
public String sayHi(String message) {
return restTemplate.getForObject("http://hello-spring-cloud-service-admin/hi?message=" + message, String.class);
}
}
@RestController
public class AdminController {
@Autowired
private AdminService adminService;
@RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi(@RequestParam String message) {
return adminService.sayHi(message);
}
}
END