入门实战: Spring Cloud 开篇

廉价感情. 提交于 2020-02-28 09:40:03

父应用

  • GroupId: top.bing
  • Artifact: battle
  • Dependencies:
<parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>2.1.4.RELEASE</version>  
</parent>  
  
<properties>  
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>  
</properties>  
  
<dependencies>  
    <!-- lombok 工具通过在代码编译时期动态的将注解替换为具体的代码,  IDEA 需要添加 lombok 插件 -->  
    <dependency>  
        <groupId>org.projectlombok</groupId>  
        <artifactId>lombok</artifactId>  
        <version>1.16.18</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-test</artifactId>  
        <scope>test</scope>  
    </dependency>  
</dependencies>  
  
<!-- 标识 SpringCloud 的版本 -->  
<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>  
  
<!-- 配置远程仓库 -->  
<repositories>  
    <repository>  
        <id>spring-milestones</id>  
        <name>Spring Milestones</name>  
        <url>https://repo.spring.io/milestone</url>  
        <snapshots>  
            <enabled>false</enabled>  
        </snapshots>  
    </repository>  
</repositories>

服务发现

服务发现服务端

  1. 新建应用
  • Artifact: discovery-server
  • Dependencies:
<dependencies>  
    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>  
    </dependency>  
</dependencies>
  1. 开启 Eureka Server
  2. application.yml 配置
server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  1. 启动 Eureka Server

服务发现客户端

  1. 新建应用
  • Artifact: discovery-client
  • Dependencies:
<dependencies>  
    <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-web</artifactId>  
    </dependency>  
  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-actuator</artifactId>  
    </dependency>  
  
    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-starter-config</artifactId>  
    </dependency>  
</dependencies>
  1. 开启 Eureka Client
  2. application.yml 配置
spring:
  application:
    name: discovery-client
management:
  endpoints:
    web:
      exposure:
        include: shutdown
  endpoint:
    shutdown:
      enabled: true
server:
  port: 8080
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  1. 启动 Eureka Client

配置管理

配置管理服务

  1. 新建应用
  • Artifact: config-server
  • Dependencies:
<dependencies>  
    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-config-server</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-actuator</artifactId>  
    </dependency>  
</dependencies>
  1. 开启配置管理服务
  2. application.yml 配置
spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config/

management:
  endpoints:
    web:
      exposure:
        include: shutdown
  endpoint:
    shutdown:
      enabled: true

server:
  port: 8888
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  1. 配置文件
  • ./config/**
  • discovery-client.yml
server:  
 port: 9997
  • discovery-client-dev.yml
  • discovery-client-prod.yml
  1. 启动配置管理服务

配置客户端

  1. 添加 Config Client 依赖
  2. 添加 bootstrap.yml
spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
  1. 使用远程默认配置启动
  2. 以 dev Profile 启动
  3. 以 prod Profile 启动

同步服务交互

服务提供者

  1. 添加领域模型 Person
public class Person {  
    private Long id;  
    private String name;  
    private Integer age;  
    private Integer serverPort;  
}
  1. 添加演示控制器
@RestController  
@RequestMapping("/people")  
public class PersonController {  
  
    @Value("${server.port}")  
    Integer port;  
  
    @PostMapping  
  public Person save(@RequestBody Person person){  
        person.setServerPort(port);  
        return person;  
    }  
  
    @GetMapping("/{id}")  
    public Person getOne(@PathVariable Long id){  
        return new Person(id, "wyf", 35 , port);  
    }  
  
    @GetMapping("/findByAge")  
    public List<Person> findByAge(@RequestParam Integer age){  
        return Arrays.asList(new Person(1l, "wyf", 35 , port),  
                new Person(2l, "foo", 35 , port));  
    }  
  
    @DeleteMapping("/{id}")  
    public String delete(@PathVariable Long id){  
        return "从端口为:" + port + "的服务删除";  
    }  
}
  1. 启动 discovery-client

服务使用者

  1. 新建应用
  • Artifact: feign-client
  • Dependencies:
<dependencies>  
    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-starter-openfeign</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-web</artifactId>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-actuator</artifactId>  
    </dependency>  
</dependencies>
  1. application.yml 配置
spring:
  application:
    name: feign-client
management:
  endpoints:
    web:
      exposure:
        include: shutdown
  endpoint:
    shutdown:
      enabled: true
server:
  port: 8082
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  1. 开启 Feign Client
  2. Feign 客户端声明
@FeignClient("discovery-client")  
public interface PersonClient {  
  
    @PostMapping("/people")  
    Person save(@RequestBody Person person);  
  
    @GetMapping("/people/{id}")  
    Person getOne(@PathVariable("id") Long id);  
  
    @GetMapping("/people/findByAge")  
    List<Person> findByAge(@RequestParam("age") Integer age);  
  
    @DeleteMapping("/people/{id}")  
    String delete(@PathVariable("id") Long id);  
}
public class Person {  
    private Long id;  
    private String name;  
    private Integer age;  
    private Integer serverPort;  
  
    public Person(Long id, String name, Integer age) {  
        this.id = id;  
        this.name = name;  
        this.age = age;  
    }  
}
  1. 使用 Feign 客户端
@Bean  
CommandLineRunner personClientClr(PersonClient personClient){  
    return args -> {  
        System.out.println(personClient.save(new Person(1l, "wyf", 35)));  
        System.out.println(personClient.getOne(1l));  
        personClient.findByAge(35).forEach(System.out::println);  
        System.out.println(personClient.delete(1l));  
    };  
}

异步服务交互

person-service

  1. 新建应用
  2. 开启支持
  3. application.yml 配置
  4. 领域模型
  5. 数据访问 Repository
  6. 事件发送
  7. 事件接收
  8. 验证控制器
  9. 正常启动应用

address-service

  1. 新建应用
  2. 开启支持
  3. application.yml 配置
  4. 领域模型
  5. 数据访问 Repository
  6. 事件发送
  7. 事件接收
  8. 验证控制器
  9. 正常启动应用

应用网关

  1. 新建应用
  • Artifact: gateway
  • Dependencies:
<dependencies>  
    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-starter-config</artifactId>  
    </dependency>  
  
    <dependency>  
        <groupId>org.springframework.cloud</groupId>  
        <artifactId>spring-cloud-starter-gateway</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-actuator</artifactId>  
    </dependency>  
</dependencies>
  1. 在“配置管理”服务进行配置 在 config-server 的 src/main/resources/config 目录下,添加gateway.xml
server:
  port: 81

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
      - id: discoveryClient
        uri: lb://discovery-client
        predicates:
        - Path=/dis/**
        filters:
        - RewritePath=/dis/(?<segment>.*), /$\\{segment}
      - id: uaa
        uri: lb://auth-server
        predicates:
        - Path=/uaa/**
        filters:
        - RewritePath=/uaa/(?<segment>.*), /$\\{segment}
      - id: resource1
        uri: lb://resource-server
        predicates:
        - Path=/res/**
        filters:
        - RewritePath=/res/(?<segment>.*), /$\\{segment}
      - id: feign
        uri: lb://feign-service
        predicates:
        - Path=/fgn/**
        filters:
        - RewritePath=/fgn/(?<segment>.*), /$\\{segment}
  1. 开启 Eureka 客户端
  2. application.yml
spring:
  application:
    name: gateway

management:
  endpoints:
    web:
      exposure:
        include: shutdown
  endpoint:
    shutdown:
      enabled: true

eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/
  1. bootstrap.yml
spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
  1. 验证
  2. 修改默认
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!