搭建SpringCloud微服务一整套完整项目(Eureka+Zuul+Hystrix+Feign)

搭建SpringCloud微服务一整套完整项目(Eureka+Zuul+Hystrix+Feign+Ribbon)

  • Eureka
  • Zuul
  • Hystrix
  • Feign
  • Ribbon(Zuul 、RestTemplate 、 Fegin都使用了ribbon负载均衡)
首先我的项目结构是一个父工程的pom文件中定义springboot和springcloud的依赖管理,其次让子工程聚合和继承父工程,这样不用每个子工程去重复去引入依赖管理,和不用maven打包的时候一个个的去打包子工程。
现在我们开始我们的搭建吧(我这使用的是springboot2.2.6.RELEASE,springcloud Hoxton.SR3
1.首先创建一个普通的springboot项目不会的看我这片博客
https://blog.csdn.net/weixin_44012722/article/details/105606595
2.导入依赖,并删除src目录因为我们这个父工程的作用就是依赖管理和聚合
2.1父工程的pom文件
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
3.创建一个eureka注册中心子模块聚合且继承父工程

在这里插入图片描述

3.1 eureka注册中心的pom文件
	<parent>
        <groupId>com.example</groupId>
        <artifactId>project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
3.2 在eureka注册中心的启动类添加标注@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}
3.3 在eureka注册中心的配置文件添加配置信息
server:
  port: 8083


eureka:
  instance:
    hostname: localhost
  client:
    #不向注册中心注册自己。
    registerWithEureka: false
    #注册中心职责是维护服务实例, 不检索服务。
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/


spring:
  application:
    name: eureka

3.4注册中心 工程就完成了 现在启动一下访问测试一下 成功!

在这里插入图片描述

4.创建一个子项目向eureka注册中心注册

4.1导入依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
4.1配置文件
server:
  port: 8084

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8083/eureka/

spring:
  application:
    name: employee

5.随便写一个Controller

@RestController
public class EmployeeController {
    @GetMapping("/test")
    public String test(){
        return "i am employee";
    }
}

6.在子项目的启动类标注上@EnableEurekaClient或者@EnableDiscoveryClient二选一

@SpringBootApplication
@EnableEurekaClient
public class EmployeeApplication {

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

}

6.启动Eureka服务注册中心,再启动子项目

访问http://localhost:8083
访问http://localhost:8084/test
6.1 Eureka搭建完成!!!

7.Fegin的使用(默认使用了Ribbon自动调用负载均衡作用)

7.1按照上面的步骤 再创建一个新的子项目 依赖 和 配置文件 如下
依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-openfeign</artifactId>
        </dependency>
配置文件
server:
  port: 8085

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8083/eureka/

spring:
  application:
    name: employeetwo

8.创建一个fegin包 在此包创建一个远程调用其他微服务的接口

@FeginClient标注value属性写application.yml里面的spring.application.name
如果调用的服务接口有接收参数一定要带上@RequestParam之类的标注否则会传送不了参数
@FeignClient(value="employee")
public interface EmployeeFegin {

    @GetMapping("/test")
    public String test();

}
8.1随便写一个Controller调用远程接口
@RestController
public class MyController {

    @Autowired
    private EmployeeFegin employeeFegin;

    @GetMapping("/test")
    public String test(){
        return employeeFegin.test();
    }

}

9.在要使用Fegin的项目的启动类标注@EnableFeignClients启动Fegin服务(调用前提是要知道发现其他服务所以@EnableEurekaClient也是必要的)

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.example.feign")
public class EmployeetwoApplication {

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

}

10.现在访问第二个子项目测试

访问 http://localhost:8083/ 看服务是否注册成功

在这里插入图片描述

访问 http://localhost:8085/test

在这里插入图片描述

11. 为了保证eureka的高可用性,我们一般采用两个eureka,相互注册,同步数据,做法很简单 ,我只在这提醒一下,只需注释掉配置文件中的registerWithEureka,fetchRegistry,defaultZone 注册地址填相互两个的eureka注册地址, 而其他微服务注册地址要填写在这两个注册中心的注册地址,用英文逗号隔开

12.Hystrix的使用(普通降级使用)

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
12.1 在启动类加上@EnableCircuitBreaker或@EnableHystrix
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class EmployeetwoApplication {

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

}
12.2 在Service实现类,写熔断后调用的回调方法
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeFegin employeeFegin;

    @Override
    @HystrixCommand(fallbackMethod = "testHystrix")
    public String test() {
        return employeeFegin.test();
    }

    private String testHystrix(){
        return "i am a testHystrix";
    }

}
12.3 在远程成调用的服务写一个Thread.sleep(5000),访问此接口测试
当 服务超过1秒未响应 则调用降级方法 可以在配置文件中配置该配置 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000

在这里插入图片描述

13.Hystrix的使用(Feign服务降级使用)

通过Feign调用接口模式,发生延迟异常,默认是不触发Hystrix降级服务,需要开启设置(D版本默认关闭ABC默认打开的)
13.1 配置文件
feign:
  hystrix:
    enabled: true
    
13.2 Fegin接口的@FeignClient注解加上fallback属性,此属性的值 就是实现Fegin接口的实现类
@FeignClient(value="employee",fallback = EmployeeHystrix.class)
public interface EmployeeFegin {

    @GetMapping("/test")
    public String test();

}
13.3 写个实现类实现Fegin接口
注意 这个类要标注@Component否则Spring容器会获取不到这个bean对象
@Component
public class EmployeeHystrix implements EmployeeFegin {
    @Override
    public String test() {
        return "i am a feign hystrix";
    }
}
13.4 测试访问

在这里插入图片描述

14 Zuul网关

14.1新创建一个子项目,引入依赖
		<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-zuul</artifactId>
        </dependency>
14.2 在启动类标注上注解@EnableZuulProxy和@EnableDiscoveryClient
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulApplication {

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

}
14.3 配置文件
server:
  port: 8086

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8083/eureka/

#配置下zuul的超时时间,因zuul启用了ribbon的负载均衡,还需要设置ribbon的超时时间,注意ribbon的超时时间要小于zuul超时时间 。
zuul:
  host:
    connect-timeout-millis: 15000 #HTTP连接超时要比Hystrix的大
    socket-timeout-millis: 60000   #socket超时
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000
14.4启动测试,访问 http://localhost:8086/employee/test , 请求方式为http://localhost:Zuul的端口/服务名/请求资源

在这里插入图片描述

到此以上四个SpringCloud配置就算完成的了,还有很多要深入了解的内容,看我另外的博客介绍






一键查询淘宝/拼多多内部优惠券,每日大额外卖红包,购物省钱的宝藏工具
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值