第112天学习打卡(SpringCloud Feign:使用接口方式调用服务 Hystrix Dashboard) (4)

导入的依赖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 "> <parent> <artifactId>springcloud</artifactId> <groupId>com.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-provider-dept-hystrix-8001</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!--hystrix--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- Eureka的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- actuator完善监控信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.kuang</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>

修改springcloud-consumer-dept-80 里面的myrule中的Kuangrule 把自定义KuangRandomRule 去掉 ,这样访问页面时可以不断跳转而不崩溃

package com.kuang.myrule; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RoundRobinRule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class KuangRule { @Bean public IRule myRule(){ return new RoundRobinRule();//默认为轮询,现在自定义了KuangRandomRule } }

先启动7001 端口 再启动7002端口 再启动 springcloud-provider-dept-hystrix-8001端口 再启动80端口

测试结果:

image-20210517094856217

image-20210517094913858

image-20210517094933464

目录

image-20210517094949064

访问正常的springcloud-provider-dept-8001

修改DeptController.java

package com.kuang.springcloud.controller; import com.kuang.springcloud.pojo.Dept; import com.kuang.springcloud.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; //Controller提供restful服务 @RestController //以json格式传输 public class DeptController { @Autowired //这里就体现了controller调service层 private DeptService deptService; //获取一些配置的信息,得到具体的微服务 @Autowired private DiscoveryClient client; @PostMapping("/dept/add")//添加请求一般以post提交 get不安全 public boolean addDept(Dept dept){ return deptService.addDept(dept); } @GetMapping("/dept/get/{id}") public Dept get(@PathVariable("id") Long id){ Dept dept = deptService.queryById(id); if (dept==null){ throw new RuntimeException("Fail"); } return dept; } @GetMapping ("/dept/list") public List<Dept> queryAll(){ return deptService.queryAll(); } //注册进来的微服务~ 获取一些消息~ @GetMapping("/dept/discovery") public Object discovery(){ // 获取微服务列表的清单 List<String> services = client.getServices(); System.out.println("discovery=>services:" + services); List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT"); for (ServiceInstance instance : instances) { System.out.println( instance.getHost()+"\t" + instance.getPort()+"\t" + instance.getUri()+"\t" + instance.getServiceId() ); } return this.client; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdgzf.html