SpringCloud知识概括

Posted GeorgeLin98

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud知识概括相关的知识,希望对你有一定的参考价值。

SpringCloud知识概括

SpringCloud基础组件

提供注册服务的服务器(第一步):

-----------------------------pom.xml-----------------------------
<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.yun</groupId>
  <artifactId>springcloud-eureka-server</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>springcloud-eureka-server</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <!--引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>
 
  <dependencies>
    <!--引入springcloud的euekea server依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
 
  </dependencies>
 
 
  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

-----------------------------appliaction.yaml-----------------------------
server:
  port: 8700 # 指定应用(注册中心方)端口自己决定
  
# 指定当前eureka客户端的注册地址,也就是eureka服务的提供方,当前配置的服务的注册服务方
eureka:
  client:
    service-url:
      defaultZone: http://$eureka.instance.hostname:$server.port/eureka
    register-with-eureka: false #指定自身是否在向eureka注册
    fetch-registry: false  #指定注册中心启动时是否禁用client的注册    
  instance:
    hostname: localhost#指定应用实例的主机名
 
#指定应用在注册中心的名称
spring:
  application:
    name: eureka-server

-----------------------------MainApplication类-----------------------------
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer //当前使用eureka的server
public class EurekaServerApplication 
    public static void main(String[] args) 
        SpringApplication.run(EurekaServerApplication.class,args);
    

在注册中心注册服务(第二步):

-----------------------------pom.xml-----------------------------
<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.yun</groupId>
  <artifactId>springcloud-eureka-server</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>springcloud-eureka-server</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <!--引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>
 
  <dependencies>
    <!--引入springcloud的euekea client依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
 
  </dependencies>
 
 
  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

-----------------------------appliaction.yaml-----------------------------
server:
  port: 8701 # 指定应用(服务提供方)端口自己决定
  
# 指定当前eureka客户端的注册地址,
eureka:
  client:
    service-url:
      defaultZone: http://$eureka.instance.hostname:$server.port/eureka
  instance:
    hostname: localhost#指定应用实例的主机名
 
#指定应用在注册中心的名称
spring:
  application:
    name: eureka-service

-----------------------------Controller类-----------------------------
package com.yun;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/Hello")
public class Controller 
    @RequestMapping("/World")
    public String helloWorld(String s)
        System.out.println("传入的值为:"+s);
        return "传入的值为:"+s;
    


-----------------------------MainApplication类-----------------------------
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableDiscoveryClient//代表自己是一个服务提供方
public class EurekaServerApplication 
    public static void main(String[] args) 
        SpringApplication.run(EurekaServerApplication.class,args);
    

搭建网关(第三步):

  • 在启动类可以配置@EnableZuulProxy和@EnableZuulServer,@EnableZuulProxy可以称为@EnableZuulServer的增强版,当Zuul与Eureka、Ribbon等组件配合使用时,我们使用@EnableZuulProxy。
-----------------------------pom.xml-----------------------------
<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.yun</groupId>
  <artifactId>springcloud-eureka-server</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>springcloud-eureka-server</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <!--引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>
 
  <dependencies>
    <!--引入springcloud的euekea client依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--引入springcloud的zuul依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zuul</artifactId>
    </dependency>
  </dependencies>
 
 
  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

-----------------------------appliaction.yaml-----------------------------  
server:
   port: 9100# 指定应用(网关服务提供方)端口自己决定
   
#指定网关应用在注册中心的名称 
spring:
  application:
    name: zuul
    
# 指定当前eureka客户端的注册地址 
eureka:
  client:
    service-url:
        defaultZone: http://$eureka.instance.hostname:$server.port/eureka 
    instance:
    	hostname: localhost#指定应用实例的主机名    
 
#路由规则定义:
#这里定义两种路由规则route1和route2代表访问网关/test01/**或/test02/**时
#路由到服务名为eurekaservice1或eurekaservice2中的服务集群去。
zuul:
  routes:
    route1:
      path: /test01/**
      serviceId: eurekaservice1
    route2:
      path: /test02/**
      serviceId: eurekaservice2
      
-----------------------------MainApplication类-----------------------------
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableZuulProxy//代表自己是一个网关服务提供方
public class EurekaServerApplication 
    public static void main(String[] args) 
        SpringApplication.run(EurekaServerApplication.class,args);
    


服务调用(restTemple+Ribbon)(第四步):

  • 第一种是直接调用:不经过注册中心那服务列表,直接访问的servicesupport
  • 第二种:是根据服务名选择调用,使用注入的负载均衡Bean用服务名去注册中心获取服务列表,当前客户端底层会做随机算法的选取获得服务并访问。
  • 第三种需要一个@Bean的注解自动注入并直接调用restTemplate对象调用服务。底层调用模式与第二种调用方式一样。如下:
package com.yun.beans; 
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
 
@Configuration
public class Beans 
    @Bean//根据方法返回值向容器注入Bean
    @LoadBalanced//表示该Bean需要做负载匀衡
    public RestTemplate getRestTemplate()
        return new RestTemplate();
    

-----------------------------pom.xml-----------------------------
<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.yun</groupId>
  <artifactId>springcloud-eureka-server</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>springcloud-eureka-server</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <!--引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>
 
  <dependencies>
    <!--引入springcloud的euekea client依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--引入springcloud的Ribbon依赖-->
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-ribbon</artifactId>
   </dependency>
  </dependencies>
 
 
  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

-----------------------------appliaction.yaml-----------------------------  
server:
   port: 8702 # 指定应用(服务提供方)端口自己决定
   
#指定网关应用在注册中心的名称 
spring:
  application:
     name: eureka-consumer
    
# 指定当前eureka客户端的注册地址 
eureka:
  client:
    service-url:
        defaultZone: http://$eureka.instance.hostname:$server.port/eureka 
    instance:
    	hostname: localhost#指定应用实例的主机名    
       
-----------------------------MainApplication类-----------------------------
package com.yun;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableDiscoveryClient//代表自己是一个服务提供方
public class EurekaServerApplication 
    public static void main(String[] args) 
        SpringApplication.run(EurekaServerApplication.class,args);
    


-----------------------------Controller类-----------------------------
package com.yun.controller; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
@RestController
@RequestMapping("/Hello")
class ConsumerController 
    @Autowired
    private LoadBalancerClient loadBalancerClient;//注入负载均衡Bean
    
    @Autowired
    private RestTemplate restTemplate;//注入rest调用器Bean
    
    @RequestMapping("/Consumer")
    public String helloWorld(String s)
        System.out.println("传入的值为:"+s);
        //第一种调用方式
        //String forObject = new RestTemplate().getForObject("http://localhost:8071/Hello/World?s=" + s, String.class);
 
        //第二种调用方式
        //根据服务名 获取服务列表 根据算法选取某个服务 并访问某个服务的网络位置。
        //ServiceInstance serviceInstance = loadBalancerClient.choose("EUREKA-SERVICE");
        //String forObject = new RestTemplate().getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/Hello/World?s="+s,String.class);
 
        //第三种调用方式 需要restTemplate注入的方式
        String forObject = restTemplate.getForObject("http://EUREKA-SERVICE/Hello/World?s=" + s, String.class);
        return forObject;
    


断路器搭建(第五步):

  • @HystrixCommand:该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断(回调)方法。
-----------------------------pom.xml-----------------------------
<?xml version="1.0" encoding="UTF-8"?>
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.yun</groupId>
  <artifactId>springcloud-eureka-server</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>springcloud-eureka-server</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
 
  <!--引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>
 
  <dependencies>
    <!--引入springcloud的euekea client依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--引入springcloud的hystrix依赖-->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-hystrix</artifactId>
	</dependency>
	<!--引入springcloud的Ribbon依赖-->
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-ribbon</artifactId>
   </dependency>
  </dependencies>
 
 
  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>以上是关于SpringCloud知识概括的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud知识概括

Jenkins知识概括

SpringCloudAlibaba知识概括

SpringCloudAlibaba知识概括

SpringBoot + SpringCloud 技术知识点自测

SpringBoot + SpringCloud 技术知识点自测