微服务实战demo(1.5.17.RELEASE&Edgware.SR5)
一、注册中心(使用eureka)
1.依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.配置:
server:
port: 8761
spring:
application:
name: register
eureka:
instance:
hostname: imooc:123456@localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
security:
basic:
enabled: true
user:
name: imooc
password: 123456
3.启动注解:
在启动类添加
@EnableEurekaServer
4.访问:
http://localhost:8761
二、 网关
(zuul,默认是Hystrix 客户端,不需要再注入Hystrix,并且能对所有的请求进行监控)
1.依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
2.配置:
server:
port: 8888
spring:
application:
name: proxy
eureka:
client:
service-url:
defaultZone: http://imooc:123456@localhost:8761/eureka
zuul:
#定义路由规则,默认路由是网关地址+应用名+应用原来的接口requestMapping
routes:
userApi:
path: /user/**
serviceId: user
# 是否去掉**前缀,默认是
stripPrefix: true
3.启动注解:
在启动类添加
@EnableEurekaClient
@EnableZuulProxy
三、服务
1.依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
2.配置:
server:
port: 8081
spring:
application:
name: user
eureka:
client:
service-url:
defaultZone: http://imooc:123456@localhost:8761/eureka
3.启动注解:
在启动类添加
@EnableEurekaClient
四、监控(Hystrix 分服务端和客户端)
1.依赖:
<!-- 服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <!-- hystrix 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <!-- 能够收集应用中的性能监控信息及开放一些管理端的接口 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、配置:
3、启动注解:
在启动类添加(服务端)
@EnableHystrixDashboard
在启动类添加(客户端)
@EnableHystrix
在接口处添加(客户端)
@HystrixCommand(fallbackMethod = "fallbackGetOrder")
*fallbackMethod = "fallbackGetOrder"是指定如果报错了,就调用该方法,要求方法的入参及出参要与原方法一样
4、服务端访问:ip:端口/hystrix
在服务页文本框里输入客户端的ip:端口/hystrix.stream 进行查看监控
五、服务间调用(Feign)
1、依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
2、配置:0
3、启动注解:
在启动类添加
@EnableFeignClients
4、代码编写:
调用端编写接口:
@FeignClient(value = "order",path = "/") public interface OrderClient { /** * 获取订单信息 * @return java.lang.String * @author wangquanqing * @date 2018/11/27 15:21 */ @GetMapping("/{id}") String getMyOrderId(@PathVariable(name = "id") Long id); }
被调用端编写接口实现:
@RestController @RequestMapping("/") public class OrderResource { @GetMapping("/{id}") public Long getMyOrder(@PathVariable(name="id") Long id){ return id; } }
本文来自博客园,作者:咔咔皮卡丘,转载请注明原文链接:https://www.cnblogs.com/anquing/p/17626566.html
分类:
微服务
标签:
笔记
, SpringCloud
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库