• Spring Cloud开发实践


    服务注册 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();
    }
    

    .

  • 相关阅读:
    我在硅谷时候写的文章
    快速上手ABP
    朝花夕拾
    ABP问题速查表
    被低估的.net(上)
    申请Office 365一年免费的开发者账号攻略(2018年10月份版本)
    office365的开发者训练营,免费,在微软广州举办
    数据恢复顾问(DRA)
    使用RMAN对数据文件进行恢复
    Linux环境下利用句柄恢复Oracle误删除的数据文件
  • 原文地址:https://www.cnblogs.com/milton/p/9853458.html
Copyright © 2020-2023  润新知