本站公告: PUI正式上线,欢迎体验。

spring Cloud 微服务学习之服务发现与注册

框架积累 Mignon 1500浏览 0评论

公司要搭建PAAS云平台,一直在忙PAAS平台的文档和编码工作,博客好久没有维护了,关于Spring Cloud系列的文章一直都没有写,今天刚刚好休息,就来讲一下spring cloud服务发现与注册。

一如既往,创建一个maven项目(eureka-server),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>com.sixtykb</groupId>
    <artifactId>edge-service</artifactId>
    <packaging>jar</packaging>

    <name>edge-service</name>
    <description>Edge Service project for Spring Cloud</description>

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>

    <properties>
        <start-class>com.sixtykb.cloud.DiscoveryApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Brixton.SR1</spring-cloud.version>
    </properties>
    
    <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>
        
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.cloud</groupId>-->
            <!--<artifactId>spring-cloud-starter-oauth2</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>com.sixtykb</groupId>-->
            <!--<artifactId>logging-sdk</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
            <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
            <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
            <resources>
                <resource>
                    <directory>${project.basedir}/src/main/resources</directory>
                </resource>
            </resources>
            <testResources>
                <testResource>
                    <directory>${project.basedir}/src/test/resources</directory>
                </testResource>
            </testResources>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
</project>

如果要看具体项目请查看:https://coding.net/u/meyer/p/discovery-service/git/tree/master

创建类:com.sixtykb.cloud.DiscoveryApplication

/**
 * Created by meyer on 16/3/4.
 */
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryApplication extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.web(true).sources(DiscoveryApplication.class);
    }
}

在目录 src/main/resources创建文件:bootstrap.yml

server:
  port: ${app.port:8761}
spring:
  application:
    name: discovery
# eureka 配置
eureka:
  instance:
    hostname: ${app.hostname:${spring.application.name}}
    app-group-name: ${app.group:COMPONENTS}
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
info:
  app.name: ${spring.application.name}
  app.hostname: ${eureka.instance.hostname}
  app.port: ${server.port}
  app.group: ${eureka.instance.app-group-name}
  app.version: ${app.version:1.0.0}
  app.description: "服务发现以注册(Eureka)"
  eureka.url: ${eureka.client.service-url.defaultZone}
  log.file: ${log.path:${java.io.tmpdir:/var/log}}/${spring.application.name}.log
logging:
  file: ${log.path:${java.io.tmpdir:/var/log}}/${spring.application.name}.log
# 开启重启api
endpoints:
  restart:
    enabled: true

使用命令启动项目:mvn spring-boot:run。这样服务注册服务端就完成,这里我们就不往深里挖了。


一如既往,创建maven项目(eureka-client),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>com.sixtykb</groupId>
    <artifactId>eureka-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Passport Server project for Spring Cloud</description>
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.3.5.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <<properties>
        <start-class>com.sixtykb.cloud.DiscoveryClientApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Brixton.SR1</spring-cloud.version>
    </properties>
    
    <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>
   <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

创建spring boot的启动类:com.sixtykb.cloud.DiscoveryClientApplication

/**
 * Created by meyer on 16/3/17.
 */
@EnableDiscoveryClient
@SpringCloudApplication
public class DiscoveryClientApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(PassportApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.web(true).sources(PassportApplication.class);
    }
}

在目录 src/main/resources创建文件:bootstrap.yml

该文件指定eureka-server运行的ip和端口号

eureka:
  client:
    serviceUrl:
      defaultZone: http://user:${eureka.password}@localhost:8761/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}

如果eureka-server运行后再启动eureka-client。

在浏览器输入:http://localhost:8761/就可以看到eureka-client是否注册成功。

有关分布式系统demo源码git地址:https://coding.net/u/meyer

Maven 阿里云资源库:http://maven.60kb.com/

转载请注明:码农博客 » spring Cloud 微服务学习之服务发现与注册

游客
发表我的评论 换个身份
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  • 验证码 (必填)点击刷新验证码