SpringCloud——获取服务(1.Ribbon)

SpringCloud学习
1.IDEA创建父子项目
2.SpringCloud——Eureka
3.SpringCloud——注册服务
4.SpringCloud——获取服务(1.Ribbon)
5.SpringCloud——获取服务(1.Feign)
6.SpringCloud——服务链路追踪
7.SpringCloud——断路器
8.SpringCloud——断路器监控
9.SpringCloud——断路器聚合监控
10.SpringCloud——网关

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值