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();
}
}
启动消费者进行测试

SpringCloud介绍及入门(二)的更多相关文章
- SpringCloud介绍及入门一
springcloud是什么 基于spring boot实现的服务治理工具包,管理和协微服务 把别人的东西拿来组合在一起,形成各种组件 微服务协调者[service registtry注册中心 Eur ...
- java框架之SpringCloud(1)-微服务及SpringCloud介绍
微服务概述 是什么 业界大牛 Martin Fowler 这样描述微服务: 参考[微服务(Microservices)-微服务原作者Martin Flower博客翻译]. 下面是关于上述博客中的部分重 ...
- 脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?
1.引言 本文接上篇<脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手>,继续脑残式的网络编程知识学习 ^_^. 套接字socket是大多数程序员都非常熟悉的概念,它是计算机 ...
- freemarker语法介绍及其入门教程实例
# freemarker语法介绍及其入门教程实例 # ## FreeMarker标签使用 #####一.FreeMarker模板文件主要有4个部分组成</br>#### 1.文本,直接输 ...
- [转帖]脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么?
脑残式网络编程入门(二):我们在读写Socket时,究竟在读写什么? http://www.52im.net/thread-1732-1-1.html 1.引言 本文接上篇<脑残式网 ...
- springcloud+eureka简单入门案例
springcloud+eureka简单入门案例 一.服务提供者 直接提供服务,入门案例没有特别要设置的地方,注意下端口,由于要启动多个服务,可能会冲突 配置文件(src/main/resources ...
- redis入门(二)
目录 redis入门(二) 前言 持久化 RDB AOF 持久化文件加载 高可用 哨兵 流程 安装部署 配置技巧 集群 原理 集群搭建 参考文档 redis入门(二) 前言 在redis入门(一)简单 ...
- C#线程学习笔记九:async & await入门二
一.异步方法返回类型 只能返回3种类型(void.Task和Task<T>). 1.1.void返回类型:调用方法执行异步方法,但又不需要做进一步的交互. class Program { ...
- Mysql数据库的简单介绍与入门
Mysql数据库的简单介绍与入门 前言 一.下载与安装 1.下载 官网下载MYSQL5.7.21版本,链接地址https://www.mysql.com/downloads/.下载流程图如下: 找到M ...
随机推荐
- JS如何做2048(详细)
在做2048之前,我们首先要了解它的游戏规则,以及运行逻辑 首先,来看上半部分 除了标题外还有记录每次获得的分数,以及总分数,还有一个重新开始按钮,这个最大分数会保存下来. 来看页面内容 页面内容由1 ...
- ubuntu无法安装usb驱动
第一步: 输入命令 lsusb 箭头指向的就是连接的手机 第二步: 输入命令,新建并打开文件 sudo gedit /etc/udev/rules.d/-android.rules [注意]如果提示没 ...
- iOS testflight 使用说明
一.告知开发者苹果手机的账户邮箱 1.通过任何形式告知即可 2.下载testflight 二.开发者发送激活邮件到测试者的账户邮箱 1.进入邮箱查看邮件点击 Accept invitation 进行下 ...
- JAVA笔记整理(四),JAVA中的封装
什么是封装 所谓的封装就是把数据项和方法作为一个独立的整体隐藏在对象的内部,具体的实施细节不对外提现,仅仅保留有限的外部接口,封装外的用户只能通过接口来进行操作.就好比开启一台电脑需要进行很多个步骤, ...
- c# 属性成员
- Python 使用 docopt 解析json参数文件
1. 背景 在深度学习的任务中,通常需要比较复杂的参数以及输入输出配置,比如需要不同的训练data,不同的模型,写入不同的log文件,输出到不同的文件夹以免混淆输出 常用的parser.add()方法 ...
- Run Code Once on First Load (Concurrency Safe)
原文: https://golangcode.com/run-code-once-with-sync/ ------------------------------------------------ ...
- 【xsy1103】随机数表(RanMat)矩阵快速幂
题目大意:你生成了一个随机数表,生成机制是这样子的: $a[i]=A1a[i-1]+A2(2≤i≤m)$ $b[i]=B1b[i-1]+B2(2≤i≤m)$ $M[1][y]=a[y]%P,(1≤y≤ ...
- Meeting Rooms II
Description Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2, ...
- IE下iframe不能正常加载,显示空白
下午帮忙看了一个web问题,index.html中嵌入<iframe>来加载同文件目录下的一个页面,在多个浏览器下测试,发现IE浏览器中会出现问题,<iframe>不能正常加载 ...