先知
架构图一览
创建Serve端
新建项目
配置文件
application.yaml
server:
port: 8080
# Eureka配置
eureka:
instance:
## Eureka实例的名称
hostname: localhostA
client:
# false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检查服务
fetch-registry: false
# false表示不向注册中心注册自己
register-with-eureka: false
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类配置
// 表示当前类为服务端Eureka服务端
@EnableEurekaServer
@SpringBootApplication
public class CloudA1Application {
public static void main(String[] args) {
SpringApplication.run(CloudA1Application.class, args);
}
}
启动测试一下
访问自己的 localhost:端口号 一切正常再继续
Eureka Client包括两个服务模块:Service Provider(服务提供方)和Service Consumer(服务消费方)。
创建Client端的服务提供端
新建项目
新增依赖
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 数据库池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!-- MP-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- druid -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置文件
application.yaml
server:
port: 8081
# Eureka配置
eureka:
client:
# 表示将自己注册进Eureka Server默认为true
register-with-eureka: true
# 是否从Eureka Server抓去已有的注册信息,默认是true
fetch-registry: true
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
service-url:
defaultZone: http://localhost:8080/eureka
#数据库配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/blog?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: xxx
type: com.alibaba.druid.pool.DruidDataSource
# 当前服务注册在Eureka Server的名称
application:
name: server-provider1
#MP配置
mybatis-plus:
# 配置外部xml映射
configuration:
# 开启SQL日志输出
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 开启驼峰映射
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*.xml
启动类配置
// 表示当前类客户端Eureka
@EnableDiscoveryClient
@SpringBootApplication
public class CloudB1Application {
public static void main(String[] args) {
SpringApplication.run(CloudB1Application.class, args);
}
}
编写控制器类
package com.learn.cloudb1.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/get1")
public String hello(){
return "hello world!";
}
}
启动测试
访问自己的 localhost:端口号(就是服务注册中心) 可以发现多了些东西,一个报错和刚刚注册好的服务提供者
创建Client端的服务消费端
创建方法和创建Client端的服务服务端一样
配置文件
server:
port: 8083
spring:
application:
name: service-customer1
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
启动类
@EnableDiscoveryClient
@SpringBootApplication
public class CloudB1Customer1Application {
public static void main(String[] args) {
SpringApplication.run(CloudB1Customer1Application.class, args);
}
}
控制器
package com.learn.cloudb1_customer1.controller;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/getMes")
public class GetMesController {
//从Euraka Server中获取服务提供方的服务地址信息
@Autowired
private DiscoveryClient ds;;
@GetMapping()
public String getMes(){
//服务提供者的名字
List<ServiceInstance> instanceList = ds.getInstances("server-provider1");
int port=0;
String host="";
//打印服务机器的信息
for (ServiceInstance instance : instanceList) {
//服务主机端口号
port = instance.getPort();
System.out.println("服务主机端口号:"+ port);
//服务主机名字
host = instance.getHost();
System.out.println("服务主机名字:"+host);
}
return host+"++++"+port;
}
}
启动测试
发现又多了一个
点击这里再输入自己定义的接口发现也可正常访问
OK!入门结束
其他更详细的适合入门的文章 https://www.cnblogs.com/h--d/p/12635204.html https://blog.csdn.net/jc_hook/article/details/122413858
标签:服务,入门,class,Eureka,2023,org,eureka,SpringCloudAlibaba,public From: https://blog.51cto.com/u_15820810/6004769