spring Cloud 环境搭建 学习笔记



spring Cloud-->
spring boot 简化xml配置,快速整合框架
spring cloud 解决一套微服务解决方案 --RPC远程调用
配置管理、注册中心eureka zk、服务发现、服务注册、断路器、路由策略、负载均衡、全局锁、客户端调用、接口网关zuul、服务管理系统


springCloud注册中心环境搭建-->euraka
rest、feign 客户端调用工具
spring Eureka服务注册中心
负载均衡使用 ribbon    负载均衡 分别有: nginx 、Lvs、HAproxy、F5、SpringCloud也实现了负载均衡
接口网关springZuul :作用是拦截请求,类似nginx。
分布式配置中心,spring config
springCloud hystrix断路器


搭建eurka-server


org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE



  UTF-8
  UTF-8
  1.8


//eureka server
org.springframework.cloud
spring-cloud-starter-eureka-server 
 
//spring boot test
org.springframework.boot
spring-boot-starter-test
test

 




org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import



 


org.springframework.boot
spring-boot-maven-plugin

   

 

 
   
      spring-milestones
      Sring Milestones
      https://repo.spring.io/milestone
     
        false
     

   

 



application.yml
eureka:instance:hostname:localhost //eureka接口名称
eureka:client:registerWithEureka:false
eureka:client:fetchRegistry:false
eureka:client:serviceUrl:defaultZone:http://${eureka.instance.hostname}:${server.port}/eureka/
server:port:8888 //eureka项目工程端口号8888
spring:application:name:service-member
 
@SpringBootApplication
@EnableEurekaServer //支持Eureka
public class EurekApp{
public static void main(String[]args){
SpringApplication.run(EurekaApp.class,args);
}
}
http://127.0.0.1:8888   启动后访问 Eureka管理界面
新建mave jar类型工程


搭建eurela 会员服务项目:sservice-member

org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE



  UTF-8
  UTF-8
  1.8


//eureka server
org.springframework.cloud
spring-cloud-starter-eureka //eurela server 是spring-cloud-starter-eureka-server
 
//web组建
org.springframework.boot
spring-boot-starter-web
test

//spring boot test
org.springframework.boot
spring-boot-starter-test
test











org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import





application.yml
eureka:client:serviceUrl:defaultZone:http://loaclhost:8888/erueka/  //注册到eureka server里面 的地址
server:port:8762 //erueka member端 端口8762
spring:application:name:service-member //提供的erueka member名 用的时候如"http://service-member/getMemberAll"


@RestController //返回json格式的contraller
public class MemberController{
@RequestMapping("/getMemberAll")
public List getMemberAll(){
List list = new ArreyList();
list.add("aaaaa");
return list;
}
}


@SpringBootApplication
@EnableEurekaClient
public class MemberApp{
public static void main(String[] args){
SpringApplication.run(MemberApp.class,args);
}
}
//启动EurekApp和MemberApp 后,在localhost:8888  会显示service-member服务
//访问12.0.0.1:8762/getMemberAll   会显示aaaaa


新建订单系统:service-order
服务调用使用 rest、feign 使用httpClient技术调用
负载均衡使用 ribbon


新建订单系统:

//eureka server
org.springframework.cloud
spring-cloud-starter-eureka 
 
//web组建
org.springframework.boot
spring-boot-starter-web
test

//spring boot test
org.springframework.boot
spring-boot-starter-test
test

//负载均衡
org.springframework.boot
spring-cloud-starter-ribbon










application.yml
eureka:client:serviceUrl:defaultZone:http://loaclhost:8888/erueka/  //注册到eureka server 里面
server:port:8764 //order端 端口8764
spring:application:name:service-order //order端 提供的服务名


@Service
public class OrderMemberService{
  @Autowired
  private RestTemplate restTemplate;
  public List getOrderUserAll(){
    return restTemplate.getForObject("http://service-member/getMemberAll", List.class);
  }
}


@RestController
public class OrderController{
  @Autowired
  private OrderMemberService orderMemberService;
  @RequestMapping("/getOrderUserAll")
  public List getOrderUserAll(){
    Systm.out.println("订单服务开始调用会员服务");
    return orderMemberService.getOrderUserAll();
  }
}


@EnableEurekaClient
@SpringBootApplication
public class OrderApp{
public static void main(String [] args){
SpringApplication.run(OrderApp.class,args);
}
@Bean //把类rest模板注入到bean容器中去
@LoadBalanced //支持负载均衡 启动ribbon
RestTemplate restTemplate(){
return new RestTemplate();
}
}
//启动EurekApp和MemberApp、OrderApp 后,在localhost:8888  会显示service-member、service-order服务
//访问12.0.0.1:8764/getOrderUserAll   会显示aaaaa,因为调用了member端的getMemberAll


负载均衡rebbon在order里面配置
1.会架设多个服务器service-member:8762
2.rebbon进行负载均衡,
3.order端当发送一个请求到rebbon,rebbon会获取到多个service-member:8762,
rebbon进行轮训到其中一个service-member:8762


1。order访问注册中心eureka 再访问到member
2。order用名称service-member访问注册中心eureka,
   eureka使用service-member映射到真实ip地址,ip交给order
3。order有了真实地址 从而访问到了member
====end spring Eureka注册中心


负载均衡 分别有: nginx 、Lvs、HAproxy、F5、SpringCloud也实现了负载均衡


接口网关springZuul :作用是拦截请求,类似nginx。
任何请求交给接口网关zuul,再由接口网关zuul转发给注册中心,类似nginx反向代理
使用项目名称区分接口网关 转发到实际地址,解决跨域问题。


搭建spring zuul接口网关项目:service-newzuul



org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE



  UTF-8
  UTF-8
  1.8


//eureka server
org.springframework.cloud
spring-cloud-starter-eureka //eurela server 是spring-cloud-starter-eureka-server
 

org.springframework.cloud
spring-cloud-starter-zuul //eurela server 是spring-cloud-starter-eureka-server
 
//web组建
org.springframework.boot
spring-boot-starter-web
test

//spring boot test
org.springframework.boot
spring-boot-starter-test
test











org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import



 


org.springframework.boot
spring-boot-maven-plugin

   

 

 
   
      spring-milestones
      Sring Milestones
      https://repo.spring.io/milestone
     
        false
     

   

 

application.yml
#eureka:instance:hostname:localhost //eureka接口名称
#eureka:client:registerWithEureka:false
#eureka:client:fetchRegistry:false
eureka:client:serviceUrl:defaultZone:http://localhost:8888/eureka/  //网关注册到注册中心的地址
server:port:8769 //zuul项目工程端口号8769
spring:application:name:service-zuul  //zuul注册名称
zuul:routes:api-a:path:/api-member/**   //只要api-member开头的url都跳转到service-member
zuul:routes:api-a:service-id:service-member
zuul:routes:api-b:path: /api-order/**
zuul:routes:api-a:service-id: service-order


@EnableZuulProxy  //开启网关
@EnableEurekaClient
@SpringBootApplication
public class AppZuull{
  public static void main(String[]args){
  SpringApplication.run(AppZuull.class,args);
  }
}
A、B 服务,怎么保证B服务只允许有A服务进行访问


----
分布式配置中心,搭建config_service环境

org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE



  UTF-8
  UTF-8
  1.8


//eureka server
org.springframework.cloud
spring-cloud-starter-eureka //eurela server 是spring-cloud-starter-eureka-server
 

org.springframework.cloud
spring-cloud-config-server //eurela server 是spring-cloud-starter-eureka-server
 
//spring boot test
org.springframework.boot
spring-boot-starter-test
test











org.springframework.cloud
spring-cloud-dependencies
Camden.SR6
pom
import



 


org.springframework.boot
spring-boot-maven-plugin

   

 

 
   
      spring-milestones
      Sring Milestones
      https://repo.spring.io/milestone
     
        false
     

   

 



application.properties
spring.application.name=config-server
server.port=8889
spring.cloud.config.server.git.uri=https://gitee.com/itmayi/cfg2.git
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=msater
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=


@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication{
public static void main(Sring [] args){
SpringApplication.run(ConfigServerApplication.class,args);
}
}
启动后访问地址: http://localhost:8889/foo/dev 


创建config client端项目:config-client



org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE



  UTF-8
  UTF-8
  1.8


//eureka server
org.springframework.cloud
spring-cloud-starter-eureka //eurela server 是spring-cloud-starter-eureka-server
 

org.springframework.cloud
spring-cloud-config //config server 是spring-cloud-config-server
 

org.springframework.boot
spring-boot-starter-web

//spring boot test
org.springframework.boot
spring-boot-starter-test
test











org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import



 


org.springframework.boot
spring-boot-maven-plugin

   

 

 
   
      spring-milestones
      Sring Milestones
      https://repo.spring.io/milestone
     
        false
     

   

 



bootstrap.properties
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8889/
server.port=8881


@RestController
public class TestController{
@Value("${userName}") //将git中配置好的变量 注入到此 
private String userName;
@RequestMapping("/getUserName")
public String getUserName(){
return userName;
}
}
@SpringBootAppLication
public class NewClientApp{
public static void main(Sring []args){
SpringApplication.run(NewClientApp.class,args);
}
}  访问项目 http://127.0.0.1:8881/getUserName




客户端调用 rest ,feign(常用,自带负载均衡ribbon,自动开启 )
创建 feign项目 :

org.springframework.cloud
spring-cloud-starter-feign

application.yml
eureka:client:serviceUrl:defaultZone:http://localhost:8888/eureka/
server:port:8765
server:tomcat:max-threads: 50 //tomcat最大支持50个线程 
spring:application:name:service-order-feign


@FeignClient(value="service-member")
public interface MemberFeign{
@RequestMapping("/getMemberAll")
public List getToOrderMemberAll();
}
@RestController
public class OrderFeginController{
@Autowired
private MemberFeign memberFeign;
@RequestMapping("/getToOrderMemberAll")
public List getToOrderMemberAll(){
System.out.println("order fegin 工程调用 member工程");
return memberFeign.getToOrderMemberAll();
}
}
@SpringBootApplication //注入到springboot管理 
@EnableEurekaClient //注册到 eureka
@EnableFeignClients //启动Feign客户端 
public class OrderFeign{
public static void main(Sting[] args){
SpringApplication.run.(OrderFeign.class,args);
}
}


服务的雪崩效应 hystrix,容断机制 、服务 降级、服务限流、解决服务雪崩效应
1.使用超时机制、服务降级(发生超时异常时,不去调用接口,调用本地fallback)
jMeter做压力测试 如:发送100个并发请求 。
产生服务堆积
2.容断机制,如果一个目标服务调用慢或者有大量超时 时,容断该服务的调用,对于后续调用请求不在调用目标服务,直接返回,快速释放资源。如果目标服务情况好转则回复调用。
隔离机制 --例如对不同类型的请求使用线程池来资源隔离,每种类型的请求互不影响,如果一种类型请求线程资源耗尽,则对后续的该类请求直接返回,例如将服务拆开,对于重要的服务使用单独服务器部署。
限流机制--主要是提前对各个类型的请求设置最高的QPS阀值 访问次数,若高于阀值则对该请求直接返回,不再调用后续资源,这种模式不能解决服务依赖的问题,只能解决系统整体资源分配问题,因为没有被限流的请求依然可能造成雪崩效应。上面的容断机制、隔离机制都属于出错后的容错处理机制,而限流模式则为预防模式。
    限流机制 使用nginx实现,使用网关zuul实现


springCloud hystrix断路器
使用原有项目:order-fgin

org.springframework.cloud
spring-cloud-starter-hystrix

application.yml
feign:hystrix:enabled: true //开启hystrix
hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 4000  //接口超时时间4秒 ,微服务一般是10秒 或5秒


@FeignClient(value="service-member", fallback=MemberFallBack.class) //连接超时、报错,都会访问MemberFallBack
public interface MemberFeign{
@RequestMapping("/getMemberAll")
public List getToOrderMemberAll();
}
@Component
public class MemberFallBack implements MemberFeign{
public List getToOrderMemberAll(){
//服务的降级处理
List list = new ArrayList();
list.add("服务发生异常。。");
return list;
}
}


@SpringBootApplication //注入到springboot管理 
@EnableEurekaClient //注册到 eureka
@EnableFeignClients //启动Feign客户端 
public class OrderFeign{
public static void main(Sting[] args){
SpringApplication.run.(OrderFeign.class,args);
}
}

你可能感兴趣的:(SpringCloud)