eureka 客户端springcloud service

版本:springcloud2

springcloud eureka注册中心搭建
https://blog.csdn.net/haveqing/article/details/86479813

文件结构

一、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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.urthink.upfs</groupId>
    <artifactId>springcloud-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-service</name>
    <description>springcloud-service project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

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

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

        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

二、DemoController.java

package com.urthink.upfs.springcloudservice.controller;


import com.urthink.upfs.springcloudservice.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 测试Controller
 *
 * @author zhao
 * @date 2019.3.8
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

    //返回数据
    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    @ResponseBody
    public String test1(HttpServletRequest request) {
        System.out.println(request.getRequestURI()); // /demo/test1
        return "eureka客户端返回数据";
    }


    //返回页面
    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2(HttpServletRequest request) {
        System.out.println(request.getRequestURI()); // /demo/test2
        return "test";
    }

    //返回对象
    @RequestMapping(value = "/test3", method = RequestMethod.GET)
    @ResponseBody
    public User test3(HttpServletRequest request) {
        System.out.println(request.getRequestURI()); // /demo/test3
        User user = new User();
        user.setId(1);
        user.setName("admin");
        return user;
    }


}

三、User.java

package com.urthink.upfs.springcloudservice.entity;

import java.io.Serializable;

public class User implements Serializable {

    private Integer id;

    private String name;


    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

四、SpringcloudServiceApplication.java

package com.urthink.upfs.springcloudservice;

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

/**
 * http://localhost:8082/demo/test1
 */
@SpringBootApplication
public class SpringcloudServiceApplication {

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

}

五、test.ftl

<html>
<head>
    <title>eureka客户端测试2</title>
</head>
<body>
eureka客户端返回页面
</body>
</html>

六、application.yml


server:
  port: 8082
  servlet:
    #context-path: /service
    session:
      timeout: 3600s


eureka:
  instance:
    prefer-ip-address: true
    #Eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则),默认30
    lease-renewal-interval-in-seconds: 10
    #Eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己),默认90
    lease-expiration-duration-in-seconds: 12
  client:
    registry-fetch-interval-seconds: 10 #eureka client刷新本地缓存时间,默认30
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/


spring:
  application:
    name: springcloud-service
  freemarker:
    #指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
    allow-request-override: true
    #指定HttpSession的属性是否可以覆盖controller的model的同名项
    allow-session-override: true
    #是否开启template caching.
    cache: true
    #设定Template的编码.
    charset: UTF-8
    #是否检查templates路径是否存在.
    check-template-location: true
    #设定Content-Type.
    content-type: text/html
    #是否允许mvc使用freemarker.
    enabled: true
    #设定所有request的属性在merge到模板的时候,是否要都添加到model中.
    expose-request-attributes: true
    #设定所有HttpSession的属性在merge到模板的时候,是否要都添加到model中.
    expose-session-attributes: true
    #设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
    expose-spring-macro-helpers: true
    #视图加载顺序
    order: 0
    #是否优先从文件系统加载template,以支持热加载,默认为true
    prefer-file-system-access: true
    #设定freemarker模板的前缀.
    #prefix:
    #指定RequestContext属性的名.
    request-context-attribute: request
    #设定FreeMarker keys.
    settings:
      #如果变量为null,转化为空字符串,比如做比较的时候按照空字符串做比较
      classic_compatible: true
      #去掉多余的空格,非常有用
      whitespace_stripping: true
      #模板更新时间,这里配置是1秒更新一次,正式环境设置为3600秒
      template_update_delay: 1
      #中国
      locale: zh_CN
      #编码utf8
      default_encoding: utf_8
      #url编码utf8
      url_escaping_charset: utf_8
      #显示日期格式
      date_format: yyyy-MM-dd
      #显示时间格式
      time_format: HH:mm:ss
      #显示日期时间格式
      datetime_format: yyyy-MM-dd HH:mm:ss
      #数字格式
      number_format: 0.######
      output_encoding: UTF-8
      #布尔型格式
      boolean_format: true,false
      #宏定义
      #auto_import="/common/index.ftl" as ui
      tag_syntax: auto_detect
    #设定模板的后缀.
    suffix: .ftl
    #设定模板的加载路径,多个以逗号分隔,默认: ["classpath:/templates/"]
    template-loader-path: classpath:/templates/
    #指定使用模板的视图列表. White list of view names that can be resolved.
    #view-names:

七、访问

http://localhost:8082/demo/test1

发布了67 篇原创文章 · 获赞 11 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/haveqing/article/details/88427366