当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot整合Eureka搭建微服务

SpringBoot整合Eureka搭建微服务

2019年01月28日  | 移动技术网IT编程  | 我要评论

 

1.创建一个services项目,添加三个子模块client(客户端)、service(服务端)、registry(注册中心)

 1.1 创建一个services项目

 1.2 添加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>
    <packaging>pom</packaging>
    <modules>
        <module>client</module>
        <module>service</module>
        <module>registry</module>
    </modules>
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.1.2.release</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.micro</groupid>
    <artifactid>services</artifactid>
    <version>0.0.1-snapshot</version>
    <name>services</name>
    <description>services</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter</artifactid>
        </dependency>

        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid>
            <version>1.4.5.release</version>
        </dependency>

        <dependency>
            <groupid>org.springframework.cloud</groupid>
            <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

    <dependencymanagement>
        <dependencies>
            <dependency>
                <groupid>org.springframework.cloud</groupid>
                <artifactid>spring-cloud-dependencies</artifactid>
                <version>finchley.release</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencymanagement>
</project>

 

 1.3 添加子模块client(客户端)

 1.4 添加子模块service(服务端)

 

 1.5 添加子模块registry(注册中心)

 

2.搭建registry(注册中心)

 2.1 application.yml 配置

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
  client: 
    registerwitheureka: false
    fetchregistry: false

 

 2.2 编写启动程序

package com.services.registry;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.netflix.eureka.server.enableeurekaserver;

@springbootapplication
@enableeurekaserver
public class registryapplication {

    public static void main(string[] args) {

        springapplication.run(registryapplication.class);
    }
}

 

3.搭建service(服务端)

  3.1 application.yml 配置

server:
  port: 8081
spring:
  application:
    name: service
eureka:
  client:
    serviceurl:
      defaultzone: http://localhost:8080/eureka/

 

 3.2 编写启动程序

package com.services.service;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.enablediscoveryclient;

@springbootapplication
@enablediscoveryclient
public class serviceapplication {

    public static void main(string[] args) {
        springapplication.run(serviceapplication.class, args);
    }

}

 

 3.3 编写服务

package com.services.service;

import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class helloworldcontroller {
    @requestmapping("hello/{name}")
    public string hello(@pathvariable string name) {
        return name + " say hello";
    }

}

 

4.搭建client(客户端)

 4.1 application.yml 配置

server:
  port: 8082
spring:
  application:
    name: client
eureka:
  client:
    serviceurl:
      defaultzone: http://localhost:8080/eureka/
app:
  service-url: http://localhost:8081/

 

 4.2 编写启动程序

package com.services.client;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
import org.springframework.context.annotation.bean;
import org.springframework.web.client.resttemplate;

@springbootapplication
@enablediscoveryclient
public class clientapplication {

    public static void main(string[] args) {
        springapplication.run(clientapplication.class, args);
    }

    @bean
    resttemplate resttemplate() {
        return new resttemplate();
    }
}

 

 4.3 编写service

package com.services.client;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.value;
import org.springframework.http.responseentity;
import org.springframework.stereotype.service;
import org.springframework.web.client.resttemplate;

@service
public class callhelloservice {

    @value("${app.service-url}")
    private string appserviceurl;

    @autowired
    private resttemplate resttemplate;

    public string callhello(string name) {
        // 是一个http client
        responseentity result = resttemplate.postforentity(appserviceurl + "hello/" + name, null, string.class);
        return result.getbody().tostring();
    }
}

 

 4.4 编写controller

package com.services.client;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class callhellocontroller {

    @autowired
    private callhelloservice callhelloservice;

    @getmapping("hello")
    public string hello(string name) {
        string result = callhelloservice.callhello(name);
        return result;
    }

}

5.启动registry-service-client模块

 5.1 registry

http://www.lhsxpumps.com/_localhost:8080/

 

 

 5.2 service

 

刷新网页 http://www.lhsxpumps.com/_localhost:8080/

 

 5.3 client

刷新网页 http://www.lhsxpumps.com/_localhost:8080/

client 访问 http://www.lhsxpumps.com/_localhost:8082/hello?name=tao

源码地址: https://github.com/80905949/springcloud.git

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网