List item
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:以下是本篇文章正文内容,下面案例可供参考
调用微服务的两种方法1.微服务名字【Ribbon】 2.接口和注解【feign】
负载均衡
使java http客户端变得更加容易实现
在feign的实现下,我们只需要创建一个接口并使用注解的方式来配置他,面向接口编程,feign集成了ribbon,通过轮询的方式实现了客户端的负载均衡,feign只需要定义服务绑定的接口且以声明式的方法,优雅而且简单的实现了服务调用。
1.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2.启动类添加注解
@EnableEurekaClient
3.config类
@Configuration
public class ConfigBeen {
@Bean
@LoadBalanced //Ribbon 负载均衡
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
4.controller类
@Autowired
private RestTemplate restTemplate;
//通过Ribbon做负载均衡
public static final String REST_URL_PREFIX ="http://SPRINGCLOUD-PROVIDER-DEPT";//http后面为所需要的服务名
@GetMapping("/consumer/dept/get/{id}")
public Dept getDept(@PathVariable("id") Long id)
{
return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
}
pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动类
@EnableEurekaClient
@EnableDiscoveryClient
配置yaml
eureka:
client:
service-url:
#单机
defaultZone: http://localhost:7001/eureka/
#集群
#defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
instance:
instance-id: springcloud-provider-8081
info:
app.name: hj
company.name: pp
获取服务具体的信息
@Autowired
private DiscoveryClient client;
@GetMapping("/dept/discover")
public Object discovery(){
List<String> list = client.getServices();
List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
return this.client;
}
// RoundRobinRule 轮询 默认
// RandomRule 随机
// AvailabilityFilteringRule 先过滤跳闸,访问故障的服务,在对剩下的服务做轮询
// RetryRule 先按照轮询获取服务,如果获取服务失败,会在指定的时间内重试,默认为500
自定义策略
1.启动类加上
//加载时调用ribbon的配置文件,自定义的文件不能被ComponentScan扫描,name为服务名称
@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = MyRuleConfig.class)
2.定义配置类,注意包不能在启动类之下,需要高于启动类
@Configuration
public class MyRuleConfig {
//自定义选择服务器
@Bean
public IRule myRule()
{
return new MyRandomRule();//默认为轮询,现在被自定义配置
}
}
3.自定义询问服务器规则仿照Ribbon的询问规则
新建一个自定义规则的类
例如将RandomRule中的下面这两行替换为自己的询问算法
//int index = this.rand.nextInt(serverCount);//获取区间随机数
//server = (Server)upList.get(index);//从活着的服务中随机选取一个
换为每个服务访问5次
if(total<=5)
{
upList.get(currentIndex);
total++;
}else {
total = 0;
currentIndex++;
if(currentIndex>upList.size()){
currentIndex =0;
}
upList.get(currentIndex);
}
1.pom导入包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2.编写service文件,注意需要与服务生产者提供接口一致
@Component
//feign调用声明接口,服务生产者的服务名
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {
@PostMapping("/dept/add")
public boolean addDept(Dept dept);
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id") Long id);
@GetMapping("/dept/get/all")
public List<Dept> queryAll();
}
3.消费者主启动类
@EnableFeignClients(basePackages = {"com.hj"})
4.消费者控制类
@Autowired
private DeptClientService service=null;