2、使用euereka、zuul、config、feign、ribbon基本组件完成微服务架构

记录每一个努力的日子!
上一篇:使用maven多module构建一个web后端项目
参考项目:github 1.0.0分支

1、工具

idea

实用功能, 父项目目录/.idea/workspace.xml 中的RunDashboard下增加配置,启用RunDashboard启动方式
<option name="configurationTypes">
  <set>
    <option value="SpringBootApplicationConfigurationType" />
  </set>
</option>

2、步骤

2.1

使用 上一篇 中添加模块的方法,添加注册中心euereka、网关zuul、配置中心config、消费者、提供者

2.2

在父模块pom.xml中添加依赖

    <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>

在父模块中声明各个子模块

<modules>
    <module>xxx</module>
    <module>yyy</module>
</modules>

2.3

在euereka模块中引入依赖

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

在euereka启动类上加上注解 @EnableEurekaServer
在euereka的yml中配置注册中心服务端

spring:
  application:
    name: registry-euereka
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: false

2.4

在zuul模块中引入依赖

<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>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

在zuul启动类上增加注解 @EnableZuulProxy
修改zuul的yml文件

server:
  port: 80
  servlet:
    context-path: /

#服务的名称
spring:
  application:
    name: api-zuul
  cloud:
    config:
      name: zuul-service
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
      retry:
        initial-interval: 2000
        max-attempts: 10
        max-interval: 3000
        multiplier: 1.1
    fail-fast: true

eureka:
  instance:
    leaseExpirationDurationInSeconds: 15
    leaseRenewalIntervalInSeconds: 5
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
    registry-fetch-interval-seconds: 15

2.5

在config模块中引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

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

在config模块启动类加上注解 @EnableConfigServer

在config模块resources下增加配置(详见项目,只配置了本地,从远程仓库拉取以后会加上)

2.6

在提供者模块引入依赖

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

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-client</artifactId>
 </dependency>

修改提供者yml

server:
  port: 8110

spring:
  application:
    name: product-service
  cloud:
    config:
      name: product-service
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
      retry:
        initial-interval: 2000
        max-attempts: 10
        max-interval: 3000
        multiplier: 1.1
      fail-fast: true

eureka:
  instance:
    leaseExpirationDurationInSeconds: 15
    leaseRenewalIntervalInSeconds: 5
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
    registry-fetch-interval-seconds: 15

2.7

在消费者模块引入依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</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-netflix-ribbon</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

修改消费者yml

server:
  port: 8120

spring:
  application:
    name: order-service
  cloud:
    config:
      name: order-service
      discovery:
        enabled: true
        service-id: CONFIG-SERVER
      retry:
        initial-interval: 2000
        max-attempts: 10
        max-interval: 3000
        multiplier: 1.1
      fail-fast: true

eureka:
  instance:
    leaseExpirationDurationInSeconds: 15
    leaseRenewalIntervalInSeconds: 5
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
    registry-fetch-interval-seconds: 15

2.8

在服务端定义rest请求/test1
在消费端定义远程消费接口,注入接口即可实现远程调用,接口示例如下

@FeignClient(name = "product-service", fallback = ProductClientFallBack.class)
@Component
public interface ProductClient {

    @RequestMapping(value = "/test1", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    String test1();
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值