spring cloud(一) 服务注册与发现使用

2018-08-31  本文已影响135人  Learn_Java

使用场景

系统中有多个服务,服务之间使用轻量级通信协议例如http restful接口.但是服务之间的调用存在硬编码,无法动态伸缩.这时候就可以使用服务注册与发现组件

准备工作

框架 版本号
Spring Boot 1.4.0.RELEASE
Spring Cloud Brixton.SR5
实例 端口 功能
eureka-server 8761 注册发现服务器(单节点)
user-server 9090、9091 用户管理服务器(多节点,演示均衡负载)
eureka-client 8090 服务消费者,调用用户管理接口(演示用本身不提供服务)

如何使用

使用Eureka组件

spring boot 的版本号

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
 </parent>

spring cloud 的版本号

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

搭建注册中心eureka-server

<dependency>
    <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
eureka:
  client:
    # 服务器自身是否注册到eureka
    register-with-eureka: false
    # 是否同步节点
    fetch-registry: false
    # eureka页面url
    service-url:
      defaultZone: http://${eureka.instance.ip-address}:${server.port}/eureka/
  instance:
    ip-address: localhost
    hostname: discovery-server

server:
  port: 8761

spring:
  application:
    name: discovery-server

@SpringBootApplication
@EnableEurekaServer
public class ExampleSpringCloudEurekaServerApplication {

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

}

从页面中看出Instances currently registered with Eureka当前还没有节点来注册中心注册

搭建用户管理服务器(user-server)

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
</dependencies>

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    hostname: user-server

server:
  port: 8090

spring:
  application:
    name: user-server

方便演示简单定义一个返回值.不用访问数据库

@RestController
@RequestMapping(value = "/user")
@Slf4j
public class UserController {

    @GetMapping
    private String getUser() {
        log.info("getUser ...");
        return "userId: 1";
    }
}
@SpringBootApplication
@EnableDiscoveryClient
public class ExampleSpringUserServerApplication {

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

}

如何启动多个实例,可以先mvn package 打包成jar.然后使用命令
java -jar user-server.jar --server.port=9090
java -jar user-server.jar --server.port=9091

搭建服务消费者(eureka-client)

<dependencies>

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

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

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

</dependencies>

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    hostname: eureka-client

server:
  port: 8090

spring:
  application:
    name: eureka-client

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ExampleSpringCloudEurekaClientApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@FeignClient(name = "user-server")
public interface UserServerFeign {

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    String getUser();
}


@RestController
public class UserFeignController {

    @Autowired
    private UserServerFeign userServerFeign;

    @GetMapping("/feign/user")
    private String getUserByFeign() {
        return userServerFeign.getUser();
    }
}

服务消费者(eureka-client)已经被注册到注册中心

http://172.16.81.1:8090这个地址是从注册中心获取的,可以点击注册中心的eureka-client,会调转到一个url

  1. 可以消费到user
  2. 用户管理服务节点的接口日志很平均.均衡负载可以正常工作
    测试通过

示例代码

https://github.com/gbKidCoding/spring-cloud-example-discovery-eureka.git

参考资料

继续阅读

spring cloud(二) 熔断器Hystrix使用

上一篇下一篇

猜你喜欢

热点阅读