使用Spring Boot和Spring Cloud搭建微服务

在Java生态系统中,您将发现一些用于构建微服务体系结构的根深蒂固的模式。如果您以前使用过Spring,那么springboot和springcloud应该是一个不错的返校节。

在本教程中,我将向您展示如何使用springboot和springcloud构建javamicroservices体系结构。

使用Spring Cloud 和 Spring Boot创建Java微服务

仓库地址:

git clone https://github.com/oktadeveloper/java-microservices-examples.git
cd java-microservices-examples/spring-boot+cloud

在spring boot+cloud目录中,有三个项目:

  • 发现服务:Netflix Eureka服务器,用于服务发现。
  • 汽车服务:一个简单的汽车服务,它使用Spring数据REST来提供汽车的restapi。
  • api网关:一个api网关,它有一个/cool cars端点,与汽车服务对话,过滤掉不酷的汽车(当然,在我看来)。

我使用start.spring.io的restapi和HTTPie创建了所有这些应用程序。

http https://start.spring.io/starter.zip javaVersion==11 \
  artifactId==discovery-service name==eureka-service \
  dependencies==cloud-eureka-server baseDir==discovery-service | tar -xzvf -

http https://start.spring.io/starter.zip \
  artifactId==car-service name==car-service baseDir==car-service \
  dependencies==actuator,cloud-eureka,data-jpa,h2,data-rest,web,devtools,lombok | tar -xzvf -

http https://start.spring.io/starter.zip \
  artifactId==api-gateway name==api-gateway baseDir==api-gateway \
  dependencies==cloud-eureka,cloud-feign,data-rest,web,cloud-hystrix,lombok | tar -xzvf -

使用Java11的Spring微服务

为了使发现服务在Java11上运行,我必须在JAXB上添加一个依赖项。

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
</dependency>

另外两个应用程序在java11上开箱即用,没有任何依赖关系的变化。

Netflix Eureka的Java服务发现

发现服务的配置与大多数Eureka服务器相同。它在其主类和属性上作为 @EnableEurekaServerannotation 来设置其端口并关闭发现。

server.port=8761
eureka.client.register-with-eureka=false

car服务和api网关项目以类似的方式配置。两者都定义了一个唯一的名称,并且car服务被配置为在端口8090上运行,这样它就不会与8080冲突。

car-service/src/main/resources/application.properties :

server.port=8090
spring.application.name=car-service

api-gateway/src/main/resources/application.properties :

spring.application.name=api-gateway

两个项目中的主类都用 @enablescoveryclient 注释。

用Spring Data REST构建Java微服务

car服务提供了一个restapi,允许您创建(创建、读取、更新和删除)汽车。当应用程序使用ApplicationRunner bean加载时,它会创建一组默认的cars。

service/src/main/java/com/example/carservice/CarServiceApplication.java :

package com.example.carservice;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.stream.Stream;

@EnableDiscoveryClient
@SpringBootApplication
public class CarServiceApplication {

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

    @Bean
    ApplicationRunner init(CarRepository repository) {
        return args -> {
            Stream.of("Ferrari", "Jaguar", "Porsche", "Lamborghini", "Bugatti
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值