# spring-cloud-demo **Repository Path**: 199115/spring-cloud-demo ## Basic Information - **Project Name**: spring-cloud-demo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-01-08 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## spring-cloud-demo ### 一.创建一个服务注册中心(eureka) 1.创建一个springboot项目,添加spring-boot-starts-eureka-server 依赖, 2.在spring启动类上添加@EnableEurekaServer注解, 3.修改application.yml/application.properties配置文件 修改如下 Server: Port:9761 端口号 Eureka: Instance: hostname:location Client: FetchRegister:false Register-with-eureka:false Server-url: defaultZone:http://${eureka.instance.hostname}:${server.prot}/eureka/ 4.启动springboot的启动类,访问localhost:8761,如果能访问eureka管理页面,即为配置成功 ### 二.服务提供者 创建一个springboot项目,创建一个User实体类 package com.springcloud.privodeuser.pojo; import org.springframework.context.annotation.Bean; import javax.persistence.*; import java.io.Serializable; /** * @author HanLiBin * @version V1.0 */ @Entity public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column private String name; @Column private String username; @Column private Integer age; @Column private double balance; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } 注解解释: 1.@Entity:springboot-jpa注解 2. @Id,表的id @GeneratedValue(strategy =GenerationType.AUTO) 这两个为表的id的注解 3.@Column:为表中的字段注解 创建一个UserDao:内容 package com.springcloud.privodeuser.dao; import com.springcloud.privodeuser.pojo.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** * @author HanLiBin * @version V1.0 */ @Repository public interface UserDao extends JpaRepository { } 解释:dao继承JpaREpository 即可使用Jpa 创建UserController package com.springcloud.privodeuser.controller; import com.springcloud.privodeuser.dao.UserDao; import com.springcloud.privodeuser.pojo.User; 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; /** * @author HanLiBin * @version V1.0 */ @RestController public class UserController { @Autowired private UserDao userDao; @GetMapping("/simple/{id}") public User findOneById(@PathVariable Integer id){ return userDao.findOne(id); } } 修改配置文件 Server Port:7900 Spring: dataSource: driver-class-name: com.mysql.jdbc.Driver username: root password: root url: jdbc:mysql://localhost:3306/test Logging: level: root: info org.hibernate: INFO org.hibernate.type.desciptor.sql.BasicBinder: trace org.hibernate.type.desciptor.sql.BasicExtractor:trace com.itmuch : debug //把服务注册到eureka中 Eureka: Client: Service-url: DefaultZone:localhost:8761/eureka 启动即可注册到服务中心中 ### 三:服务消费者 1.服务消费者也需要把服务注册到服务注册中心,所以也需要eureka的依赖,需要把依赖添加到pom文件中 2.创建pojo文件夹,在此文件夹的下面添加一个User.java 类,内容和服务提供者中的类相同, 3.在springboot 的启动类中添加RestTempLate属性,并在方法上添加@Bean 与@LoadBalanced Private RestTempLate restTempLate;//属性 @Bean @LoadBalanced Public RestTempLate restTempLate(){ Returne new RestTempLate(); } 4.创建一个controller名称为UserController.java @Autowired private RestTemplate template; @GetMapping("/movice/{id}") public User findOneById(@PathVariable Integer id) { return template.getForObject("http://localhost:7900/simple/"+id,User.class); } 修改配置文件:application.yml 1.server: port: 7901 //端口号 spring: application: name: customer-user 服务在eureka中的名称 eureka: Instance: instance-id:${spring.application.name}:${spring.application .instance_ id:${server.port}} client: service-url: defaultZone: http://admin:admin@localhost:8761/eureka/ //服务注册中心 ### 四:Ribbon服务器端的负载均衡 ##### 1.ribbon默认配置 1.如果服务提供者有多个,此时需要添加负载均衡,遵循设定的轮询机制,调用服务, 2.负载均衡分为服务器端和客户端 服务端: nginx 客户端: ribbon 3.spring-cloud推荐使用ribbon作为微服务的负载均衡:次为修改三的服务消费者 (1)需要添加负载均衡的ribbon 的依赖 (2)在spring的启动类中添加的实例化RestTempLate 的方法上添加@LoadBalanced即可将RestTempLate设置成为负载均衡类 (3)修改RestTempLate.getForObject(url,Class)方法 ①Url:http://privode-user/simple/2 ②Class:User.class 注:其中privode-user 为服务器中配置未见中的spring.application.name的值 (4)启动多个服务提供者 ##### 2.自定义ribbon配置 ####### ribbon可以使用两种方法覆盖默认的配置 1.使用java 代码配置:基于java代码比较复杂,不容易学习,在此推荐使用配置文件配置 2.使用配置文件 在application.yml中添加: provide-server: //注册服务提供者的名称(一般为spring.applicaiton.name) ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule //值为规则RandromRule等 ##### 3.如何使用Ribbon不使用Eureka 3.1 在application.yml 中添加 stores: 具体的服务名称(服务提供者spring.applicaiton.name) ribbon: listOfServers: 具体的服务名称(localhost:7900) 3.2 ribbon: eureka: enabled: false 注:如果没有添加eureka的依赖没有添加,3.2可以不用添加 #### 五:feign声明式服务 1.要使用feign 声明式接口,需要引入feign的依赖spring-cloud-strater-feign 2.在Springboot的启动类上添加@feignClient注解 3.创建一个接口如 FeignUserClient.java;内容如下 @FeignClient("provied-user") 1 public interface FeignUserClient(){ @GetMapping("/movice/{id}") public User findById(@PathViliable("id") Integer id); 2 } 注释:1.添加注解@FeignClient 值为服务提供者的spring.application.name 2.此方法和服务提供者的一样 启动PathVariable中的声明不能省略否者会报错 3.网上有些教程说不能使用springboot的混合注解只是针对于低版本,高版本不存在这个问题,但是id是不能省略的:针对于复杂的参数如对象可以使用GetMapping注解 #### 六:深入eureka 1.更改eureka的元数据 1.1 更改eureka实例id: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}} 实例: application.yml eureka: instance: instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} 1.2 可以直接注入EurekaClient 已使用eureka提供的api 2.注册服务:作为一个实例也包括定期到注册表,默认连接时间为30s,服务器,本地,客户端,都具有相同的元数据(因此需要三个心跳也就是90s时间来注册一个服务,但是可以通过修改eureka.instance.leaseRenewalIntervalInSeconds 更改期限)但是一般不建议这么做,以为eureka真多心跳做了一些计算