springcloud内容知识点

一、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:服务间调用,有负载均衡功能,要在eureka服务下
    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:熔断器,要在eureka服务下
    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", "熔断器触发");
                }
            }
四、zuul:网关,要在eureka服务下
    1、步骤
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            </dependency>
        2、application.yml
            zuul:
              routes:
                EurekaClientA:
                  path: /a/**
                  serviceId: EurekaClientA #服务名
        3、启动类
            @SpringBootApplication
            @EnableEurekaClient
            @EnableZuulProxy
            public class MyEurekaClient {
                public static void main(String[] args) {
                    SpringApplication.run(MyEurekaClient.class);
                }
            }
    2、网关过滤器和请求头丢失问题
        @Component
        public class MyEurekaClientFilter extends ZuulFilter {
            /**
             * 在请求前pre或者后post执行
             *
             * @return
             */
            @Override
            public String filterType() {
                return "pre";
            }

            /**
             * 多个过滤器的执行顺序,数字越小,表示越先执行
             *
             * @return
             */
            @Override
            public int filterOrder() {
                return 0;
            }

            /**
             * 当前过滤器是否开启true表示开启
             *
             * @return
             */
            @Override
            public boolean shouldFilter() {
                return true;
            }

            /**
             * 过滤器内执行的操作 return任何object的值都表示继续执行
             * RequestContext.getCurrentContext().setSendZuulResponse(false)表示不再继续执行
             *
             * @return
             * @throws ZuulException
             */
            @Override
            public Object run() throws ZuulException {
                RequestContext requestContext = RequestContext.getCurrentContext();
                HttpServletRequest request = requestContext.getRequest();
                String header = request.getHeader("token");
                if (header != null && !"".equals(header)) {
                    //Authorization、Cookie、Set-Cookie不会转发
                    requestContext.addZuulRequestHeader("token", header);
                }
                return null;
            }
        }
五、配置中心
    1、配置中心服务(上传至git仓库的文件命名规则:[name]-[profile].yml)
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
        2、application.yml
            server:
              port: 8085
            spring:
              application:
                name: EurekaConfig
              cloud:
                config:
                  server:
                    git:
                      uri: https://gitee.com/YiSiYiNian/cloudconfig.git #git仓库地址
        3、启动类
            @SpringBootApplication
            @EnableConfigServer
            public class MyApplication {
                public static void main(String[] args) {
                    SpringApplication.run(MyApplication.class);
                }
            }
        4、测试
            请求地址:配置中心服务地址+文件名
    2、其他服务
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
        2、bootstrap.yml
            spring:
              cloud:
                config:
                  name: application #文件名-前部分
                  profile: zuul #文件名-后部分
                  label: master #分支
                  uri: http://127.0.0.1:8085 #配置中心地址
六、bus:消息总线,要和消息队列一起使用,更改配置文件,不用重启服务
    1、消息总线服务
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-bus</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
            </dependency>
        2、application.yml
            server:
              port: 8085
            spring:
              application:
                name: EurekaConfig
              cloud:
                config:
                  server:
                    git:
                      uri: https://gitee.com/YiSiYiNian/cloudconfig.git #git仓库地址
              rabbitmq:
                host: 127.0.0.1
            management: #暴露触发消息总线的地址
              endpoints:
                web:
                  exposure:
                    include: bus-refresh
    2、其他服务
        1、pom.xml
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-bus</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        2、application.yml
            server:
              port: 8082
            spring:
              application:
                name: EurekaClient
              rabbitmq:
                host: 127.0.0.1
            eureka:
              client:
                service-url:
                  defaultZone: http://127.0.0.1:8081/eureka/
              instance:
                prefer-ip-address: true #跨域
            zuul:
              routes:
                EurekaClientA:
                  path: /a/**
                  serviceId: EurekaClientA #服务名
            person:
              name: mengmeiqi
        3、向消息队列发送消息
            url(消息总线服务):http://127.0.0.1:8085/actuator/bus-refresh
            methods:post
        4、自定义配置修改
            组件上加注解:@RefreshScope,框架自己的配置无需加注解
原文地址:https://www.cnblogs.com/linding/p/13716605.html