SpringCloud 备忘 2 - 生产者 Provider 服务搭建

1、在父工程 “microservicecloud” 下新建一个 Module,名称为 “provider-8201”

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">
    <parent>
        <artifactId>microservicecloud</artifactId>
        <groupId>com.lakey.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider-8201</artifactId>
    <name>provider-8201</name>
    <description>Provider 8201</description>

    <dependencies>
        <!-- Eureka 客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Actuator 监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- SpringBoot Web 容器依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

3、在 java 路径下创建目录 “com.lakey.springcloud” 并添加启动类 Provider8201Application.java

package com.lakey.springcloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
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 // 本服务启动后会自动注册进eureka服务中
@RestController
public class Provider8201Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Provider8201Application.class);
        application.setBannerMode(Banner.Mode.OFF);// 不输出Banner
        application.run(args);
        System.out.println("  ____    ____     ___   __     __  ___   ____    _____   ____     ___    ____     ___    _ \n" +
                " |  _ \\  |  _ \\   / _ \\  \\ \\   / / |_ _| |  _ \\  | ____| |  _ \\   ( _ )  |___ \\   / _ \\  / |\n" +
                " | |_) | | |_) | | | | |  \\ \\ / /   | |  | | | | |  _|   | |_) |  / _ \\    __) | | | | | | |\n" +
                " |  __/  |  _ <  | |_| |   \\ V /    | |  | |_| | | |___  |  _ <  | (_) |  / __/  | |_| | | |\n" +
                " |_|     |_| \\_\\  \\___/     \\_/    |___| |____/  |_____| |_| \\_\\  \\___/  |_____|  \\___/  |_|\n" +
                "                                                                                            ");
    }

    @Value("${server.port}")
    String port;

    /**
     * 请求测试入口
     * 
     * @param name
     * @return
     */
    @RequestMapping("/hello")
    public String sayHello(@RequestParam(value = "name", defaultValue = "word") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

}

4、在 resources 路径下添加配置文件 application.yml

# 服务器配置
server:
  port: 8201

# Spring 配置
spring:
  application:
    name: provider  # 服务间调用所使用的名称

# Eureka 配置
eureka:
  client: # 客户端注册进 Eureka 服务列表
    serviceUrl:
      defaultZone: http://localhost:8101/eureka/
  instance:
      instance-id: provider-8201   # 自定义服务名称信息
      prefer-ip-address: true     # 访问路径可以显示 IP 地址

# Eureka 微服务详细信息配置
info:
  app.name: provider-8201
  company.name: www.lakey.com
  build.artifactId: microservicecloud
  build.version: 1.0-SNAPSHOT

5、参考模块 “provider-8201” 再新建一个生产者服务模块 “provider-8202”,代码复制的过程需要注意替换相应端口

6、目录结构截图:

7、程序运行截图:

9、参考文章:

https://github.com/forezp/SpringCloudLearning

10、码云源码:

https://gitee.com/nangongyanya/microservicecloud

猜你喜欢

转载自blog.csdn.net/nangongyanya/article/details/90024490