springCloud springboot basis

**

Common Annotations

**

springBoot
@ComponentScan 指定一个包名 扫描这个包和子包
		不指定的话就进行默认扫描 :自动扫描主程序所在的包和子包  所以我们一般直接定义主程序所在的包的子包
@Controller 声明对象是控制器程序
@RestCOntroller是组合注解主要用于以数据返回的微服务中 相当于  @Controller和@Responsbody  因为有这两个作为原注解
@EnableTransactionManagement 启动事务管理器   @Transcational注解就可以起作用了
@MapperScan 扫描DAO 创建代理对象 进而注入service用的		

springCloud
@Mapper注解写在你的Mapper映射接口上面
@SpringBootApplication写在主程序上面
@Configuration 写在配置类上面    @Bean写在配置类中的返回新的对象的方法上面
@EnableEurekaServer把当前微服务标记为Eureka注册中心 接收其他微服务的注册
@EnableEurekaClient注册该微服务到Eureka中
@LoadBalanced该注解写在配置RestTemplate的配置类方法上来启动ribbon负载均衡
@EnableFeignClients写在主程序上来支持feign
@HystrixCommand(fallbackMethod=“你的方法”)			@EnableCircuitBreaker //启用对Hystrix熔断机制的支持
@FeignClient(value="服务名",fallbackFactory=实现FallbackFactory的类.class) 实现服务降级
@EnableHystrixDashboard 加在主程序上启动服务监控
@FeignClient(value=“服务名”)写在接口上 来调用远程服务
@EnableZuulProxy写在主程序上启动zuul路由访问功能

**

SpringApplication source code analysis

**

我们在启动SpringBoot的时候调用了SpringApplication.run(AtCrowdfundingApplication.class,args);

(1)这个方法的作用其实是在底层创建了一个对象    new SpringApplication(object数组).run(args);  
			SpringApplication springApplication = new SpringApplication(new Object[]{AtCrowdfundingApplication.class}); 
 			springApplication.run(args);所以我们可以用这句代码替代上面的代码
 			好处就是可以在springApplication.setxxx中自定义设置
 (2)SpringApplication构造器:
 		调用了initialize()方法  把传入的数组转换成集合再转换成linkedHashSet中
 	1	deduceWebEnvironment()判断是否引入了web模块是的话返回true
 		判断Servlet ConfigurableWebApplicationContext两个类是否存在 是就是web环境 才会做web环境所需要的配置
 	2  setInitializer:拿6个工厂对象做底层实例对象的管理存放在initializers(LIST集合)里面 传入的参数是ApplicationContextInitializer.class

Here Insert Picture Description

 		ApplicationContext 就是我们的IOC容器 在初始化的时候 需要我们工厂做一些bean对象生命周期的管理
	
		首先获取当前线程类的应用类加载器 然后获取工厂类名进而创建工厂类对象
		通过反射拿ApplicationContextInitializer的类名
		从jar包中找到META-INF/spring.factories并且加载成properties  找键是ApplicationContextInitializer的值 就是上图中的6个类名
		通过反射创建对象
 	3 setListener把监听器放在listeners(list集合)中 过程原理和上面是一样的

Here Insert Picture Description

	4 deduceMainApplicationClass() 判断主程序类是哪一个类
		首先获取异常栈 找到main方法 就可以找到主程序类并放回	 
	
(3)run方法 使用构造器初始化的成员变量
		通过反射创建IOC容器并且进行初始化

**

SpringBootApplication comment

**

有很多原注解  
 @Target
 @Retention
 @Documented
 @Inherited
 @springBootConfiguration   这个注解又有一个@Configuration 说明两个都可以用来声明配置类
 @EnableAutoCOnfiguration很重要 : @AUtoConfigurationPackage启动自动扫描包
 								  @Import(EnableAutoConfigurationImportSelector.class) 使用条件注解@Conditional...判断你的运行环境(你引入的starter)来配置你需要的

**

SpringBOOT integration mybatis

Need application.yml profile
**

---  
spring: 
  datasource:
    name: mydb
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://127.0.0.1:3306/cc
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver
mybatis:   
  mapper-locations: classpath*:/mybatis/mapper-*.xml
  type-aliases-package: com.xx.**.bean

---表示分隔符
mybatis配置的是mapepr配置文件 可有可无 没有的话就直接在dao接口中写sql注解  简单的sql可以这样写 复杂的例如动态sql可以用配置文件写

**

SpringBOOT hot deployment

**

只需要引入jar包
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional><!-- 这个需要为 true 热部署才有效-->
 </dependency>
项目出现了devtools就说明可以支持热部署了

**

SpringBOOT dynamic set the port number

Portfolio allocation
**

创建application-test.properties文件 写自己所要的端口号码
在run as  configuration配置中 program arguments填写参数
 --spring.profiles.active=test     test名字可以自己起  就可以关联了

原理是利用ConfigFileApplicationLIstener监听器监听有没有spring.profiles.active参数 有就会通过这个参数启动配置文件
原本application.properties中的配置还是可以起作用的 application-test.properties里面有的配置就会覆盖掉application.properties的

**

SpringBoot cluster

**

使用内置的tomcat配置集群:
server.address=127.0.0.1   //只有本机可以直接访问tomcat
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true

测试:
两个项目 两个配置文件 两个端口号 启动就行 使用nginx进行负载均衡


使用外置的tomcat配置集群:
	更改pom文件: <packaging>jar</packaging>  到  <packaging>war</packaging>
	把tomcat模块的依赖类库设置成provided范围 因为我们使用的外部的tomcat 在部署的时候不需要内置的tomcat
	将当前Application启动类继承SpringBootServletInitializer, 并重写方法configure 此时就不需要原本的main方法了
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
	        builder.sources(Application.class);// Application为启动类
	        return super.configure(builder);
	} 
打成war包 放在tomcat文件夹的webapps下 名字是ROOT.war 运行的时候就会形成ROOT文件夹

**

springBoot integration redis

Do session sharing
**

放入依赖的jar包后 会有RedisAutoConfiguration配置类就会启动

Here Insert Picture Description

需要增加一个集成配置类 来启动
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}

当然要实现session共享 pojo对象要序列化  
这样就成功了  在session中存储数据的时候就会存储到redis中

**

springBoot integrated health monitoring

Monitor project status
**

引入相关配置后 在http://ip:port/health查看健康状态
up表示正在运行

可以在项目yml中间中定义:

Here Insert Picture Description

就可以在info资源路径中拿到: 项目名字 父工程描述信息 版本 springboot父版本

还有很多其他路径~

**

Spring Cloud

**

(1)注册中心  倒入Eurka Server
		在主程序上加 @EnableEurekaServer 
		propertites配置
		spring.application.name=eureka-server
		server.port=1001
		eureka.instance.hostname=127.0.0.1
		eureka.client.register-with-eureka=false
		eureka.client.fetch-registry=false
		eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
		
		服务项目要导入 Eurka Discovery 
		主程序要加入@EnableDIscoveryClient
		spring.application.name=eureka-member-service
		server.port=2001
		eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1001/eureka/

		客户端项目 要导入 Eurka Discovery Feign
		主程序  @EnableDIscoveryClient   @EnableFeignClients启动feign的方式进行远程调用
		spring.application.name=eureka-member-client
		server.port=80
		eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
		service接口里的方法要跟远程的controller里的方法声明保持一样  加上@FeignClient(远程服务名字 可以在注册中心或者配置文件中找到)

(2)ribbon负载均衡 在客户端做 在我们客户端找到多个远程服务的时候就会使用ribbon进行轮询     
		 nginx是在服务器端做负载均衡~ Feign中已经内置了ribbon所以不需要额外的操作 但是redis要自己搭建一个服务器
(3)Hystrix 进行系统的容错
			熔断器加在web端  主程序中加@EnableHystrix
			增加熔断方法 @HystrixCommand(fallbackMethod="你的熔断方法")
Published 63 original articles · won praise 44 · views 6242

Guess you like

Origin blog.csdn.net/weixin_40695328/article/details/93603985