开发工具
- jdk1.8
- maven 3.6.2
- IntelliJ IDEA
- springCloud Finchley.RELEASE
- springBoot 2.0.3
java
project 依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| <?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>
<groupId>com.qn</groupId> <artifactId>springcloud</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version>
<modules> <module>consumer-order</module> <module>provider-user</module> <module>appEureka</module> <module>consumer-order-ribbon</module> </modules>
<properties> <java.version>1.8</java.version> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency>
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
|
eureka 客户端依赖、启动类、配置
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xmlversion="1.0"encoding="UTF-8"?> <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>com.qn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>appEureka</artifactId>
</project>
|
启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableEurekaServer public class Eureka{ public static void main(String[]args){ SpringApplication.run(Eureka.class,args); } }
|
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server: port: 9999
eureka:
instance: hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
|
消费者依赖、启动类、配置、bean、业务类
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xmlversion="1.0"encoding="UTF-8"?> <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>com.qn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>consumer-order</artifactId>
</project>
|
启动类
1 2 3 4 5 6 7 8 9 10 11
| @SpringBootApplication @EnableEurekaClient public class ConsumerApp{ public static void main(String[]args){ SpringApplication.run(ConsumerApp.class,args); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } }
|
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server: port:8900 spring: application: name:consumer-order eureka: instance: hostname: localhost
lease-expiration-duration-in-seconds: 15
lease-renewal-interval-in-seconds: 5 client: service-url: defaultZone:http://localhost:9999/eureka
|
bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class User{ private Long id; private Date date;
public User(){
}
public User(Long id){ this.id=id; this.date=newDate(); }
public Longget Id(){ return id; }
public void setId(Long id){ this.id=id; }
public Date getDate(){ return date; }
public void setDate(Datedate){ this.date=date; } }
|
业务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @RestController public class OrderController{ @Autowired private RestTemplate restTemplate;
@Autowired private EurekaClient eurekaClient;
@GetMapping("/order/{id}") public Userget Order(@PathVariableLongid){ InstanceInfo nextServerFromEureka=eurekaClient.getNextServerFromEureka("PROVIDER-USER",false); Stringurl=nextServerFromEureka.getHomePageUrl(); Useruser=restTemplate.getForObject(url+"/user/"+id,User.class); return user; } }
|
提供者依赖、配置、启动类、bean、业务类
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xmlversion="1.0"encoding="UTF-8"?> <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>com.qn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>provider-user</artifactId>
</project>
|
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server: port:7900#自定义boot项目访问端口 spring: application: name:provider-user eureka: instance: hostname: localhost
lease-expiration-duration-in-seconds: 15
lease-renewal-interval-in-seconds: 5 client: service-url: defaultZone:http://localhost:9999/eureka
|
启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableEurekaClient public class ProviderApp{ public static void main(String[]args){ SpringApplication.run(ProviderApp.class,args); } }
|
bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class User{ private Long id; private Date date;
public User(){
}
public User(Long id){ this.id=id; this.date=newDate(); }
public Longget Id(){ return id; }
public void setId(Long id){ this.id=id; }
public Date getDate(){ return date; }
public void setDate(Datedate){ this.date=date; } }
|
业务类
1 2 3 4 5 6 7 8 9 10
| @RestController public class UserController{ @Autowired private EurekaClient eurekaClient;
@GetMapping("/user/{id}") publicUsergetUser(@PathVariableLongid){ return new User(id); } }
|
测试
- 启动 eureka、消费者、提供者,访问 eureka 客户端
- 消费者通过 eureka 访问提供者