传统的服务架构
不管是采用http还是nginx还是host都需要将服务端或者客服端的ip保存起来通过IP去关联,如果IP变化
或者增加服务模块传统HTTP就会在写死的对应调用端去修改其和增加IP。nginx则不管是关联服务端或者是客户端都需要将新增和修改的IP进行更新。
Eureka
当客户端也就是模块注册到Eureka上就会彼此感知存在(通过客户名具体IP等信息由server端进行解析)就可以通过服务名进行调用,每个Client都会从server当中获取到其他Client的信息
新建Server
pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cx</groupId>
<artifactId>spring-cloud-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml
spring:
application:
name: service-register-center
mandatory-file-encoding: UTF-8
http:
encoding:
enabled: true
charset: UTF-8
logging: #日志文件
level:
root: info
file:
max-size: 15MB
path: service-register-center-log
pattern:
dateformat: yyyy-MM-dd HH:mm:ss.SSS
server:
port: 10099
undertow:
accesslog:
enabled: true
dir: undertow-access-logs
pattern: common
prefix: service_register_center_access_log.
suffix: log
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
shutdown:
enabled: true
eureka:
instance:
hostname: localhost
## 表示为Erureka客户端
client:
## 单台server为服务器不需要注册到服务器上
register-with-eureka: false
## 获取注册列表的信息 道理同上 基本两者参数是一样的
fetch-registry: false
service-url: # Eureka服务提供地址
defaultZone: http://localhost:10099/eureka/
在springboot启动器上加上@EnableEurekaServer 注解标识为服务器
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
新建Client
pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cx</groupId>
<artifactId>spring-cloud-eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml
server:
port: 9090
spring:
application:
name: eureka-client
eureka:
## 表示为Erureka客户端
client:
## 单台server为服务器不需要注册到服务器上
register-with-eureka: true
## 获取注册列表的信息 道理同上 基本两者参数是一样的
fetch-registry: true
service-url: # Eureka服务提供地址
defaultZone: http://localhost:10099/eureka/
student:
information:
address: shanghai
name: zhangsan
zai springBoot启动器上标识Client为客户端@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaClientApplication.class, args);
}
}
依次启动server 和client 访问
http://localhost:10099 如图
测试使用
在client中编写Controller
@RestController
public class Mycontroller {
@Value("${student.information.address}")
private String address;
@Value("${student.information.name}")
private String name;
@GetMapping("info")
public String info() {
String result = "welcome:" + this.address + ",your address is " + this.address;
return result;
}
}
访问http://localhost:9090/info