SpringCloud介绍及入门(二)
接口的实现
在user_service_interface中添加一个User的类。 增加私有属性 id,name , 并利用快捷键Alt+Insert 实现get,set的快速生成。
实体类User
要注意要有一个无参的构造函数,否则消费者调用的时候会报错
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);
}
SANPSHOP是什么
一个快照版本。这样可以不用频繁更新。
在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个属性构成的,分别是groupId、artifactId以及version。这三个属性可以唯一确定一个组件(Jar包或者War包)
一个仓库一般分为public(Release)仓和SNAPSHOT仓,前者存放正式版本,后者存放快照版本。如果在项目配置文件中(无论是build.gradle还是pom.xml)指定的版本号带有’-SNAPSHOT’后缀,比如版本号为’Junit-4.10-SNAPSHOT’,那么打出的包就是一个快照版本。
参考:https://www.cnblogs.com/huang0925/p/5169624.html
User_service_provider_8001生产者的实现。
思路:该项目主要是提供服务,然后在Eureka注册中心中去注册,业务逻辑主要是主要是实现接口层的方法 。做为eureserver的客户端,导入client的包,导入接口的包。
provider中pom.xml的主要配置
<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>
provider中的启动类
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);
}
}
yml的配置
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,否则发现不了。
测试生产者调用结果:
注册中心会自动显示出生产者
生产者直接调用结果
消费者代码的实现
消费者项目pom.xml中引用接口和相应的依赖,web,test,cloud
<?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>
Resource的yml中进行相应的配置
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);
}
}
创建webcontroller实现调用生产者的代码
要注意不能直接用生产者的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);
}
}
关于RestTemplate的配置
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();
}
}