建立一个项目(用的是idea开发工具) (1) 如图进行选择和配置 前端
server:
port: 9999 #服务注册中心端口号
eureka:
instance:
hostname: 127.0.0.1 #服务注册中心IP地址
client:
registerWithEureka: false #是否向服务注册中心注册本身
fetchRegistry: false #是否检索服务
serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
(4) 启动看效果 java
#####2、先建立一个服务者(并注册到eureka上) (1)建立一个项目,跟建立euraka项目大体选择同样,就不上图了。只有选择依赖的时候,多选一个 spring web。由于服务者要提供服务未来要用到controller这种注解。 (2)修改代码。 使其变成eureka的客户端:在Spring-boot的启动类上经过注解@EnableEurekaClient 代表本身是一个eurekaclient. 提供服务:写一个controller,并编写具体接口。web
package com.rest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class BasketballController {
/**
* 假如这个客户端要提供一个getUser的方法
* @return
*/
@GetMapping(value = "/buy")
@ResponseBody
public Map<String,Object> getUser(){
Map<String,Object> data = new HashMap<>();
// data.put("id",id);
data.put("size","7#");
data.put("from","阳光篮球场");
return data;
}
}
复制代码
(3)配置调整 将application.properties 改成 application.yml,并修改内容spring
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:9999/eureka/
server:
port: 8081 #服务端口号
spring:
application:
name: service-provider #服务名称--调用的时候根据名称来调用该服务的方法
复制代码
(4) 启动看效果 bash
#####3、该来人买篮球了:建立一个消费者(并注册到eureka上) (1)建立一个项目,选择与服务者同样。要选择eureka和spring web,由于也要发起请求。 (2)修改代码,使其变成eureka的客户端并能够调用服务。(我在里面加了其余的注解,喜欢研究的能够尝试的去掉看看有没有影响)服务器
package com.eureka.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@EnableEurekaClient
@EntityScan
@ServletComponentScan
@ComponentScan
@RestController
public class ConsumerApplication {
@Autowired
RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate rest() {
return new RestTemplate();
}
/**
* Rest服务端使用RestTemplate发起http请求,而后获得数据返回给前端
*
* @return
*/
@GetMapping(value = "/getUser")
@ResponseBody
public Map<String, Object> getUser() {
Map<String, Object> data = restTemplate.getForObject("http://service-provider/buy", Map.class);
return data;
}
}
复制代码
(3)别忘了修改配置文件,使其注册到eureka上。app
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:9999/eureka/
server:
port: 9000 #服务端口号
spring:
application:
name: service-consumer #服务名称--调用的时候根据名称来调用该服务的方法
复制代码
(4)启动看效果 eureka上已经有了这个instance了。 负载均衡
下一篇写eureka的简单负载均衡。dom