• spirngcloud服务治理(注册中心)


    背景:

            Eureka 是 Netflix 公司开源的一个服务注册与发现的组件 。 Eureka 和其他 Netflix 公司的服务组件(例如负载均衡、熔断器、网关等) 一起,被 Spring Cloud 社区整合为    

            Spring-Cloud-Netflix 模块。 Eureka 包含两个组件:Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。

    问题:

    当service A机器发生故障后,provider机器配置的ip发生变化,所有访问provider的机器的路径也要发生变化,需要重新配置一下,往往会有多个访问的比较麻烦

    解决:

    当Consumer需要访问Proavider时,直接向Registry(注册中心)发送请求,注册中心会返回指定Provider的IP

    1.Eureka

    1.1介绍

    • Eureka 是 Netflix 公司开源的一个服务注册与发现的组件 。
    • Eureka 和其他 Netflix 公司的服务组件(例如负载均衡、熔断器、网关等) 一起,被 Spring Cloud 社区整合为     Spring-Cloud-Netflix 模块。
    • Eureka 包含两个组件:Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。

     1.2搭建步骤

    1. 搭建 Provider  和 Consumer 服务。
    2. 使用 RestTemplate 完成远程调用。
    3. 搭建 Eureka Server 服务。
    4. 改造 Provider  和 Consumer 称为 Eureka Client。
    5. Consumer 服务 通过从 Eureka Server 中抓取 Provider 地址 完成 远程调用

    1.3搭建

    1.3.1 搭建 Provider  和 Consumer 服务。

    (1.创建一个空项目做父项目——>修改maven仓库为自己的仓库——>删除src文件(用不到)——>用maven新建两个模块分别是consumer和provider

    示例:

     

     (2. 在父亲项目中引入springboot环境和导入字体编码依赖

    1. <parent>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-parentartifactId>
    4. <version>2.1.0.RELEASEversion>
    5. <relativePath/>
    6. parent>
    7. <properties>
    8. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    9. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    10. <java.version>1.8java.version>
    11. properties>

    (3.分别在consumer和provider中引入web依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. dependency>
    6. dependencies>

    4.编写启动类

    1. @SpringBootApplication
    2. public class ProviderApp {
    3. public static void main(String[] args){
    4. SpringApplication.run(ProviderApp.class,args);
    5. }
    6. }

    (5.完成provider

    1. @RestController
    2. @RequestMapping("/goods")
    3. public class GoodsController {
    4. @Autowired
    5. private GoodsService goodsService;
    6. @GetMapping("/findOne/{id}")
    7. public Goods findOne(@PathVariable("id") int id){
    8. Goods goods = goodsService.findOne(id);
    9. return goods;
    10. }
    11. }
    1. @Repository
    2. public class GoodsDao {
    3. public Goods findOne(int id){
    4. return new Goods(1,"华为手机",3999,10000);
    5. }
    6. }
    1. @Service
    2. public class GoodsService {
    3. @Autowired
    4. private GoodsDao goodsDao;
    5. public Goods findOne(int id){
    6. return goodsDao.findOne(id);
    7. }
    8. }
    1. package com.gao.domain;
    2. public class Goods {
    3. private int id;
    4. private String title;
    5. private double price;
    6. private int count;
    7. public Goods(int id, String title, double price, int count) {
    8. this.id = id;
    9. this.title = title;
    10. this.price = price;
    11. this.count = count;
    12. }
    13. public int getId() {
    14. return id;
    15. }
    16. public void setId(int id) {
    17. this.id = id;
    18. }
    19. public String getTitle() {
    20. return title;
    21. }
    22. public void setTitle(String title) {
    23. this.title = title;
    24. }
    25. public double getPrice() {
    26. return price;
    27. }
    28. public void setPrice(double price) {
    29. this.price = price;
    30. }
    31. public int getCount() {
    32. return count;
    33. }
    34. public void setCount(int count) {
    35. this.count = count;
    36. }
    37. @Override
    38. public String toString() {
    39. return "Goods{" +
    40. "id=" + id +
    41. ", title='" + title + '\'' +
    42. ", price=" + price +
    43. ", count=" + count +
    44. '}';
    45. }
    46. }
    1. server:
    2. port: 8000

    (6.完成consumer

    1. import com.gao.domain.Goods;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. //服务的调用方
    7. @RestController
    8. @RequestMapping("/order")
    9. public class OrderController {
    10. @GetMapping("/goods/{id}")
    11. public Goods findGoodsById(@PathVariable("id") int id){
    12. System.out.println("findGoodsById"+id);
    13. //远程调用Goods服务中的findOne接口
    14. return null;
    15. }
    16. }
    1. server:
    2. port: 9000

    1.3.2 使用 RestTemplate 完成远程调用

    • Spring提供的一种简单便捷的模板类,用于在java代码里访问restful服务
    • 其功能与HttpClient类似,但是Restemplate实现更优雅,使用更方便

     

     

    (1. 定义Bean restTemplate

    1. import org.springframework.context.annotation.Bean;
    2. import org.springframework.context.annotation.Configuration;
    3. import org.springframework.web.client.RestTemplate;
    4. @Configuration
    5. public class RestTemplateConfig {
    6. @Bean
    7. public RestTemplate restTemplate(){
    8. return new RestTemplate();
    9. }
    10. }

    (2.注入Bean

    1. @Autowired
    2. private RestTemplate restTemplate;

    (3.调用方法

    1. String url = "http://localhost:8000/goods/findOne/"+id;
    2. //3.调用方法
    3. Goods goods = restTemplate.getForObject(url,Goods.class);
    4. return goods;

    注意:Goods里需要有空参构造

    通过访问consumer的路径,可以访问到provider即可说明成功。

     

    网络三要素:协议 IP 端口

  • 相关阅读:
    Linux 图形界面配置RAID
    资源预测数字模型搭建思路分享
    云计算技术 — 混合云 — 技术架构
    用ChatGPT编写一个词卡显示网页
    【计算机网络篇】数据链路层(13)共享式以太网与交换式以太网的对比
    【Bond与你白话IaC之Terraform for Docker篇】 攻城狮如何向女友解释IaC呢?
    Ununtu服务器安装Nginx与PHP
    (附源码)springboot篮球场地预约系统 毕业设计 345655
    推荐一款国内首个开源全链路压测平台
    mybatis date类型比较
  • 原文地址:https://blog.csdn.net/qq_51497041/article/details/126048352