Spring Cloud入门系列(三)——服务注册与发现之Eureka(已停更,建议切换到Nacos)

news/2024/7/24 4:06:45 标签: spring, eureka, java

新增服务消费者80

在这里插入图片描述
ApplicationContextConfig

java">package com.banana.springcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author layman
 * @description: 配置类
 * @date 2021/1/9
 */
@Configuration
public class ApplicationContextConfig {
    /**
     * Configuration注解:定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,
     *      这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,
     *      并用于构建bean定义,初始化Spring容器
     * LoadBalanced注解:可以使RestTemplate在请求时拥有客户端负载均衡的能力,只有加上,才能通过微服务名称调用服务
     * @return RestTemplate :rest微服务请求的模板类
     */
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }
}

OrderController

java">package com.banana.springcloud.controller;

import com.banana.springcloud.entity.CommonResult;
import com.banana.springcloud.entity.Payment;
import com.banana.springcloud.loadbalancer.MyLoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @author layman
 * @description: 消费者controller
 * @date 2021/1/9
 */
@RestController
@Slf4j
@RequestMapping("/consumer/payment")
public class OrderController {

    //服务调用地址
    private static final String PAYMENT_URL = "http://cloud-payment-service";
    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }
    @GetMapping("/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }

OrderMain80

java">package com.banana.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

/**
 * @author layman
 * @description: TODO
 * @date 2021/1/9
 */
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
    public static void main(String[] args) {
            SpringApplication.run(OrderMain80.class,args);
        }
}

appliction.yml

server:
  port: 80

eureka:
  client:
    #是否将自己注册到注册中心, 默认true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,单机无所谓,集群必须设置为true配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka  #单机版(将payment8001注册到7001)
    # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  #集群版

spring:
  application:
    name: cloud-order-service

启动服务消费者80

刷新Eureka服务端。
在这里插入图片描述
可以看到 cloud-order-service 成功注册到7001 Eureka服务端。

Eureka集群

在这里插入图片描述
集群配置:略(无非就是你注册我,我注册你,非常简单。)


http://www.niftyadmin.cn/n/902010.html

相关文章

IDEA 创建类注释模板

一、引言 在写Java代码过程中&#xff0c;当我们创建类的时候往往需要在类上写一些注释信息&#xff0c;而这些注释信息就主要是几个注释点&#xff0c;如果全部都手写就比较浪费时间了&#xff0c;这个时间后我们就可以通过使用注释模板添加自己的自定义的注释在类或者方法上。…

Spring Cloud入门系列(四)——服务注册与发现之Eureka(已停更,建议切换到Nacos)

服务集群 新建相同的模块&#xff0c;更改端口&#xff0c;即可&#xff08;略&#xff09; 服务发现 以 8001为例&#xff1a; DiscoveryClient就是服务发现。 8001的PaymentController的代码如下&#xff1a; package com.banana.springcloud.controller;import com.ban…

Spring Cloud入门系列(五)——服务注册与发现之Consul(已停更,建议切换到Nacos)

Consul是什么 Consul能干什么 如何使用 官网下载链接&#xff1a;https://www.consul.io/downloads 官方使用文档&#xff1a;https://docs.spring.io/spring-cloud-consul/docs/2.2.7.RELEASE/reference/html/ 默认端口是 8500 Eureka&#xff0c;Zookeeper&#xff0c;C…

IDEA 常用快捷键介绍

一、引言 在使用IDEA写代码的时候&#xff0c;如果充分利用Intellij IDEA的快捷键&#xff0c;可以加快开发进程&#xff0c;减少重复的操作。 二、常用快捷键 (一)、"提示"常用快捷键 Ctrl空格:代码提示 CtrlAltSpace: 类名或接口名提示 CtrlP: 使用方法参数提示&…

windows 安装Nacos步骤

一、Nacos中文文档网址 Nacos中文文档网址&#xff1a;Nacos 快速开始https://nacos.io/zh-cn/docs/quick-start.html 二、参考 1、去到Nacos的github地址&#xff1a;https://github.com/alibaba/nacos下载nacos的window压缩包。 ①&#xff1a;去到Nacos的github地址页面&am…

Spring Cloud入门系列(六)——负载均衡之Ribbon

概述 负载均衡 集中式LB 进程内LB 架构说明 其实ribbon已经被Eureka整合进去了。 RestTemplate Ribbon的负载策略 IRule是Ribbon实现负载均衡的顶层接口。 自定义策略 在80端口&#xff0c;新增com.banana.rule.MyRule文件 MyRule package com.banana.rule;import co…

JAVA操作Excel-EasyExcel(一)

一、引言 在写Java代码的时候&#xff0c;项目常常需要Java解析、生成Excel文件&#xff0c;而目前比较好的操作Excel库主要是阿里巴巴开源的EasyExcel&#xff0c;其 官网地址是&#xff1a;https://www.yuque.com/easyexcel/doc/easyexcel&#xff0c; github地址是:https:…

Spring Cloud入门系列(七)- 负载均衡之Open Feign

OpenFeign是什么 OpenFeign能做什么 OpenFeign和Feign的区别 如何使用 代码演示 定义新的模块 cloud-consumer-openfeign-order80 pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0&q…