Spring Cloud開發實踐 - 02 - 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();
}

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM