6.1_springboot2.x分布式-整合SpringCloud

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/jatpen/article/details/102691449

1、SpringCloud简介

Spring Cloud是一个分布式的整体解决方案。Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,leader、选举,分布式、session,集群状态)中快速构建的工具,使用Spring Cloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。

•SpringCloud分布式开发五大常用组件

•服务发现——Netflix Eureka

•客服端负载均衡——Netflix Ribbon

•断路器——Netflix Hystrix

•服务网关——Netflix Zuul

•分布式配置——Spring Cloud Config

1、创建provider

在这里插入图片描述
在这里插入图片描述

application.yml

server:
  port: 8002
spring:
  application:
    name: provider-ticket

eureka:
  instance:
    prefer-ip-address: true #注册服务的时候使用服务的注册地址
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/


controller

package com.example.providerticket.controller;

import com.example.providerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {

    @Autowired
    TicketService ticketService;

    @GetMapping("/ticket")
    public String getTicket(){

        return ticketService.getTicket();
    }

}

service

package com.example.providerticket.service;

import org.springframework.stereotype.Service;

@Service
public class TicketService {

    public String getTicket(){
        System.out.println("8002");

        return "《海贼王》";
    }
}

2、创建Eureka注册中心

在这里插入图片描述

application.yml配置

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server #eureka实例的主机名
  client:
    register-with-eureka: false  #不把自己注册到euraka
    fetch-registry: false  #不从euraka上获取服务的注册中心
    service-url:
      defaultZone: http://localhost:8761/eureka/

启用注册信息功能:

/**
 * 注册中心
 * 1、配置eureka信息
 * 2、@EnableEurekaServer启用注册信息功能
 * */

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

注意启动顺序:先启动注册中心,后启动provider

发送请求/ticket

在这里插入图片描述

可见:http://localhost:8761/这里可以在注册中心注册多个provider:

分别将provider以8001,8002端口进行打包,然后运行java -jar

在这里插入图片描述

3、创建consumer

创建过程与provider一样,

1、application.yml

spring:
  application:
    name: consumer-user
server:
  port: 8200

eureka:
  instance:
    prefer-ip-address: true #注册服务的时候使用服务的注册地址
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2、controller

@RestController
public class UserController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/buy")
    public String buyTicket(String name){
        String  s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
        return name+"购买了"+s;
    }
}

3、启动consumer

package com.example.consumeruser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;

@EnableDiscoveryClient//开启发现服务功能
@SpringBootApplication
public class ConsumerUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerUserApplication.class, args);
    }

    @LoadBalanced//使用负载均衡机制
    @Bean
    public RestTemplate restTemplate(){

        return new RestTemplate();
    }
}

RestTemplate可以帮助发送http请求@LoadBalanced//使用负载均衡机制

启动发现在注册中心已注册

在这里插入图片描述

4、进行测试

发送/buy请求

在这里插入图片描述
测试完成,这里结构如图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jatpen/article/details/102691449