在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(); } }