1.什么是微服务
微服务就是将一个单体应用的架构拆分为一个个独立运行的程序,他们之间通过http协议通信,这些独立运行的程序可以采用不同的编程语言,不同的存储技术
2.什么是springcloud
springcloud是微服务放假的一种规范,它整合了许多市面上流行的框架,是一种一站式的解决方案,微服务将要面对的许多问题
1.服务注册与发现
2.负载均衡
3.服务熔断和限流
4.路由
。。。。
刚好springcloud Netflix就能解决这些方案:
1.springcloud eureka(服务注册与发现)
2.springcloud ribbon ,springcloud feign(负载均衡)
3.springcloud hystrix(服务熔断和限流)
4.springcloud zuul (路由网管)
5.springcloud config(配置统一管理)
3.springcloud eureka (单机)
springcloud eureka是Netflix卡原组织提供的一个服务高可用的解决方案
1.eureka服务端
1.导入依赖
<!--springcloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springcloud eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifacId>spring-cloud-starter-netflix-eureka-server</artifacId>
</dependencity>
2.application.yml/application.properties配置
server:
port: 7001 #设置端口号
#eureka配置服务
eureka:
instance:
hostname: localhost #设置eureka的主机名
client:
register-with-eureka: false #表示不向eureka注册中心注册自己
fetch-registry: false #表示自己是服务端
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
#设置eureka的
3.添加注解
@SpringBootApplication //springboot启动注解
@EnableEurekaServer //eureka客户端启动注解
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class);
}
}
4.运行测试
2.eureka的provider配置
1.添加依赖
<!--springcloud-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka- client</artifactId>
</dependency>
2.application.yml/application.properties配置
#端口
server:
port: 8001
spring:
application:
name: springcloud-mapper #指定项目的名字
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #要将此服务注册到的地址
instance:
instance-id: ${spring.application.name}:${server.port}
#指定eureka服务注册中心显示微服务的名字
3.添加注解
@SpringBootApplication
@EnableEurekaClient //启动eureka客户端
public class springcloud_mapper {
public static void main(String[] args) {
SpringApplication.run(springcloud_mapper.class);
}
}
4.运行测试
3.eureka customer端
可以通过resttemplate调用远程方法访问
1.provider提供方法
@GetMapping("/user/findId/{id}")
public User findId(@PathVariable("id") Integer id){
return userService.findId(id);
};
2.customer注册resttemplate
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
3.customer的controller
@Autowired
private RestTemplate restTemplate;
private final String MAPPER_URL="http://localhost:7001";
@GetMapping("/user/findId/{id}")
public User findId(@PathVariable("id") Integer id){
return restTemplate.getForObject(MAPPER_URL+"/user/findId/"+id,User.class);
};
4.application.yml/application.application配置
server:
port: 9001 #设置端口号
5.运行测试
4.springcloud eureka(集群)
1.赋值三个项目
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vCXGzoJM-1582700293900)(D:\aaa\colony.png)]
2.application.yml/application.properties配置
1.7001
server:
port: 7001
#eureka
eureka:
instance:
hostname: localhost1
client:
register-with-eureka: false #表示是否想eureka注册中心注册自己
fetch-registry: false #表示自己是服务端
service-url:
defaultZone: http://localhost2:7002/eureka,http://localhost3:7003/eureka
2.7002
server:
port: 7002
#eureka
eureka:
instance:
hostname: localhost2
client:
register-with-eureka: false #表示是否想eureka注册中心注册自己
fetch-registry: false #表示自己是服务端
service-url:
defaultZone: http://localhost1:7001/eureka,http://localhost3:7003/eureka
3.7003
server:
port: 7003
#eureka
eureka:
instance:
hostname: localhost3
client:
register-with-eureka: false #表示是否想eureka注册中心注册自己
fetch-registry: false #表示自己是服务端
service-url:
defaultZone: http://localhost1:7001/eureka,http://localhost2:7002/eureka
3.运行测试
6.springcloud ribbon配置
springcloud ribbon是一个客户端负载均衡的工具,通过springcloud的封装,可以让我们轻松将面向服务的rest模板请求自动转换成客户端负载均衡的服务调用
1.添加依赖
<!--robbin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.是resttemplate拥有调用eureka的功能
@Bean
@LoadBalanced //该注解使resttemplates拥有eureka访问的功能
public RestTemplate restTemplate(){
return new RestTemplate();
}
3.application.yml/application.properties
server:
port: 9001
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
register-with-eureka: false #不像eureka注册中心注册自己
4.controller
通过eureka服务注册中心的名字进行调用
@Autowired
private RestTemplate restTemplate;
private final String MAPPER_URL="http://SPRINGCLOUD-MAPPER";
@GetMapping("/user/findId/{id}")
public User findId(@PathVariable("id") Integer id){
return restTemplate.getForObject(MAPPER_URL+"/user/findId/"+id,User.class);
};
5.调用测试
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XiFQ12tV-1582700293903)(D:\aaa\customer-find.png)]
7.springcloud Feign
springcloud feign是基于ribbon开发的一个面向接口的为服务客户端负载均衡的工具
1.添加依赖
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.创建接口并配置feign
@FeignClient(value = "SPRINGCLOUD-MAPPER")
public interface UserInterface {
@GetMapping("/user/findId/{id}")
public User findId(@PathVariable("id") Integer id);
}
3.application.yml/application.properties
server:
port: 9002
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka
4.启动feign
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.hao.springcloud"})#指定接口所在的位置
public class feignApplication {
public static void main(String[] args) {
SpringApplication.run(feignApplication.class,args);
}
}
5.controller
@Autowired
private UserInterface userInterface;
@GetMapping("/user/findId/{id}")
public User findId(@PathVariable("id") Integer id){
return userInterface.findId(id);
}
6.运行测试
8.springcloud hystrix
微服务中当一个服务调用另一个服务时,因为网络或者自身的原因出现了问题,那么调用者就会等待被调用者的回应,随着时间的增长,越来越多的服务会访问到这个项目,就会引起雪崩效应,springcloud hystrix就是为了解决这个问题,springclou hystrix分为服务熔断与服务降级
服务熔断是“好死不如赖活着”,当被调用者的某个业务出现问题的时候,调用hystrix提供的备用方法,使其返回某些信息,不至于导致服务雪崩
服务降级:加入有a,b,c三个微服务,在某一个时间a的微服务的访问量猛然整多,导致a服务随时可能出现问题,而c的微服务的访问量比较少,那么就忍痛将c的微服务关掉,从客户端返回个客户一些信息,将资源分配给a服务
1.服务熔断
1.添加依赖
<!--springcloud-client-->
<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-hystrix</artifactId>
</dependency>
2.启动hystrix
@SpringBootApplication
@EnableEurekaClient //启动eureka客户端
@EnableCircuitBreaker //开启hystrix
public class springcloud_mapper {
public static void main(String[] args) {
SpringApplication.run(springcloud_mapper.class);
}
}
3.controller
@GetMapping("/user/findId/{id}")
@HystrixCommand(fallbackMethod = "findIdHystrix")
public User findId(@PathVariable("id") Integer id){
User id1 = userService.findId(id);
if(id1==null){
throw new RuntimeException("错误");
}
return id1;
};
public User findIdHystrix(Integer id){
return new User(id,"错误");
}
4.application.yml/application.properties
#端口
server:
port: 8001
spring:
application:
name: springcloud-mapper #指定项目的名字
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #要将此服务注册到的地址
instance:
instance-id: ${spring.application.name}:${server.port} #指定显示微服务的名字
5.调用测试
2.服务降级(fegin配合使用)
1.添加依赖
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.修改fegin接口
@FeignClient(value = "SPRINGCLOUD-MAPPER",fallbackFactory = UserFackbackFactory.class)
@Configuration
public interface UserInterface {
@PostMapping("/user/insert")
public boolean insert(User user);
@GetMapping("/user/findId/{id}")
public User findId(@PathVariable("id") Integer id);
@GetMapping("/user/findAll")
public List<User> findAll();
}
3.新建UserFackbackFactory接口并继承FallbackFactory
@Component
public class UserFackbackFactory implements FallbackFactory {
@Override
public UserInterface create(Throwable throwable) {
return new UserInterface() {
@Override
public boolean insert(User user) {
return false;
}
@Override
public User findId(Integer id) {
return new User(id,"错误1");
}
@Override
public List<User> findAll() {
return null;
}
};
}
}
4.application.yml/application.properties配置
server:
port: 9002
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka
#开启降级
feign:
hystrix:
enabled: true
5.运行测试
3.hystrix监控页面
1.新建项目导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
@SpringBootApplication
@EnableHystrixDashboard //开启监控页面
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class,args);
}
}
server:
port: 8767
访问http://localhost:8767/hystrix
2.被监控项目导入依赖
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: hystrix.stream
@SpringBootApplication
@EnableEurekaClient //启动eureka客户端
@EnableCircuitBreaker //开启hystrix
public class springcloud_mapper {
public static void main(String[] args) {
SpringApplication.run(springcloud_mapper.class);
}
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
servletRegistrationBean.addUrlMappings("/actuator/hystrix.stream");
return servletRegistrationBean;
}
}
3.填写监控路径
3.运行测试
9.springcloud zuul
zuul是Netflix设计用来为所有面向设备、web网站提供服务的所有应用的门面,zuul可以提供动态路由、监控、弹性扩展、安全认证等服务,他还可以根据需求将请求路由到多个应用中。
1.新建项目添加依赖
<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-zuul</artifactId>
</dependency>
2.添加配置
spring:
application:
name: springcloud-zuul5001
server:
port: 80
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
zuul:
routes:
myuser.serviceId: SPRINGCLOUD-MAPPER #要映射的eureka服务注册中心的名字
myuser.path: /abc/** #映射路径
ignored-services: "*" #拦截所有服务,ignored-services:SPRINGCLOUD-MAPPER拦截SPRINGCLOUD-MAPPER服务,加了这个配置不能再通过http://localhost/s/springcloud-mapper/user/findId/1进行访问,不加则可以
prefix: /s#前缀
@SpringBootApplication
@EnableZuulProxy #开启网管
public class ZuulApplication5001 {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication5001.class,args);
}
}
3.运行测试
访问http://localhost/s/abc/user/findId/1
来源:CSDN
作者:zuiai-
链接:https://blog.csdn.net/a776968499/article/details/104517319