# springCloud_Demo **Repository Path**: hjd7893git/springCloud_Demo ## Basic Information - **Project Name**: springCloud_Demo - **Description**: springcloud公开课简单例子 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-01-06 - **Last Updated**: 2021-01-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SpringCloud示例 ## 系统框架层级 eureka 微服务 sso 单点登陆 user 用户dao ## 构建 如果使用IDEA自带的构建工具那么 请使用 **maven** 构建 1. 构建eureka,两个eureka,并进行自注册 ``` spring-boot:run -D port=6868 -D eureka.server=http://localhost:6869/eureka spring-boot:run -D port=6869 -D eureka.server=http://localhost:6868/eureka ``` 2. 注册user到eureka * 在main中添加@EnableDiscoveryClient注释,并在相关配置文件中增加eureka服务配置 ```java @EnableDiscoveryClient @SpringBootApplication @ComponentScan(basePackages={"com.itheima.microservice"}) @MapperScan("com.itheima.microservice.user.mapper") public class UserApplication { public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(UserApplication.class, args); SpringContextUtils.setApplicationContext(applicationContext); } } ``` ```yaml eureka: client: registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true fetchRegistry: true #是否从Eureka中获取注册信息,默认为true serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址 defaultZone: http://127.0.0.1:6868/eureka/,http://127.0.0.1:6869/eureka/ eurekaServerConnectTimeoutSeconds: 60 eurekaServerReadTimeoutSeconds: 60 instance: prefer-ip-address: true #将自己的ip地址注册到Eureka服务中 ip-address: 127.0.0.1 instance-id: ${spring.application.name}:${server.port} #指定实例id lease-expiration-duration-in-seconds: 30 #续约更新时间间隔(默认30秒) lease-renewal-interval-in-seconds: 10 # 续约到期时间(默认90秒) leaseRenewalIntervalInSeconds: 10 #心跳时间 ``` * 构建两个user(user1,user2),注入到eureka中【两个是高可用,下同】 ``` spring-boot:run -D port=8282 spring-boot:run -D port=8281 ``` 3.注册单点登陆sso(sso1,sso2)(负载均衡) ``` spring-boot:run -D port=8381 spring-boot:run -D port=8382 ``` ``` 服务器端负载均衡 Nginx Nginx 基于C语言,快速,性能高5w/s。 Redis 5w/s,RibbatMQ 1.2w/s ApacheActiveMQ 0.6w/s 业务系统,kafka 20w~50w/s大数据,Zuul2.0 200w/s 负载均衡、反向代理,代理后端服务器。隐藏真实地址,防火墙,不能外网直接访问,安全性较高。属于服务器端负载均衡。既请求由 nginx 服务器端进行转发。 客户端负载均衡 Ribbon Ribbon 是从 eureka 注册中心服务器端上获取服务注册信息列表,缓存到本地,然后在本地实现轮询负载均衡策略。 既在客户端实现负载均衡。 应用场景的区别: Nginx 适合于服务器端实现负载均衡 比如 Tomcat ,Ribbon 适合与在微服务中 RPC 远程调用实现本地服务负载均衡,比如 Dubbo、SpringCloud 中都是采用本地负载均衡。 Feign Feign 是一个声明web服务客户端, 这便得编写web服务客户端更容易Spring Cloud Netflix 的微服务都是以 HTTP 接口的形式暴露的,所以可以用 Apache 的 HttpClient 或 Spring 的 RestTemplate 去调用,而 Feign 是一个使用起来更加方便的 HTTP 客戶端,使用起来就像是调用自身工程的方法,而感觉不到是调用远程方法 Feign包含了ribben ``` 4.注册服务网管zuul(zuul1,zuul2) ``` spring-boot:run -D port=6677 spring-boot:run -D port=6678 ```