zoukankan      html  css  js  c++  java
  • spring cloud注册服务与发现(踩着坑往前爬)

    spring cloud简介

    Spring Cloud为开发人员提供了快速构建分布式系统中的一些通用模式(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式 会话,群集状态)。

    分布式系统的协调导致了锅炉板模式,并且使用Spring Cloud开发人员可以快速地站起来实现这些模式的服务和应用程序。 它们可以在任何分布式环境中正常工作,包括开发人员自己的笔记本电脑,裸机数据中心和受管平台,如Cloud Foundry。

    创建注册中心

    使用工具:idea

    新建一个maven工程,啥都不要勾选

    然后再选一个module

    填写项目名和包名

    创建完了之后啥也不要改

    创建完了之后pom.xml如下

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>cn.zhiwei</groupId>
        <artifactId>eureka-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>eureka-server</name>
        <description>Demo project for Spring Boot</description>
        <!--父节点-->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <!--编码配置-->
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
        </properties>
        <!--会实际下载jar包-->
        <dependencies>
            <!--eureka-server服务-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
            <!--测试用的-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <!--//只是对版本进行管理,不会实际引入jar  -->
        <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>
        <!--版本库-->
        <repositories>
            <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshots</name>
                <url>https://repo.spring.io/snapshot</url>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
    
    </project>
    View Code

    我的idea创建的spring boot的版本是二点多的

    启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加

    package cn.zhiwei;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer//表明自己是个服务端
    public class EurekaServerApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaServerApplication.class, args);
    	}
    }
    

      eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),

    在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml

    server:
      port: 8761
    server的配置文件appication.yml
    eureka:
      instance:
        hostname: localhost
      client:
      #通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
      #不给这几句话会报错
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    eureka server 是有界面的,启动工程,打开浏览器访问:http://localhost:8761 ,界面如下:

     创建一个服务提供者 (eureka client)

    当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

    和刚才一样新建一的module

    在pom.xm里面新加一个节点

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

    通过注解@EnableEurekaClient 表明自己是一个eurekaclient.

    package cn.zhiwei;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    @EnableEurekaClient//表明自己是个客户端
    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 String name) {
    		return "hi "+name+",i am from port:" +port;
    	}
    }
    

      仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:

    eureka:
      client:
        serviceUrl:
        #注册地址
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8763
    spring:
      application:
      #spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
        name: service-hi

    启动工程,打开http://localhost:8761 ,即eureka server 的网址:

    你会发现多了一个服务

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

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

    hi 威哥,i am from port:8763
    

      

  • 相关阅读:
    Java中数据结构对应mysql数据类型
    pom.xml设置字符编码
    java.lang.IllegalStateException: Service id not legal hostname (/test-gw-aqa)
    org.springframework.context.ApplicationContextException: Unable to start web server; nested exceptio
    nacos的三种部署方式
    o.s.c.a.n.c.NacosPropertySourceBuilder : get data from Nacos error,dataId:application-dev.yaml
    java使用split注意事项
    《非暴力沟通》之读书心得
    js存储token
    SpringCloudGateWay之网关跨域问题解决
  • 原文地址:https://www.cnblogs.com/liuzhiw/p/8732536.html
Copyright © 2011-2022 走看看