Spring Cloud 入门 -- 搭建Eureka注册中心 实现服务者与消费者的服务调用_eureka 注册中心调用服务
4.0.0@Data//支持链式编程4.0.0server:port: 7001。
<groupId>org.example</groupId>
<artifactId>spring_cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloud-api</module>
<module>springcloud-provider</module>
<module>springcloud-eureka</module>
<module>springcloud-consumer</module>
</modules>
<!-- 打包方式为 pom -->
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--springCloud的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--SpringBoot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--SpringBoot 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--日志测试~-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
#### ⌚实体类模块
**pom.xml**
<?xml version="1.0" encoding="UTF-8"?>
spring_cloud
org.example
1.0-SNAPSHOT
4.0.0
<artifactId>springcloud-api</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
**Dept实体类**
package com.wanshi.springcloud.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@NoArgsConstructor
//支持链式编程
@Accessors(chain = true)
public class Dept {
private Integer dept_noid;
private String dept_name;
private String db_source;
}
#### ⌚注册中心模块
**pom.xml**
<?xml version="1.0" encoding="UTF-8"?>
spring_cloud
org.example
1.0-SNAPSHOT
4.0.0
<artifactId>springcloud-eureka</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- 热部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
**application.yml**
server:
port: 7001
eureka 配置
eureka:
instance:
hostname: localhost # eureka 服务端的实例名称
client:
register-with-eureka: false # 是否向 eureka 注册自己,不注册自己
fetch-registry: false # fetch-registry 如果为false,表示自己为注册中心
service-url: # 将默认的url去掉,替换为自己的,监控页面
defaultZone: http://
e
u
r
e
k
a
.
i
n
s
t
a
n
c
e
.
h
o
s
t
n
a
m
e
:
{eureka.instance.hostname}:
eureka.instance.hostname:{server.port}/eureka/
**启动类**
package com.wanshi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// EnableEurekaServer 启动Eureka服务
@EnableEurekaServer
public class EurekaServerMainApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMainApplication.class, args);
}
}
#### ⌚服务提供者模块
**pom.xml**
<!-- Eureka -->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- actuator 监控中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 引入实体类 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jetty -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!-- 热部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
**application.yml**
server:
port: 8001
servlet:
context-path: /springcloud-provider-dept/
spring:
application:
name: SpringCloudProviderDept
datasource:
url: jdbc:mysql://localhost:3306/db_spring_cloud01
username: root
password: 111111
driver-class-name: com.mysql.jdbc.Driver
Eureka的配置,服务注册到那里
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-dept8001 # 修改 Eureka上的默认描述信息
info:
app.name: wanshi-springcloud
company.name: blog.wanshi.com
**DeptController**
package com.wanshi.springcloud.controller;
import com.wanshi.springcloud.entity.Dept;
import com.wanshi.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(“/dept/”)
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("list")
public List<Dept> list() {
return deptService.list();
}
@PostMapping("insert")
public Integer insert(@RequestBody Dept param) {
return deptService.insert(param);
}
@PostMapping("get/{id}")
public Dept get(@PathVariable("id") Integer id) {
Dept param = new Dept();
param.setDept\_noid(id);
return deptService.get(param);
}
}
#### ⌚服务消费者模块
**pom.xml**
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- Eureka -->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</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-devtools</artifactId>
</dependency>
</dependencies>
**application.yml**
server:
port: 80
Eureka配置
eureka:
client:
register-with-eureka: false # 不向注册中心注册自己
service-url:
defaultZone: http://localhost:7001/eureka/
**ConfigBean配置**
package com.wanshi.springcloud.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
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
**DeptConsumerController**
package com.wanshi.springcloud.controller;
import com.wanshi.springcloud.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping(“/consumer/dept/”)
public class DeptConsumerController {
@Autowired
private RestTemplate restTemplate;
private static final String REST_URL_PREFIX = "http://localhost:8001/springcloud-provider-dept";
@GetMapping("get/{id}")
public Integer get(@PathVariable("id") Integer id) {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Integer.class);
}
@GetMapping("list")
public List<Dept> list() {
return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
}
@PostMapping("insert")
public Integer insert(@RequestBody Dept param) {
return restTemplate.patchForObject(REST_URL_PREFIX + "/dept/insert", param, Integer.class);
}
}
**启动类**
package com.wanshi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
// Ribbon 和 Eureka 整合以后,客户端可以直接调用,不用关心IP地址和端口号
@SpringBootApplication
@EnableEurekaClient
public class DeptConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DeptConsumerApplication.class, args);
}
}
## 八、源码下载
[Spring Cluod搭建Eureka注册中心实现服务者与消费者的服务调用]( )
## ♨️往期精彩热文回顾
✈️ **[Netty进阶 – WebSocket长连接开发]( )**
✈️ **[Netty进阶 – 非阻塞网络编程 实现群聊+私聊+心跳检测系统]( )**
✈️ **[Postman测试工具调试接口详细教程【向后端发送Json数据并接收返回的Json结果】]( )**
✈️ **[Java面向对象 — 吃货联盟订餐系统(完整版)]( )**
✈️ **[一分钟教你快速 搭建Vue脚手架(Vue-Cli)项目并整合ElementUI]( )**
## ⛵小结
更多推荐
所有评论(0)