Spring Cloud开发实践(二): Eureka服务和接口定义

目录

服务注册 EurekaServer

Eureka服务模块只有三个文件, 分别是pom.xml, application.yml 和 EurekaServerApplication.java, 内容如下

pom.xml

spring-boot-maven-plugin: 使用 goal=repackage 可以打包出一个包含所有依赖的fat jar
maven-deploy-plugin: skip=true 表示当执行deploy时, 这个模块不会被提交到maven的repository

<?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>com.rockbb</groupId>
        <artifactId>scot</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../scot/pom.xml</relativePath>
    </parent>
    <artifactId>scot-eureka</artifactId>
    <packaging>jar</packaging>
    <name>Scot: Eureka Server</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>scot-eureka</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

将自己配置为 Eureka Server

server:  
  port: ${PORT:8761}
    
eureka:  
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    enableSelfPreservation: true
    renewalPercentThreshold: 0.1

EurekaServerApplication.java

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}

接口定义 Commons API

scot-commons-api模块定义了服务的接口Service和参数类型DTO. 因为Spring Cloud的特殊性, 这里的Service定义使用了@FeignClient和@RequestMapping注解, 这样在被下游调用时, 可以通过 @EnableFeignClients(basePackages = {"com.rockbb.scot.commons.api"}) 很方便地将服务引入.

本模块只有三个文件, pom.xml, UserDTO.java, UserDTOService.java

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>com.rockbb</groupId>
        <artifactId>scot</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../scot/pom.xml</relativePath>
    </parent>
    <artifactId>scot-commons-api</artifactId>
    <packaging>jar</packaging>
    <name>Scot: Commons API</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.rockbb</groupId>
            <artifactId>scot-commons-lib</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

UserDTO.java

注意如果要添加带参数的 constructor, 一定要把无参的constructor也加上, 否则下游无法将对象反序列化.

public class UserDTO implements Serializable {
    private String id;
    private String name;
 
    public UserDTO initialize(String id, String name) {
        this.id = id;
        this.name = name;
        return this;
    }
 
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
 
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

UserDTOService.java

  • @FeignClient 的value, 来自于 scot-commons-impl 模块的 spring.application.name, 多个DTOService可以使用相同的value.
  • @RequestParam 一定要添加 value = "xx" , 否则在调用中即使你指定了method=GET, feign依然会使用POST进行调用, 导致错误
  • @RequestMapping 可以像Controller一样同时定义于class和method
@FeignClient(value = "scot-commons")
@RequestMapping(value = "/user")
public interface UserDTOService {
 
    @RequestMapping(value = "/diagnos", method = RequestMethod.GET)
    String diagnos(@RequestParam(value = "name") String name);
 
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    UserDTO get(@RequestParam(value = "id") String id);
 
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    List<UserDTO> list();
 
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    long count();
}

posted on 2018-10-25 22:47  Milton  阅读(1623)  评论(0编辑  收藏  举报

导航