在user_service_interface中添加一個User的類。 增長私有屬性 id,name , 並利用快捷鍵Alt+Insert 實現get,set的快速生成。html
要注意要有一個無參的構造函數,不然消費者調用的時候會報錯java
package com.project.microservice.domain; public class User { private Long Id; private String Name; public Long getId() { return Id; } public void setId(Long id) { Id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public User(Long id, String name) { Id = id; Name = name; } public User(){ } @Override public String toString() { return "User{" + "Id=" + Id + ", Name='" + Name + '\'' + '}'; } }
package com.project.microservice.service; import com.project.microservice.domain.User; public interface IUserService { User getUserInfo(Long id,String name); }
一個快照版本。這樣能夠不用頻繁更新。web
在Maven依賴管理中,惟一標識一個依賴項是由該依賴項的三個屬性構成的,分別是groupId、artifactId以及version。這三個屬性能夠惟一肯定一個組件(Jar包或者War包)spring
一個倉庫通常分爲public(Release)倉和SNAPSHOT倉,前者存放正式版本,後者存放快照版本。若是在項目配置文件中(不管是build.gradle仍是pom.xml)指定的版本號帶有’-SNAPSHOT’後綴,好比版本號爲’Junit-4.10-SNAPSHOT’,那麼打出的包就是一個快照版本。apache
參考:http://www.javashuo.com/article/p-zldqfhkk-ke.htmlapp
思路:該項目主要是提供服務,而後在Eureka註冊中心中去註冊,業務邏輯主要是主要是實現接口層的方法 。作爲eureserver的客戶端,導入client的包,導入接口的包。dom
<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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class UserProviderApplication { public static void main(String[] args) { SpringApplication.run(UserProviderApplication.class,args); } }
server: port: 8001 spring: application: name: microservice-provider-user eureka: register-with-eureka: true # fetch-registry: true # client: service-url: defaultZone: http://127.0.0.1:7001/eureka/ instance: prefer-ip-address: true
import com.project.microservice.domain.User; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/provider") public class UserController { @RequestMapping("/user/{id}") public User getUserInfo(@PathVariable(value = "id")Long id, String name) { return new User(id,name); } }
register-with-eureka: true # fetch-registry: true #
這兩項配置在服務中心中要設置爲false,表示不發現自身服務,可是在客戶端,必定要設置爲true,不然發現不了。maven
註冊中心會自動顯示出生產者ide
生產者直接調用結果函數
<?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"> <parent> <artifactId>microservice_paent</artifactId> <groupId>com.project</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>User_service_consume_9001</artifactId> <dependencies> <dependency> <groupId>com.project</groupId> <artifactId>User_service_interface</artifactId> <version>1.0-SNAPSHOT</version> </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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
server: port: 9001 eureka: register-with-eureka: false # fetch-registry: false # client: service-url: defaultZone: http://127.0.0.1:7001/eureka/ instance: prefer-ip-address: true
引用SpringBootApplication和EnableEurekaClient兩個註解。
package com.project; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class UserConsumeApplication { public static void main(String[] args) { SpringApplication.run(UserConsumeApplication.class,args); } }
要注意不能直接用生產者的ip地址,由於生產纔在註冊中心註冊之後,會變,用ip找不到。
package com.project.web.controller; import com.project.microservice.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/consumer/user") public class UserController { private static final String URL_PREFIX="http://MICROSERVICE-PROVIDER-USER"; @Autowired private LoadBalancerClient loadBalancerClient; @Autowired private RestTemplate restTemplate; @RequestMapping("/{id}") public User get(@PathVariable("id") Long id, String name) { return restTemplate.getForObject(URL_PREFIX+"/provider/user/"+id+"?name="+name,User.class); } }
package com.project.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }