0


【无标题】

springCloud

1.搭建父工程

使用springboot搭建

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9h4OGfXv-1661184648520)(C:\Users\20372\AppData\Roaming\Typora\typora-user-images\image-20220821114909096.png)]

在pom依赖中导入依赖

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><packaging>pom</packaging><modules><module>eureka-server</module></modules><!--spring boot 环境 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.11.RELEASE</version><relativePath/></parent><groupId>org.ymh</groupId><artifactId>springCloud</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-cloud.version>Hoxton.SR12</spring-cloud.version></properties><!-- springcloud版本管理--><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>

2.搭建eureka-server

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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"><parent><artifactId>springCloud</artifactId><groupId>org.ymh</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>

yml文件

# eureka 配置
# eureka 一共有4部分 配置
# 1. dashboard:eureka的web控制台配置
# 2. server:eureka的服务端配置
# 3. client:eureka的客户端配置
# 4. instance:eureka的实例配置
server:
  port: 8761

eureka:
  instance:
    hostname: localhost # 主机名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka # http://139.155.8.246:8763/eureka, http://192.168.11.128:8762/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
    register-with-eureka: false # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
    fetch-registry: false # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
spring:
  application:
    name: eureka-server

主启动类

packagecom.code;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublicclassEurekaServerApplication{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaServerApplication.class, args);}}

3.搭建euraka-provider

pom文件

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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"><parent><artifactId>springCloud</artifactId><groupId>org.ymh</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka-provider</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><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></dependencies></project>

yml文件

server:port:8001eureka:instance:hostname: localhost # 主机名client:service-url:defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信spring:application:name: eureka-provider # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径

启动类

packagecom.code;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublicclassEurekaProviderApplication{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaProviderApplication.class, args);}}

eureka-consumer使用ribbon

a. 导入依赖
<!--Ribbon的依赖--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency>
b.负载均衡策略 也可以在配置文件中配置
importcom.netflix.loadbalancer.IRule;importcom.netflix.loadbalancer.RoundRobinRule;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/**
 * ribbion负载均策略
 */@ConfigurationpublicclassMyRule{@BeanpublicIRuleiRule(){returnnewRoundRobinRule();}}
c. controller中
importcom.code.domain.Goods;importcom.code.feign.GoodsFeign;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.cloud.client.discovery.DiscoveryClient;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.client.RestTemplate;@RestController@RequestMapping("/order")publicclassOrderController{@AutowiredprivateRestTemplate restTemplate;//发现客户端@AutowiredprivateDiscoveryClient discoveryClient;@AutowiredprivateGoodsFeign goodsFeign;@GetMapping("/add/{id}")publicGoodsadd(@PathVariable("id")int id){//业务逻辑//1 查询商品//2减库存//3支付//4 物流//ribbin调用String url ="http://EUREKA-PROVIDER/goods/findById/"+id;System.out.println(url);Goods goods = restTemplate.getForObject(url,Goods.class);if(goods.getGoodId()==-1){//打日志//提醒}return goods;}}
d. yml配置中添加
# 设置Ribbon的超时时间ribbon:ConnectTimeout:1000# 连接超时时间 默认1sReadTimeout:3000# 逻辑处理的超时时间 默认3s
e. 主启动类
importcom.code.config.MyRule;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.cloud.netflix.ribbon.RibbonClient;importorg.springframework.cloud.netflix.ribbon.RibbonClients;importorg.springframework.cloud.openfeign.EnableFeignClients;@EnableEurekaClient@SpringBootApplication//@RibbonClient(name = "EUREKA-PROVIDER", configuration = MyRule.class)@RibbonClients(value ={@RibbonClient(name ="EUREKA-PROVIDER", configuration =MyRule.class)})publicclassConsumerApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ConsumerApplication.class, args);}}

eureka-consumer使用feign

a. 导入依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
b. 编写feign接口
importcom.code.config.FeignLogConfig;importcom.code.domain.Goods;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;@FeignClient(value ="EUREKA-PROVIDER", configuration =FeignLogConfig.class, fallback =GoodsFeignCallBack.class)publicinterfaceGoodsFeign{@GetMapping("goods/findById/{id}")publicGoodsfindById(@PathVariable("id")int id);}
c. 主启动类上添加注解
@EnableFeignClients
importcom.code.config.MyRule;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.cloud.netflix.ribbon.RibbonClient;importorg.springframework.cloud.netflix.ribbon.RibbonClients;importorg.springframework.cloud.openfeign.EnableFeignClients;@EnableEurekaClient@SpringBootApplication//@RibbonClient(name = "EUREKA-PROVIDER", configuration = MyRule.class)@EnableFeignClients@RibbonClients(value ={@RibbonClient(name ="EUREKA-PROVIDER", configuration =MyRule.class)})publicclassConsumerApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ConsumerApplication.class, args);}}
d. 开启feign日志 feign只支持debug级别的日志
  • yml中配置
  • #日志logging:level:com.code: debug
  • 日志配置类
  • importfeign.Logger;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassFeignLogConfig{@BeanpublicLogger.Levellevel(){returnLogger.Level.FULL;//打印日志的详细程度}}

eureka-provider中使用服务降级、熔断、线程池隔离

a. 导入依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>

b. 主启动类添加注解@EnableCircuitBreaker //开启Hystrix功能

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.cloud.netflix.hystrix.EnableHystrix;@SpringBootApplication@EnableEurekaClient@EnableCircuitBreaker//开启Hystrix功能publicclassEurekaProviderApplication{publicstaticvoidmain(String[] args){SpringApplication.run(EurekaProviderApplication.class, args);}}

服务方服务降级与熔断

importcom.code.entity.Goods;importcom.code.service.GoodsService;importcom.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;importcom.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.cloud.context.config.annotation.RefreshScope;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/goods")@RefreshScope// 开启刷新功能publicclassGoodsController{@AutowiredGoodsService goodsService;@Value("${server.port}")privateInteger port;@Value("${ydlclass}")privateString str;/**
       提供方服务降级
       1. 超时
       2. 业务逻辑超时
       3. 熔断在一定时间类失败次数达到阈值服务断开 过一段时间服务成半开启状态 当成功次数又达到阈值服务完全开启
    */@GetMapping("findById/{id}")@HystrixCommand(fallbackMethod ="findById_fallBack", commandProperties ={//设置Hystrix的超时时间, 默认为1s@HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds", value ="3000"),//监控时间 默认5000 毫秒@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value ="5000"),//失败次数。默认20次@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value ="20"),//失败率 默认50%@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value ="50")})publicGoodsfindById(@PathVariable("id")int id){Goods goods = goodsService.findById(id);
        goods.setTitle(goods.getTitle()+"|端口号:"+port+"|"+str);return goods;}/**
     * 定义服务方降级方法
     * 1 方法的返回值和原方法一样
     * 2 方法参数和原方法一样
     */publicGoodsfindById_fallBack(int id){Goods goods =newGoods();
        goods.setGoodId(-1);
        goods.setTitle("provider提供方降级!");
        goods.setPrice(-9.9);
        goods.setStore(-10);return goods;}}

全部配置文件yml

server:port:8000eureka:instance:hostname: localhost
    prefer-ip-address:true#是否将自己的Ip注册到eureka种,默认false 注册 主机名ip-address: 127.0.0.1 #设置当前实例ip#instance-id: 修改instance-id显示instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port}lease-renewal-interval-in-seconds:30#每一次eureka client 先eureka server发送心跳的时间间隔lease-expiration-duration-in-seconds:90#如果90秒 eureka -server 没有eureka client 发送心跳包 注册书中删除注册client:service-url:defaultZonezz: http://localhost:8761/eureka #,http://eureka-server1:8763/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信register-with-eureka:true#启动时是否将自己注册到eureka上fetch-registry:true#是否需要从eureka种抓取数据spring:application:name: eureka-provider #设置当前应用名称zipkin:base-url: http://localhost:9411/  # 设置zipkin的服务端路径sleuth:sampler:probability:1# 采集率 默认 0.1 百分之十。ydlclass: itymhLLLL9999888888尹明洪6667777

eureka-consumer中使用Hystrix

a. 由于feign中已经集成了Hystrix所以不需要在引入依赖

b. 在yml中配置、
# 开启feign对hystrix的支持feign:hystrix:enabled:true

c. 编写feign接口的实现类服务消费方降级

importcom.code.domain.Goods;importorg.springframework.stereotype.Component;@ComponentpublicclassGoodsFeignCallBackimplementsGoodsFeign{@OverridepublicGoodsfindById(int id){Goods goods =newGoods();
        goods.setGoodId(-2);
        goods.setTitle("调用方降级了!");
        goods.setPrice(-5.5);
        goods.setStore(-10);return goods;}}

d.主启动类上添加注解@EnableFeignClients

importcom.code.config.MyRule;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.cloud.netflix.ribbon.RibbonClient;importorg.springframework.cloud.netflix.ribbon.RibbonClients;importorg.springframework.cloud.openfeign.EnableFeignClients;@EnableEurekaClient@SpringBootApplication//@RibbonClient(name = "EUREKA-PROVIDER", configuration = MyRule.class)@EnableFeignClients@RibbonClients(value ={@RibbonClient(name ="EUREKA-PROVIDER", configuration =MyRule.class)})publicclassConsumerApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ConsumerApplication.class, args);}}

e. 在feign接口上注解添加 fallback = GoodsFeignCallBack.class

importcom.code.config.FeignLogConfig;importcom.code.domain.Goods;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;@FeignClient(value ="EUREKA-PROVIDER", configuration =FeignLogConfig.class, fallback =GoodsFeignCallBack.class)publicinterfaceGoodsFeign{@GetMapping("goods/findById/{id}")publicGoodsfindById(@PathVariable("id")int id);}

服务消费者yml

server:port:9000eureka:instance:hostname: localhost # 主机名client:service-url:defaultZone: http://localhost:8761/eureka #注册到eureka上register-with-eureka:truefetch-registry:truespring:application:name: eureka-consumer ## 设置Ribbon的超时时间ribbon:ConnectTimeout:1000# 连接超时时间 默认1sReadTimeout:3000# 逻辑处理的超时时间 默认3s#日志logging:level:com.code: debug

# 开启feign对hystrix的支持feign:hystrix:enabled:true

getway网关

a. 导入依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>

b. 配置yml

server:port:80spring:application:name: api-gateway-server

  cloud:# 网关配置# 微服务名称配置discovery:locator:enabled:true# 设置为true 请求路径前可以添加微服务名称lower-case-service-id:true# 允许为小写gateway:# 路由配置:转发规则routes:#集合。# id: 唯一标识。默认是一个UUID# uri: 转发路径# predicates: 条件,用于请求网关路径的匹配规则# filters:配置局部过滤器的-id: eureka-provider
          # 静态路由#uri: http://localhost:8000# 动态路由uri: lb://eureka-PROVIDER
          predicates:- Path=/goods/**filters:- AddRequestParameter=username,zhangsan
            - AddResponseHeader=ydl,ymh

        -id: eureka-consumer
          # uri: http://localhost:8001uri: lb://eureka-CONSUMER
          predicates:- Path=/order/**default-filters:- AddResponseHeader=ydl,ymh

eureka:client:service-url:defaultZone: http://localhost:8761/eureka

c. 主启动类

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String[] args){SpringApplication.run(Application.class, args);}}

网关作用

  • 路由
  • 过滤 内置局部 内置全局 自定义局部 自定义全局
importorg.springframework.cloud.gateway.filter.GatewayFilterChain;importorg.springframework.cloud.gateway.filter.GlobalFilter;importorg.springframework.core.Ordered;importorg.springframework.http.HttpStatus;importorg.springframework.http.server.ServletServerHttpRequest;importorg.springframework.http.server.reactive.ServerHttpRequest;importorg.springframework.http.server.reactive.ServerHttpResponse;importorg.springframework.stereotype.Component;importorg.springframework.web.server.ServerWebExchange;importreactor.core.publisher.Mono;importjava.net.InetAddress;importjava.net.InetSocketAddress;@ComponentpublicclassIpFilterimplementsGlobalFilter,Ordered{//写业务逻辑@OverridepublicMono<Void>filter(ServerWebExchange exchange,GatewayFilterChain chain){// 黑客ip,直接拒接ServerHttpRequest request = exchange.getRequest();ServerHttpResponse response = exchange.getResponse();InetSocketAddress inetSocketAddress = request.getRemoteAddress();String hostName = inetSocketAddress.getHostName();System.out.println("HostName"+hostName);InetAddress address = inetSocketAddress.getAddress();String hostAddress = address.getHostAddress();System.out.println("HostAddress"+hostAddress);String hostName1 = address.getHostName();System.out.println("hostname1"+hostName1);if(hostAddress.equals("192.168.3.158")){//拒绝
            response.setStatusCode(HttpStatus.UNAUTHORIZED);return response.setComplete();}//走完了该到下一个过滤器了return chain.filter(exchange);}//返回数值 数值越小越先执行@OverridepublicintgetOrder(){return0;}}
importorg.springframework.cloud.gateway.filter.GatewayFilterChain;importorg.springframework.cloud.gateway.filter.GlobalFilter;importorg.springframework.core.Ordered;importorg.springframework.http.server.reactive.ServerHttpRequest;importorg.springframework.http.server.reactive.ServerHttpResponse;importorg.springframework.web.server.ServerWebExchange;importreactor.core.publisher.Mono;importjava.net.URI;publicclassUrlFilterimplementsGlobalFilter,Ordered{@OverridepublicMono<Void>filter(ServerWebExchange exchange,GatewayFilterChain chain){// 黑客ip,直接拒接ServerHttpRequest request = exchange.getRequest();ServerHttpResponse response = exchange.getResponse();URI uri = request.getURI();String path = uri.getPath();if(path.contains("goods/findGoodsById")){//打日志System.out.println("path危险");}return chain.filter(exchange);}@OverridepublicintgetOrder(){return1;}}

spring-cloud配置中心与bus消息总线

a. 导入依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!-- eureka-client --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- bus --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies>

b. 主启动

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.config.server.EnableConfigServer;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableConfigServer@EnableEurekaClientpublicclassConfigServerApp{publicstaticvoidmain(String[] args){SpringApplication.run(ConfigServerApp.class, args);}}

c. yml配置文件

server:port:9527spring:application:name: config-server
  # spring cloud configcloud:config:server:# git 的 远程仓库地址git:uri: https://gitee.com/hearts1/config-ydl.git
          force-pull:trueusername: hearts1
          password: Bayueershisi0824
      label: master # 分支配置#配置rabbitmq信息rabbitmq:host: 124.221.54.94
    port:5672username: guest
    password: guest
    virtual-host: /

eureka:client:service-url:defaultZone: http://localhost:8761/eureka

# 暴露bus的刷新端点management:endpoints:web:exposure:include:'bus-refresh'

d. 在eureka-provider中添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- bus --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>

e. bootstrap配置文件

# 配置config-server地址# 配置获得配置文件的名称等信息spring:cloud:config:# 配置config-server地址#uri: http://localhost:9527# 配置获得配置文件的名称等信息name: provider # 文件名profile: dev # profile指定,  config-dev.ymllabel: master # 分支discovery:enabled:trueservice-id: CONFIG-SERVER

  #配置rabbitmq信息rabbitmq:host: 124.221.54.94
    port:5672username: guest
    password: guest
    virtual-host: /

management:endpoints:web:exposure:include:'*'

f. controller上添加注解@RefreshScope // 开启刷新功能

@RestController@RequestMapping("/goods")@RefreshScope// 开启刷新功能publicclassGoodsController{...........}

g. 当eureka-provider中配置文件发生改变时发送请求

curl -X POST http://localhost:9527/actuator/bus-refresh

本文转载自: https://blog.csdn.net/m0_51503509/article/details/126476100
版权归原作者 给各位大佬提鞋 所有, 如有侵权,请联系我们删除。

“【无标题】”的评论:

还没有评论