• spring-cloud(一)服务的注册和发现


    1.SpringClound特点

    Spring Cloud focuses on providing good out of box experience for typical use cases and extensibility mechanism to cover others.

    • Distributed/versioned configuration
    • Service registration and discovery
    • Routing
    • Service-to-service calls
    • Load balancing
    • Circuit Breakers
    • Distributed messaging

    2.创建服务注册中心

    2.1创建spring-boot工程后再依赖中添加eureka-server依赖
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    </dependencies>
    2.2添加工程配置,配置工程为注册中心
    server:
    port: 8761

    eureka:
    instance:
    hostname: localhost
    client:
    registerWithEureka: false(表明是服务端)
    fetchRegistry: false(表明是服务端)
    serviceUrl:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    spring:
    application:
    name: eurka-server
    2.3在启动类中添加注册中心的注解

    @SpringBootApplication
    @EnableEurekaServer()
    public class EurekaServerApplication {

    public static void main(String[] args) {
    SpringApplication.run( EurekaServerApplication.class, args );
    }
    }
    2.4eureka server 是有界面的,启动工程,打开浏览器访问: http://localhost:8761 ,界面如下:

    3.创建服务注册客户端中心

    3.1创建spring-boot工程后再依赖中添加eureka-client依赖
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    </dependencies>
    3.2添加工程配置,配置工程为注册中心
    server:
    port: 8762

    spring:
    application:
    name: service-hi

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

    3.3在启动类中添加注册中心的注解
    @SpringBootApplication
    @EnableEurekaClient
    @RestController
    public class ServiceHiApplication {
    
        public static void main(String[] args) {
            SpringApplication.run( ServiceHiApplication.class, args );
        }
    
        @Value("${server.port}")
        String port;
    
        @RequestMapping("/hi")
        public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
            return "hi " + name + " ,i am from port:" + port;
        }
    
    }

    2.4启动工程后发现client已经在server中注册

    需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。 启动工程,打开http://localhost:8761 ,即eureka server 的网址:

    Paste_Image.png

    你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862

    这时打开 http://localhost:8762/hi?name=forezp ,你会在浏览器上看到 :

    hi forezp,i am from port:8762

    
    
  • 相关阅读:
    hbase全分布安装配置
    ElasticSearch概述及Linux下的单机ElasticSearch安装
    Redis 集群搭建详细指南
    spark集群安装配置
    hbase全分布安装配置
    zookeeper图形界面工具zooinspector
    storm集群安装配置
    kafka集群安装配置
    sqoop配置安装以及导入
    Flume的安装部署
  • 原文地址:https://www.cnblogs.com/fengyan20150508/p/9936257.html
Copyright © 2020-2023  润新知