SpringCloud代码实战

news2024/9/24 11:30:34

项目结构

实例实现功能:实现通过id查询用户的订单信息

OrderCommon:公共的一些模块类型,此处为一个user对象 

Eureka-Service:配置Eureka的启动类,服务端

Order-Service:提供查询功能的服务端

Order-Client:查询的客户端

OrderCommon代码

package org.example.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @ClassName Order
 * @Author 23
 * @Date 2024/7/10 18:17
 * @Version 1.0
 * @Description TODO
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
    //定义一个信息类
    private Integer id;
    private String username;
    private Integer userid;
    private String orderinformation;
}

Eureka-Service代码

package org.example;

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

/**
 * @ClassName EurekaStart
 * @Author 23
 * @Date 2024/7/10 19:12
 * @Version 1.0
 * @Description TODO
 **/
@SpringBootApplication
@EnableEurekaServer
public class EurekaStart {
    public static void main(String[] args) {
        SpringApplication.run(EurekaStart.class,args);
    }
}

yml配置:

server:
  port: 10077
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: yihttp://${eureka.instance.hostname}:${server.port}/eureka/

 pom.xml配置

<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>
        <groupId>org.example</groupId>
        <artifactId>Springcloud-Netflix</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Eureka-Service</artifactId>
    <packaging>jar</packaging>

    <name>Eureka-Service</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- Eureka服务端支持 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

Order-Service代码

controller

package org.example.Controller;

import org.example.domain.Order;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName OderController
 * @Author 23
 * @Date 2024/7/10 18:33
 * @Version 1.0
 * @Description TODO
 **/
@RestController
@RequestMapping("/OrderService")
public class OderController {
    @GetMapping("/user/{id}")
    public List<Order> getOrderByUserid(@PathVariable("id") Integer id){
        return Arrays.asList(new Order(1,"Jack",071021,"尊贵的vip用户"));
    }

}

 启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @ClassName OrderStart
 * @Author 23
 * @Date 2024/7/10 18:34
 * @Version 1.0
 * @Description TODO
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class OrderStart {
    public static void main(String[] args) {
        SpringApplication.run(OrderStart.class,args);
    }
}

yml配置

server:
  port: 10010
spring:
  application:
    name: order-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10077/eureka #告诉服务提供者要把服务注册到哪儿
  instance:
    prefer-ip-address: true # 当调用getHostname获取实例的hostname时,返回ip而不是host名称

pom.xml

<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>
        <groupId>org.example</groupId>
        <artifactId>Springcloud-Netflix</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Order-Service</artifactId>
    <packaging>jar</packaging>

    <name>Order-Service</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!--变成一个springboot项目-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!--继承一下common-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Order-Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

Order-Client代码

BeanConfig

package org.example.Config;

/**
 * @ClassName BeanConfig
 * @Author 23
 * @Date 2024/7/10 18:56
 * @Version 1.0
 * @Description TODO
 **/
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

//<beans>
@Configuration  //<beans><bean></bean</beans>
public class BeanConfig {

    @Bean  //<bean></bean  把new出来的对象放入spring管理
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

UserController

package org.example.Controller;

import org.example.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
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 java.util.Arrays;
import java.util.List;

/**
 * @ClassName UserController
 * @Author 23
 * @Date 2024/7/10 18:52
 * @Version 1.0
 * @Description TODOj
 **/
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/getOrder/{id}")
    public List<Order> getOrderById(@PathVariable("id") Integer id) {
//        Order[] order = restTemplate.getForObject("http://localhost:10010/OrderService/user/" + id, Order[].class);
//        return Arrays.asList(order);
        List<ServiceInstance> instances=discoveryClient.getInstances("order-service");//通过服务的名字去获取服务实例
        ServiceInstance serviceInstance=instances.get(0);//定死只获取第一个服务实例对象
        String ip=serviceInstance.getHost();//获取服务对象ip
        int port=serviceInstance.getPort();//获取获取的实例对象的服务端口号
        Order[] orders=restTemplate.getForObject("http://"+ip+":"+port+"/OrderService/user/"+id,Order[].class);
        return Arrays.asList(orders);

    }
}

User启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @ClassName ClientStart
 * @Author 23
 * @Date 2024/7/10 18:52
 * @Version 1.0
 * @Description TODO
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class UserStart {
    public static void main(String[] args) {
        SpringApplication.run(UserStart.class,args);
    }
}

yml配置

server:
  port: 10020
spring:
  application:
    name: user-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10077/eureka
  instance:
    prefer-ip-address: true

pom.xml配置

<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>
        <groupId>org.example</groupId>
        <artifactId>Springcloud-Netflix</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Order-Client</artifactId>
    <packaging>jar</packaging>

    <name>Order-Client</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Order-Common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1915239.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++类与对象-基础篇

目录 一、什么是类 1.1 语法定义 1.2 访问限定符 1.3 类域 二、类的实例化 2.1 什么是实例化 2.2 类的大小 三、this指针 3.1 引入 3.2 this指针的使用 一、什么是类 1.1 语法定义 class 类名 {}; 说明 类似于C语言中的结构体&#xff0c;括号后分号不能丢类内成员可…

类型“RouteRecordName”上不存在属性“includes”。 类型“symbol”上不存在属性“includes”

确定 route.name 运行时是 字符串&#xff0c;强制转换 为字符串。 removeRoute(id: string) { this.dynamRoute this.dynamRoute.filter(route > !(route.name as string).includes(id)) localStorage.setItem(dynamRoute, JSON.stringify(this.dynamRoute)) delete this.t…

[leetcode] shortest-subarray-with-sum-at-least-k 和至少为 K 的最短子数组

. - 力扣&#xff08;LeetCode&#xff09; class Solution { public:int shortestSubarray(vector<int>& nums, int k) {int n nums.size();vector<long> preSumArr(n 1);for (int i 0; i < n; i) {preSumArr[i 1] preSumArr[i] nums[i];}int res n…

【服务器】在Linux查看运行的Python程序,并找到特定的Python程序

在Linux查看运行的Python程序并找到特定的Python程序 写在最前面1. 使用ps命令查看所有Python进程查看详细信息 2. 使用pgrep命令查找Python进程ID 3. 使用top或htop命令使用top命令使用htop命令 4. 使用lsof命令查找Python进程打开的文件 5. 使用nvidia-smi命令查看GPU使用情况…

九、函数递归

——————————————————————————————————————————— 目录 1、递归是什么&#xff1f; 1.1、递归的思想 1.2、递归的限制条件 2、递归举例 2.1、举例1&#xff1a;求n的阶乘 2.1.1、分析和代码实现 2.2.1分析和代码实现 3、递归与…

百度智能云将大模型引入网络故障定位的智能运维实践

物理网络中&#xff0c;某个设备发生故障&#xff0c;可能会引起一系列指标异常的告警。如何在短时间内从这些告警信息中找到真正的故障原因&#xff0c;犹如大海捞针&#xff0c;对于运维团队是一件很有挑战的事情。 在长期的物理网络运维工作建设中&#xff0c;百度智能云通…

文献阅读(1)——深度强化学习求解车辆路径问题的研究综述

doi&#xff1a; 10.3778/j.issn.1002-8331.2210-0153 深度强化学习求解车辆路径问题的研究综述 (ceaj.org) 组合最优化问题&#xff08; combinatorial optimization problem&#xff0c; COP &#xff09; 日常生活中常见的 COP 问题有旅行商问题&#xff08;traveling sale…

碾压SOTA!最新视觉SLAM:渲染速度提升176倍,内存占用减少150%

视觉SLAM&#xff0c;一种结合了CV与机器人技术的先进方法。与激光SLAM相比&#xff0c;它成本低廉且信息量大&#xff0c;易于安装&#xff0c;拥有更优秀的场景识别能力&#xff0c;因此在自动驾驶等许多场景上都非常适用&#xff0c;是学术界与工业界共同关注的热门研究方向…

【从零开始实现stm32无刷电机FOC】【理论】【3/6 位置、速度、电流控制】

目录 PID控制滤波单独位置控制单独速度控制单独电流控制位置-速度-电流串级控制 上一节&#xff0c;通过对SVPWM的推导&#xff0c;我们获得了控制电机转子任意受力的能力。本节&#xff0c;我们选用上节得到的转子dq轴解耦的SVPWM形式&#xff0c;对转子受力进行合理控制&…

C++——map和set类用法指南

一、前言 1.1 关联式容器 关联式容器也是用来存储数据的&#xff0c;与序列式容器不同的是&#xff0c;其里面存储的是<key,value>结构的键值对&#xff0c;在数据检索时比序列式容器效率更高。 1.2 键值对 用来表示具有一一对应关系的一种结构&#xff0c;该结构中一般…

ARM功耗管理标准接口之PSCI

安全之安全(security)博客目录导读 思考&#xff1a;功耗管理有哪些标准接口&#xff1f;ACPI&PSCI&SCMI&#xff1f; Advanced Configuration and Power Interface Power State Coordination Interface System Control and Management Interface ARM V8架构的软件分…

组件设计原则和度量方法

在日常开发过程中&#xff0c;Spring、Dubbo、Mybatis等都是我们常用的开源框架。当你在使用这些框架时&#xff0c;不可避免需要通过分析源码来理解内部的实现原理。那么&#xff0c;你在翻阅源代码时&#xff0c;有没有想过这些框架的代码结构为什么要这样进行设计和实现呢&a…

自学鸿蒙HarmonyOS的ArkTS语言<五>attributeModifier动态属性和用attributeModifier封装公共组件

【官方文档传送门】 一、抽取组件样式 class MyModifier implements AttributeModifier<ButtonAttribute> {applyNormalAttribute(instance: ButtonAttribute): void {instance.backgroundColor(Color.Black)instance.width(200)instance.height(50)instance.margin(10…

2008年上半年软件设计师【下午题】真题及答案

文章目录 2008年上半年软件设计师下午题--真题2008年上半年软件设计师下午题--答案 2008年上半年软件设计师下午题–真题 2008年上半年软件设计师下午题–答案

数字滚动动画~

前言 数字从0.00滚动到某个数值的动画 实现&#xff08;React版本&#xff09; Dom <div className"number" ref{numberRef}>0.00</div> JS const _initNumber () > {const targetNumber 15454547.69;const duration 1500;const numberElement…

[leetcode]subarray-product-less-than-k 乘积小于K的子数组

. - 力扣&#xff08;LeetCode&#xff09; class Solution { public:int numSubarrayProductLessThanK(vector<int>& nums, int k) {if (k 0) {return 0;}int n nums.size();vector<double> logPrefix(n 1);for (int i 0; i < n; i) {logPrefix[i 1] …

E. Beautiful Array(cf954div3)

题意&#xff1a;给定一个数组&#xff0c;可以先对数组进行任意排序&#xff0c;每次操作可以选择一个ai&#xff0c;将它变成aik&#xff0c; 想让这个数组变成一个美丽数组&#xff08;回文数组&#xff09;&#xff0c;求最少操作次数 分析&#xff1a; 先找出相同的数字…

Android liveData 监听异常,fragment可见时才收到回调记录

背景&#xff1a;在app的fragment不可见的情况下使用&#xff0c;发现注册了&#xff0c;但是没有回调导致数据一直未更新&#xff0c;只有在fragment可见的时候才收到回调 // 观察通用信息mLightNaviTopViewModel.getUpdateCommonInfo().observe(this, new Observer<Common…

常用的JVM启动参数

JVM的启动参数有很多&#xff0c;但是我们平常能用上的并不是特别多&#xff0c;这里介绍几个我们常用的&#xff1a; 1. 堆设置&#xff1a; 。 -Xms&#xff1a;设置堆的初始大小。 。.-Xmx&#xff1a;设置堆的最大大小。 2. 栈设置&#xff1a; 。 -XsS&#xff1a;设置每个…

国产大模型第一梯队玩家,为什么pick了CPU?

AI一天&#xff0c;人间一年。 现在不论是大模型本身&#xff0c;亦或是AI应用的更新速度简直令人直呼跟不上—— Sora、Suno、Udio、Luma……重磅应用一个接一个问世。 也正如来自InfoQ的调查数据显示的那般&#xff0c;虽然AIGC目前还处于起步阶段&#xff0c;但市场规模已…