一个简单的springcloud案例

使用的组件:Eureka、Ribbon、Feign、Hystrix

首先创建一个maven父工程,并提供pom

在 这个pom中指定了springcloud版本以及springboot的版本

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cloud-test</groupId><artifactId>cloud-test</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><properties><java.version>1.8</java.version></properties><!-- 声明项目的父项目 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version></parent><!-- 添加依赖管理 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.M9</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><modules><module>ek-service</module><module>service-provider</module><module>service-invoker</module><module>ribbon-test</module><module>feign-client</module><module>hystrix</module></modules>
</project>

创建Eureka工程

pom

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cloud-test</groupId><artifactId>cloud-test</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>ek-service</artifactId><dependencies><!-- 引用Eureka Server依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>

yml配置文件

server:port: 8761
eureka:client:registerWithEureka: falsefetchRegistry: false

启动类

package org.crazyit.cloud;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class FirstServer {public static void main(String[] args) {new SpringApplicationBuilder(FirstServer.class).run(args);}
}

创建服务提供者工程

pom

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cloud-test</groupId><artifactId>cloud-test</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>service-provider</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies></project>

yml配置文件

spring:application:name: first-service-provider
eureka:instance:hostname: localhostclient:serviceUrl:defaultZone: http://localhost:8761/eureka/

启动类

package org.crazyit.cloud;import java.util.Scanner;import javax.servlet.http.HttpServletRequest;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@SpringBootApplication
@EnableEurekaClient
@RestController
public class FirstServiceProvider {public static void main(String[] args) {
//        new SpringApplicationBuilder(FirstServiceProvider.class).run(args);// 读取控制台输入作为端口参数Scanner scan = new Scanner(System.in);String port = scan.nextLine();// 设置启动的服务器端口new SpringApplicationBuilder(FirstServiceProvider.class).properties("server.port=" + port).run(args);}@GetMapping(value = "/person/{personId}", produces = MediaType.APPLICATION_JSON_VALUE)public Person findPerson(@PathVariable("personId") Integer personId,HttpServletRequest request) {Person person = new Person(personId, "Crazyit", 30);person.setMessage(request.getRequestURL().toString());return person;}
}
package org.crazyit.cloud;public class Person {private Integer id;private String name;private Integer age;private String message;public Person() {super();}public Person(Integer id, String name, Integer age) {super();this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}}

创建服务消费者工程

pom

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>cloud-test</groupId><artifactId>cloud-test</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>service-invoker</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency></dependencies></project>

yml配置文件

server:port: 9001
spring:application:name: first-service-invoker
eureka:instance:hostname: localhostclient:serviceUrl:defaultZone: http://localhost:8761/eureka/
feign:hystrix:enabled: true

启动类

package org.crazyit.cloud;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
@EnableCircuitBreaker
public class FirstInvoker {public static void main(String[] args) {SpringApplication.run(FirstInvoker.class, args);}@Bean@LoadBalancedpublic RestTemplate getRestTemplate() {return new RestTemplate();}@GetMapping(value = "/router", produces = MediaType.APPLICATION_JSON_VALUE)public String router() {RestTemplate restTpl = getRestTemplate();// 根据应用名称调用服务String json = restTpl.getForObject("http://first-service-provider/person/1", String.class);return json;}@AutowiredPersonClient personClient;@GetMapping(value = "/router2", produces = MediaType.APPLICATION_JSON_VALUE)public Person router2() {return personClient.findById(1);}
}
package org.crazyit.cloud;import org.crazyit.cloud.PersonClient.PersonClientFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;//声明调用的服务名称
@FeignClient(name ="first-service-provider",fallback = PersonClientFallback.class)
public interface PersonClient {@RequestMapping(method = RequestMethod.GET, value = "/person/{personId}")Person findById(@PathVariable("personId") Integer personId);@Componentstatic class PersonClientFallback implements PersonClient {@Overridepublic Person findById(Integer personId) {// 回退方法System.out.println("执行 findByid 的回退方法,返回一个预先设定好的 Person");Person p = new Person(-1, "模拟Person", 0);return p;}}
}
package org.crazyit.cloud;public class Person {private Integer id;private String name;private Integer age;public Person() {super();}public Person(Integer id, String name, Integer age) {super();this.id = id;this.name = name;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}

RibbonTest

<dependencies><dependency><groupId>com.netflix.ribbon</groupId><artifactId>ribbon-loadbalancer</artifactId><version>2.2.5</version></dependency><dependency><groupId>com.netflix.ribbon</groupId><artifactId>ribbon-core</artifactId><version>2.2.5</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency></dependencies>
public class ChoseServerTest {public static void main(String[] args) {// 创建负载均衡器BaseLoadBalancer lb = new BaseLoadBalancer();// 添加服务器List<Server> servers = new ArrayList<Server>();servers.add(new Server("localhost", 8080));servers.add(new Server("localhost", 8081));lb.addServers(servers);// 进行6次服务器选择for(int i = 0; i < 6; i++) {Server s = lb.chooseServer(null);System.out.println(s);}}
}

FeignTest

 <dependencies><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-core</artifactId><version>9.5.1</version></dependency><dependency><groupId>io.github.openfeign</groupId><artifactId>feign-gson</artifactId><version>9.5.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.18</version></dependency></dependencies>
import lombok.Data;
import feign.Param;
import feign.RequestLine;/*** Person客户端服务类*/
public interface PersonClient {@RequestLine("GET /person/{personId}")Person findById(@Param("personId") Integer personId);// 为所有属性加上setter和getter等方法@Dataclass Person {Integer id;String name;Integer age;String message;}
}
import org.crazyit.cloud.PersonClient.Person;import feign.Feign;
import feign.gson.GsonDecoder;/*** Person服务的运行主类* **/
public class PersonMain {public static void main(String[] args) {PersonClient personClient = Feign.builder().decoder(new GsonDecoder()).target(PersonClient.class, "http://localhost:8081/");// 调用/person/{personId}服务Person person = personClient.findById(2);System.out.println(person.id + "---" + person.name + "---" + person.age+"---"+person.getMessage());}
}

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

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

相关文章

[蓝桥杯2024]-PWN:fd解析(命令符转义,标准输出重定向)

查看保护 查看ida 这里有一次栈溢出&#xff0c;并且题目给了我们system函数。 这里的知识点没有那么复杂 完整exp&#xff1a; from pwn import* pprocess(./pwn) pop_rdi0x400933 info0x601090 system0x400778payloadb"ca\\t flag 1>&2" print(len(paylo…

消息服务应用1——java项目使用websocket

在当前微服务项目中&#xff0c;由于业务模块众多&#xff0c;消息服务的使用场景变得异常活跃。而WebSocket由于其自身的可靠性强&#xff0c;实时性好&#xff0c;带宽占用更小的优势&#xff0c;在实时通讯应用场景中独占鳌头&#xff0c;加上HTML5标准的普及流行&#xff0…

分类神经网络3:DenseNet模型复现

目录 DenseNet网络架构 DenseNet部分实现代码 DenseNet网络架构 论文原址&#xff1a;https://arxiv.org/pdf/1608.06993.pdf 稠密连接神经网络&#xff08;DenseNet&#xff09;实质上是ResNet的进阶模型&#xff08;了解ResNet模型请点击&#xff09;&#xff0c;二者均是…

Java面试八股文-2024

面试指南 TMD&#xff0c;一个后端为什么要了解那么多的知识&#xff0c;真是服了。啥啥都得了解 MySQL MySQL索引可能在以下几种情况下失效&#xff1a; 不遵循最左匹配原则&#xff1a;在联合索引中&#xff0c;如果没有使用索引的最左前缀&#xff0c;即查询条件中没有包含…

Altera FPGA 配置flash读写

目录 一、读写控制器的配置 二、生成flash的配置文件 三、关于三种配置文件的大小 四、其他 一、读写控制器的配置 Altera ASMI Parallel&#xff08;下文简称ASMI)这个IP就仅仅是个Flash读写控制器&#xff0c;可以自由的设计数据来源。 关于这个IP的使用&#xff0c;可以…

MAC有没有免费NTFS tuxera激活码 tuxera破解 tuxera for mac2023序列号直装版 ntfs formac教程

Tuxera NTFS 2023破解版是一款非常好用的在线磁盘读写工具&#xff0c;该软件允许mac用户在Windows NTFS格式的硬盘上进行读写操作&#xff0c;Mac的文件系统是HFS&#xff0c;而Windows则使用NTFS格式&#xff0c;这导致在Mac系统上不能直接读写Windows格式的硬盘。然而&#…

程序员:写好代码就行了,为什么要学写作

&#x1f341; 展望&#xff1a;关注我, AI 学习之旅上&#xff0c;我与您一同成长&#xff01; 一、引言 在当今这个信息爆炸的时代&#xff0c;程序员们往往沉浸在代码的世界里&#xff0c;用代码来解决问题。然而&#xff0c;你是否曾想过&#xff0c;除了代码&#xff0c;…

INSTEAD OF 触发器的创建

Oracle从入门到总裁:​​​​​​https://blog.csdn.net/weixin_67859959/article/details/135209645 INSTEAD OF 触发器&#xff0c;也称替换触发器&#xff0c;是一种特殊的触发器&#xff0c;和其他建立在数据表上的触发器不同&#xff0c;INSTEAD OF 触发器建立在视图上。…

Podman入门全指南:安装、配置与运行容器

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 Podman入门全指南&#xff1a;安装、配置与运行容器 前言Podman简介什么是 Podman&#xff1f;Podman 与 Docker 的主要区别 安装Podman支持的操作系统和环境安装步骤详解LinuxUbuntuCentOS/RHEL MacO…

双系统下删除ubuntu

絮絮叨叨 由于我在安装Ubuntu的时候没有自定义安装位置&#xff0c;而是使用与window共存的方式让Ubuntu自己选择安装位置&#xff0c;导致卸载时我不知道去格式化哪个分区&#xff0c;查阅多方资料后无果&#xff0c;后在大佬帮助下找到解决方案 解决步骤 1、 插上Ubuntu安…

Axure如何调起浏览器的打印功能

Axure如何调起浏览器的打印功能 答&#xff1a;javascript:window.print(); 不明白的继续往下看 应用场景&#xff1a; 原型设计中&#xff0c;页面上的打印按钮&#xff0c;需要模拟操作演示&#xff0c;需要点击指定的按钮时&#xff0c;唤起浏览器的打印功能&#xff08…

使用Pandas从Excel文件中提取满足条件的数据并生成新的文件

目录 一、引言 二、环境准备 三、读取Excel文件 四、数据筛选 五、保存为新的Excel文件 六、案例与代码总结 七、进阶用法与注意事项 八、结语 在数据处理的日常工作中&#xff0c;我们经常需要从大量数据中筛选出满足特定条件的数据集。Pandas是一个强大的Python数据分…

比 PSD.js 更强的下一代 PSD 解析器,支持 WebAssembly

比 PSD.js 更强的下一代 PSD 解析器&#xff0c;支持 WebAssembly 1.什么是 webtoon/ps webtoon/ps 是 Typescript 中轻量级 Adobe Photoshop .psd/.psb 文件解析器&#xff0c;对 Web 浏览器和 NodeJS 环境提供支持&#xff0c;且做到零依赖。 Fast zero-dependency PSD par…

2024 年最好的免费数据恢复软件,您可以尝试的几个数据恢复软件

由于系统崩溃而丢失数据可能会给用户带来麻烦。我们将重要的宝贵数据和个人数据保存在我们的 PC、笔记本电脑和其他数字设备上。您可能会因分区丢失、意外删除文件和文件夹、格式化硬盘驱动器而丢失数据。数据丢失是不幸的&#xff0c;如果您不小心从系统中删除了文件或数据&am…

深入理解 Srping IOC

什么是 Spring IOC&#xff1f; IOC 全称&#xff1a;Inversion of Control&#xff0c;翻译为中文就是控制反转&#xff0c;IOC 是一种设计思想&#xff0c;IOC 容器是 Spring 框架的核心&#xff0c;它通过控制和管理对象之间的依赖关系来实现依赖注入&#xff08;Dependenc…

正点原子[第二期]ARM(I.MX6U)裸机篇学习笔记-1.2

前言&#xff1a; 本文是来自哔哩哔哩网站上视频“正点原子[第二期]Linux之ARM&#xff08;MX6U&#xff09;裸机篇”的学习笔记&#xff0c;在这里会记录下正点原子Linux ARM MX6ULL 开发板根据配套的哔哩哔哩学习视频所作的实验和笔记内容。本文大量的引用了正点原子哔哔哩网…

结构体内存对齐(未完成版)

前言 我们已经掌握了结构体的基本使用了。 现在我们深入讨论一个问题&#xff1a;计算机构体的大小。 这也是一个特别热门的考点&#xff1a;结构体内存对齐 练习导入 对齐规则

vue项目npm run build 打包之后如何在本地访问

vue项目npm run build 打包之后如何在本地访问 如果直接访问时&#xff0c;则会报错如下的信息&#xff1a; 报错码&#xff1a; Access to script at file:///D:/assets/index-DDVBfHVo.js from origin null has been blocked by CORS policy: Cross origin requests are on…

【转载】如何在MacBookPro上把Ubuntu安装到移动硬盘里过程记录

以下主要目的是记录安装过程中的问题&#xff0c;安装步骤等信息怕忘记 环境信息&#xff1a; Mac &#xff1a;macOS High Sierra 10.13.6 内存8G(Swap时用到) Ubuntu: ubuntu-22.04.4-desktop-amd64.ios 金士顿U盘&#xff1a;Kingston-64G 烧录软件&#xff1a;balenaEtcher…

牛客NC371 验证回文字符串(二)【简单 双指针 C++/Java/Go/PHP】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/130e1a9eb88942239b66e53ec6e53f51 思路 直接看答案&#xff0c;不难参考答案C class Solution {public:/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返回方法规定的值即可…