以下所有配置仅保证SpringBoot版本为2以上可成功运行
1. 发现服务 EurekaClient
<!--SpringBoot版本为 2.1.2.RELEASE -->
<!--客户端 -->
<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-netflix-eureka-server</artifactId>
</dependency>
通过服务名获取服务的地址
@Autowired
private EurekaClient eurekaClient;
@GetMapping("/getServiceUrl")
public String getServiceUrl() {
InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("springcloud-client-1",false);
return instanceInfo.getHomePageUrl();
}
2. 调用服务 RestTemplate
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getOtherService")
public String info1() {
return restTemplate.getForObject("http://localhost:9002/getUser", String.class);
}
3. 负载均衡 ribbon
注解版
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
随机算法轮训
@RibbonClient(name="springcloud-client-1",configuration=RibbonConfig.class)
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(SpringCloudApp.class, args);
}
}
---------------------------------------------------------------------
/**
* 这个类不能被@SpringBootApplication和@ComponentScan 扫描到
*/
@Configuration
public class RibbonConfig {
@Autowired
private IClientConfig clientConfig;
@Bean
public IRule getRibbonRule(IClientConfig clientConfig) {
//返回随机算法
return new RandomRule();
}
}
yml 配置版
#给指定的服务配置轮训算法
springcloud-client-1:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
4.Eureka的高可用
服务之间互相注册 用,分割
defaultZone: http://localhost:9002/eureka/,http://localhost:9003/eureka/
5. 服务调用 feign
<!-- SpringBoot版本 2.1.2.RELEASE -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
/**
* 主程序
*/
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(SpringCloudApp.class, args);
}
}
----------------------------------------------------------
/**
* 创建一个接口 访问路径与服务提供者一致
*/
@FeignClient("springcloud-client-1")
public interface UserFeign {
@GetMapping("/getUser")
public String getUser();
}
----------------------------------------------------------
/**
* 服务调用
*/
@RestController
public class OrderController {
@Autowired
private UserFeign userFeign;
@GetMapping("/getUserByFeign")
public String getUserByFeign() {
return userFeign.getUser();
}
}
6. hystrix容错
6.1 基于feign
1.加入依赖
<!--SpringBoot版本 2.1.2 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.开启hystrix
feign:
hystrix:
enabled: true
3. 在feign中加入fallback 即容错类此接口的实现类 并把当且接口标为bean
@Component
@FeignClient(name="springcloud-client-1",fallback=UserFeignHystrix.class)
public interface UserFeign {
@GetMapping("/getUser")
public String getUser();
}
4.写实现类 标为bean 重写的方法就是出现错误时调用的方法
@Component
public class UserFeignHystrix implements UserFeign{
@Override
public String getUser() {
return " i am hystrix , i made a mistake...";
}
}
7. Zuul网关
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class SpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(SpringCloudApp.class, args);
}
}
server:
port: 9007
spring:
application:
name: springcloud-zuul
eureka:
client:
service-url:
defaultZone: http://516:sx516@localhost:9001/eureka