zoukankan      html  css  js  c++  java
  • SpringBoot整合Eureka搭建微服务

    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://localhost:8080/

     5.2 service

     

    刷新网页 http://localhost:8080/

     5.3 client

    刷新网页 http://localhost:8080/

    client 访问 http://localhost:8082/hello?name=tao

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

  • 相关阅读:
    团队博客-会议之初
    5.2 个人作业2
    5.1 如何利用Intellij Idea搭建python编译运行环境
    4.27 python神器——Anaconda的安装与优化配置
    4.26 AlertDialog(对话框)详解
    4.25 xmapp启动mysql出现Error: MySQL shutdown unexpectedly.
    4.24 Android Studio下应用签名的方法以及获取 MD5、SHA1(签名)、SHA256 值
    4.23 2020.2新版本idea创建javaEE web文件
    4.22 Android studio 通过获取验证码用户登陆成功
    4.21 Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
  • 原文地址:https://www.cnblogs.com/i-tao/p/10326358.html
Copyright © 2011-2022 走看看