java版spring cloud分布式微服务:SpringBoot整合SpringCloud

创建父Maven工程
首先我们创建一个Maven项目了解springcloud架构可以加求求:三五三六二四七二五九
我们把此项目当做我们项目的父项目,以后我们所有的子项目都应该继承这个项目,来看一下此项目的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>

    <groupId>cn.org.zhixiang</groupId>
    <artifactId>spring-cloud-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <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>

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

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

</project>

 

创建服务提供者

右键项目名称:spring-cloud-demo,选择New–>Module,选择Maven,然后点击next,然后呢artifactId给项目取名为cloud-demo-provider,现在服务提供者项目就创建好了。因为接下来我们测试需要访问程序,所以接着我们引入所需的web依赖。

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

2.在resources文件夹下新建application.properties或者application.yml(二者任选其一),添加如下配置:

properties文件这样写
spring.application.name=provider-demo
server.port=8078
yml这样写

spring:
application:
name: provider-demo
server:
port: 8078
spring.application.name呢,意思是给给项目起个名字叫做provider-demo

server.port呢,指定项目启动时的端口为8078

3 .在Java文件夹下创建包cn.org.zhixiang(这个包名是我的网站域名的反转,大家可以自行定义),在此包下新建CloudDemoProviderApplication类

package cn.org.zhixiang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CloudDemoProviderApplication {

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

@SpringBootApplication注解呢是SpringBoot的一个组合注解,主要注解为@Configuration、@EnableAutoConfiguration、@ComponentScan

我们使用SpringBoot启动项目时一般都会使用此注解,一会启动项目的时候我们在这个类中右键选择run CloudDemoProviderApplication 就可以启动了

4.在cn.org.zhxiang包下新建包domain,在domain包中创建一个User实体类

public class User{

    private long id;
    private String name;
    private int age;
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
 

5.在cn.org.zhxiang包下新建包controller,在controller包中创建一个类UserController

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping(value = "/getUser/{id}")
    public User getUser(@PathVariable Long id){
        User user=new User();
        user.setId(id);
        user.setName("张三");
        user.setAge(18);
        return user;
    }
    @GetMapping(value = "/getName")
    public String getName(){
        return "张三";
    }
}

@RestController注解呢,其实就是@ResponseBody + @Controller的组合注解,@GetMapping呢,相当于@RequestMapping(value="", method = RequestMethod.GET)

你可能感兴趣的:(java版spring cloud分布式微服务:SpringBoot整合SpringCloud)