1 Star 0 Fork 0

张龙飞 / example_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

学习springcloud笔记:

一. 父pom文件的引入

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    <spring-boot.version>1.5.10.RELEASE</spring-boot.version>
  </properties>
<dependencyManagement>
    <dependencies>
      <!-- 定义 spring cloud 版本 -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- 定义 springboot 版本 -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

二. erueka注册中心

1. pom引入;
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    
2. 主函数注解;
    @EnableEurekaServer
    
3. application.yml文件;
    eureka:
      instance:
        hostname: localhost
        ip-address: localhost
      client:
        serviceUrl:
          defaultZone: http://localhost:7001/eureka/
        register-with-eureka: false
        fetch-registry: false

三. eureka集群

修改application.yml文件(**spring.application.name可不相同**)
eureka:
  instance:
    hostname: localhost
    ip-address: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
    register-with-eureka: false
    fetch-registry: false

四. ribbon负载均衡

(客户端配置)

1. pom文件引入
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    
2. 主函数配置restTemplate(@SpringBootApplication包含了@Configuration注解)
    @Bean
    @LoadBalanced // 开启rebbon的负载均衡(客户端的软负载均衡)
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    
3.自定义负载均衡规则
    3.1 主函数添加:
        @RibbonClient(name = "study-provider", configuration = RibbonRoleConfig.class)
    
    3.2 新建类RibbonRoleConfig:
        @Configuration
        public class RibbonRoleConfig {
            @Bean
            public IRule ribbonRule(){
                return new RandomRule();
            }
        }

五. feign负载均衡

(客户端配置)

1. pom文件引入
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>

2. 主函数添加:
    @EnableFeignClients(basePackages = {"com.customer.feign"})
    
3. 配置接口
    @FeignClient(value = "${feign.provider}")

4. 自定义负载均衡规则
    @Bean
    @LoadBalanced
    public IRule getRule(){
        return new RandomRule();
    }

六. 服务熔断

(服务端配置)

1. pom引入
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    
2. 主函数添加:
    @EnableCircuitBreaker

3. 接口类方法添加
    @HystrixCommand(fallbackMethod = "hystrix_demo")

4. 同接口新建hystrix_demo方法
    public String hystrix_demo(){
        return "服务出异常了,这是熔断返回值";
    }
    
5. 访问 http://localhost:8081/feign.stream

七. 服务降级

(客户端配置)在feign基础上

1. yml文件修改配置
   feign:
     hystrix:
       enabled: true

2. 接口类修改
    @FeignClient(value = "${feign.provider}", fallbackFactory = MyFallBackFactory.class)

3. 新建类MyFallBackFactory
    @Component
    public class MyFallBackFactory implements FallbackFactory<TestFeignService> {
        @Override
        public TestFeignService create(Throwable cause) {
            return new TestFeignService() {
                @Override
                public String getDemo() {
                    return "这里是服务降级~~生产者挂掉了!!!";
                }
            };
        }
    }

八. 熔断服务监控

1. pom引入
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix</artifactId>
   </dependency>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
   </dependency>
   
2. 主函数添加
    @EnableHystrixDashboard
    
3. 访问 http://localhost:9001/hystrix

九. zuul网关

1. pom引入
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zuul</artifactId>
    </dependency>

2. 主函数添加
    @EnableZuulProxy
    
3. yml文件添加
    zuul:
      routes:
        my.serviceId: study-provider
        my.path: /my/**
      ignored-services: "*" #屏蔽原路由 http://localhost:9527/study-provider/demo, 使用 http://localhost:9527/my/demo
      prefix: /api    # 统一添加前缀 http://localhost:9527/api/my/demo

十. 配置中心服务端

连接远程仓库

1. pom文件引入
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!--解决git插件报的异常-->
    <dependency>
        <groupId>org.eclipse.jgit</groupId>
        <artifactId>org.eclipse.jgit</artifactId>
        <version>4.8.0.201706111038-r</version>
    </dependency>
    
2. 主函数添加
    @EnableConfigServer

3. yml文件添加
    spring:
      application:
        name: study-config
      cloud:
        config:
          server:
            git:
              uri: git@gitee.com:zlfmm/example_study_config.git

4. gitee创建example_study_config远程仓库
    4.1 提交文件application.yml文件
        spring:
          profiles: 
            active: 
            - dev
        ---
        spring:
          profiles: dev
          application: 
            name: study-config-dev
        ---
        spring:
          profiles: test
          application: 
            name: study-config-test
    4.2 访问 http://localhost:9999/application-dev.yml

十一. 配置中心客户端

配置服务地址,从而从远程仓库取配置数据

1. pom文件引入
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
2. 主函数添加
    @EnableEurekaClient

3. 新建bootstrap.yml文件
    spring:
      cloud:
        config:
          name: study-config-client #需要从gitee上读取的资源名称,注意没有yml后缀名
          profile: test  #本次访问的配置项
          label: master
          uri: http://localhost:9999  # 本微服务启动后先去找9999号服务,通过SpringCloudConfig获取Gitee的服务地址

4. 新建application.yml文件
    spring:
      application:
        name: study-config-client

空文件

简介

Spring Cloud框架学习demo代码。 包含了eureka、zuul、ribbon、feign、admin、config server组件及其配置方法 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/zlfmm/example_study.git
git@gitee.com:zlfmm/example_study.git
zlfmm
example_study
example_study
master

搜索帮助