SpringCloud-NetFlix常用组件及其搭建

1常用组件:

(1)erueka

(2)ruul

(3)ribbon(feign、openFeign)

(5)hystrix(sentinel)

(6)config

EruekaServer的环境搭建

描述:导入eruekaServer的依赖;

在启动类开启@EnableEruekaServer;

在配置文件中配置其:ip、端口、自己的服务注册地址

实操

<!--        eureka的jar包,里面有eureka的服务端和客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
@SpringBootApplication
@EnableEurekaServer
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
server:
  port: 1001 #eureka端口号

eureka:
  instance:
    hostname: localhost #eureka的ip地址
  client:
    registerWithEureka: false #关闭服务注册--这是eureka自己的服务注册
    fetchRegistry: false #关闭服务发现--这是eureka自己的服务发现
    serviceUrl:  #自己的服务注册地址,自己给自己注册
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: false #关闭自我保护

EruekaClient的环境搭建

描述:导入EruekaClient依赖+web依赖;

启动类:@EnableDiscoveryClient 或 @EnableEurekaClient注解,也可以不加,自动开启;

yaml配置:端口、服务名、实例id,erueka的注册地址

实操

<!--     eureka的客户端jar包   -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
<!--       必须要SpringBoot和web的整合包才能正常连接 eureka服务器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
@SpringBootApplication
//@EnableDiscoveryClient
//@EnableEurekaClient
public class UserApp {
    public static void main(String[] args) {
        SpringApplication.run(UserApp.class, args);
    }
}
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka/   #Eureka服务注册中心地址
  instance:
    instance-id: springCloud-user5   #实例id
server:
  port: 1005  #自身端口号
spring:
  application:
    name: springCloud-user  #服务名

Ribbon环境搭建

描述:导入ribbon依赖;

在没有使用Feign之前,服务之间是通过restTemplate对象通过getForObject调用接口的,

我们需要在自定义restTemplate对象上加上@LoaderBalanced开启负载均衡功能;

实操

         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
@SpringBootApplication
public class OrderApp
{

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
    public static void main( String[] args )
    {
        SpringApplication.run(OrderApp.class,args);
    }
}
@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/order/{id}")
    public User getUsers(@PathVariable("id") Long id) {
//        User user = restTemplate.getForObject("http://localhost:1004/user/" + id, User.class);
        //做了负载均衡的代码书写
        User user = restTemplate.getForObject("http://springCloud-user/user/" + id, User.class);
        return user;
    }
}

hystrix环境搭建

两种情况:

1.如果没有使用Feign,采用的是ribbon做的负载均衡的话

2.使用了Feign

情况一:

  1. 第一步,导入hystrix依赖
  2. 第二部,主启动类加注解,@EnableCircuitBreaker,开启熔断功能
  3. 第三步,在需要开启熔断功能的方法上,加注解@HystrixCommand(fallbackMethod="xxx"),xxx是降级方法
  4. 第四步,定义降级方法,方法名需要和fallbackMethod的值一致,形参列表和返回值类型需要和目标方法一致

情况二:使用了Feign或者OpenFeign,做hystrix熔断降级

第一步:Feign或者OpenFeign整合了hystrix,则yaml配置:

  1. feign.hystrix.enable=true,开启hystrix功能
  2. @FeignClient标签中,定义fallbackFactory,指定降级类

  3. 实现FallbackFactory接口,同时指定泛型为feign接口,覆写create方法,返回一个feign接口的匿名内部类,类中写降级方法

OpenFeign的环境搭建

描述:导入openFein的依赖;在主启动类上加上@EnableFeignClients;

创建Fein接口;

实操

        <!--2.导入Feign的包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
@SpringBootApplication
@EnableFeignClients //开启Feign
public class PayApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(PayApp.class,args);
    }
}
import com.lh.domain.User;
import com.lh.web.fallBackFactory.UserClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @FeignClient("springCloud-user") --访问服务的服务名
 */
@FeignClient(value = "springCloud-user",fallbackFactory = UserClientFallbackFactory.class)
public interface UserClient {
    @GetMapping("/user/{id}")
    User getUsers(@PathVariable("id") Long id);
}

sentinel的环境搭建(和Hystrix步骤差不多)

描述:sentinel和hystrix都是熔断降级,但Feign/OpenFeign整合了ribbon和hystrix,没有整合sentinel;

步骤:1.导入sentinel依赖

2.yaml开启sentinel

3.feign接口@FeignClient标签中,定义fallbackFactory,指定降级类

4.写降级类

Ruul的环境搭建

描述:导入Ruul依赖+EruekaClient依赖;

启动类@EnableZuulProxy

yaml配置:端口、服务名、实例id、Erueka服务注册中心地址

实操

       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
       </dependency>
@SpringBootApplication
@EnableZuulProxy
public class ZuulApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(ZuulApp.class,args);
    }
}
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka/  #Erueka服务注册中心地址
  instance:
    instance-id: springCloud-zuul  #实例id
server:
  port: 10011   #端口
spring:
  application:
    name: springCloud-zuul  #服务名
zuul:  #配置路由routes
  ignored-services: "*"  #禁止通过服务名直接访问
  prefix: "/services"  #访问zuul时加上前缀
  routes:
    "springCloud-user": "/user/**"  #给访问的服务配置别名
    "springCloud-pay": "/pay/**"

Config环境搭建

描述:

配置中心服务端配置:

第一步,导入config-server依赖+EruekaClient

第二步,主启动类加注解,@EnableConfigServer,开启配置中心

第三步,配置文件中,配置远程仓库地址,仓库账号密码;端口,服务名、实例id,Erueka注册中心地址

客户端配置:

第一步,导入config-client依赖

第二步,创建bootstrap.yml配置文件,配置中心地址config.uri,要拉取的配置文件名name,环境名profile

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值