SpringCloud初识(Eureka+hystrix+ribbon)

0:

image.png

本文的项目结构图,用户请求myweb,myweb通过eureka请求对应的服务提供者

1.Eureka服务注册中心

新建项目加入依赖:

 
        org.springframework.boot
        spring-boot-starter-parent
        1.5.13.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Edgware.SR3
                pom
                import
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
              
        
            org.springframework.cloud
            spring-cloud-config-server
            1.4.3.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
            1.4.4.RELEASE
        
    

启动类:

@SpringBootApplication
@EnableEurekaServer
public class MyEurake {
    public static void main(String[] args) {
        SpringApplication.run(MyEurake.class, args);
    }
}

配置文件application.yml:

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

8761为默认端口,eureka.client.register-with-eureka=false,eureka.client.fetch-registry: false 则表示不需要把自己注册到服务端
接下来启动服务,访问localhost:8761,


image.png

如图则为Eureka页面,我们提供服务会注册到Eureka则会显示到这边;

2.服务提供者向Eureka注册服务

新建项目加入依赖:


        org.springframework.boot
        spring-boot-starter-parent
        1.5.13.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Edgware.SR3
                pom
                import
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
    

启动类:

@SpringBootApplication
@EnableEurekaClient
public class ProviderMain {
    public static void main(String[] args) {
        SpringApplication.run(ProviderMain.class, args);
    }
}

配置文件application.yml:

server:
  port: 28080
spring:
  application:
    name: service-provider
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}

写个测试接口:

@RestController
public class TestController {
    @GetMapping("/test")
    public Map test(){
        return new HashMap(){
            {
                put("test","success");
            }
        };
    }
}

spring.application.name为注册的服务名,eureka.instance为注册的实例配置,配置完成后启动项目,再次刷新localhost:8761则发现多了个服务
image.png

关于actuator,可查看服务健康状态在hystrix中也需要使用到actuator
pom.xml加入:

        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-security
        

配置文件:

management:
  context-path: /admin
security:
  basic:
    path: /admin
  user:
      password: 123
      name: abc

则表示管理页面的基础路径为/admin,security针对/admin的路径做认证处理,
访问localhost:28080/admin/health或localhost:28080/admin/health.json


image.png

访问localhost:28080/admin/beans


image.png

3.通过服务注册中心调用服务:

新建项目加入依赖:


        org.springframework.boot
        spring-boot-starter-parent
        1.5.13.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Edgware.SR3
                pom
                import
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
    

配置文件:

server:
  port: 38080
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
      register-with-eureka: false

主要是指定注册中心地址,然后自己不注册到中心
启动类:

@SpringBootApplication
@EnableEurekaClient
public class MyWeb {
    public static void main(String[] args) {
        SpringApplication.run(MyWeb.class,args);
    }
}

Controller类调用服务:

@RestController
public class TestController {
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/test")
    public Map test(){
        List list = discoveryClient.getInstances("SERVICE-PROVIDER");
        String serviceurl = list.get(0).getUri().toString();
        RestTemplate restTemplate = new RestTemplate();
        Map object = restTemplate.getForObject(serviceurl + "/test", Map.class);
        return object;
    }

通过注入discoveryClient来调用服务,获取实例,获取url,拼接url,进行调用


image.png

4.使用feign进行服务调用,ribbon做负载均衡

复制项目serviceProvider到serviceProvider2,修改端口为28081,修改接口test返回值
在myweb项目中,修改pom.xml加入如下:

       
            org.springframework.cloud
            spring-cloud-starter-ribbon
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
        

myweb启动类添加注解@EnableFeignClients
myweb项目新增接口类:TestProvider

@FeignClient(name = "SERVICE-PROVIDER")
public interface TestProvider {
    @GetMapping("/test")
    Map test();
}

Controller中修改使用testProvider进行调用

    @Autowired
    TestProvider testProvider;
    @GetMapping("/test")
    public Map test(){
        return testProvider.test();
    }

启动两个provider,查看注册中心
image.png

访问localhost:38080/test,两个服务提供方的访问结果交叉出现

修改ribbon的负载策略

ribbon默认的负载策略为轮询加载,可通过配置规则来修改负载策略
修改负载策略为随机:
myweb项目中新增类RibbonConfig

@Configuration
public class RibbonConfig {
    @Bean
    public IRule getRule(){
        //默认 RoundRobinRule  可不配置
        return new RandomRule();
    }
}

重启myweb即可

5.hystrix服务降级以及dashboard服务监控

两个服务提供项目serviceProvider中修改pom.xml加入:

       
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        

启动类新增注解@EnableCircuitBreaker
接口类TestController的接口中添加注解 @HystrixCommand如:

@RestController
public class TestController {
    @GetMapping("/test")
    @HystrixCommand
    public Map test(){
        return new HashMap(){
            {put("test","success");}
        };
    }
}

myweb中修改pom.xml

        
            org.springframework.cloud
            spring-cloud-starter-hystrix
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
        

启动类增加注解

@EnableCircuitBreaker
@EnableHystrix
@EnableHystrixDashboard

配置文件新增节点:feign.hystrix.enabled= true
新增类TestHystrix做降级处理:

@Component
public class TestHystrix implements FallbackFactory {
    @Override
    public TestProvider create(Throwable throwable) {
        return new TestProvider() {
            @Override
            public Map test() {
                Map map = new HashMap();
                map.put("msg","server error");
                return map;
            }
        };
    }
}

修改TestProvider,添加降级处理:

@FeignClient(name = "SERVICE-PROVIDER",fallbackFactory = TestHystrix.class)
public interface TestProvider {
    @GetMapping("/test")
    Map test();
}

修改完成后,重启服务,当进行负载均衡的服务提供方突然停掉一台的时候(因为hystrix进行降级处理的默认失败率为50,当失败率为50时 即进行降级处理):


image.png
服务监控

当我们访问localhost:28080/admin/hystrix.stream时,如图:

image.png

如果没有请求,则会一直ping,监控的接口需要有注解@HystrixCommand
访问http://localhost:38080/hystrix如图:
image.png

在输入框中输入我们的http://localhost:28080/admin/hystrix.stream则可看到服务的相关信息
image.png

你可能感兴趣的:(SpringCloud初识(Eureka+hystrix+ribbon))