1.Ribbon是什么?
.接下来,我们就要访问前面注册好的数据微服务了。 springcloud 提供了两种方式,一种是 Ribbon,一种是 Feig。
Ribbon 是使用 restTemplate 进行调用
Feign 是什么呢? Feign 是对 Ribbon的封装,调用起来更简单
2.新建一个子项目product-view-service-ribbon
https://blog.csdn.net/m0_45025658/article/details/106433731
3.pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>SpringCloud</artifactId>
<groupId>org.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>product-view-service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>product-view-service-ribbon</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
4.新建产品实体类
package org.example.pojo;
public class Product {
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Product() {
}
public Product(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
}
5.Ribbon客户端
Ribbon 客户端, 通过 restTemplate 访问 http://PRODUCT-DATA-SERVICE/products ,
而 product-data-service 既不是域名也不是ip地址,而是 数据服务在 eureka 注册中心的名称。
package org.example.service;
import org.example.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@Component
public class RibbonClient {
@Autowired
RestTemplate restTemplate;
public List<Product> listProduct(){
return restTemplate.getForObject("http://product-data-service", List.class);
}
}
6.控制层
将从data模块注册到eureka的数据通过ribbon的方式获得,并将数据返回给前端
package org.example.Controller;
import org.example.pojo.Product;
import org.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/products")
public Object products(Model m){
List<Product> ps = productService.listProduct();
m.addAttribute("ps","ps");
return ps;
}
}
7.前端
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>products</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div>
<table>
<thead>
<tr>
<th>id</th>
<th>产品名称</th>
<th>价格</th>
</tr>
</thead>
<tbody>
<tr th:each="p: ${ps}">
<td th:text="${p.id}"></td>
<td th:text="${p.name}"></td>
<td th:text="${p.price}"></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
8.application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: product-view-service-ribbon
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
content-type: text/html
mode: HTML5
9.启动类
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class viewApplication
{
public static void main( String[] args ) {
new SpringApplication(viewApplication.class).run(args);
}
}
10.配置类
在springboot1.3版本中会默认提供一个RestTemplate的实例Bean,当在springboot1.4以及以后的版本中,需要手动创建一个RestTemplate的配置
package org.example.conf;
import org.springframework.boot.web.client.RestTemplateBuilder;
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 TemplatesConf {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
11.启动eureka,product-data-service,product-view-service-ribbon
访问euraka: http://127.0.0.1:8761/
可以看到数据微服务和视图微服务都已经注册到eureka上,视图微服务就可以通过ribbon的方式来拿到数据然后显示到前台上了。
本章详细信息请看:https://how2j.cn/k/springcloud/springcloud-ribbon/2040.html#nowhere