版权声明:@CopyRight转载请注明出处 https://blog.csdn.net/LI_AINY/article/details/87283504
- 中间会出现很多莫名其妙的问题, 服务提供者、服务消费者、服务发现组件的关系自己查,有dubbo和zookeeper 的使用经验基于更好了,两者差不了太多
代码https://pan.baidu.com/s/1NHJlwUPkpongev6OCpJ_lA - 建一个Eureka服务注册与消费中间件
新建一个工程(file->new->Spring Starter Project)
在 build.gradle 中加入了compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
即为:
plugins {
id 'org.springframework.boot' version '2.1.2.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
}
在application.yml中添加
#服务端口
server:
port: 8808
#Eureka 服务端信息
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8808/eureka
#不加这句话可能Eureka主页不能正常显示
spring.freemarker.prefer-file-system-access: fals
#register-with-eureka 表示是否讲自己注册到 Eureka Server, 默认为true。 由于当前工程就是Eureka Server所以设置成false。
#fetch-registry 表示是否从其他 Eureka Server 获取注册信息,默认true。由于本例中仅示范建一个单节点服务,所以暂设置为false,不同步其他Eureka Server注册信息。
#service-url.defaultZone 设置与 Eureka Server 交互的地址,查询和注册服务都需要依赖这个地址。 多个地址可使用英文逗号(,)隔开。
在启动类中增加 @EnableEurekaServer 标识是 Eureka Server
package com.comment.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//在启动类中增加 @EnableEurekaServer 标识是 Eureka Server
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 将服务提供者注册到Eureka 主机
新建一个工程用来发布服务
在 build.gradle 文件中,增加 compile(‘org.springframework.cloud:spring-cloud-starter-netflix-eureka-client’)
plugins {
id 'org.springframework.boot' version '2.1.2.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
}
application.yml中
server:
port: 8801
#开放信息
info:
app:
name: 提供用户查询服务
version: 1.0
#Eureka配置信息
#application.name 用于指定注册到 Eureka Server 上的应用名称;
# instance.prefer-ip-address 表示将自己的IP注册到 Eureka Server,
#若不配置该属性,或者设置为false,则标识注册所在操作系统的hostname到 Eureka Server
spring:
application:
name: 用户服务端
eureka:
client:
service-url:
defaultZone: http://localhost:8808/eureka
instance:
prefer-ip-address: true
启动类中增加 @EnableDiscoveryClient 注解,声明这是一个 Eureka Client。
package com.comment.user;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动类中增加 @EnableDiscoveryClient 注解,声明这是一个 Eureka Client。服务提供者
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 服务消费者注册
修改 build.gradle 文件,增加 compile(‘org.springframework.cloud:spring-cloud-starter-netflix-eureka-client’)
plugins {
id 'org.springframework.boot' version '2.1.2.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
}
application.yml
#服务端口
server:
port: 8802
#开放信息
info:
app:
name: 用户查询消费端
version: 1.0
#Eureka配置信息
spring:
application:
name: 用户消费端
eureka:
client:
service-url:
defaultZone: http://localhost:8808/eureka
instance:
prefer-ip-address: true
入口类Application
package com.comment.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class Application {
/**
* 我们通常使用RestTemplate调用其他服务的API
* 文件中实例化一个RestTemplate,这样其他类中可以用@Autowired获得到RestTemplate对象。
* 这里用到了@Bean注解,作用是实例化一个Bean并使用该方法的名称命名。
* @return RestTemplate
*/
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
调用接口类
package com.comment.consumer;
import java.util.List;
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.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/getUser")
public String getUser() {
// return restTemplate.getForObject("http://127.0.0.1:8801/user/getName", String.class);
List<ServiceInstance> list = discoveryClient.getInstances("porvider-user");
if (list.size() > 0) {
String url = list.get(0).getUri().toString();
// System.out.println(url+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
return restTemplate.getForObject(url + "/user/getName", String.class);
} else {
return null;
}
}
}
- 结果
启动三个入口类 先注册中心 然后提供服务入口类 最后调用服务入口类
代码https://pan.baidu.com/s/1NHJlwUPkpongev6OCpJ_lA