SPRING INITIALIZR構建工程
spring boot 可通過SPRING INITIALIZR構建項目
訪問SPRING INITIALIZR官網,填寫項目相關信息后,生成項目。
將下載的項目解壓,打開idea,file-->new-->project from existing sources。
import project選擇maven項目,jdk選擇1.8或以上,一直next即可。
spring cloud server 提供服務注冊
1.修改pom.xml
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gyt</groupId><artifactId>helloworld.Eureka.server</artifactId><version>0.0.1-SNAPSHOT</version><name>helloworld.Eureka.server</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!--eureka server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- spring boot test--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</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></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
2.通過SPRING INITIALIZR構建工程后,修改配置文件application.properties,我這里使用的是application.yml,兩者只在格式上存在差別。
3.啟動類application.java添加@EnableEurekaServer注解。
@EnableEurekaServer
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
4.訪問http://localhost:8000/
spring cloud client 提供服務
1.同樣創建項目后,修改pom.xml
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gyt</groupId><artifactId>helloworld.eureka.client</artifactId><version>0.0.1-SNAPSHOT</version><name>helloworld.eureka.client</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</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-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</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></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
2.application.yml
eureka:client:serviceUrl:#server對應的地址defaultZone: http://localhost:8000/eureka/instance:hostname: localhost
server:port: 8089
spring:application:#應用名name: service-helloworld
ribbon:eureka:enabled: true
其中關于ribbon的學習來自于 https://www.jianshu.com/p/f86...
3.ApplicationClient.java
@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationClient {public static void main(String[] args) {SpringApplication.run(ApplicationClient.class, args);}}
4.HelloController.java
@RestController
public class HelloController {@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello "+name+",hahahaha!";}
}
5.訪問http://localhost:8089/hello?name=w
此時再看服務端,多了service-helloworld這個應用。
spring cloud consumer 使用服務
1.新建項目,修改pom.xml
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gyt</groupId><artifactId>helloworld.eureka.consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>helloworld.eureka.consumer</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!--eureka server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- spring boot test--><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-feign</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</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></plugins></build><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
2.application.propertites【這里特意沒有用yml】
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3.ApplicationConsumer.java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ApplicationConsumer {public static void main(String[] args) {SpringApplication.run(ApplicationConsumer.class, args);}}
4.創建接口類,HelloTest.java
//這里的name指定客戶端的應用名
@FeignClient(name= "service-helloworld")
public interface HelloTest {//這里requestMapping與客戶端相同@RequestMapping(value = "/hello")public String hello(@RequestParam(value = "name") String name);
}
5.ConsumerController.java
@RestController
public class ConsumerController {@AutowiredHelloTest helloTest;@RequestMapping("/hello/{name}")public String index(@PathVariable("name") String name) {return helloTest.hello(name);}}
6.訪問http://localhost:9001/hello/w
此時,服務端存在兩個應用