有事没事领个红包

创建多模块springcloud应用eureka server和client和消费端demo

使用环境是 STS + maven 

1 创建父级 项目,springcloud-demo1

new -> maven project -> 

按照要求进行配置即可。然后删除 src目录,因为父级项目只是配置项目依赖和版本控制使用。

 

1.2 修改 pom 文件

<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.huitong</groupId>
  <artifactId>springcloud-demo1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Edgware.SR4</spring-cloud.version>
  </properties>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
  </parent>
  
  
  
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      
    </dependencies>
  </dependencyManagement>    
  
  <modules>
  </modules>
</project>

 

此时也要注意springcloud 和 springboot 版本之间有一定的关联性,最好使用官网推荐的对应版本。否则可能会出现版本不兼容的问题。我就是这么趟的坑过来的

 

2  创建 eureka server端项目,spring-cloud-sureka-server

在 springcloud-demo1 项目上右击, new -> maven module

 

2.2 修改 pom 文件,添加 eureka-server 依赖, spring-cloud-starter-eureka-server

<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>com.huitong</groupId>
    <artifactId>springcloud-demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>spring-cloud-eureka-server</artifactId>
  
  <dependencies>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
      
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

 

2.3 添加配置文件 application.yml

server:
  port: 8010

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: 127.0.0.1
  server:
#    关闭自我保护,当服务出现故障自动剔除。清理间隔(单位毫秒,默认是60*1000)
    enableSelfPreservation: false
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

 

eureka 配置说明 可见 http://www.cnblogs.com/li3807/p/7282492.html

在 server 端 service-url 指定暴露出的注册服务地址。让客户端可以将应用注册到该地址上。

 

2.4 添加启动类,StartEurekaServer

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class StartEurekaServer {

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

}

 

3 新建一个服务提供端  module,spring-cloud-eureka-client-demo1,跟 eureka 方法一样,在springcloud-demo1项目上右击, new -> maven module

 

3.2 修改 pom 文件

<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>com.huitong</groupId>
    <artifactId>springcloud-demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>spring-cloud-eureka-client-demo1</artifactId>
  
  <dependencies>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>
    
    
  </dependencies>
  
 
</project>

 

3.3 添加配置文件 application.yml

server:
  port: 8020
  
spring:
  application:
    name: eureka-client-demo1
    
eureka:
  instance:
    preferIpAddress: true
    hostname: 127.0.0.1
    instanceId: ${spring.cloud.client.ipAddress:127.0.0.1}:${server.port}
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${eureka.port:8010}/eureka/

 

3.4 编写启动类,SpringCloudDemo1

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudDemo1 {

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

    }

}

 

3.5 简单的实体类,Student

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Student {
    private String name;
    private Integer age;

}

 

3.6 Controller

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {
    
    @RequestMapping(value="/student/demo1", method=RequestMethod.GET)
    public Student getStu1() {
        Student  result = Student.builder().name("allen").age(23).build();
        if (ObjectUtils.allNotNull(result)) {
            System.out.println(result);
        }
        
        return result;
    }

}

此时访问页面 :http://localhost:8010/

 

 

 

 

说明:由于springcloud 版本低或者什么原因,在编写配置时,使用的是 横线命名法   启动的时候一直给我报错,com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect

 

后来查阅大量资料发现将配置属性全部改为 驼峰命名法 就好了

 

posted @ 2018-08-27 00:17  crazyCodeLove  阅读(4278)  评论(1编辑  收藏  举报