本文主要是介绍SpringCloud项目整合【eureka+ribbon+zuul+hystrix+hystrix dashboard】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文不适合没有SpringCloud基础的人学习。
本编文章,是我学习SpringCloud之后自己整理的一个架子。主要用于自己学习
需要源码的同学,关注下面的公众号回复:xdxcloud
服务架构图
1、创建父工程
1-1:创建项目
1-2:添加pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<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><groupId>www.xdx97.cloud</groupId><artifactId>xdxcloud</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><springboot.mybatis.version>2.0.1</springboot.mybatis.version><mybatis-plus-boot-starter.version>3.0.2</mybatis-plus-boot-starter.version><springboot.pagehelper.version>1.2.3</springboot.pagehelper.version><log4j.version>1.2.17</log4j.version><lombok.version>1.18.2</lombok.version><druid.version>1.1.10</druid.version><mysql.connector.version>5.1.45</mysql.connector.version><jackson.version>2.10.1</jackson.version></properties><dependencyManagement><dependencies><!-- Spring Cloud 依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR1</version><type>pom</type><scope>import</scope></dependency><!-- Spring Boot 依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.0.RELEASE</version><type>pom</type><scope>import</scope></dependency><!-- MySQL 连接驱动依赖 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.connector.version}</version></dependency><!-- Spring Boot Mybatis 依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${springboot.mybatis.version}</version></dependency><!-- Spring Boot Mybatis 增强插件 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus-boot-starter.version}</version></dependency><!-- mysql数据库连接池 pool --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><!-- MyBatis 分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>${springboot.pagehelper.version}</version></dependency><!-- lombok 插件 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><!-- log4j 依赖 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><!-- @JsonFormat注解的使用 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>${jackson.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.10.1</version></dependency></dependencies></dependencyManagement></project>
2、搭建配置eureka集群
2-1:创建maven项目(后面创建maven项目都参考这个)
2-3:以上面相同的方式创建, eureka-7002
2-4:配置pom.xml
2-4-1: eureka-7001
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>xdxcloud</artifactId><groupId>www.xdx97.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-7001</artifactId><dependencies><!-- eureka Server 服务端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
</project>
2-4-2: eureka-7002
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>xdxcloud</artifactId><groupId>www.xdx97.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-7002</artifactId><dependencies><!-- eureka Server 服务端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
</project>
2-5、创建启动类
2-5-1: EurekaServer7001
package com.xdx97.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7001 {public static void main(String[] args) {SpringApplication.run(EurekaServer7001.class,args);}
}
2-5-1: 同理创建 EurekaServer7002
2-6 配置域名映射
在末尾加上下面的代码,然后保存。
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
2-7: 创建 application.yml
2-7-1: 7001 application.yml
server:port: 7001eureka:instance:hostname: eureka7001.com # eureka服务端的实例名称client:register-with-eureka: false #false表示不向注册中心注册自己fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url:#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 单机版 设置Eureka Server交互地址查询服务与注册服务defaultZone: http://eureka7002.com:7002/eureka/ # 集群版 设置Eureka Server交互地址查询服务与注册服务
2-7-2: 7001 application.yml
server:port: 7002eureka:instance:hostname: eureka7002.com # eureka服务端的实例名称client:register-with-eureka: false #false表示不向注册中心注册自己fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url:#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 设置Eureka Server交互地址查询服务与注册服务defaultZone: http://eureka7001.com:7001/eureka/ # 集群版 设置Eureka Server交互地址查询服务与注册服务
2-8 测试 eureka 集群
2-8-1 启动两个 eureka 服务
2-8-2 访问 http://eureka7001.com:7001/
3、创建公共API部分
3-1:使用上面的方法创建一个maven项目,项目名 cloud-api
3-2:pom 依赖
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>xdxcloud</artifactId><groupId>www.xdx97.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-api</artifactId><dependencies><!-- lombok 插件 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- Spring Boot Mybatis 依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${springboot.mybatis.version}</version></dependency><!-- Spring Boot Mybatis 增强插件 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus-boot-starter.version}</version></dependency><!-- @JsonFormat注解的使用 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency></dependencies>
</project>
3-3:创建User实体
这里我使用了,lombok、MybatisPlus、@JsonFormat 插件,上面 maven 已经引入了
package com.xdx97.cloud.entitys;import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;import java.io.Serializable;
import java.util.Date;@Data
@Accessors(chain = true)
public class User implements Serializable {/*** 用户id*/@TableIdprivate String userId;/*** 用户名*/private String userName;/*** 用户手机号*/private String userPhone;/*** 角色id*/private String roleId;/*** 用户密码*/private String userPassword;/*** 加密盐*/private String salt;/*** openid*/private String openid;/*** 用户状态:1启用0停用*/private Byte userStatus;/*** 头像路径*/private String headImgPath;/*** 创建时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")private Date gmtCreate;/*** 修改时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")private Date gmtModified;/*** 创建人*/private String userCreate;/*** 修改人*/private String userModified;private static final long serialVersionUID = 1L;
}
4、创建 一个数据库
CREATE TABLE `user` (`user_id` char(18) NOT NULL COMMENT '用户id',`user_name` varchar(50) NOT NULL COMMENT '用户名',`user_phone` varchar(20) NOT NULL COMMENT '用户手机号',`role_id` char(18) NOT NULL COMMENT '角色id',`user_password` char(40) NOT NULL COMMENT '用户密码',`salt` char(10) NOT NULL COMMENT '加密盐',`openid` varchar(50) NOT NULL DEFAULT '' COMMENT 'openid',`user_status` tinyint(4) NOT NULL COMMENT '用户状态:1启用0停用',`head_img_path` varchar(100) NOT NULL DEFAULT '' COMMENT '头像路径',`gmt_create` datetime NOT NULL COMMENT '创建时间',`gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',`user_create` char(18) NOT NULL COMMENT '创建人',`user_modified` char(18) DEFAULT NULL COMMENT '修改人',PRIMARY KEY (`user_id`) USING BTREE,KEY `id` (`user_name`,`user_phone`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='用户表';
5、 创建服务提供方
5-1:创建一个Maven项目, 项目名: cloud-user-8001
5-2:pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>xdxcloud</artifactId><groupId>www.xdx97.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-user-8001</artifactId><dependencies><!-- 引入api通用包 --><dependency><groupId>www.xdx97.cloud</groupId><artifactId>cloud-api</artifactId><version>${project.version}</version></dependency><!-- MySQL 连接驱动依赖 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- Spring Boot 依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><!-- Spring Boot Mybatis 增强插件 --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- mysql数据库连接池 pool --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId></dependency><!-- MyBatis 分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId></dependency><!-- SpringBoot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Eureka Clien 客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 监控信息完善 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 添加Hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency></dependencies></project>
5-3:配置 yml 文件
server:port: 8001mybatis:type-aliases-package: com.xdx97.cloud.entitys # 所有Entity别名类所在包mapper-locations: classpath*:mappers/**/*Mapper.xml # mapper映射文件 - classpath:mybatis/mapper/**/*.xmlspring:application:name: xdxcloud-userdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/xdx_cloud?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=falseusername: rootpassword: 123456dbcp2:min-idle: 5 # 数据库连接池最小维持连接数initial-size: 5 # 初始连接数max-total: 5 # 最大连接数max-wait-millis: 200 # 等待链接获取的最大超时时间# 将服务注册进eureka
eureka:client:service-url:defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 设置Eureka Server交互地址查询服务与注册服务instance:instance-id: xdxcloud-user8001-hystrix # 修改注册到eureka服务的名称prefer-ip-address: true # 访问路径可以显示ip# 配置info信息
info:app.name: xdxcloudcompany.name: www.xdx97.combuild.artifactId: $project.artifactId$build.version: $project.version$
5-4:配置启动类
package com.xdx97.cloud;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka
@EnableDiscoveryClient // 服务发现
@EnableCircuitBreaker //
public class UserApp8001 {public static void main(String[] args) {SpringApplication.run(UserApp8001.class,args);}@Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}
}
5-5:配置一个基本的查询
5-5-1:配置 mapper
package com.xdx97.cloud.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xdx97.cloud.entitys.User;
import org.apache.ibatis.annotations.Mapper;/*** 继承这个BaseMapper是MybatisPlus的功能*/
@Mapper
public interface UserMapper extends BaseMapper<User> {}
因为我这里只需要2个简单查询方法,我直接用MybatisPlus完成就好了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xdx97.cloud.mapper.UserMapper"></mapper>
5-5-2:配置 service
UserService
package com.xdx97.cloud.service;import com.xdx97.cloud.entitys.User;import java.util.List;public interface UserService {List<User> selectList();User getById(String id);
}
UserServiceImpl
package com.xdx97.cloud.service.impl;import com.xdx97.cloud.entitys.User;
import com.xdx97.cloud.mapper.UserMapper;
import com.xdx97.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> selectList() {List<User> xUsers = userMapper.selectList(null);System.out.println("我是 8001号服务");return xUsers;}@Overridepublic User getById(String id) {User user = userMapper.selectById(id);return user;}
}
5-5-2:配置 controller
package com.xdx97.cloud.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xdx97.cloud.entitys.User;
import com.xdx97.cloud.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
public class UserController {@Autowiredprivate UserServiceImpl userServiceImpl;@GetMapping("/user/list")public List<User> list(){return userServiceImpl.selectList();}/*** @HystrixCommand* 这是 hystrix的一个服务容错处理,当然该方法异常的时候,就去调用 processHystrix_Get 方法*/@GetMapping("/user/getById/{id}")@HystrixCommand(fallbackMethod = "processHystrix_Get")public User getById(@PathVariable("id") String id){System.out.println("id = " + id);User user = userServiceImpl.getById(id);if (user == null){throw new RuntimeException("测试异常!");}return user;}public User processHystrix_Get(@PathVariable("id") String id) {System.out.println("进入异常了~~~~");User user = new User().setUserName("报错了,出异常了!!!");return user;}
}
5-5-3:测试
1、先启动两个eureka,再启动8001服务
2、我们来测试一下hysrix的熔断服务,也就是当访问出现了异常的时候,我现在的数据库只有一个id=1的数据
6、创建zuul路由网关
6-1: 创建一个maven项目: cloud-zuul-9527
6-2:pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>xdxcloud</artifactId><groupId>www.xdx97.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-zuul-9527</artifactId><dependencies><!-- 引入api通用包 --><dependency><groupId>www.xdx97.cloud</groupId><artifactId>cloud-api</artifactId><version>${project.version}</version></dependency><!-- SpringBoot Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Eureka Clien 客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- 监控信息完善 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- 添加Hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!-- 添加zuul依赖 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency></dependencies></project>
6-3:配置yml文件
server:port: 9527spring:application:name: xdx-cloud-zuuleureka:instance:instance-id: gateway-9527.comprefer-ip-address: trueclient:service-url:#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 单机版 设置Eureka Server交互地址查询服务与注册服务defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7002.com:7002/eureka/zuul:routes:myuser.serviceId: xdxcloud-usermyuser.path: /myuser/**#ignored-services: xdxcloud-user # 不允许使用真实的微服务名访问 单个ignored-services: "*" # 不允许使用真实的微服务名访问 全部prefix: /xdx # 设置同一访问前缀# 配置info信息
info:app.name: xdxcloudcompany.name: www.xdx97.combuild.artifactId: $project.artifactId$build.version: $project.version$
6-4:配置域名映射
127.0.0.1 myzuul.com
6-5:配置启动类
package com.xdx97.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableZuulProxy
public class ZuulApp_9527 {public static void main(String[] args) {SpringApplication.run(ZuulApp_9527.class, args);}
}
6-6:测试
按照下面顺序依次打开
http://myzuul.com:9527/xdx/myuser/user/getById/1
7、使用zuul配置【服务降级】
服务降级: 简单理解当某个服务挂掉的时候,给出一个应急措施
UserFallback
package com.xdx97.cloud.fallback;import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;@Component
public class UserFallback implements FallbackProvider {@Overridepublic String getRoute() {// 对所有微服务降级return "*";// 仅对指定的微服务进行降级// return "XDXCLOUD-USER";}@Overridepublic ClientHttpResponse fallbackResponse(String route, Throwable cause) {System.out.println("route = " + route);return new ClientHttpResponse() {@Overridepublic HttpStatus getStatusCode() throws IOException {// 返回状态常量return HttpStatus.SERVICE_UNAVAILABLE;}@Overridepublic int getRawStatusCode() throws IOException {// 返回状态码,这里为503return HttpStatus.SERVICE_UNAVAILABLE.value();}@Overridepublic String getStatusText() throws IOException {// 返回状态码对应的状态短语,这里为"Service Unavailable"return HttpStatus.SERVICE_UNAVAILABLE.getReasonPhrase();}@Overridepublic void close() { }@Overridepublic InputStream getBody() throws IOException {// 设置降级信息// String msg = "fallback:" + ConsumerFallback.this.getRoute();String msg = "fallback:" + route;return new ByteArrayInputStream(msg.getBytes());}@Overridepublic HttpHeaders getHeaders() {// 设置降级响应头信息HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);return headers;}};}
}
测试
1、开启所有服务,然后访问
http://myzuul.com:9527/xdx/myuser/user/getById/1
2、然后我们关闭 8001 服务再次访问
8、在配置一个服务的提供方做负载均衡
8-1:创建一个Maven项目:cloud-user-8002
把cloud-user-8001的全部内容复制到8002
做如下修改:
测试:依次启动程序
反复访问这个地址,去控制台发现是轮询调用8001和8002服务(zuul采用ribbon的负载均衡,默认是轮询算法)
9、创建Hyscrix Dashboard 监控
9-1:创建一个Maven项目:cloud-hysrix-dashboard-9001
9-2:pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<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"><parent><artifactId>xdxcloud</artifactId><groupId>www.xdx97.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-hysrix-dashboard-9001</artifactId><dependencies><!-- 添加Hystrix Dashboard --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency></dependencies></project>
9-3:application.yml
server:port: 9001
9-4:启动类
package com.xdx97.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableHystrixDashboard
public class HystrixDashboardApp {public static void main(String[] args) {SpringApplication.run(HystrixDashboardApp.class, args);}
}
9-4:测试
1、依次开启下面的服务
2、访问 http://localhost:9001/hystrix 看到下面的截图,表示开启成功
3、http://127.0.0.1:8001/hystrix.stream
4、然后我们再访问 http://myzuul.com:9527/xdx/myuser/user/getById/1
5、如果想要监控某个接口,需要在接口上配置 @HystrixCommand 注解
这篇关于SpringCloud项目整合【eureka+ribbon+zuul+hystrix+hystrix dashboard】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!