Spring Cloud 微服务注册

1:Intellij 新建服务 user-service

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>
    <parent>
        <groupId>cn.tigee</groupId>
        <artifactId>user-service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>user-core</artifactId>


    <properties>
<!--        <java.version>1.8</java.version>-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.tigee</groupId>
            <artifactId>user-client</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

3:启动类

复制代码
package cn.tigee;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationMain {
    static final Logger logger = LoggerFactory.getLogger(ApplicationMain.class);

    public static void main(String[] args) {
        logger.debug("服务启动..start......");
        SpringApplication.run(ApplicationMain.class, args);
        logger.debug("服务启动..end......");
    }

}

4:application.xml

复制代码
package cn.tigee;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationMain {
    static final Logger logger = LoggerFactory.getLogger(ApplicationMain.class);

    public static void main(String[] args) {
        logger.debug("服务启动..start......");
        SpringApplication.run(ApplicationMain.class, args);
        logger.debug("服务启动..end......");
    }

}

5:restfull 接口

复制代码
package cn.tigee.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${server.port}")
    private int port;
    @Value("${spring.application.name}")
    private String serve;

    @RequestMapping("hello")
    public String hello(){

        return "你好:"+serve+":"+port;
    }
}

6:启动

7:访问 localhost:7003/hello?name=12

8:查看注册中心

相关推荐
米丘2 天前
微前端之 Web Components 完全指南
微服务·html
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
咖啡八杯4 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
Flittly4 天前
【AgentScope Java新手村系列】(10)实战-多Agent天气助手
java·spring boot·spring
李少兄4 天前
从原理到实战:Spring IoC/DI 核心知识体系与高频面试题全解
java·后端·spring
shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
慧一居士4 天前
Feign的GET请求如何传递对象参数?
java·spring cloud
ofoxcoding4 天前
在AI API聚合平台配置DeepSeek V3.2提示词缓存实战:快速接入与成本优化指南
人工智能·spring·缓存·ai
一杯奶茶¥4 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目