一:创建父工程springclould-parent
父工程的打包方式:pom
父工程锁定springboot和spring-cloud的版本
父工程的pom文件如下:
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 | <!--springboot版本号--> < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.0.3.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < 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 >Finchley.RELEASE</ spring-cloud.version > </ properties > <!--锁定springcloud的版本--> < 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 > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > <!--无论服务提供者还是服务消费者都需要tomcat运行,都是web工程--> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > <!--无论服务提供者还是服务消费者,他们都是eureka客户端--> < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-netflix-eureka-client</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
二:Service Provider服务提供者
1:新建springcloud-service-provider子模块
2:继承父工程
3:修改application.properties配置文件
1 2 3 4 5 6 7 8 | #允许注册eureka注册中心,默认值就是true eureka.client.register-with-eureka=true #允许从eureka注册中心找服务,并调用,默认值就是true eureka.client.fetch-registry=true #eureka的server地址,如果eureka服务多个用逗号隔开 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ #应用名 spring.application.name=spring-cloud-service-provider |
4:主程序加上@EnableEurekaClient注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.wendao.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient //或者@EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication. class , args); } } |
5:启动该模块,访问http://localhost:8761/(注册中心ui界面地址)看到如下界面,代表服务已经注册成功。
6:暴露一个rest api
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 | package com.wendao.provider.pojo; public class User { private String username; private int age; public User() { } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } public User(String username, int age) { super (); this .username = username; this .age = age; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.wendao.provider.web; import com.wendao.provider.pojo.User; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProviderController { @RequestMapping (value = "/user/{uesrId}" ) public User findUserById( @PathVariable Integer uesrId) { //调用service层获取用户信息,这步省略... return new User( "朱锡铭" , 12 ); } } |