SpringCloud将现在一些流行的技术整合到一起,实现如:配置管理,服务发现,智能路由,负载均衡,熔断器,控制总线,集群状态等等功能。主要涉及的组件有

netflix

  • Eureka:注册中心

  • Zuul:服务网关

  • Ribbon:负载均衡

  • Feign:服务调用

  • Hystix:熔断器

环境准备:一个数据库和表tb_user

1.创建一个父工程,和子模块consumer-demo,eureka-server,eureka-server2(两个是eureka的高可用性练习),user-server,(user-server2是对user server的集群练习可以不要)zuul-Demo。

(完整代码:https://gitee.com/mountaincold/cloudDemo)

2. 创建注册中心 eureka-server

  1.pom.xml 导入依赖 eureka服务端

  2. 创建启动类 eureka1

  3. 设置application.yml

eureka-server2相同把端口换成10087,注册地址换成10086就行了

2.创建服务提供者userservice

  1. pom.xml的配置 导入依赖

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>

  2. 设置application.yml配置文件

server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis
username: root
password: 960326
application:
name: userservice
logging:
level:
com.practice:
debug
eureka:
client:
service-url: #eurekaServer 地址
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
instance:
prefer-ip-address: true #当调用getHostname获取实例的hostname时 返回ip而不是host名称
ip-address: 127.0.0.1 #指定自己的ip信息 不指定的话会自己寻找
lease-expiration-duration-in-seconds: 10 #服务失效时间 默认90秒
lease-renewal-interval-in-seconds: 5 #服务续约时间的间隔,默认30秒
instance-id: ${spring.application.name}:${server.port} #设置id

  2.创建启动类,和pojo对象类

@Table(name = "tb_user")
public class User implements Serializable { private static final long serialVersionUID = 1L; @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; // 用户名
private String userName; // 密码
private String password; // 姓名
private String name; // 年龄
private Integer age; // 性别,1男性,2女性
private Integer sex; // 出生日期
private Date birthday; // 创建时间
private Date created; // 更新时间
private Date updated; // 备注
// private String note; // 。。。省略getters和setters
//TODO 需要手动添加getter,setter
}

  

  3.创建controller,service ,mapper

package com.practice.controller;

import com.practice.pojo.User;
import com.practice.service.UserService;
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.RestController;
import org.springframework.web.bind.annotation.RequestMapping; @RestController
@RequestMapping("user")
public class UserController { @Autowired
private UserService userService; @GetMapping("/{id}")
public User queryById(@PathVariable("id") Long id) {
return this.userService.queryById(id);
}
} /**
*设置睡眠是为了测试熔断器
*/
package com.practice.service; import com.practice.mapper.UserMapper;
import com.practice.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserService { @Autowired
private UserMapper userMapper;
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
public User queryById(Long id) {
Long startTime = System.currentTimeMillis();
int time = (int)(Math.random()*2000+1);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
User user = this.userMapper.selectByPrimaryKey(id);
Long endTime = System.currentTimeMillis();
logger.info("本次查询:{},花费时间为{}ms",id,endTime-startTime);
return user;
}
} /**
*通用mapper的使用,可以动态生成实现子类和查询语言适用于单表查询
*/ package com.practice.mapper; import com.practice.pojo.User;
import tk.mybatis.mapper.common.Mapper; public interface UserMapper extends Mapper<User> {
}

3创建服务消费者cosumer-demo

  1.pom.xml配置 需要的依赖

 <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加OkHttp支持 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.</version>
</dependency>
<!-- Eureka客户端 -->
<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-test</artifactId>
</dependency>
<!--Hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--Feign的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

  2. application.xml的配置

Spring:
application:
name: consumer
eureka:
client:
service-url: #eurekaServer地址
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
registry-fetch-interval-seconds: 5 #获取更新服务列表 默认时间为30秒 instance:
prefer-ip-address: true #当其它服务获取地址时提供ip而不是hostname
ip-address: 127.0.0.1 #指定自己的ip信息 不指定的话会自己寻找
ribbon: #修改服务负载均衡的分流规则
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #随机 默认为轮询
ConnectTimeout: 250 # ribbon 连接超时时间
ReadTimeout: 1000 #Ribbon 数据读取超时时间
feign:
hystrix:
enabled: true #开启feign的熔断支持默认关闭
compression:
request:
enabled: true # 开启请求压缩
mime-types: text/html,application/xml,application/json # 设置压缩的数据类型
min-request-size: 2048 # 设置触发压缩的大小下限
logging:
level:
com.consumer:
debug

  3. 创建启动类 和pojo对象类

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient
@SpringBootApplication
//@EnableHystrix
@EnableFeignClients
public class Consumer {
// @Bean
// @LoadBalanced //开启负载均衡
// public RestTemplate restTemplate(){
// // 使用OKHTTP客户端,只需注入工厂
// return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
// }
// 因为Feign 中自动集成 Ribbon 负载均衡 所以不需要自定义RestTemplate
public static void main(String[] args) {
SpringApplication.run(Consumer.class); }
}
/**
*属性类型和user-service中相同但是不用加@table之类的注解
*/
public class User { private static final long serialVersionUID = 1L; private Long id; // 用户名
private String userName; // 密码
private String password; // 姓名
private String name; // 年龄
private Integer age; // 性别,1男性,2女性
private Integer sex; // 出生日期
private Date birthday; // 创建时间
private Date created; // 更新时间
private Date updated; //TODO 需要手动添加getter,setter
}

  4. 创建 controller,service,config (dao是测试hystrix时创建的,feign中支持熔断所以不用创建)

package com.consumer.controller;

import com.consumer.pojo.User;
import com.consumer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
@RequestMapping("consume")
public class ConsumerController {
@Autowired
private UserService userService; @GetMapping
public List<User> consume(@RequestParam("ids")List<Long> ids){
return this.userService.queryUserByIds(ids);
}
} package com.consumer.service; import com.consumer.config.UserFeignClient;
import com.consumer.config.impl.UserFeignClientImpl;
import com.consumer.dao.UserDao;
import com.consumer.pojo.User;
//import com.netflix.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import java.util.ArrayList;
import java.util.List; @Service
public class UserService { /**
* 根据服务名称,获取服务实例
* */ @Autowired
private UserFeignClient userFeignClient;
public List<User> queryUserByIds(List<Long> ids) {
List<User> users = new ArrayList<>(); ids.forEach(id ->{
users.add(userFeignClient.queryById(id));
/* try {
Thread.sleep(500);
线程睡眠用于测试可以删除
} catch (InterruptedException e) {
e.printStackTrace();
}*/
});
return users;
} } package com.consumer.config; import com.consumer.config.impl.FeignConfig;
import com.consumer.config.impl.UserFeignClientImpl;
import com.consumer.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; @FeignClient(value = "userservice",fallback = UserFeignClientImpl.class,configuration = FeignConfig.class) public interface UserFeignClient {
@GetMapping("/user/{id}")
public User queryById(@PathVariable("id") Long id);
} package com.consumer.config.impl; import com.consumer.config.UserFeignClient;
import com.consumer.pojo.User;
import org.springframework.stereotype.Component; @Component
public class UserFeignClientImpl implements UserFeignClient { @Override
public User queryById(Long id) {
User user = new User();
user.setId(id);
user.setName("请求超时,请稍后重试--feign");
return user;
}
} package com.consumer.config.impl; import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* 编写配置类定义feign的日志级别 feign支持四种级别
* - NONE:不记录任何日志信息,这是默认值。
* - BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
* - HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
* - FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据
*/
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}

4. 创建zuul网关

1.pom.xml中的依赖

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

  2. application.yml

server:
port: 10010
spring:
application:
name: gateway
zuul:
routes:
userservice: userservice/** #简便方式
prefix: /api #添加路由前缀
# path: /userservice/** #这是映射路径
# serviceId: userservice #指定服务名称
# url: http://127.0.0.1:8081 映射路径对应的实际url地址
eureka:
client:
registry-fetch-interval-seconds: 5 #循环获取服务列表
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
prefer-ip-address: true
ip-address: 127:0.0.1

  3. 创建启动器

package com.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy //开启网关功能
public class Zuul {
public static void main(String[] args) {
SpringApplication.run(Zuul.class);
}
}

  测试成功的页面:

认识并学会springCloud的使用的更多相关文章

  1. SpringCloud(二)- 服务注册与发现Eureka

    离上一篇微服务的基本概念已经过去了几个月,在写那篇博客之前,自己还并未真正的使用微服务架构,很多理解还存在概念上.后面换了公司,新公司既用了SpringCloud也用了Dubbo+Zookeeper, ...

  2. SpringCloud 入门知识篇

    SpringCloud 入门 springcloud 学习 7天学会springcloud 教程 https://www.cnblogs.com/skyblog/category/738524.htm ...

  3. Spring Boot初识

    今天准备开一个新系列springboot,springboot结束后会更新springcloud,想要学会springcloud先学springboot吧.以后springboot和hadoop轮流更 ...

  4. 如何学习SpringCloud?(SpringCloud模板)

    前言 对于SpringCloud来说(下面简称SC),现在网上已经有很多教程写的非常详细,因为SC的组件特别多,虽然不是所有组件都需要用到,但是学习的时候我们都需要去学习和了解.所以我想如果再写把每一 ...

  5. SpringCloud笔记四:Ribbon

    目录 什么是Ribbon? Ribbon的配置 Maven引入 开启注解 Ribbon负载均衡 新建provider8002和8003 Ribbon核心组件IRule Ribbon自定义 什么是Rib ...

  6. SpringCloud教程 | 第一篇: 服务的注册与发现

    一.spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运 ...

  7. 使用Java类加载SpringBoot、SpringCloud配置文件

    我们都知道平常在使用SpringBoot和SpringCloud的时候,如果需要加载一两个配置文件的话我们通常使用@Value("${属性名称}")注解去加载.但是如果配置文件属性 ...

  8. 每天学点SpringCloud(四):Feign的使用及自定义配置

    Feign:SpringCloud的官网对它的定义是这样的: 是一个声明式的Web服务客户端.它支持Feign本身的注解.JAX-RS注解以及SpringMVC的注解.Spring Cloud集成Ri ...

  9. SpringCloud学习6-如何创建一个服务消费者consumer

    上一节如何创建一个服务提供者provider已经启动了一个provider的server,提供用户信息查询接口.接下来,我们启动另一个provider,由于是同一台机器本地测试,我们换一个端口 --s ...

随机推荐

  1. 一篇不错的BIO, NIO文章

    菜菜的我硬是读了2个小时, 哭了 BIO到NIO源码的一些事儿之BIO https://juejin.im/post/5c2cc075f265da611037298e#heading-3 整体上 BI ...

  2. Anadi and Domino

    C - Anadi and Domino 参考:Anadi and Domino 思路:分为两种情况: ①n<=6,这个时候肯定可以保证降所有的边都放上一张多米诺牌,那么答案就是m ②n==7, ...

  3. kubectl管理kubernetes集群

    [root@master ~]# kubectl get nodes  查看集群节点NAME      STATUS    AGEnode1     Ready     25mnode2     Re ...

  4. python配置文件

    python有两种配置文件,file.ini和file.json 一.ini文件如下: db_config.ini [baseconf] host=127.0.0.1 port=3306 user=r ...

  5. SpringMVC @ResponseBody返回中文乱码

    SpringMVC的@ResponseBody返回中文乱码的原因是SpringMVC默认处理的字符集是ISO-8859-1, 在Spring的org.springframework.http.conv ...

  6. Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2

    Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2 问题 Why do I get unexpected C ...

  7. airflow当触发具有多层subDAG的任务的时候,出现[Duplicate entry ‘xxxx’ for key dag_id]的错误的问题处理

    当触发一个具有多层subDAG的任务时,会发现执行触发的task任务运行失败,但是需要触发的目标DAG已经在运行了,dag log 错误内容: [2019-11-21 17:47:56,825] {b ...

  8. 01 numpy库(一)

    01-numpy NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库. NumPy 是一个运行 ...

  9. Python sys.argv[] 使用

    sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径;比如在CMD命令行输入 “python  test.py -help”,那么sys.argv[0]就代表“tes ...

  10. IDEA Cannot access alimaven (http://maven.aliyun.com/nexus/content/groups/public/)

    [ERROR] Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could n ...