zoukankan      html  css  js  c++  java
  • springCloud的使用01-----服务的注册和发现

    1 搭建eureka注册服务器

      1.1 创建springboot项目,导入相应的jar包依赖

    <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.ibeifeng.hadoop</groupId>
        <artifactId>beifeng-spring-cloud-eureka-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>beifeng-spring-cloud-eureka-server</name>
        <url>http://maven.apache.org</url>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
            <relativePath />
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.RC1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- 声明为web项目 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- 注册服务器需要的依赖  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
    
        </dependencies>
        
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    </project>

      1.2 配置注册服务器的配置信息

    server: 
     port: 8761
    eureka: 
     instance: 
      hostname: localhost
     client: 
      registerWithEureka: false #有这两个属性表示不需要向自己注册,表明自己为ecureka server
      fetchRegistry: false
      serviceUrl: 
       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

      1.3 在启动类中声明该项目为注册服务器

    @SpringBootApplication  //相当于@Configuration、@EnableAutoConfiguration和 @ComponentScan
    @EnableEurekaServer
    public class SpringCloudEurekaServer {
        
        public static void main(String[] args) {
            SpringApplication.run(SpringCloudEurekaServer.class, args);
        }
    }

      1.4 启动查看是否搭建成功

                 

    2  搭建提供服务的服务提供者

      2.1 创建springboot项目,引入相关依赖

    <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.ibeifeng.hadoop</groupId>
        <artifactId>beifeng-spring-cloud-eureka-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>beifeng-spring-cloud-eureka-client</name>
        <url>http://maven.apache.org</url>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.2.RELEASE</version>
            <relativePath />
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.RC1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- 声明为web项目 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- 配置eureka -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
        </dependencies>
        
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

      2.2 配置服务提供者的信息

    eureka: 
     client: 
      serviceUrl: 
       defaultZone: http://localhost:8761/eureka/ #注册服务器地址
    server: 
     port: 8762  #提供服务站的端口
    spring:
     application: 
      name: service-hi  #服务提供者的名称

      2.3 在启动类中声明为服务提供者

    @SpringBootApplication
    @EnableEurekaClient //声明为服务提供者
    @RestController
    public class SpringCloudEurekaClient {
        
        @Value("${server.port}")
        private String port;
        
        @RequestMapping("/hi")
        String hello(){
            return "hello eureka client "+port;
        }
        
        public static void main(String[] args) {
            SpringApplication.run(SpringCloudEurekaClient.class, args);
        }
    }

      2.4 启动并查看是否注册成功

            

                  

      

        

  • 相关阅读:
    使用samba实现linux和windows文件共享
    使用li列举属性表中的某一属性
    popuptemplate的使用
    html中自动分配界面
    div中移除和添加元素
    使用v-html绑定数据,实现图片的动态转换
    使用js下载数据
    使用FeatureTable对FeatureLayer中的数据进行显示
    使用ant的checkboxGroup将列表信息添加为多选框,并根据多选框的转换进行操作
    arcgis api绘制多个点
  • 原文地址:https://www.cnblogs.com/lifeone/p/9007296.html
Copyright © 2011-2022 走看看