从零开始搭springcloud(四) ribbon和feign实现负载均衡

一、简介

    Springcloud有两种方式实现负载均衡调用服务,一种是ribbon+restTemplate,另一种是feign,首先是使用前者。

二、ribbon+restTemplate

   首先配置pom:
  <dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-ribbon</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
   接着在yml文件中指定服务注册中心,以及程序名,ip地址,如果是从配置中心读取配置,还需要配置注册中心地址。
eureka:
  client:
    serviceUrl:
      defaultZone: http://username:pass@ip/eureka/
  instance:
    ip-address: ip
    prefer-ip-address: true
server:
  port: 8764
spring:
  application:
    name: service-ribbon
  sleuth:
    sampler:
      percentage: 1.0
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
      label: master
      profile: dev
      name: hfz-ribbon
      username: usename
      password: pass
   在Application.java中通过@EnableDiscoveryClient向服务中心注册;并且注入一股bean--restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
@EnableHystrix
@EnableHystrixDashboard
@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
public class HfzribbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(HfzribbonApplication.class, args);
	}

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}
   写一个类HelloService,通过restTemplate来消费服务的“/hi”接口,在这里可以直接用在注册中心配置的程序名替代来具体的url地址,ribbon会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名
@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVER-HI/hi?name="+name,String.class);
    }
    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}
   然后可以在Controller中调用服务就行
@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;
    @RequestMapping(value="/hi")

    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }

三、Feign

   Feign是一个声明式的伪Http客户端,这使得用来写Http客户端变得更简单。要使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,可以Eureka结合,默认实现了负载均衡的效果。以下是搭建过程    首先是pom文件:
        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
    yml文件如下:
	eureka:
	  client:
	    serviceUrl:
	      defaultZone: http://localhost:8761/eureka/
	server:
	  port: 8765
	spring:
	  application:
	    name: service-feign
   在程序的启动类Application上加上@EnableFeignClients注解开启Feign的功能
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class HfzfeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(HfzfeignApplication.class, args);
	}
}
   定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务
	@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
	public interface SchedualServiceHi {
	
	    @RequestMapping(value = "/hi",method = RequestMethod.GET)
	    String sayHiFromClientOne(@RequestParam(value = "name") String name);
	}
   然后是controller层上对外暴露的接口 @RestController public class HiController {
	    @Autowired
	    SchedualServiceHi schedualServiceHi;
	
	    @RequestMapping(value = "/hi",method = RequestMethod.GET)
	    public String sayHi(@RequestParam String name){
	        return schedualServiceHi.sayHiFromClientOne(name);
	    }
	}
   然后可以通过访问接口来测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值