SpringCloud+Eureka+Zuul搭建

*一、系统框架服务说明

1、Eureka简介
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。Eureka包含两个组件:一个是Eureka Server,作为服务注册中心,提供注册服务,用于注册各个微服务,以便服务相互之间调用;一个是Eureka Client,Eureka Client是一个java客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。
2、Zuul
Zuul是Netflix开源的服务网关/API网关,提供动态路由、监控、弹性、安全性等功能。Zuul 和 Eureka 进行整合时,Zuul 将自身注册到 Eureka 服务中,同时从 Eureka 中获取其他微服务信息,以便请求可以准确的通过 Zuul 转发到具体微服务上。

二、项目搭建

1、Eureka Server注册中心
1.1 新建一个Eureka微服务,pom.xml引入Eureka Server对应jar包

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
1.2 在application.yml中添在这里插入代码片加Eureka配置
server:
  port: 8080
  servlet:
    context-path: /
spring:
  application:
    name: eureka
eureka:
  instance:
    hostname: 192.168.1.107
  client:
    #实例是否在eureka服务器上注册自己的信息以提供其他服务发现,服务端为false;
    register-with-eureka: false
    #是否获取eureka服务器注册表上的注册信息,服务端为true
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
1.3 在程序启动类上添加@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}
1.4 启动程序,访问Eureka主页,可查看已注册的微服务,地址为:服务器地址+微服务端口号,如:http://192.168.1.107:8080/

2、Eureka Client微服务(可有多个)
2.1 新建一个微服务,pom.xml引入Eureka Client对应jar包

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
2.2 application.yml添加Eureka配置
server:
  port: 8081
  servlet:
    context-path: /
spring:
  application:
   name: loser-service
eureka:
  client:
    service-url:
      defaultZone: http://192.168.1.107:8080/eureka/
    #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
    fetch-registry: true
    #实例是否在eureka服务器上注册自己的信息以提供其他服务发现,默认为true
    register-with-eureka: true
  instance:
    status-page-url:
      http://localhost:${server.port}/swagger-ui.html
    prefer-ip-address: true

2.3 微服务启动类添加@EnableEurekaClient注解

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

2.4 启动程序,访问Eureka主页,查看该服务是否成功注册到注册中心
在这里插入图片描述
3、Feign客户端
3.1 说明
注册到Eureka Server注册中心的微服务之间可以通过Feign注解相互访问,该项目作为一个服务间相互访问的样例
3.2 新建一个Feign项目,引入对应的jar包

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

3.3 application.yml添加Eureka配置

server:
  port: 8082
  servlet:
    context-path: /
spring:
  application:
   name: feign-service
eureka:
  client:
    service-url:
      defaultZone: http://192.168.1.107:8080/eureka/
    #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
    fetch-registry: true
    #实例是否在eureka服务器上注册自己的信息以提供其他服务发现,默认为true
    register-with-eureka: true
  instance:
    status-page-url:
      http://localhost:${server.port}/swagger-ui.html
    prefer-ip-address: true

3.4 在微服务启动类上添加注册

@SpringBootApplication
@EnableEurekaClient
//basePackages 值为服务间调用接口所在的包路径
@EnableFeignClients(basePackages = "com.fuck.study.feign")
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class);
    }
}

3.5 服务间调用接口定义

package com.fuck.study.feign;

import com.fuck.study.model.UserVo;
import com.fuck.study.param.ResponseBase;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

//value值为所要调用微服务的服务名
@FeignClient(value = "LOSER-SERVICE")
public interface LoserClientFiegn {
    @GetMapping("/user/getUser")
    //参数必须加上@RequestParam等注解,否则可能会报请求方式出错等错误
    public ResponseBase<UserVo> getUser(@RequestParam("account") String account);
}

这里也展示一下所调用微服务LOSER-SERVICE里的这个接口是怎么写的

@RestController
@RequestMapping("/user")
@Api(description = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("查询账号信息")
    @GetMapping("/getUser")
    @ApiImplicitParam(name = "account",value = "账号",required = true)
    public ResponseBase<UserVo> getUser(@RequestParam("account") String account){
        return userService.getUser(account);
    }
}

3.6 接口调用,可直接访问到另一个微服务的接口

@Service
@Slf4j
public class UserService {

    @Autowired
    LoserClientFiegn loserClientFiegn;
    public ResponseBase<UserVo> getUser(String account) {
        return loserClientFiegn.getUser(account);
    }
}

4、Zuul搭建
4.1 新建Zuul微服务,pom.xml引入对应的jar

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

4.2 application.yml添加Eureka和Zuul配置

server:
  port: 8083
  servlet:
    context-path: /
    
eureka:
  client:
    service-url:
      defaultZone: http://192.168.1.107:8080/eureka/
    #此客户端是否获取eureka服务器注册表上的注册信息,默认为true
    fetch-registry: true
    #实例是否在eureka服务器上注册自己的信息以提供其他服务发现,默认为true
    register-with-eureka: true

zuul:
  routes:
    #传统的路由配置,此名称可以自定义
    loser:
      #映射的url
      path: /loser/**
      #被映射的url
      serviceId: LOSER-SERVICE
    feign:
      path: /feign/**
      serviceId: FEIGN-SERVICE

4.3 微服务启动类添加注解

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

4.4 启动服务,之前访问LOSER-SERVICE的/user/getUser接口的访问地址为:http://192.168.1.107:8081/user/getUser?account=admin,现可通过Zuul网关代理进行访问,地址为:http://192.168.1.107:8083/loser/user/getUser?account=admin,这样即使LOSER-SERVICE服务的IP和端口发生变更,通过Zuul访问的地址也不会变

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值