(二)spring-cloud入门学习:服务消费rest+ribbon
前面讲了如何将服务注册到Eureka服务端,为了保证服务的高可用,同一个服务都会采用集群形式注册多个节点,那如何去消费这些服务?通过IP+端口的形式明显不合理,那就需要通过服务应用名来进行访问,同时需要能够将请求均衡转发到各节点。而Ribbon就很好地解决了这个问题,Ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。1. 创建一个服务消费者工程eureka_ribbon,.
前面讲了如何将服务注册到Eureka服务端,为了保证服务的高可用,同一个服务都会采用集群形式注册多个节点,那如何去消费这些服务?通过IP+端口的形式明显不合理,那就需要通过服务应用名来进行访问,同时需要能够将请求均衡转发到各节点。而Ribbon就很好地解决了这个问题,Ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。
1. 创建一个服务消费者工程eureka_ribbon, 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.0</modelVersion>
<groupId>com.kevin</groupId>
<artifactId>eureka_ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka_ribbon</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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-netflix-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2. application.properties
spring.application.name=eurekaRibbon
server.port=9201
#注册到eureka
eureka.client.serviceUrl.defaultZone=http://localhost:9101/eureka/
3. Application.java
使用@EnableDiscoveryClient开启服务发现,同时需要申明RestTemplate。
package com.kevin.eureka_ribbon;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main( String[] args ) {
new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.SERVLET).run(args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
4. 创建一个service,HelloService.java
使用RestTemplate进行服务调用
package com.kevin.eureka_ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
public String sayHello() {
return restTemplate.getForObject("http://eurekaClient/sayHello", String.class);
}
}
备注:通过服务注册的name来访问api,该name不能包含下划线,否则报错Request URI does not contain a valid hostname.
5. 创建一个api对外访问,HelloController.java
package com.kevin.eureka_ribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kevin.eureka_ribbon.service.HelloService;
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("sayHello")
public String sayHello() {
return helloService.sayHello();
}
}
6. 启动项目并访问http://127.0.0.1:9201/sayHello
能够正常访问结果,同时查看两个服务client的后台,请求7次,eureka_client9102收到4次请求,eureka_client9103收到3次请求,说明ribbon会将请求负载均衡到各服务端。
更多推荐
所有评论(0)