码迷,mamicode.com
首页 > 编程语言 > 详细

springcloud内容知识点

时间:2020-09-24 21:06:33      阅读:52      评论:0      收藏:0      [点我收藏+]

标签:ring   信息   div   ati   object   code   hystrix   pid   cloud   

 

一、eureka:服务注册
    1、服务端(先启动)
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        2、application.yml
            server:
              port: 8081
            spring:
              application:
                name: EurekaServer
            eureka:
              client:
                register-with-eureka: false #Eureka服务本身无需注册
                fetch-registry: false #Eureka服务本身无需获取注册信息
                service-url:
                  defaultZone: http://127.0.0.1:${server.port}/eureka/
        3、启动类
            @SpringBootApplication
            @EnableEurekaServer
            public class MyEurekaServer {
                public static void main(String[] args) {
                    SpringApplication.run(MyEurekaServer.class);
                }
            }
    2、客户端
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        2、application.yml
            server:
              port: 8082
            spring:
              application:
                name: EurekaClient
            eureka:
              client:
                service-url:
                  defaultZone: http://127.0.0.1:8081/eureka/
              instance:
                prefer-ip-address: true #跨域
        3、启动类
            @SpringBootApplication
            @EnableEurekaClient
            public class MyEurekaClient {
                public static void main(String[] args) {
                    SpringApplication.run(MyEurekaClient.class);
                }
            }
二、feign:服务间调用,有负载均衡功能
    1、被调用方不用动代码(@ResponseBody返回值不能是Object)
    2、调用方
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
        2、feign接口(调用方直接调接口的方法,即可实现服务间调用)
            @FeignClient("EurekaClientA")
            public interface AFeign {
                @RequestMapping(value = "/a/amethod/{id}")
                Msg ametohd(@PathVariable("id") Integer id);
            }
        3、启动类
            @SpringBootApplication
            @EnableEurekaClient
            @EnableDiscoveryClient
            @EnableFeignClients
            public class MyEurekaClientB {
                public static void main(String[] args) {
                    SpringApplication.run(MyEurekaClientB.class);
                }
            }
三、hystrix:熔断器
    1、被调用方不用动代码
    2、调用方
        1、application.yml
            feign:
              hystrix:
                enabled: true
        2、feign接口
            @FeignClient(value = "EurekaClientA", fallback = AFeignImpl.class)
            public interface AFeign {
                @RequestMapping(value = "/a/amethod/{id}")
                Msg ametohd(@PathVariable("id") Integer id);
            }
        3、实现feign接口(被调用方停止服务,即会调用熔断器方法,被调用方重新提供服务,即会重新调用所提供的的服务)
            @Component
            public class AFeignImpl implements AFeign {
                @Override
                public Msg ametohd(Integer id) {
                    return Msg.fail().add("branch", "熔断器触发");
                }
            }

 

springcloud内容知识点

标签:ring   信息   div   ati   object   code   hystrix   pid   cloud   

原文地址:https://www.cnblogs.com/linding/p/13716605.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!