spring cloud微服务example 入门第一个例子

新建Maven工程

删除src目录,修改poml.xml

<modelVersion>4.0.0</modelVersion><groupId>org.example</groupId>
<artifactId>SpringCloud_example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version>
</parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.cloud-version>Hoxton.SR8</spring.cloud-version>
</properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.18</version><scope>provided</scope></dependency>
</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud-version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

Eureka注册中心

File -> New -> Module

添加依赖

<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
</dependencies>

启动类

package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}

配置文件application.yml

server:port: 9000spring:application:name: eureka-servereureka:instance:hostname: eureka9000.comclient:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: "https://${eureka.instance.hostname}:${server.port}/eureka/"

common微服务

File -> New -> Module

产品类

package org.example.common.entity;import lombok.Data;@Data
public class Product {private Long id;private String name;
}

修改父pom.xml

<dependencyManagement><dependencies><dependency><groupId>org.example</groupId><artifactId>common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</dependencyManagement>

产品微服务

File -> New -> Module

添加依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>common</artifactId></dependency>
</dependencies>

配置文件application.yml

server:port: 9010spring:application:name: product-serviceeureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: trueinstance-id: ${spring.application.name}:${server.port}lease-renewal-interval-in-seconds: 5lease-expiration-duration-in-seconds: 10

启动类

package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}
}

controller类

package org.example.controller;import org.example.common.entity.Product;
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;@RestController
@RequestMapping("/product")
public class ProductController {@GetMapping("/{id}")public Product findById(@PathVariable Long id) {Product product = new Product();product.setId(id);product.setName("product test");return product;}
}

订单微服务

File -> New -> Module

依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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-openfeign</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>common</artifactId></dependency>
</dependencies>

配置文件

server:port: 9020spring:application:name: order-serviceeureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:hostname: localhostprefer-ip-address: truelease-renewal-interval-in-seconds: 5lease-expiration-duration-in-seconds: 10product-service:ribbon:ConnectTimeout: 250ReadTimeout: 1000OkToRetryOnAllOperations: trueMaxAutoRetriesNextServer: 1MaxAutoRetries: 1

启动类

package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}

feign接口

package org.example.controller;import org.example.common.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@Component
@FeignClient(value = "product-service")
public interface ProductFeign {@GetMapping(value = "/product/{id}")Product findById(@PathVariable Long id);
}

controller类

package org.example.controller;import org.example.common.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
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;@RestController
@RequestMapping("/order")
public class OrderController {@AutowiredProductFeign productFeign;@GetMapping("/{id}")public Product buy(@PathVariable Long id) {Product product = productFeign.findById(id);return product;}
}

网关微服务

File -> New -> Module

添加依赖

<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-gateway</artifactId>
</dependency>

启动类

package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}

配置文件

server:port: 9030spring:application:name: api-gatewaycloud:gateway:routes:- id: product-serviceuri: lb://product-servicepredicates:- Path=/product/**- id: order-serviceuri: lb://order-servicepredicates:- Path=/order/**eureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port}

启动eureka-server,product-service,order-service和api-gateway微服务,打开http://localhost:9030/order/1,返回{"id":1,"name":"product test"}

github:GitHub - JJJ2018/SpringCloud_example

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

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

相关文章

苹果cms:伪静态设置教程

官方默认的网站模式是动态模式&#xff0c;动态模式下链接中自带有“index.php”想要去除网站链接中的index.php&#xff0c;首先需要开启网站的模式为伪静态模式。这样比动态模式那一长串的链接看着也舒服一些&#xff0c;最重要的是迎合搜索引擎的喜好加快收录提高排名。 1、…

【CSP CCF记录】202206-2 寻宝!大冒险!

题目 过程 思路 1.绿化图坐标边界太大&#xff0c;不能直接用矩阵表示&#xff0c;可以用一个二维数组存储有树坐标的x,y值。 定义两个数组&#xff1a;绿化图arr[1005][2]、宝藏图数组b[55][55] 2. 依据条件&#xff0c;从绿化图中第一棵树的坐标开始区域遍历。统计绿化图…

Java进阶10 IO流

Java进阶10 IO流 IO流就是数据传输。I为input的缩写&#xff0c;表示输入读取&#xff1b;O为out的缩写&#xff0c;表示输出写出。 一、IO流体系结构 ♥巧记区分tips&#xff1a;以Stream结尾的是字节流&#xff1b;以Reader、Writer结尾的都是字符流 二、字节输出流FileOu…

免翻,剪映出品的AI作图和AI视频官网免费体验!

哈喽&#xff0c;各位小伙伴们好&#xff0c;我是给大家带来各类黑科技与前沿资讯的小武。 近日&#xff0c;据剪映 Dreamina 官方消息&#xff0c;Deramina正式更名为即梦&#xff0c;同时宣布其AI作图和AI视频生成功能已全量上线。 ▲ 官网主页面 AI作图 1、通过文字描述或…

Mysql与Java连接----JDBC

前言: 当将Java与MySQL数据库连接时&#xff0c;JDBC&#xff08;Java Database Connectivity&#xff09;是一种重要的技术。JDBC允许Java应用程序通过标准的数据库访问方式与不同的关系型数据库进行通信&#xff0c;其中包括MySQL。通过使用JDBC&#xff0c;Java开发人员可以…

n的阶乘(函数)(C语言)

一、运行结果&#xff1b; 二、源代码&#xff1b; # define _CRT_SECURE_NO_WARNINGS # include <stdio.h>//声明n的阶乘函数fac; int fac(int n);int main() {//初始化变量值&#xff1b;int n, result;//获取要阶乘的数&#xff1b;printf("请输入要阶乘的n&…

基于SSM的理发店会员管理系统的设计和实现(有报告)。Javaee项目。ssm项目。

演示视频&#xff1a; 基于SSM的理发店会员管理系统的设计和实现&#xff08;有报告&#xff09;。Javaee项目。ssm项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0…

Today At Apple 2024.04.22 Iphone 入门 Mac 办公高4个效技巧

官网&#xff1a; https://www.apple.com/today/Apple 亚洲第一大商店&#xff1a;Apple 静安零售店现已在上海开幕如下预约课程&#xff1a;下载 Apple Store&#xff08;不是app store&#xff09;&#xff0c;点击课程预约笔记&#xff1a;Today At Apple Notes果粉加群 &am…

鸿蒙ArkUI开发:常用布局【交叉轴】

交叉轴 垂直于主轴方向的轴线。Row容器交叉轴为纵向&#xff0c;Column容器交叉轴为横向。通过alignItems属性设置子元素在交叉轴&#xff08;排列方向的垂直方向&#xff09;上的对齐方式alignSelf属性用于控制单个子元素在容器交叉轴上的对齐方式&#xff0c;其优先级高于al…

亚马逊测评实操干货,跨境电商做测评,卖家店铺测评攻略!

亚马逊在全球有超过 2 亿付费 Prime 会员和数以亿计的活跃买家账户&#xff0c;卖家们可以通过亚马逊一流的国际物流扩展业务。这是一个知名度极高的老牌跨境电商平台&#xff0c;而且平台流量非常大&#xff0c;成功打造爆款Listing这是每位亚马逊卖家渴望的诉求&#xff0c;做…

外卖 点金推广实战课程,2024外卖 点金推广全流程(7节课+资料)

课程内容&#xff1a; 外卖点金推广实操课程 资料 01 1-了解外卖.mp4 02 第一节:点金推广的说明.mp4 03 第二节:如何降低点金推广的成本,mp4 04 第三节:如何计算点金推广的流速,mp4 05 第四节:如何提升点金的精准度,mp4 06 第五节:点金推广实操,mp4 07 点金推广高级教程…

0508GoodsContent的Maven项目

0508GoodsContent的Maven项目包-CSDN博客 数据库字段 页面需求