新建Maven工程
删除src目录,修改poml.xml
<modelVersion>4.0.0</modelVersion><groupId>org.example</groupId>
<artifactId>SpringCloud_example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version>
</parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring.cloud-version>Hoxton.SR8</spring.cloud-version>
</properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.18</version><scope>provided</scope></dependency>
</dependencies><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>
Eureka注册中心
File -> New -> Module
添加依赖
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
</dependencies>
启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
配置文件application.yml
server:port: 9000spring:application:name: eureka-servereureka:instance:hostname: eureka9000.comclient:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: "https://${eureka.instance.hostname}:${server.port}/eureka/"
common微服务
File -> New -> Module
产品类
package org.example.common.entity;import lombok.Data;@Data
public class Product {private Long id;private String name;
}
修改父pom.xml
<dependencyManagement><dependencies><dependency><groupId>org.example</groupId><artifactId>common</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</dependencyManagement>
产品微服务
File -> New -> Module
添加依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>common</artifactId></dependency>
</dependencies>
配置文件application.yml
server:port: 9010spring:application:name: product-serviceeureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: trueinstance-id: ${spring.application.name}:${server.port}lease-renewal-interval-in-seconds: 5lease-expiration-duration-in-seconds: 10
启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}
}
controller类
package org.example.controller;import org.example.common.entity.Product;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/product")
public class ProductController {@GetMapping("/{id}")public Product findById(@PathVariable Long id) {Product product = new Product();product.setId(id);product.setName("product test");return product;}
}
订单微服务
File -> New -> Module
依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>common</artifactId></dependency>
</dependencies>
配置文件
server:port: 9020spring:application:name: order-serviceeureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:hostname: localhostprefer-ip-address: truelease-renewal-interval-in-seconds: 5lease-expiration-duration-in-seconds: 10product-service:ribbon:ConnectTimeout: 250ReadTimeout: 1000OkToRetryOnAllOperations: trueMaxAutoRetriesNextServer: 1MaxAutoRetries: 1
启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}
feign接口
package org.example.controller;import org.example.common.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@Component
@FeignClient(value = "product-service")
public interface ProductFeign {@GetMapping(value = "/product/{id}")Product findById(@PathVariable Long id);
}
controller类
package org.example.controller;import org.example.common.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/order")
public class OrderController {@AutowiredProductFeign productFeign;@GetMapping("/{id}")public Product buy(@PathVariable Long id) {Product product = productFeign.findById(id);return product;}
}
网关微服务
File -> New -> Module
添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}
配置文件
server:port: 9030spring:application:name: api-gatewaycloud:gateway:routes:- id: product-serviceuri: lb://product-servicepredicates:- Path=/product/**- id: order-serviceuri: lb://order-servicepredicates:- Path=/order/**eureka:client:service-url:defaultZone: http://localhost:9000/eureka/instance:prefer-ip-address: trueinstance-id: ${spring.cloud.client.ip-address}:${server.port}
启动eureka-server,product-service,order-service和api-gateway微服务,打开http://localhost:9030/order/1,返回{"id":1,"name":"product test"}
github:GitHub - JJJ2018/SpringCloud_example