二、Eureka注册中心

Eureka注册中心服务端

引入依赖

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

编写启动类@EnableEurekaServer

复制代码
package com.gwf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @Author gwf
 * @Data 2024/2/17 下午5:24
 **/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

编写配置文件

复制代码
# 访问端口
server:
  port: 10086

#项目名
spring:
  application:
    name: eureka-server

# eureka-service配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka/

Eureka服务注册

引入依赖

复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>4.0.2</version>
</dependency>

配置

userservice
复制代码
server:
  port: 9000

spring:
  application:
    name: userservice

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
orderservice
复制代码
server:
    port: 9000

spring:
    application:
        name: orderservice
eureka:
    client:
        service-url:
            defaultZone: http://127.0.0.1:10086/eureka

多次启动userService

复制服务修改端口启动

远程调用

给RestTemplate这个Bean添加一个@LoadBalanced注解

复制代码
package com.gwf.orderservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

修改调用

复制代码
package com.gwf.orderservice;

import com.gwf.orderservice.entity.Order;
import com.gwf.orderservice.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @Author gwf
 * @Data 2024/2/17 下午3:39
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId")Long orderId){
        Order order = new Order();
        order.setId(orderId);
        order.setUserId(1L);
        order.setName("手机");
        order.setNum(2);
        order.setPrice(1999L);

        String url ="http://userservice:8081/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        order.setUser(user);
        return order;
    }
}
相关推荐
人生匆匆6 小时前
k8s通过域名访问 StatefulSet的pod
云原生·容器·kubernetes
寂寞旅行7 小时前
k8s实现多人同时使用pod
云原生·容器·kubernetes
数据库知识分享者小北9 小时前
免费体验《自建 MySQL 迁移至 PolarDB 分布式 V2.0》
数据库·分布式·mysql·阿里云·云原生·polardb
阿里云云原生12 小时前
AI 网关这一年,成了 AI 进化的缩影
云原生
2501_9418072613 小时前
从单机限流到分布式动态流控体系落地的互联网系统工程实践随笔与多语言语法思考
eureka·etcd
刘一说13 小时前
2026年Java技术栈全景图:从Web容器到云原生的深度选型指南(附避坑指南)
java·前端·spring boot·后端·云原生·tomcat·mybatis
阿里云云原生13 小时前
AI 原生应用开源开发者沙龙·广州站精彩回顾 & PPT 下载
云原生
2501_9412256814 小时前
面向微服务分布式事务与最终一致性的互联网系统高可用设计与多语言工程实践分享
eureka·flask
虫小宝15 小时前
导购返利APP服务网格实践:基于Istio的微服务流量管理与监控
微服务·云原生·istio
阿里云云原生15 小时前
应对 Nginx Ingress 退役,是时候理清这些易混淆的概念了
云原生