spring boot 微服务例子一

package com.example.hello.demo;

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication@RestController@RequestMapping(value="hello")public class DemoApplication {

public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);

}    @RequestMapping(value="/{firstName}/{lastName}",method = RequestMethod.GET)    public String  hello(@PathVariable("firstName") String fName,@PathVariable("lastName") String lName){        return String.format("Hello zhukezhi :%s %s",fName,lName);

}

}

pom文件如下:使用idea的new---》spring initialr功能可以自动生成这个文件。
<?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.example.Hello</groupId>    <artifactId>demo</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>

<name>demo</name>    <description>Demo project for helloSpring Boot</description>

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

<properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <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.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>

<build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>

</project>
完成以后编译文件并使用内置的tomcat运行http://localhost:8080/hello/julia/david/返回字符串或者json串

原文地址:https://www.cnblogs.com/davidzhu/p/9622533.html

时间: 2024-07-29 19:01:23

spring boot 微服务例子一的相关文章

【原创】Docker容器及Spring Boot微服务应用

Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复杂问题 传统项目实施过程中经常会出现“程序在我这跑得好好的,在你那怎么就不行呢?! ” 这是一个典型的应用场景,Docker image中包含了程序需要的所有的运行时依赖,比如java的程序,肯定要在image中包含jdk:比如Python的程序,肯定要在image中包含对应版本的Python解释器.Docker把整个运行时环境打包放到image中,所以搞定了环境依赖问题

Spring Boot微服务如何集成fescar解决分布式事务?

什么是fescar? 关于fescar的详细介绍,请参阅fescar wiki. 传统的2PC提交协议,会持有一个全局性的锁,所有局部事务预提交成功后一起提交,或有一个局部事务预提交失败后一起回滚,最后释放全局锁.锁持有的时间较长,会对并发造成较大的影响,死锁的风险也较高. fescar的创新之处在于,每个局部事务执行完立即提交,释放本地锁:它会去解析你代码中的sql,从数据库中获得事务提交前的事务资源即数据,存放到undo_log中,全局事务协调器在回滚的时候直接使用undo_log中的数据覆

使用http://start.spring.io/构建maven微服务项目的几个坑及eclipse构建spring boot微服务项目

一,使用http://start.spring.io/构建maven微服务项目 本来嘛,直接构建的项目导入时没有任何问题的导入就可以运行,可是最近构建好项目,然后导入,种种报错 1.导入之后POM报错 将parent版本更改下,将2.1.6改为2.0.1就可以消除错误 2.启动报错,直接退出 pom中导入此依赖,问题就解决了 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g

Spring Boot微服务启动脚本

#!/bin/bash#description: starts and stops the boot.sh app_name=$1pid=ps aux | grep java | grep $app_name | grep -v "grep" | awk '{print $2}' usags() {echo "Usags: sh boot.sh [eureka|config|auth|emqtt|crm|upm|uc|upload|gateway|notification|w

Spring Cloud微服务接口这么多怎么调试?

前言 今天和大家聊一下Spring Cloud微服务下服务接口调试及管理的话题!我们知道在微服务架构下,软件系统会被拆分成很多个独立运行的服务,而这些服务间需要交互通信,就需要定义各种各样的服务接口.具体来说,在基于Spring Cloud的微服务模式中,各个微服务会基于Spring MVC的Controller定义多个该微服务需要向外部发布的接口. 根据各个微服务功能边界定义的不同,有些微服务会提供与具体业务相关的接口,如支付接口.账户接口等:而有些微服务则会提供一些公共性质的服务接口,如短信

spring cloud微服务分布式云架构-eureka 基础

在构建项目之前,我们先学习一下eureka,这是官方的讲解,我这边再重新帮大家回顾一下: 服务发现:Eureka客户端 Spring Cloud大型企业分布式微服务云架构源码请加一七九一七四三三八零哦 服务发现是基于微服务架构的关键原则之一.尝试配置每个客户端或某种形式的约定可能非常困难,可以非常脆弱.Netflix服务发现服务器和客户端是Eureka.可以将服务器配置和部署为高可用性,每个服务器将注册服务的状态复制到其他服务器. 如何包含Eureka客户端 要在您的项目中包含Eureka客户端

在阿里云容器服务上开发基于Docker的Spring Cloud微服务应用

本文为阿里云容器服务Spring Cloud应用开发系列文章的第一篇. 一.在阿里云容器服务上开发Spring Cloud微服务应用(本文) 二.部署Spring Cloud应用示例 三.服务发现 四.服务间通信与集成 五.服务智能路由 六.集中配置管理 七.高可用和容错 八.监控和日志 九.服务的部署和发布策略 微服务概述 单体应用通常指在一个程序中满足多个业务或技术领域的需求,不同的需求领域内化为模块.假定我们要开发一个Web应用,通常的MVC模式可以满足要求.针对不同领域有不少代码生成工具

关于Spring Cloud微服务架构

微服务架构 Spring Cloud解决的第一个问题就是:服务与服务之间的解耦.很多公司在业务高速发展的时候,服务组件也会相应的不断增加.服务和服务之间有着复杂的相互调用关系,经常有服务A调用服务B,服务B调用服务C和服务D ...,随着服务化组件的不断增多,服务之间的调用关系成指数级别的增长,这样最容易导致的情况就是牵一发而动全身.经常出现由于某个服务更新而没有通知到其它服务,导致上线后惨案频发.这时候就应该进行服务治理,将服务之间的直接依赖转化为服务对服务中心的依赖.Spring Cloud

spring cloud微服务分布式云架构集成项目

Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Cloud之前大家必须了解一下相关项目,希望可以帮助到大家. Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消息总线,用于在集群(例如,配置变化事件)中传