Using the environment is STS + maven
1 Creating a parent project, SPRINGCLOUD-DEMO1
Maven Project, New
Configure them as required. Then delete the SRC directory because the parent project is only configuration item dependent and version control is used.
1.2 Modifying the Pom file
<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.huitong</groupId> <artifactid>springcloud-demo1</ Artifactid> <version>0.0.1-snapshot</version> <packaging>pom</packaging> <properties> < project.build.sourceencoding>utf-8</project.build.sourceEncoding> <project.reporting.outputencoding>utf-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR4</spring-cloud.version> </properties> & Lt;parent> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-parent</artifactid> <version>1.5. -. release</version> </parent> <dependencyManagement> <dependencies> <DEPENDENCY&G T <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-dependencies</ artifactid> <version>${spring-cloud.version}</version> <type>pom</type> &L t;scope>import</scope> </dependency> </dependencies> </dependencyManagement> <modules> </modules></project>
At this point also note that there is a certain correlation between the Springcloud and springboot versions, preferably using the corresponding version recommended by the official website. Otherwise, there may be an issue with incompatible versions. That's how I came to the pit.
2 Creating the Eureka Server-side project, Spring-cloud-sureka-server
Right-click on the Springcloud-demo1 project, new MAVEN module
2.2 Modify Pom file, add eureka-server dependency, Spring-cloud-starter-eureka-server
<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>com.huitong</groupId> <artifactId> Springcloud-demo1</artifactid> <version>0.0.1-snapshot</version> </parent> <artifactId>spring-cloud-eureka-server</artifactId> < dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid >spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <gr Oupid>org.springframework.boot</groupid> <artifactId>spring-boot-devtools</artifactId> < Optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPRING-BOOT-MAVEN-PLUGIN</ARTIFAC tid> </plugin> </plugins> </build></project>
2.3 Adding a configuration file application.yml
Server: 8010 Eureka: instance: 127.0. 0.1 prefertrue client: Register false Fetchfalse service-URL: default-zone:http:// ${eureka.instance.hostname}:${server.port}/eureka/
Eureka Configuration Description Visible http://www.cnblogs.com/li3807/p/7282492.html
Specify the exposed registration service address on the server side Service-url. Allows the client to register the app with the address.
2.4 Adding the Startup class, Starteurekaserver
Import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.netflix.eureka.server.EnableEurekaServer, @SpringBootApplication @enableeurekaserver Public class starteurekaserver { publicstaticvoid Main (string[] args) { springapplication.run (starteurekaserver. class , args);} }
3 Create a new service provider Module,spring-cloud-eureka-client-demo1, just like the Eureka method, right-click on the Springcloud-demo1 project, the new MAVEN module
3.2 Modifying the Pom file
<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>com.huitong</groupId> <artifactId> Springcloud-demo1</artifactid> <version>0.0.1-snapshot</version> </parent> <artifactId>spring-cloud-eureka-client-demo1</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifa ctid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <GROUPID&G T;org.springframework.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </ dependency> <!--https://Mvnrepository.com/artifact/org.projectlombok/lombok --<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <!--https://Mvnrepository.com/artifact/org.apache.commons/commons-lang3 --<dependency> <groupId>org.apache.commons</groupId> <ARTIFACTID>COMMONS-LANG3</ARTIFAC tid> </dependency> </dependencies> </project>
3.3 Adding a configuration file application.yml
Server:port:8020Spring:application:name:eureka-client-demo1 eureka:instance:preferIpAddress:truehostname:127.0.0.1instanceId: ${spring.cloud.client.ipaddress:127.0.0.1}:${server.port} Client:registerwitheureka:trueFetchregistry:trueserviceUrl:defaultZone:http://${eureka.instance.hostname}:${eureka.port:8010}/eureka/
3.4 Writing the startup class, SpringCloudDemo1
Import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.netflix.eureka.EnableEurekaClient, @SpringBootApplication @enableeurekaclient publicclass SpringCloudDemo1 { publicstaticvoid Main ( String[] args) { springapplication.run (SpringCloudDemo1. class , args);} }
3.5 Simple entity class, Student
import Lombok. Allargsconstructor;import Lombok. Builder;import Lombok. Data;import Lombok. Noargsconstructor, @Data @noargsconstructor@allargsconstructor@builderpublicclass Student { private String name; Private Integer age;}
3.6 Controller
import Org.apache.commons.lang3.objectutils;import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.RestController; @RestController Public classStudentcontroller {@RequestMapping (value="/student/demo1", method=requestmethod.get) PublicStudent getStu1 () {Student result= Student.builder (). Name ("Allen"). Age ( at). build (); if(Objectutils.allnotnull (Result)) {System. out. println (Result); } returnresult; }}
Visit page at this time: http://localhost:8010/
Description: Due to the low Springcloud version or any reason, in the writing of the configuration, using the horizontal line named when the start of the time to give me an error, Com.sun.jersey.api.client.ClientHandlerException:java. NET. Connectexception:connection refused:connect
Later, a large amount of data was found to change the configuration property to the hump naming method .
Create a multi-module Springcloud application Eureka Server and client and consumer demo