• eureka 服务注册与发现


    1.创建父工程来管理依赖包

    <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>com.guo</groupId>
      <artifactId>springcloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>pom</packaging>
      
      	<parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/>
        </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.RELEASE</spring-cloud.version>
        </properties>
    
    	<dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <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>
    
        <modules>
        	<module>guo-eureka-server</module>
        	<module>guo-eureka-client</module>
        </modules>
    </project>
    

      

    2.创建jar子项目 guo-eureka-server , guo-eureka-client
    guo-eureka-server pom.xml

    <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>
      <parent>
        <groupId>com.guo</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>guo-eureka-server</artifactId>
      
      <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
           </dependency>
       </dependencies>
        
    </project>
    

      


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

    package com.guo.eurekaserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    /**
     * 重要的事情说三遍:
     * 文档地址: http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
     */
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run( EurekaServerApplication.class, args );
        }
    }
    

      

    4.eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),
    在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml

    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
    

      

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

    6.创建服务提供者 

    guo-eureka-client pom.xml

    <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>
      <parent>
        <groupId>com.guo</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>guo-eureka-client</artifactId>
      
      <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
      
    </project>
    

      7.配置 application.yml 指定注册中心 defaultZone: http://localhost:8761/eureka/

    server:
      port: 8762
    
    spring:
      application:
        name: service-hi
    
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    

      8.注册服务

    添加注解 @EnableEurekaClient

    package com.guo.eurekaclient;
    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
    @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;
        }
    
    }
    

      

  • 相关阅读:
    JavaScript DOMContentLoaded 和 load事件的区别
    Redux源码分析之combineReducers
    Redux源码分析之createStore
    LeetCode003 无重复字符的最长子串
    Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
    在Win10中安装虚拟机:VMware Workstation Player+Ubuntu20.04
    C# 对象复制三种方法效率对比——反射、序列化、表达式树
    nginx在个人网站上的优化(一)
    VUE3.0的打包配置修改
    Jenkins构建项目连接Repository URL的填坑之路
  • 原文地址:https://www.cnblogs.com/yun965861480/p/10385952.html
Copyright © 2020-2023  润新知