- Spring Cloud 学习笔记(一)
** 搭建过程
搭建maven父子工程->包括父工程、eureka-server、eureka-client
创建maven父工程,pom文件如下:
<?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.wat</groupId> <artifactId>wat-spring-cloud-study</artifactId> <version>${project.version}</version> <name>wat-spring-cloud-study</name> <packaging>pom</packaging> <!-- spring cloud parent project --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <!-- <relativePath /> --> <!-- used to download every time --> </parent> <!-- several modules --> <modules> <module>wat-spring-eureka-server</module> <module>wat-spring-eureka-client</module> </modules> <!-- properties used for clean --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <project.version>0.0.1-SNAPSHOT</project.version> </properties> <!-- like spring cloud properties without this will get error --> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <useSystemClassLoader>false</useSystemClassLoader> <skip>true</skip> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
创建maven子工程->eureka-server
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.wat</groupId> <artifactId>wat-spring-cloud-study</artifactId> <version>${project.version}</version> </parent> <artifactId>wat-spring-eureka-server</artifactId> <name>wat-spring-eureka-server</name> <url>http://maven.apache.org</url> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
创建maven子工程->eureka-client
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.wat</groupId> <artifactId>wat-spring-cloud-study</artifactId> <version>${project.version}</version> </parent> <artifactId>wat-spring-eureka-client</artifactId> <name>wat-spring-eureka-client</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <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> </project>
eureka-server写代码:
package org.wat.spring.eureka.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Hello world! * */ @EnableEurekaServer @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
在eureka-server添加配置文件/src/main/resources/application.properties:
server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false logging.level.com.netflix.eureka=OFF logging.level.com.netflix.discovery=OFF
在eureka-client中写代码:
package org.wat.spring.eureka.client; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @EnableDiscoveryClient @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } @RestController class ServiceInstanceRestController { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/service-instances/{applicationName}") public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); } }
- 在eureka-client添加配置文件/src/main/resources/bootstrap.properties:
xml spring.application.name=wat-eureka-client-1
先启动程序eureka-server,并登陆http://localhost:8761看到instance里面没有程序:
启动程序eureka-client,看到instance里面多了一个程序:
到此第一篇入门就记录完毕啦、希望能每天成长一点
转载请注明出处:https://www.cnblogs.com/shijunyu/p/9634845.html